SQLLogger.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. File root = Environment.getExternalStorageDirectory();
  35. if(root != null && HelperUtils.isExternalStorageWritable()){
  36. File file = new File(root, "hostage_" + format + "_" + System.currentTimeMillis() + ".log");
  37. log = new FileOutputStream(file);
  38. }else {
  39. Toast.makeText(context, "Could not write to SD Card", Toast.LENGTH_SHORT).show();
  40. return;
  41. }
  42. } else{
  43. log = context.openFileOutput("hostage_" + format + "_" + System.currentTimeMillis() + ".log", Context.MODE_PRIVATE);
  44. }
  45. ArrayList<Record> records = dbh.getAllRecords();
  46. for(Record record : records){
  47. if(format.equals("JSON")){
  48. log.write((record.toStringJson() + "\n").getBytes());
  49. }else {
  50. log.write((record.toString() + "\n").getBytes());
  51. }
  52. }
  53. log.flush();
  54. log.close();
  55. } catch (Exception e) {
  56. e.printStackTrace();
  57. }
  58. }
  59. public void uploadDatabase(){
  60. SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);
  61. Editor editor = pref.edit();
  62. int lastUploadedAttackId = pref.getInt("LAST_UPLOADED_ATTACK_ID", -1);
  63. int currentAttackCount = dbh.getAttackCount();
  64. if(lastUploadedAttackId == currentAttackCount || currentAttackCount == 0){
  65. return;
  66. }
  67. HttpClient httpclient = HelperUtils.createHttpClient();
  68. ArrayList<Record> recordList = dbh.getRecordOfEachAtack(lastUploadedAttackId + 1);
  69. try {
  70. HttpPost httppost;
  71. for(Record record: recordList){
  72. httppost = new HttpPost("https://ssi.cased.de");
  73. StringEntity se = new StringEntity(record.toStringJson());
  74. httppost.setEntity(se);
  75. httpclient.execute(httppost);
  76. }
  77. } catch (UnsupportedEncodingException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. } catch (ClientProtocolException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. catch (IOException e) {
  85. // TODO Auto-generated catch block
  86. e.printStackTrace();
  87. }
  88. editor.putInt("LAST_UPLOADED_ATTACK_ID",currentAttackCount - 1);
  89. editor.commit();
  90. }
  91. public int getAttackCount(){
  92. return dbh.getAttackCount();
  93. }
  94. public int getAttackPerProtokollCount(String protocol){
  95. return dbh.getAttackPerProtokolCount(protocol);
  96. }
  97. public void clearLog(){
  98. dbh.clearData();
  99. }
  100. @Override
  101. public void close() {
  102. // TODO Auto-generated method stub
  103. }
  104. }