Skip to content

Commit 3e13981

Browse files
committed
Core changes for response handler to be easy to use such as send custom response with custom header .
Add some core methods such as get mac and ip address .
1 parent 71f8e56 commit 3e13981

6 files changed

Lines changed: 315 additions & 106 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ routes.add(new Route("/test",ServerHelper.METHOD.GET, true, new RouteCallBack()
7070
routes.add(new Route("/html",ServerHelper.METHOD.GET, new RouteCallBack() {
7171
@Override
7272
public void onRequested(Request request, ResponseHandler responseHandler) {
73-
responseHandler.sendHtmlFileResponse(ServerHelper.RESPONSE_CODE.OK, "html/index.html");
73+
responseHandler.sendAssetFile(ServerHelper.RESPONSE_CODE.OK,ServerHelper.CONTENT_TYPE.HTML, "html/index.html");
7474
}
7575
}));
7676

@@ -83,7 +83,7 @@ routes.add(new Route("/htmlplaceholder",ServerHelper.METHOD.GET, new RouteCallBa
8383

8484
Map<String, String> placeHolders = new HashMap<>();
8585
placeHolders.put("error_place_holder", "The text to be rendered");
86-
responseHandler.sendHtmlFileResponse(ServerHelper.RESPONSE_CODE.NOT_FOUND,
86+
responseHandler.sendAssetFileWithPlaceHolder(ServerHelper.RESPONSE_CODE.NOT_FOUND,ServerHelper.CONTENT_TYPE.HTML,
8787
"html/500.html", placeHolders);
8888
}
8989
}));

server/src/main/java/com/androthink/server/core/RoutesHandler.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,23 +57,21 @@ void Handle(Request request, ResponseHandler responseHandler) throws IOException
5757
request.setApiKey(authKey);
5858
route.getCallBack().onRequested(request, responseHandler);
5959
} else
60-
responseHandler.sendHtmlFileResponse(ServerHelper.RESPONSE_CODE.UNAUTHORIZED, "html/login.html");
60+
responseHandler.sendAssetFile(ServerHelper.RESPONSE_CODE.UNAUTHORIZED,ServerHelper.CONTENT_TYPE.HTML, "html/login.html");
6161

6262
}
6363
} else
6464
route.getCallBack().onRequested(request, responseHandler);
6565
} else {
6666
if (request.getMethod().equals(ServerHelper.METHOD.GET)) {
6767
if (request.getRoutePath().startsWith("/images/")) {
68-
responseHandler.sendImageResponse(ServerHelper
69-
.getFileFromAssets(responseHandler.getContext(),
70-
request.getRoutePath().replace("/images/", "img/")));
68+
responseHandler.sendAssetMediaFile(ServerHelper.RESPONSE_CODE.OK,ServerHelper.CONTENT_TYPE.JPEG,
69+
request.getRoutePath().replace("/images/", "img/"));
7170

7271
return;
7372
} else if (request.getRoutePath().startsWith("/sounds/")) {
74-
responseHandler.sendImageResponse(ServerHelper
75-
.getFileFromAssets(responseHandler.getContext(),
76-
request.getRoutePath().replace("/sounds/", "sound/")));
73+
responseHandler.sendAssetMediaFile(ServerHelper.RESPONSE_CODE.OK,ServerHelper.CONTENT_TYPE.MPEG,
74+
request.getRoutePath().replace("/sounds/", "sound/"));
7775

7876
return;
7977
}
@@ -91,7 +89,7 @@ void Handle(Request request, ResponseHandler responseHandler) throws IOException
9189
else {
9290
Map<String, String> placeHolders = new HashMap<>();
9391
placeHolders.put("page_place_holder", "" + request.getRoutePath());
94-
responseHandler.sendHtmlFileResponse(ServerHelper.RESPONSE_CODE.NOT_FOUND, "html/404.html", placeHolders);
92+
responseHandler.sendAssetFileWithPlaceHolder(ServerHelper.RESPONSE_CODE.NOT_FOUND,ServerHelper.CONTENT_TYPE.HTML ,"html/404.html", placeHolders);
9593
}
9694
}
9795
}

server/src/main/java/com/androthink/server/core/ServerRequestThread.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.androthink.server.core;
22

