Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update java11 template add getBodyBytes setBodyBytes #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private InvokeHandler(IHandler handler) {

@Override
public void handle(HttpExchange t) throws IOException {
String requestBody = "";
byte[] requestBody = null;
String method = t.getRequestMethod();

if (method.equalsIgnoreCase("POST")) {
Expand All @@ -54,9 +54,9 @@ public void handle(HttpExchange t) throws IOException {
result.write(buffer, 0, length);
}
// StandardCharsets.UTF_8.name() > JDK 7
requestBody = result.toString("UTF-8");
requestBody = result.toByteArray();
}

// System.out.println(requestBody);
Headers reqHeaders = t.getRequestHeaders();
Map<String, String> reqHeadersMap = new HashMap<String, String>();
Expand All @@ -73,11 +73,10 @@ public void handle(HttpExchange t) throws IOException {
// }

IRequest req = new Request(requestBody, reqHeadersMap,t.getRequestURI().getRawQuery(), t.getRequestURI().getPath());

IResponse res = this.handler.Handle(req);

String response = res.getBody();
byte[] bytesOut = response.getBytes("UTF-8");
byte[] bytesOut = res.getBodyBytes();

Headers responseHeaders = t.getResponseHeaders();
String contentType = res.getContentType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@

public interface IRequest {
String getBody();
byte[] getBodyBytes();
Map<String, String> getHeaders();
String getHeader(String key);
String getQueryRaw();
Map<String, String> getQuery();
String getPathRaw();
Map<String, String> getPath();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public interface IResponse {
String getBody();
void setBody(String body);

byte[] getBodyBytes();
void setBodyBytes(byte[] body);

String getHeader(String key);
void setHeader(String key, String value);
Map<String, String> getHeaders();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
public class Request implements IRequest {

private Map<String, String> headers;
private String body;
private byte[] body;
private Map<String, String> queryParameters;
private String queryRaw;
private String pathRaw;
private Map<String, String> path;

public Request(String body, Map<String, String> headers) {
public Request(byte[] body, Map<String, String> headers) {
this.body = body;
this.headers = headers;
}
public Request(String body, Map<String, String> headers,String queryRaw, String path) {

public Request(byte[] body, Map<String, String> headers,String queryRaw, String path) {
this.body = body;
this.headers = headers;
this.queryRaw = queryRaw;
Expand All @@ -33,6 +33,14 @@ public Request(String body, Map<String, String> headers,String queryRaw, String
}

public String getBody() {
try {
return new String(this.body, "UTF-8");
} catch (UnsupportedEncodingException e) {
return new String(this.body);
}
}

public byte[] getBodyBytes() {
return this.body;
}

Expand All @@ -47,7 +55,7 @@ public String getHeader(String key) {

return this.headers.get(key);
}

@Override
public String getQueryRaw() {
return queryRaw;
Expand Down Expand Up @@ -90,7 +98,7 @@ private Map<String, String> parsePathParameters() {

return res;
}

private Map<String, String> parseQueryParameters() {
Map<String, String> reqParametersMap = new HashMap<String, String>();
if (queryRaw != null) {
Expand Down Expand Up @@ -118,4 +126,4 @@ private Map<String, String> parseQueryParameters() {
return reqParametersMap;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

package com.openfaas.model;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class Response implements IResponse {

private int statusCode = 200;
private String body;
private byte[] body;
private String contentType;
private Map<String, String> headers;

public Response() {
this.body = "";
this.body = null;
this.contentType = "";
this.headers = new HashMap<String, String>();
}
Expand Down Expand Up @@ -58,10 +59,26 @@ public String getContentType() {
}

public void setBody(String body) {
try {
this.body = body.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
this.body = null;
}
}

public void setBodyBytes(byte[] body) {
this.body = body;
}

public String getBody() {
try {
return new String(this.body, "UTF-8");
} catch (UnsupportedEncodingException e) {
return new String(this.body);
}
}

public byte[] getBodyBytes() {
return this.body;
}
}