1
1
package ru .bartwell .ultradebugger .module .logger ;
2
2
3
3
import android .content .Context ;
4
+ import android .net .Uri ;
4
5
import android .support .annotation .NonNull ;
6
+ import android .support .annotation .Nullable ;
5
7
import android .text .TextUtils ;
6
8
9
+ import java .io .ByteArrayInputStream ;
10
+ import java .io .InputStream ;
11
+ import java .text .SimpleDateFormat ;
12
+ import java .util .Date ;
13
+ import java .util .Locale ;
7
14
import java .util .Map ;
8
15
import java .util .Random ;
9
16
@@ -48,44 +55,60 @@ public String getDescription() {
48
55
@ NonNull
49
56
@ Override
50
57
public HttpResponse handle (@ NonNull HttpRequest request ) {
51
- Page page = new Page ();
52
- page .setTitle (getName ());
53
- Content content = new Content ();
58
+ if (request .getUri ().contains (Utils .DOWNLOAD_URI_PART )) {
59
+ InputStream inputStream = getLogsFileStream ();
60
+ if (inputStream == null ) {
61
+ return new HttpResponse (HttpResponse .Status .INTERNAL_SERVER_ERROR );
62
+ } else {
63
+ HttpResponse response = new HttpResponse ("text/plain" , inputStream );
64
+ String fileName = Uri .parse (request .getUri ()).getLastPathSegment ();
65
+ if (fileName .contains (Utils .LOG_FILE_EXTENSION )) {
66
+ StorageHelper .addLog (getContext (), "--- Download log file " + fileName + " ---" );
67
+ response .addHeader ("content-disposition" , "attachment; filename=\" " + fileName + "\" " );
68
+ }
69
+ return response ;
70
+ }
71
+ } else {
72
+ Page page = new Page ();
73
+ page .setTitle (getName ());
74
+ Content content = new Content ();
54
75
55
- StorageHelper .rotateLogs (getContext ());
76
+ StorageHelper .rotateLogs (getContext ());
56
77
57
- if (isActionParameterValid (request , PARAMETER_CLEAR_LOGS )) {
58
- StorageHelper .clearLogs (getContext ());
59
- }
60
- String deleteValue = HttpUtils .getParameterValue (request .getParameters (), PARAMETER_DELETE_VALUE_KEY );
61
- if (isActionParameterValid (request , PARAMETER_DELETE_VALUE ) && !TextUtils .isEmpty (deleteValue )) {
62
- StorageHelper .removeValue (getContext (), deleteValue );
63
- }
78
+ if (isActionParameterValid (request , PARAMETER_CLEAR_LOGS )) {
79
+ StorageHelper .clearLogs (getContext ());
80
+ }
81
+ String deleteValue = HttpUtils .getParameterValue (request .getParameters (), PARAMETER_DELETE_VALUE_KEY );
82
+ if (isActionParameterValid (request , PARAMETER_DELETE_VALUE ) && !TextUtils .isEmpty (deleteValue )) {
83
+ StorageHelper .removeValue (getContext (), deleteValue );
84
+ }
64
85
65
- mLastPageRandom = getRandInt ();
66
- page .addNavigationLink ("?" + PARAMETER_CLEAR_LOGS + "=" + mLastPageRandom , "Clear logs" );
67
-
68
- Map <String , ?> allValues = StorageHelper .getAllValues (getContext ());
69
- if (!allValues .isEmpty ()) {
70
- content .add (new HeadingContentPart (3 , "Saved values" ));
71
- Table table = new Table ();
72
- int i = 0 ;
73
- for (Map .Entry <String , ?> entry : allValues .entrySet ()) {
74
- table .add (0 , i , new RawContentPart (entry .getKey ()));
75
- table .add (1 , i , new RawContentPart (String .valueOf (entry .getValue ())));
76
- table .add (2 , i , new Link ("?" + PARAMETER_DELETE_VALUE + "=" + mLastPageRandom
77
- + "&" + PARAMETER_DELETE_VALUE_KEY + "=" + entry .getKey ()
78
- , "Remove" ));
79
- i ++;
86
+ mLastPageRandom = getRandInt ();
87
+ page .addNavigationLink (Utils .getDownloadPath (), "Download logs" );
88
+ page .addNavigationLink ("?" + PARAMETER_CLEAR_LOGS + "=" + mLastPageRandom , "Clear logs" );
89
+
90
+ Map <String , ?> allValues = StorageHelper .getAllValues (getContext ());
91
+ if (!allValues .isEmpty ()) {
92
+ content .add (new HeadingContentPart (3 , "Saved values" ));
93
+ Table table = new Table ();
94
+ int i = 0 ;
95
+ for (Map .Entry <String , ?> entry : allValues .entrySet ()) {
96
+ table .add (0 , i , new RawContentPart (entry .getKey ()));
97
+ table .add (1 , i , new RawContentPart (String .valueOf (entry .getValue ())));
98
+ table .add (2 , i , new Link ("?" + PARAMETER_DELETE_VALUE + "=" + mLastPageRandom
99
+ + "&" + PARAMETER_DELETE_VALUE_KEY + "=" + entry .getKey ()
100
+ , "Remove" ));
101
+ i ++;
102
+ }
103
+ content .add (table );
80
104
}
81
- content .add (table );
82
- }
83
105
84
- content .add (new HeadingContentPart (3 , "Logs" ));
85
- content .add (new RawContentPart (StorageHelper .readLogs (getContext (), "<br/>" )));
106
+ content .add (new HeadingContentPart (3 , "Logs" ));
107
+ content .add (new RawContentPart (StorageHelper .readLogs (getContext (), "<br/>" )));
86
108
87
- page .setContent (content );
88
- return new HttpResponse (page .toHtml ());
109
+ page .setContent (content );
110
+ return new HttpResponse (page .toHtml ());
111
+ }
89
112
}
90
113
91
114
private int getRandInt () {
@@ -96,4 +119,14 @@ private boolean isActionParameterValid(@NonNull HttpRequest request, @NonNull St
96
119
String value = HttpUtils .getParameterValue (request .getParameters (), parameter );
97
120
return !TextUtils .isEmpty (value ) && value .equals (String .valueOf (mLastPageRandom ));
98
121
}
122
+
123
+ @ Nullable
124
+ private InputStream getLogsFileStream () {
125
+ try {
126
+ return new ByteArrayInputStream (StorageHelper .readLogs (getContext (), "\r \n " ).getBytes ());
127
+ } catch (Exception e ) {
128
+ e .printStackTrace ();
129
+ }
130
+ return null ;
131
+ }
99
132
}
0 commit comments