33
import android.content.Context;
4+
import android.content.res.Resources;
45

56
import com.androthink.server.callback.RequestCallBack;
67
import com.androthink.server.handler.ResponseHandler;
@@ -56,6 +57,16 @@ public void run() {
5657
ex.printStackTrace();
5758
}
5859

60+
e.printStackTrace();
61+
} catch (Resources.NotFoundException e) {
62+
try {
63+
if (responseHandler != null)
64+
responseHandler.sendJsonResponse(ServerHelper.RESPONSE_CODE.NOT_FOUND,
65+
"{\"status\":false,\"error\":\"FileNotFound : " + e.toString() + "\"}");
66+
} catch (IOException ex) {
67+
ex.printStackTrace();
68+
}
69+
5970
e.printStackTrace();
6071
} catch (Exception e) {
6172
try {
@@ -66,7 +77,7 @@ public void run() {
6677
else {
6778
Map<String, String> placeHolders = new HashMap<>();
6879
placeHolders.put("error_place_holder", "" + e.toString());
69-
responseHandler.sendHtmlFileResponse(ServerHelper.RESPONSE_CODE.NOT_FOUND, "html/500.html", placeHolders);
80+
responseHandler.sendAssetFileWithPlaceHolder(ServerHelper.RESPONSE_CODE.NOT_FOUND,ServerHelper.CONTENT_TYPE.HTML, "html/500.html", placeHolders);
7081
}
7182
}
7283
} catch (IOException ex) {

server/src/main/java/com/androthink/server/handler/ResponseHandler.java

Lines changed: 129 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import com.androthink.server.helper.ServerHelper;
88

9-
import java.io.BufferedInputStream;
109
import java.io.DataOutputStream;
1110
import java.io.IOException;
1211
import java.util.Date;
@@ -26,6 +25,10 @@ private ResponseHandler(Context context, DataOutputStream responseStream) {
2625
this.responseStream = responseStream;
2726
}
2827

28+
public Context getContext() {
29+
return this.context;
30+
}
31+
2932
/**
3033
* @param json Json to be sent as Response .
3134
* @throws IOException throws exception if response channel closed .
@@ -89,136 +92,203 @@ public void sendJsonResponse(int code, String json) throws IOException {
8992

9093
/**
9194
* @param code custom response code
92-
* @param filename filename for the html file to be sent .
95+
* @param type Content-Type
96+
* @param assetPath filename for the assets file to be sent .
9397
* @param customHeaders headers to be sent with the response .
9498
* @throws IOException throws exception if response channel closed .
9599
*/
96-
// HTML Response ..
97-
public void sendHtmlFileResponseWithCustomHeader(int code, String filename, Map<String, String> customHeaders) throws IOException {
100+
public void sendAssetFile(int code, String type, String assetPath, Map<String, String> customHeaders) throws IOException {
98101

99-
String page = ServerHelper.getHtmlFromAsset(context, filename);
102+
byte[] data = ServerHelper.loadFileFromAsset(context, assetPath, true);
103+
sendResponseHeader(code, type, data.length, customHeaders);
104+
this.responseStream.write(data);
105+
this.responseStream.flush();
106+
this.responseStream.close();
107+
}
100108

101-
byte[] data = page.getBytes("UTF-8");
102-
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length, customHeaders);
109+
/**
110+
* @param code custom response code
111+
* @param type Content-Type
112+
* @param filename filename for the file to be sent .
113+
* @param customHeaders headers to be sent with the response .
114+
* @throws IOException throws exception if response channel closed .
115+
*/
116+
public void sendFile(int code, String type, String filename, Map<String, String> customHeaders) throws IOException {
117+
118+
byte[] data = ServerHelper.loadFileAsBytes(filename, true);
119+
sendResponseHeader(code, type, data.length, customHeaders);
120+
this.responseStream.write(data);
121+
this.responseStream.flush();
122+
this.responseStream.close();
123+
}
124+
125+
/**
126+
* @param code custom response code
127+
* @param type Content-Type
128+
* @param assetPath filename for the assets file to be sent .
129+
* @param customHeaders headers to be sent with the response .
130+
* @throws IOException throws exception if response channel closed .
131+
*/
132+
public void sendAssetMediaFile(int code, String type, String assetPath, Map<String, String> customHeaders) throws IOException {
103133

134+
byte[] data = ServerHelper.loadFileFromAsset(context, assetPath, false);
135+
sendResponseHeader(code, type, data.length, customHeaders);
136+
this.responseStream.write(data);
137+
this.responseStream.flush();
138+
this.responseStream.close();
139+
}
140+
141+
/**
142+
* @param code custom response code
143+
* @param type Content-Type
144+
* @param filename filename for the file to be sent .
145+
* @param customHeaders headers to be sent with the response .
146+
* @throws IOException throws exception if response channel closed .
147+
*/
148+
public void sendMediaFile(int code, String type, String filename, Map<String, String> customHeaders) throws IOException {
149+
150+
byte[] data = ServerHelper.loadFileAsBytes(filename, false);
151+
sendResponseHeader(code, type, data.length, customHeaders);
152+
this.responseStream.write(data);
153+
this.responseStream.flush();
154+
this.responseStream.close();
155+
}
156+
157+
/**
158+
* @param code custom response code
159+
* @param type Content-Type
160+
* @param assetPath filename for the assets file to be sent .
161+
* @throws IOException throws exception if response channel closed .
162+
*/
163+
public void sendAssetFile(int code, String type, String assetPath) throws IOException {
164+
165+
byte[] data = ServerHelper.loadFileFromAsset(context, assetPath, true);
166+
sendResponseHeader(code, type, data.length);
104167
this.responseStream.write(data);
105168
this.responseStream.flush();
106169
this.responseStream.close();
107170
}
108171

109172
/**
110173
* @param code custom response code
111-
* @param filename filename for the html file to be sent .
174+
* @param type Content-Type
175+
* @param filename filename for the file to be sent .
112176
* @throws IOException throws exception if response channel closed .
113177
*/
114-
public void sendHtmlFileResponse(int code, String filename) throws IOException {
178+
public void sendFile(int code, String type, String filename) throws IOException {
115179

116-
String page = ServerHelper.getHtmlFromAsset(context, filename);
180+
byte[] data = ServerHelper.loadFileAsBytes(filename, true);
181+
sendResponseHeader(code, type, data.length);
182+
this.responseStream.write(data);
183+
this.responseStream.flush();
184+
this.responseStream.close();
185+
}
117186

118-
byte[] data = page.getBytes("UTF-8");
119-
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length);
187+
/**
188+
* @param code custom response code
189+
* @param type Content-Type
190+
* @param assetPath filename for the assets file to be sent .
191+
* @throws IOException throws exception if response channel closed .
192+
*/
193+
public void sendAssetMediaFile(int code, String type, String assetPath) throws IOException {
120194

195+
byte[] data = ServerHelper.loadFileFromAsset(context, assetPath, false);
196+
sendResponseHeader(code, type, data.length);
197+
this.responseStream.write(data);
198+
this.responseStream.flush();
199+
this.responseStream.close();
200+
}
201+
202+
/**
203+
* @param code custom response code
204+
* @param type Content-Type
205+
* @param filename filename for the file to be sent .
206+
* @throws IOException throws exception if response channel closed .
207+
*/
208+
public void sendMediaFile(int code, String type, String filename) throws IOException {
209+
210+
byte[] data = ServerHelper.loadFileAsBytes(filename, false);
211+
sendResponseHeader(code, type, data.length);
121212
this.responseStream.write(data);
122213
this.responseStream.flush();
123214
this.responseStream.close();
124215
}
125216

126217
/**
127218
* @param code custom response code
219+
* @param type Content-Type
128220
* @param filename filename for the html file to be sent .
129221
* @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
130222
* @throws IOException throws exception if response channel closed .
131223
*/
132-
public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String, String> placeHolders) throws IOException {
224+
public void sendFileWithPlaceHolder(int code, String type, String filename, @NonNull Map<String, String> placeHolders) throws IOException {
133225

134-
String page = ServerHelper.getHtmlFromAsset(context, filename);
226+
String page = new String(ServerHelper.loadFileAsBytes(filename, true), "UTF-8");
135227
String value;
136228
for (String key : placeHolders.keySet()) {
137229
value = placeHolders.get(key);
138230
page = page.replace(key, (value != null ? value : ""));
139231
}
140232

141233
byte[] data = page.getBytes("UTF-8");
142-
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length);
234+
sendResponseHeader(code, type, data.length);
143235

144236
this.responseStream.write(data);
145237
this.responseStream.flush();
146238
this.responseStream.close();
147239
}
148240

149241
/**
150-
* @param code custom response code
151-
* @param filename filename for the html file to be sent .
152-
* @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
153-
* @param customHeaders headers to be sent with the response .
242+
* @param code custom response code
243+
* @param type Content-Type
244+
* @param filename filename for the html file to be sent .
245+
* @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
154246
* @throws IOException throws exception if response channel closed .
155247
*/
156-
public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String, String> placeHolders, Map<String, String> customHeaders) throws IOException {
248+
public void sendAssetFileWithPlaceHolder(int code, String type, String filename, @NonNull Map<String, String> placeHolders) throws IOException {
157249

158-
String page = ServerHelper.getHtmlFromAsset(context, filename);
250+
String page = new String(ServerHelper.loadFileFromAsset(context, filename, true), "UTF-8");
159251
String value;
160252
for (String key : placeHolders.keySet()) {
161253
value = placeHolders.get(key);
162254
page = page.replace(key, (value != null ? value : ""));
163255
}
164256

165257
byte[] data = page.getBytes("UTF-8");
166-
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length, customHeaders);
258+
sendResponseHeader(code, type, data.length);
167259

168260
this.responseStream.write(data);
169261
this.responseStream.flush();
170262
this.responseStream.close();
171263
}
172264

