diff --git a/template/java8/entrypoint/src/main/java/com/openfaas/entrypoint/App.java b/template/java8/entrypoint/src/main/java/com/openfaas/entrypoint/App.java index 3bebcd48..ccd1ac32 100644 --- a/template/java8/entrypoint/src/main/java/com/openfaas/entrypoint/App.java +++ b/template/java8/entrypoint/src/main/java/com/openfaas/entrypoint/App.java @@ -45,17 +45,7 @@ public void handle(HttpExchange t) throws IOException { String requestBody = ""; String method = t.getRequestMethod(); - if (method.equalsIgnoreCase("POST")) { - InputStream inputStream = t.getRequestBody(); - ByteArrayOutputStream result = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024]; - int length; - while ((length = inputStream.read(buffer)) != -1) { - result.write(buffer, 0, length); - } - // StandardCharsets.UTF_8.name() > JDK 7 - requestBody = result.toString("UTF-8"); - } + // System.out.println(requestBody); Headers reqHeaders = t.getRequestHeaders(); @@ -72,12 +62,11 @@ public void handle(HttpExchange t) throws IOException { // System.out.println("Req header " + entry.getKey() + " " + entry.getValue()); // } - IRequest req = new Request(requestBody, reqHeadersMap,t.getRequestURI().getRawQuery(), t.getRequestURI().getPath()); + IRequest req = new Request(reqHeadersMap,t.getRequestURI().getRawQuery(), t.getRequestURI().getPath(), t.getRequestBody()); IResponse res = this.handler.Handle(req); - String response = res.getBody(); - byte[] bytesOut = response.getBytes("UTF-8"); + byte[] bytesOut = res.getBodyData(); Headers responseHeaders = t.getResponseHeaders(); String contentType = res.getContentType(); @@ -93,7 +82,7 @@ public void handle(HttpExchange t) throws IOException { OutputStream os = t.getResponseBody(); os.write(bytesOut); - os.close(); + t.close(); System.out.println("Request / " + Integer.toString(bytesOut.length) +" bytes written."); } diff --git a/template/java8/model/src/main/java/com/openfaas/model/IRequest.java b/template/java8/model/src/main/java/com/openfaas/model/IRequest.java index 09d5afbf..26e7c869 100644 --- a/template/java8/model/src/main/java/com/openfaas/model/IRequest.java +++ b/template/java8/model/src/main/java/com/openfaas/model/IRequest.java @@ -3,6 +3,7 @@ package com.openfaas.model; +import java.io.InputStream; import java.util.Map; public interface IRequest { @@ -13,4 +14,5 @@ public interface IRequest { Map getQuery(); String getPathRaw(); Map getPath(); + InputStream getInputStream(); } \ No newline at end of file diff --git a/template/java8/model/src/main/java/com/openfaas/model/IResponse.java b/template/java8/model/src/main/java/com/openfaas/model/IResponse.java index eb66940b..3a6c7972 100644 --- a/template/java8/model/src/main/java/com/openfaas/model/IResponse.java +++ b/template/java8/model/src/main/java/com/openfaas/model/IResponse.java @@ -7,7 +7,9 @@ public interface IResponse { String getBody(); + byte[] getBodyData(); void setBody(String body); + void setBodyData(byte[] body); String getHeader(String key); void setHeader(String key, String value); diff --git a/template/java8/model/src/main/java/com/openfaas/model/Request.java b/template/java8/model/src/main/java/com/openfaas/model/Request.java index 7df3e45a..a66f06e6 100644 --- a/template/java8/model/src/main/java/com/openfaas/model/Request.java +++ b/template/java8/model/src/main/java/com/openfaas/model/Request.java @@ -3,10 +3,11 @@ package com.openfaas.model; +import java.io.*; +import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.HashMap; import java.net.URLDecoder; -import java.io.UnsupportedEncodingException; public class Request implements IRequest { @@ -17,23 +18,45 @@ public class Request implements IRequest { private String queryRaw; private String pathRaw; private Map path; + private InputStream inputStream; + + public Request(String body, Map headers) { + this(body, headers, "", ""); + } + + public Request(String body, Map headers, String queryRaw, String path) { + this(headers, queryRaw, path, new ByteArrayInputStream(body != null ? body.getBytes(StandardCharsets.UTF_8) : new byte[0])); - public Request(String body, Map headers) { - this.body = body; - this.headers = headers; } - - public Request(String body, Map headers,String queryRaw, String path) { - this.body = body; + + public Request(Map headers, String queryRaw, String path, InputStream inputStream) { this.headers = headers; this.queryRaw = queryRaw; - this.queryParameters = this.parseQueryParameters(); + this.inputStream = inputStream; + this.queryParameters = this.parseQueryParameters(); this.pathRaw = path; this.path = this.parsePathParameters(); } public String getBody() { - return this.body; + if (this.body != null) { + return this.body; + } + try { + ByteArrayOutputStream result = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) != -1) { + result.write(buffer, 0, length); + } + this.body = result.toString(StandardCharsets.UTF_8.name()); + return this.body; + } catch (IOException e) { + // ByteArrayOutputStream.write does not actually throw any IOException, + // but OutputStream.write is declared to throw one. + // To not break getBody(), throw the exception wrapped in a RuntimeException, but it should never be thrown + throw new RuntimeException(e); + } } public Map getHeaders() { @@ -118,4 +141,8 @@ private Map parseQueryParameters() { return reqParametersMap; } + @Override + public InputStream getInputStream() { + return inputStream; + } } \ No newline at end of file diff --git a/template/java8/model/src/main/java/com/openfaas/model/Response.java b/template/java8/model/src/main/java/com/openfaas/model/Response.java index dee5b35f..d55b6bc2 100644 --- a/template/java8/model/src/main/java/com/openfaas/model/Response.java +++ b/template/java8/model/src/main/java/com/openfaas/model/Response.java @@ -3,18 +3,20 @@ package com.openfaas.model; +import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; 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 headers; public Response() { - this.body = ""; + this.body = new byte[0]; this.contentType = ""; this.headers = new HashMap(); } @@ -58,10 +60,21 @@ public String getContentType() { } public void setBody(String body) { + this.body = body.getBytes(StandardCharsets.UTF_8); + } + + + @Override + public void setBodyData(byte[] body) { this.body = body; } public String getBody() { + return new String(this.body); + } + + @Override + public byte[] getBodyData() { return this.body; } } diff --git a/template/java8/model/src/test/java/RequestTest.java b/template/java8/model/src/test/java/RequestTest.java index c2523e07..2ad4fe5a 100644 --- a/template/java8/model/src/test/java/RequestTest.java +++ b/template/java8/model/src/test/java/RequestTest.java @@ -1,6 +1,9 @@ import org.junit.Test; import static org.junit.Assert.*; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -10,43 +13,43 @@ public class RequestTest { @Test public void testSingleRequestParameterGetSet() { - Request request = new Request(null,null,"testParam=testParamValue", null); + Request request = new Request((String)null, null, "testParam=testParamValue", null); assertEquals("testParamValue", request.getQuery().get("testParam")); } - + @Test public void testMultipleRequestParametersGetSet() { - Request request = new Request(null,null,"testParam1=testParamValue1&testParam2=testParamValue2", null); + Request request = new Request("", null,"testParam1=testParamValue1&testParam2=testParamValue2", null); assertEquals("testParamValue1", request.getQuery().get("testParam1")); assertEquals("testParamValue2", request.getQuery().get("testParam2")); } - + @Test public void testNullRequestParameterGetSet() { - Request request = new Request(null,null,null, null); + Request request = new Request("",null,null, null); assertEquals(null, request.getQuery().get("testParam")); } - + @Test public void testEmptyRequestParameterGetSet() { - Request request = new Request(null,null,"", null); + Request request = new Request("", null, "", null); assertEquals(null, request.getQuery().get("testParam")); } - + @Test public void testRequestRawGetSet() { - Request request = new Request(null,null,"testRaw=testRawValue", null); + Request request = new Request("", null,"testRaw=testRawValue", null); assertEquals("testRaw=testRawValue", request.getQueryRaw()); } @Test public void testGetPath() { - Request request = new Request(null,null, null, "/test/path"); + Request request = new Request("", null, null, "/test/path"); try { assertEquals("/test/path", request.getPathRaw()); } catch (AssertionError e) { @@ -58,7 +61,7 @@ public void testGetPath() @Test public void testGetPathWithNullPath() { - Request request = new Request(null,null, null, null); + Request request = new Request("", null,null, null); try { assertNull(request.getPathRaw()); } catch (AssertionError e) { @@ -70,7 +73,7 @@ public void testGetPathWithNullPath() @Test public void testParseParametersWithoutAnyParameters() { - Request request = new Request(null,null, null, "/"); + Request request = new Request("", null, null, "/"); try { assertEquals(0, request.getPath().size()); } catch (AssertionError e) { @@ -78,7 +81,7 @@ public void testParseParametersWithoutAnyParameters() throw e; } - Request emptyRequest = new Request(null,null, null, ""); + Request emptyRequest = new Request(null, null, null, ""); try { assertEquals(0, emptyRequest.getPath().size()); } catch (AssertionError e) { @@ -90,7 +93,7 @@ public void testParseParametersWithoutAnyParameters() @Test public void testParseParametersWithEvenParameters() { - Request request = new Request(null,null, null, "/param1/value1/param2/value2"); + Request request = new Request("", null, null, "/param1/value1/param2/value2"); Map params = request.getPath(); try { @@ -110,7 +113,7 @@ public void testParseParametersWithEvenParameters() @Test public void testParseParametersWithOddParameters() { - Request request = new Request(null,null, null, "/param1/value1/param2"); + Request request = new Request("", null, null, "/param1/value1/param2"); Map params = request.getPath(); try { @@ -126,4 +129,61 @@ public void testParseParametersWithOddParameters() throw e; } } + + @Test + public void testRequestGetBodyWithString() { + Request request = new Request(null, null, null, new ByteArrayInputStream("Øl får meg glad".getBytes())); + assertEquals("Øl får meg glad", request.getBody()); + } + + @Test + public void testRequestInputStream() { + try { + String requestBody = "Øl får meg glad"; + byte[] originalData = requestBody.getBytes(); + Request request = new Request(null, null, null, new ByteArrayInputStream(requestBody.getBytes())); + byte[] buffer = new byte[64]; + int read = request.getInputStream().read(buffer); + byte[] requestBodyData = new byte[read]; + System.arraycopy(buffer, 0, requestBodyData, 0, read); + assertTrue(Arrays.equals(originalData, requestBodyData)); + } catch (IOException e) { + throw new AssertionError(e); + } catch (AssertionError e) { + System.out.println(e.getMessage()); + throw e; + } + } + + @Test + public void testAlternateSetBodyGetBodyData() { + try { + String requestBody = "Øl får meg glad"; + byte[] originalData = requestBody.getBytes(); + Request request = new Request(requestBody, null, null, null); + byte[] buffer = new byte[64]; + int read = request.getInputStream().read(buffer); + byte[] requestBodyData = new byte[read]; + System.arraycopy(buffer, 0, requestBodyData, 0, read); + assertTrue(Arrays.equals(originalData, requestBodyData)); + } catch (IOException e) { + throw new AssertionError(e); + } catch (AssertionError e) { + System.out.println(e.getMessage()); + throw e; + } + } + + @Test + public void testAlternateSetBodyDataGetBody() { + try { + String originalRequestBody = "Øl får meg glad"; + Request request = new Request(null, null, null, new ByteArrayInputStream(originalRequestBody.getBytes())); + String requestBody = request.getBody(); + assertEquals(originalRequestBody, requestBody); + } catch (AssertionError e) { + System.out.println(e.getMessage()); + throw e; + } + } } \ No newline at end of file diff --git a/template/java8/model/src/test/java/ResponseTest.java b/template/java8/model/src/test/java/ResponseTest.java index 72c307a2..20868bb9 100644 --- a/template/java8/model/src/test/java/ResponseTest.java +++ b/template/java8/model/src/test/java/ResponseTest.java @@ -8,6 +8,8 @@ import com.openfaas.model.Response; +import java.util.Arrays; + public class ResponseTest { @Test public void testResponseHeaderSetGetValue() { Response r = new Response(); @@ -49,4 +51,18 @@ public class ResponseTest { r.setStatusCode(404); assertEquals(404, r.getStatusCode()); } + + @Test public void testResponseSetBodyWithString() { + Response r = new Response(); + r.setBody("Øl får meg glad"); + assertEquals(r.getBody(), "Øl får meg glad"); + } + + @Test public void testResponseSetBodyWithBytes() { + Response r = new Response(); + r.setBodyData("Øl får meg glad".getBytes()); + assertEquals(r.getBody(), "Øl får meg glad"); + + assertTrue(Arrays.equals(r.getBodyData(), "Øl får meg glad".getBytes())); + } }