TracingSyncService.java 5.1 KB

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