package de.tudarmstadt.informatik.hostage.logging; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import de.tudarmstadt.informatik.hostage.commons.HelperUtils; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Environment; import android.preference.PreferenceManager; import android.widget.Toast; public class SQLLogger implements Logger{ Context context; DatabaseHandler dbh; public SQLLogger(Context context){ this.context = context; dbh = new DatabaseHandler(context); } public synchronized void write(Record record) { dbh.addRecord(record); } public void exportDatabase(String format){ try { FileOutputStream log; SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); if(pref.getBoolean("pref_external_storage", false)){ //TODO Fehlermeldung wenn keine SD karte gefunden werden kann File root = Environment.getExternalStorageDirectory(); if(root != null && HelperUtils.isExternalStorageWritable()){ File file = new File(root, "hostage_" + format + "_" + System.currentTimeMillis() + ".log"); log = new FileOutputStream(file); }else { Toast.makeText(context, "Could not write to SD Card", Toast.LENGTH_SHORT).show(); return; } } else{ log = context.openFileOutput("hostage_" + format + "_" + System.currentTimeMillis() + ".log", Context.MODE_PRIVATE); } ArrayList records = dbh.getAllRecords(); for(Record record : records){ if(format.equals("JSON")){ log.write((record.toStringJson() + "\n").getBytes()); }else { log.write((record.toString() + "\n").getBytes()); } } log.flush(); log.close(); } catch (Exception e) { e.printStackTrace(); } } public void uploadDatabase(){ SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context); Editor editor = pref.edit(); int lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1); int currentAttackCount = dbh.getAttackCount(); if(lastUploadedAttackId == currentAttackCount || currentAttackCount == 0){ return; } HttpClient httpclient = HelperUtils.createHttpClient(); ArrayList recordList = dbh.getRecordOfEachAtack(lastUploadedAttackId + 1); try { HttpPost httppost; for(Record record: recordList){ httppost = new HttpPost("https://ssi.cased.de"); StringEntity se = new StringEntity(record.toStringJson()); httppost.setEntity(se); httpclient.execute(httppost); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } editor.putInt("LAST_UPLOADED_ATTACK_ID",currentAttackCount - 1); editor.commit(); } public int getAttackCount(){ return dbh.getAttackCount(); } public int getAttackPerProtokollCount(String protocol){ return dbh.getAttackPerProtokolCount(protocol); } public void clearLog(){ dbh.clearData(); } @Override public void close() { // TODO Auto-generated method stub } }