Skip to content

Commit

Permalink
Fix issues with start with route .
Browse files Browse the repository at this point in the history
Add Doc For parameters .
  • Loading branch information
AndroThink committed Nov 22, 2019
1 parent 74aadc5 commit 82b0162
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private Route getRoute(String method, String path) {
private Route getRouteWithStartWith(String method, String path) {

for (Route route : routeList) {
if (route.isRouteStartWith() && route.getMethod().equals(method) && route.getPath().startsWith(path))
if (route.isRouteStartWith() && route.getMethod().equals(method) && path.startsWith(route.getPath()))
return route;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ private ResponseHandler(Context context, DataOutputStream responseStream) {
this.responseStream = responseStream;
}

/**
* @param json Json to be sent as Response .
* @throws IOException throws exception if response channel closed .
*/
// JSON Responses ..
public void sendJsonResponse(String json) throws IOException {
if (json != null) {
Expand All @@ -36,6 +40,11 @@ public void sendJsonResponse(String json) throws IOException {
this.responseStream.close();
}

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

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

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

/**
* @param code custom response code
* @param filename filename for the html file to be sent .
* @param customHeaders headers to be sent with the response .
* @throws IOException throws exception if response channel closed .
*/
// HTML Response ..
public void sendHtmlFileResponseWithCustomHeader(int code, String filename,Map<String,String>customHeaders) throws IOException {
public void sendHtmlFileResponseWithCustomHeader(int code, String filename, Map<String, String> customHeaders) throws IOException {

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

byte[] data = page.getBytes();
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length,customHeaders);
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length, customHeaders);

this.responseStream.write(data);
this.responseStream.flush();
this.responseStream.close();
}

/**
* @param code custom response code
* @param filename filename for the html file to be sent .
* @throws IOException throws exception if response channel closed .
*/
public void sendHtmlFileResponse(int code, String filename) throws IOException {

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

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

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

public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String, String> placeHolders,Map<String,String>customHeaders) throws IOException {
/**
* @param code custom response code
* @param filename filename for the html file to be sent .
* @param placeHolders data values to be replaced with placeholders in the file to be sent with the response .
* @param customHeaders headers to be sent with the response .
* @throws IOException throws exception if response channel closed .
*/
public void sendHtmlFileResponse(int code, String filename, @NonNull Map<String, String> placeHolders, Map<String, String> customHeaders) throws IOException {

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

byte[] data = page.getBytes();
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length,customHeaders);
sendResponseHeader(code, ServerHelper.CONTENT_TYPE.HTML, data.length, customHeaders);

this.responseStream.write(data);
this.responseStream.flush();
Expand All @@ -129,6 +173,10 @@ public Context getContext() {
return this.context;
}

/**
* @param image image to be sent .
* @throws IOException throws exception if response channel closed .
*/
// Response With Image (Response closed after image sent)
public void sendImageResponse(@NonNull byte[] image) throws IOException {

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

/**
* @param sound sound to be sent .
* @throws IOException throws exception if response channel closed .
*/
public void sendSoundResponse(@NonNull byte[] sound) throws IOException {

sendResponseHeader(ServerHelper.RESPONSE_CODE.OK, ServerHelper.CONTENT_TYPE.MPEG, sound.length);
Expand Down
52 changes: 43 additions & 9 deletions server/src/main/java/com/androthink/server/model/Route.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public class Route {
private boolean isRouteStartWith;
private RouteCallBack callBack;

/**
* @param path route path
* @param method request method
* @param callBack response callback
*/
public Route(String path, String method, RouteCallBack callBack) {
this.path = path;
this.isAuth = false;
Expand All @@ -17,14 +22,27 @@ public Route(String path, String method, RouteCallBack callBack) {
this.callBack = callBack;
}

public Route(String path, String method,boolean isAuth, RouteCallBack callBack) {
/**
* @param path route path
* @param method request method
* @param isAuth is authenticated
* @param callBack response callback
*/
public Route(String path, String method, boolean isAuth, RouteCallBack callBack) {
this.path = path;
this.isAuth = isAuth;
this.method = method;
this.callBack = callBack;
this.isRouteStartWith = false;
}

/**
* @param path route path
* @param method request method
* @param isAuth is authenticated
* @param isRouteStartWith is route paths is a start with
* @param callBack response callback
*/
public Route(String path, String method, boolean isAuth, boolean isRouteStartWith, RouteCallBack callBack) {
this.path = path;
this.method = method;
Expand All @@ -41,19 +59,35 @@ public void setRouteStartWith(boolean routeStartWith) {
isRouteStartWith = routeStartWith;
}

public String getPath() { return path; }
public String getPath() {
return path;
}

public void setPath(String path) { this.path = path; }
public void setPath(String path) {
this.path = path;
}

public String getMethod() { return method; }
public String getMethod() {
return method;
}

public void setMethod(String method) { this.method = method; }
public void setMethod(String method) {
this.method = method;
}

public boolean isAuth() { return isAuth; }
public boolean isAuth() {
return isAuth;
}

public void setAuth(boolean auth) { isAuth = auth; }
public void setAuth(boolean auth) {
isAuth = auth;
}

public RouteCallBack getCallBack() { return callBack; }
public RouteCallBack getCallBack() {
return callBack;
}

public void setCallBack(RouteCallBack callBack) { this.callBack = callBack; }
public void setCallBack(RouteCallBack callBack) {
this.callBack = callBack;
}
}

0 comments on commit 82b0162

Please sign in to comment.