173265
/**
174-
* @param filename filename for the html file to be sent .
175-
* @param contentType response content type .
266+
* @param code custom response code
267+
* @param type Content-Type
268+
* @param response Response to be sent .
176269
* @throws IOException throws exception if response channel closed .
177270
*/
178-
public void sendResourceFileResponse(String filename, String contentType) throws IOException {
179-
180-
BufferedInputStream file = ServerHelper.getResourceFromAsset(context, filename);
181-
sendResponseHeader(ServerHelper.RESPONSE_CODE.OK, contentType, file.available());
271+
public void sendResponse(int code, String type, @NonNull byte[] response) throws IOException {
182272

183-
int u;
184-
byte[] buffer = new byte[1024];
185-
while ((u = file.read(buffer, 0, buffer.length)) != -1) {
186-
this.responseStream.write(buffer, 0, u);
187-
}
188-
file.close();
273+
sendResponseHeader(code, type, response.length);
274+
this.responseStream.write(response);
189275
this.responseStream.flush();
190276
this.responseStream.close();
191277
}
192278

193-
public Context getContext() {
194-
return this.context;
195-
}
196-
197-
/**
198-
* @param image image to be sent .
199-
* @throws IOException throws exception if response channel closed .
200-
*/
201-
// Response With Image (Response closed after image sent)
202-
public void sendImageResponse(@NonNull byte[] image) throws IOException {
203-
204-
sendResponseHeader(ServerHelper.RESPONSE_CODE.OK, ServerHelper.CONTENT_TYPE.JPEG, image.length);
205-
206-
responseStream.write(image);
207-
responseStream.flush();
208-
responseStream.close();
209-
}
210-
211279
/**
212-
* @param sound sound to be sent .
280+
* @param code custom response code
281+
* @param type Content-Type
282+
* @param response Response to be sent .
283+
* @param customHeaders headers to be sent with the response .
213284
* @throws IOException throws exception if response channel closed .
214285
*/
215-
public void sendSoundResponse(@NonNull byte[] sound) throws IOException {
216-
217-
sendResponseHeader(ServerHelper.RESPONSE_CODE.OK, ServerHelper.CONTENT_TYPE.MPEG, sound.length);
286+
public void sendResponse(int code, String type, @NonNull byte[] response, Map<String, String> customHeaders) throws IOException {
218287

219-
responseStream.write(sound);
220-
responseStream.flush();
221-
responseStream.close();
288+
sendResponseHeader(code, type, response.length, customHeaders);
289+
this.responseStream.write(response);
290+
this.responseStream.flush();
291+
this.responseStream.close();
222292
}
223293

224294
// Stream (Response not closed after image sent)

0 commit comments

Comments
 (0)