TracingSyncService.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package de.tudarmstadt.informatik.hostage.sync.tracing;
  2. import java.security.KeyStore;
  3. import java.util.ArrayList;
  4. import org.apache.http.HttpVersion;
  5. import org.apache.http.client.HttpClient;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.conn.ClientConnectionManager;
  8. import org.apache.http.conn.scheme.PlainSocketFactory;
  9. import org.apache.http.conn.scheme.Scheme;
  10. import org.apache.http.conn.scheme.SchemeRegistry;
  11. import org.apache.http.conn.ssl.SSLSocketFactory;
  12. import org.apache.http.entity.StringEntity;
  13. import org.apache.http.impl.client.DefaultHttpClient;
  14. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  15. import org.apache.http.params.BasicHttpParams;
  16. import org.apache.http.params.HttpParams;
  17. import org.apache.http.params.HttpProtocolParams;
  18. import org.apache.http.protocol.HTTP;
  19. import android.app.IntentService;
  20. import android.content.Intent;
  21. import android.content.SharedPreferences;
  22. import android.content.SharedPreferences.Editor;
  23. import android.os.Handler;
  24. import android.preference.PreferenceManager;
  25. import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
  26. import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
  27. import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
  28. import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
  29. public class TracingSyncService extends IntentService{
  30. public static final int RECORD_UPLOADED = 0x00;
  31. public static final int SYNC_COMPLETE = 0x01;
  32. private HttpClient httpClient;
  33. private Handler mHandler;
  34. HostageDBOpenHelper dbh;
  35. SharedPreferences pref;
  36. Editor editor;
  37. public TracingSyncService(Handler handler) {
  38. super("TracingSyncService");
  39. mHandler = handler;
  40. httpClient = createHttpClient();
  41. pref = PreferenceManager.getDefaultSharedPreferences(this);
  42. editor = pref.edit();
  43. dbh = new HostageDBOpenHelper(this);
  44. }
  45. /**
  46. * The IntentService calls this method from the default worker thread with
  47. * the intent that started the service. When this method returns, IntentService
  48. * stops the service, as appropriate.
  49. */
  50. @Override
  51. protected void onHandleIntent(Intent intent) {
  52. //TODO Fallunterscheidung Intents
  53. uploadNewRecords();
  54. getRemoteData();
  55. dbh.clearSyncInfos();
  56. }
  57. /**
  58. * Uploads all new Records to a server, specified in the settings.
  59. */
  60. private void uploadNewRecords() {
  61. int lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1);
  62. String serverAddress = pref.getString("pref_upload", "https://ssi.cased.de");
  63. ArrayList<AttackRecord> recordList = dbh.getRecordOfEachAttack(lastUploadedAttackId);
  64. int size = recordList.size();
  65. int offset = 1;
  66. for(AttackRecord record: recordList){
  67. editor.putInt("LAST_UPLOADED_ATTACK_ID", lastUploadedAttackId + offset);
  68. uploadSingleRecord(record, serverAddress);
  69. if(mHandler != null)
  70. mHandler.obtainMessage(RECORD_UPLOADED, offset, size);
  71. offset++;
  72. }
  73. }
  74. /**
  75. * Uploads a single Record to a server, specified in the settings.
  76. *
  77. * @param record The Record to upload.
  78. * @serverAddress Address of the target server
  79. * @return True if the upload was successful, else false.
  80. */
  81. private boolean uploadSingleRecord(AttackRecord record, String serverAddress) {
  82. // Create a https client. Uses MySSLSocketFactory to accept all certificates
  83. HttpPost httppost;
  84. try {
  85. // Create HttpPost
  86. httppost = new HttpPost();
  87. // Create JSON String of Record
  88. //TODO FIX ME
  89. StringEntity se = new StringEntity( null/*record.toString(TraCINgFormatter
  90. .getInstance()) */);
  91. httppost.setEntity(se);
  92. // Execute HttpPost
  93. httpClient.execute(httppost);
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. return false;
  97. }
  98. return true;
  99. }
  100. /**
  101. * Uploads a single Record to a server, specified in the settings.
  102. *
  103. * @param record
  104. * The Record to upload.
  105. * @return True if the upload was successful, else false.
  106. */
  107. private void getRemoteData() {
  108. //TODO GET DATA FROM SERVER
  109. //TODO SAVE DATA IN DATABASE
  110. }
  111. /**
  112. * Creates a HttpClient with an own SSL Socket.
  113. *
  114. * @return HttpsClient who accepts accepts all certificates.
  115. * @see MySSLSocketFactory
  116. */
  117. private HttpClient createHttpClient() {
  118. try {
  119. KeyStore trustStore = KeyStore.getInstance(KeyStore
  120. .getDefaultType());
  121. trustStore.load(null, null);
  122. SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
  123. sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  124. HttpParams params = new BasicHttpParams();
  125. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  126. HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  127. SchemeRegistry registry = new SchemeRegistry();
  128. registry.register(new Scheme("http", PlainSocketFactory
  129. .getSocketFactory(), 80));
  130. registry.register(new Scheme("https", sf, 443));
  131. ClientConnectionManager ccm = new ThreadSafeClientConnManager(
  132. params, registry);
  133. return new DefaultHttpClient(ccm, params);
  134. } catch (Exception e) {
  135. e.printStackTrace();
  136. return new DefaultHttpClient();
  137. }
  138. }
  139. }