Skip to content

Commit 82b0162

Browse files
committed
Fix issues with start with route .
Add Doc For parameters .
1 parent 74aadc5 commit 82b0162

3 files changed

Lines changed: 100 additions & 14 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private Route getRoute(String method, String path) {
107107
private Route getRouteWithStartWith(String method, String path) {
108108

109109
for (Route route : routeList) {
110-
if (route.isRouteStartWith() && route.getMethod().equals(method) && route.getPath().startsWith(path))
110+
if (route.isRouteStartWith() && route.getMethod().equals(method) && path.startsWith(route.getPath()))
111111
return route;
112112
}
113113

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ private ResponseHandler(Context context, DataOutputStream responseStream) {
2525
this.responseStream = responseStream;
2626
}
2727

28+
/**
29+
* @param json Json to be sent as Response .
30+
* @throws IOException throws exception if response channel closed .
31+
*/
2832
// JSON Responses ..
2933
public void sendJsonResponse(String json) throws IOException {
3034
if (json != null) {
@@ -36,6 +40,11 @@ public void sendJsonResponse(String json) throws IOException {
3640
this.responseStream.close();
3741
}
3842

43+
/**
44+
* @param json Json to be sent as Response .
45+
* @param customHeaders headers to be sent with the response .
46+
* @throws IOException throws exception if response channel closed .
47+
*/
3948
public void sendJsonResponse(String json, Map<String, String> customHeaders) throws IOException {
4049
if (json != null) {
4150
byte[] data = json.getBytes("UTF-8");
@@ -46,6 +55,12 @@ public void sendJsonResponse(String json, Map<String, String> customHeaders) thr
4655
this.responseStream.close();
4756
}
4857

58+
/**
59+
* @param code custom response code
60+
* @param json Json to be sent as Response .
61+
* @param customHeaders headers to be sent with the response .
62+
* @throws IOException throws exception if response channel closed .
63+
*/
4964
public void sendJsonResponse(int code, String json, Map<String, String> customHeaders) throws IOException {
5065
if (json != null) {
5166
byte[] data = json.getBytes("UTF-8");
@@ -56,6 +71,11 @@ public void sendJsonResponse(int code, String json, Map<String, String> customHe
5671
this.responseStream.close();
5772
}
5873

74+
/**
75+
* @param code custom response code
76+
* @param json Json to be sent as Response .
77+
* @throws IOException throws exception if response channel closed .
78+
*/
5979
public void sendJsonResponse(int code, String json) throws IOException {
6080
if (json != null) {
6181
byte[] data = json.getBytes("UTF-8");
@@ -66,19 +86,30 @@ public void sendJsonResponse(int code, String json) throws IOException {
6686
this.responseStream.close();
6787
}
6888

89+
/**
90+
* @param code custom response code
91+
* @param filename filename for the html file to be sent .
92+
* @param customHeaders headers to be sent with the response .
93+
* @throws IOException throws exception if response channel closed .
94+
*/
6995
// HTML Response ..
70-
public void sendHtmlFileResponseWithCustomHeader(int code, String filename,Map<String,String>customHeaders) throws IOException {
96+
public void sendHtmlFileResponseWithCustomHeader(int code, String filename, Map<String, String> customHeaders) throws IOException {
7197

7298
String page = ServerHelper.getHtmlFromAsset(context, filename);
7399

74100
byte[] data = page.getBytes();
75-
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length,customHeaders);
101+
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length, customHeaders);
76102

77103
this.responseStream.write(data);
78104
this.responseStream.flush();
79105
this.responseStream.close();
80106
}
81107

108+
/**
109+
* @param code custom response code
110+
* @param filename filename for the html file to be sent .
111+
* @throws IOException throws exception if response channel closed .
112+
*/
82113
public void sendHtmlFileResponse(int code, String filename) throws IOException {
83114

84115
String page = ServerHelper.getHtmlFromAsset(context, filename);
@@ -91,6 +122,12 @@ public void sendHtmlFileResponse(int code, String filename) throws IOException {
91122
this.responseStream.close();
92123
}
93124

125+
/**
126+
* @param code custom response code
127+
* @param filename filename for the html file to be sent .
128+
* @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
129+
* @throws IOException throws exception if response channel closed .
130+
*/
94131
public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String, String> placeHolders) throws IOException {
95132

96133
String page = ServerHelper.getHtmlFromAsset(context, filename);
@@ -108,7 +145,14 @@ public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String,
108145
this.responseStream.close();
109146
}
110147

111-
public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String, String> placeHolders,Map<String,String>customHeaders) throws IOException {
148+
/**
149+
* @param code custom response code
150+
* @param filename filename for the html file to be sent .
151+
* @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
152+
* @param customHeaders headers to be sent with the response .
153+
* @throws IOException throws exception if response channel closed .
154+
*/
155+
public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String, String> placeHolders, Map<String, String> customHeaders) throws IOException {
112156

113157
String page = ServerHelper.getHtmlFromAsset(context, filename);
114158
String value;
@@ -118,7 +162,7 @@ public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String,
118162
}
119163

120164
byte[] data = page.getBytes();
121-
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length,customHeaders);
165+
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length, customHeaders);
122166

123167
this.responseStream.write(data);
124168
this.responseStream.flush();
@@ -129,6 +173,10 @@ public Context getContext() {
129173
return this.context;
130174
}
131175

176+
/**
177+
* @param image image to be sent .
178+
* @throws IOException throws exception if response channel closed .
179+
*/
132180
// Response With Image (Response closed after image sent)
133181
public void sendImageResponse(@NonNull byte[] image) throws IOException {
134182

@@ -139,6 +187,10 @@ public void sendImageResponse(@NonNull byte[] image) throws IOException {
139187
responseStream.close();
140188
}
141189

190+
/**
191+
* @param sound sound to be sent .
192+
* @throws IOException throws exception if response channel closed .
193+
*/
142194
public void sendSoundResponse(@NonNull byte[] sound) throws IOException {
143195

144196
sendResponseHeader(ServerHelper.RESPONSE_CODE.OK, ServerHelper.CONTENT_TYPE.MPEG, sound.length);

server/src/main/java/com/androthink/server/model/Route.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class Route {
99
private boolean isRouteStartWith;
1010
private RouteCallBack callBack;
1111

12+
/**
13+
* @param path route path
14+
* @param method request method
15+
* @param callBack response callback
16+
*/
1217
public Route(String path, String method, RouteCallBack callBack) {
1318
this.path = path;
1419
this.isAuth = false;
@@ -17,14 +22,27 @@ public Route(String path, String method, RouteCallBack callBack) {
1722
this.callBack = callBack;
1823
}
1924

20-
public Route(String path, String method,boolean isAuth, RouteCallBack callBack) {
25+
/**
26+
* @param path route path
27+
* @param method request method
28+
* @param isAuth is authenticated
29+
* @param callBack response callback
30+
*/
31+
public Route(String path, String method, boolean isAuth, RouteCallBack callBack) {
2132
this.path = path;
2233
this.isAuth = isAuth;
2334
this.method = method;
2435
this.callBack = callBack;
2536
this.isRouteStartWith = false;
2637
}
2738

39+
/**
40+
* @param path route path
41+
* @param method request method
42+
* @param isAuth is authenticated
43+
* @param isRouteStartWith is route paths is a start with
44+
* @param callBack response callback
45+
*/
2846
public Route(String path, String method, boolean isAuth, boolean isRouteStartWith, RouteCallBack callBack) {
2947
this.path = path;
3048
this.method = method;
@@ -41,19 +59,35 @@ public void setRouteStartWith(boolean routeStartWith) {
4159
isRouteStartWith = routeStartWith;
4260
}
4361

44-
public String getPath() { return path; }
62+
public String getPath() {
63+
return path;
64+
}
4565

46-
public void setPath(String path) { this.path = path; }
66+
public void setPath(String path) {
67+
this.path = path;
68+
}
4769

48-
public String getMethod() { return method; }
70+
public String getMethod() {
71+
return method;
72+
}
4973

50-
public void setMethod(String method) { this.method = method; }
74+
public void setMethod(String method) {
75+
this.method = method;
76+
}
5177

52-
public boolean isAuth() { return isAuth; }
78+
public boolean isAuth() {
79+
return isAuth;
80+
}
5381

54-
public void setAuth(boolean auth) { isAuth = auth; }
82+
public void setAuth(boolean auth) {
83+
isAuth = auth;
84+
}
5585

56-
public RouteCallBack getCallBack() { return callBack; }
86+
public RouteCallBack getCallBack() {
87+
return callBack;
88+
}
5789

58-
public void setCallBack(RouteCallBack callBack) { this.callBack = callBack; }
90+
public void setCallBack(RouteCallBack callBack) {
91+
this.callBack = callBack;
92+
}
5993
}

0 commit comments

Comments
 (0)