SQLLogger.java 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package de.tudarmstadt.informatik.hostage.logging;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.ArrayList;
  7. import org.apache.http.client.ClientProtocolException;
  8. import org.apache.http.client.HttpClient;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.entity.StringEntity;
  11. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  12. import android.content.Context;
  13. import android.content.SharedPreferences;
  14. import android.content.SharedPreferences.Editor;
  15. import android.os.Environment;
  16. import android.preference.PreferenceManager;
  17. import android.widget.Toast;
  18. public class SQLLogger implements Logger{
  19. Context context;
  20. DatabaseHandler dbh;
  21. public SQLLogger(Context context){
  22. this.context = context;
  23. dbh = new DatabaseHandler(context);
  24. }
  25. public synchronized void write(Record record) {
  26. dbh.addRecord(record);
  27. }
  28. public void exportDatabase(String format){
  29. try {
  30. FileOutputStream log;
  31. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
  32. if(pref.getBoolean("pref_external_storage", false)){
  33. //TODO Fehlermeldung wenn keine SD karte gefunden werden kann
  34. log = context.openFileOutput("hostage_" + format + "_" + System.currentTimeMillis() + ".log", Context.MODE_PRIVATE);
  35. } else{
  36. File file = new File(Environment.getExternalStorageDirectory(), "hostage_" + format + "_" + System.currentTimeMillis() + ".log");
  37. log = new FileOutputStream(file);
  38. }
  39. ArrayList<Record> records = dbh.getAllRecords();
  40. for(Record record : records){
  41. if(format.equals("JSON")){
  42. log.write((record.toStringJson() + "\n").getBytes());
  43. }else {
  44. log.write((record.toString() + "\n").getBytes());
  45. }
  46. }
  47. log.flush();
  48. log.close();
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53. public void uploadDatabase(){
  54. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
  55. Editor editor = pref.edit();
  56. int lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1);
  57. int currentAttackCount = dbh.getAttackCount();
  58. if(lastUploadedAttackId == currentAttackCount || currentAttackCount == 0){
  59. return;
  60. }
  61. HttpClient httpclient = HelperUtils.createHttpClient();
  62. ArrayList<Record> recordList = dbh.getRecordOfEachAtack(lastUploadedAttackId + 1);
  63. try {
  64. HttpPost httppost;
  65. for(Record record: recordList){
  66. httppost = new HttpPost("https://ssi.cased.de");
  67. StringEntity se = new StringEntity(record.toStringJson());
  68. httppost.setEntity(se);
  69. httpclient.execute(httppost);
  70. }
  71. } catch (UnsupportedEncodingException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. } catch (ClientProtocolException e) {
  75. // TODO Auto-generated catch block
  76. e.printStackTrace();
  77. }
  78. catch (IOException e) {
  79. // TODO Auto-generated catch block
  80. e.printStackTrace();
  81. }
  82. editor.putInt("LAST_UPLOADED_ATTACK_ID",currentAttackCount - 1);
  83. editor.commit();
  84. }
  85. public int getAttackCount(){
  86. return dbh.getAttackCount();
  87. }
  88. public int getAttackPerProtokollCount(String protocol){
  89. return dbh.getAttackPerProtokolCount(protocol);
  90. }
  91. public void clearLog(){
  92. dbh.clearData();
  93. }
  94. @Override
  95. public void close() {
  96. // TODO Auto-generated method stub
  97. }
  98. }