123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package de.tudarmstadt.informatik.hostage.sync.tracing;
- import java.security.KeyStore;
- import java.util.ArrayList;
- import org.apache.http.HttpVersion;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ClientConnectionManager;
- import org.apache.http.conn.scheme.PlainSocketFactory;
- import org.apache.http.conn.scheme.Scheme;
- import org.apache.http.conn.scheme.SchemeRegistry;
- import org.apache.http.conn.ssl.SSLSocketFactory;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpParams;
- import org.apache.http.params.HttpProtocolParams;
- import org.apache.http.protocol.HTTP;
- import android.app.IntentService;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.content.SharedPreferences.Editor;
- import android.os.Handler;
- import android.preference.PreferenceManager;
- import de.tudarmstadt.informatik.hostage.logging.AttackRecord;
- import de.tudarmstadt.informatik.hostage.logging.formatter.TraCINgFormatter;
- import de.tudarmstadt.informatik.hostage.net.MySSLSocketFactory;
- import de.tudarmstadt.informatik.hostage.persistence.HostageDBOpenHelper;
- public class TracingSyncService extends IntentService{
-
- public static final int RECORD_UPLOADED = 0x00;
- public static final int SYNC_COMPLETE = 0x01;
-
- private HttpClient httpClient;
- private Handler mHandler;
-
- HostageDBOpenHelper dbh;
-
- SharedPreferences pref;
- Editor editor;
- public TracingSyncService(Handler handler) {
- super("TracingSyncService");
- mHandler = handler;
- httpClient = createHttpClient();
- pref = PreferenceManager.getDefaultSharedPreferences(this);
- editor = pref.edit();
- dbh = new HostageDBOpenHelper(this);
- }
- /**
- * The IntentService calls this method from the default worker thread with
- * the intent that started the service. When this method returns, IntentService
- * stops the service, as appropriate.
- */
- @Override
- protected void onHandleIntent(Intent intent) {
- //TODO Fallunterscheidung Intents
- uploadNewRecords();
- getRemoteData();
- dbh.clearSyncInfos();
- }
-
-
- /**
- * Uploads all new Records to a server, specified in the settings.
- */
- private void uploadNewRecords() {
- int lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1);
- String serverAddress = pref.getString("pref_upload", "https://ssi.cased.de");
- ArrayList<AttackRecord> recordList = dbh.getRecordOfEachAttack(lastUploadedAttackId);
- int size = recordList.size();
- int offset = 1;
- for(AttackRecord record: recordList){
- editor.putInt("LAST_UPLOADED_ATTACK_ID", lastUploadedAttackId + offset);
- uploadSingleRecord(record, serverAddress);
- if(mHandler != null)
- mHandler.obtainMessage(RECORD_UPLOADED, offset, size);
- offset++;
- }
- }
-
- /**
- * Uploads a single Record to a server, specified in the settings.
- *
- * @param record The Record to upload.
- * @serverAddress Address of the target server
- * @return True if the upload was successful, else false.
- */
- private boolean uploadSingleRecord(AttackRecord record, String serverAddress) {
- // Create a https client. Uses MySSLSocketFactory to accept all certificates
- HttpPost httppost;
- try {
- // Create HttpPost
- httppost = new HttpPost();
- // Create JSON String of Record
- //TODO FIX ME
- StringEntity se = new StringEntity( null/*record.toString(TraCINgFormatter
- .getInstance()) */);
- httppost.setEntity(se);
- // Execute HttpPost
- httpClient.execute(httppost);
- } catch (Exception e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
-
- /**
- * Uploads a single Record to a server, specified in the settings.
- *
- * @param record
- * The Record to upload.
- * @return True if the upload was successful, else false.
- */
- private void getRemoteData() {
- //TODO GET DATA FROM SERVER
- //TODO SAVE DATA IN DATABASE
- }
-
- /**
- * Creates a HttpClient with an own SSL Socket.
- *
- * @return HttpsClient who accepts accepts all certificates.
- * @see MySSLSocketFactory
- */
- private HttpClient createHttpClient() {
- try {
- KeyStore trustStore = KeyStore.getInstance(KeyStore
- .getDefaultType());
- trustStore.load(null, null);
- SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
- HttpParams params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", PlainSocketFactory
- .getSocketFactory(), 80));
- registry.register(new Scheme("https", sf, 443));
- ClientConnectionManager ccm = new ThreadSafeClientConnManager(
- params, registry);
- return new DefaultHttpClient(ccm, params);
- } catch (Exception e) {
- e.printStackTrace();
- return new DefaultHttpClient();
- }
- }
- }
|