Skip to content

Commit

Permalink
Added support for binary data in request and response
Browse files Browse the repository at this point in the history
Signed-off-by: Erlend Valle <[email protected]>
  • Loading branch information
erlendv committed Jul 19, 2019
1 parent f498818 commit d843d6f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.openfaas.model;

import java.io.InputStream;
import java.util.Map;

public interface IRequest {
Expand All @@ -13,4 +14,5 @@ public interface IRequest {
Map<String, String> getQuery();
String getPathRaw();
Map<String, String> getPath();
InputStream getInputStream();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

public interface IResponse {
String getBody();
byte[] getBodyData();
void setBody(String body);
void setBody(byte[] body);

String getHeader(String key);
void setHeader(String key, String value);
Expand Down
35 changes: 29 additions & 6 deletions template/java8/model/src/main/java/com/openfaas/model/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

package com.openfaas.model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.HashMap;
import java.net.URLDecoder;
Expand All @@ -17,23 +20,39 @@ public class Request implements IRequest {
private String queryRaw;
private String pathRaw;
private Map<String, String> path;
private InputStream inputStream;

public Request(String body, Map<String, String> headers) {
this.body = body;
public Request(Map<String, String> headers) {
this.headers = headers;
}

public Request(String body, Map<String, String> headers,String queryRaw, String path) {
this.body = body;
public Request(Map<String, String> 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);
}
// StandardCharsets.UTF_8.name() > JDK 7
this.body = result.toString("UTF-8");
return this.body;
} catch (IOException e) {
// this method should be throwing the exception, but that would break with current behaviour
return "";
}
}

public Map<String, String> getHeaders() {
Expand Down Expand Up @@ -118,4 +137,8 @@ private Map<String, String> parseQueryParameters() {
return reqParametersMap;
}

@Override
public InputStream getInputStream() {
return inputStream;
}
}
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 = new byte[0];
this.contentType = "";
this.headers = new HashMap<String, String>();
}
Expand Down Expand Up @@ -58,10 +59,25 @@ public String getContentType() {
}

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


@Override
public void setBody(byte[] body) {
this.body = body;
}

public String getBody() {
return new String(this.body);
}

@Override
public byte[] getBodyData() {
return this.body;
}
}
41 changes: 33 additions & 8 deletions template/java8/model/src/test/java/RequestTest.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -10,14 +13,14 @@ public class RequestTest {
@Test
public void testSingleRequestParameterGetSet()
{
Request request = new Request(null,null,"testParam=testParamValue", null);
Request request = new Request(null,"testParam=testParamValue", null, 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, null);
assertEquals("testParamValue1", request.getQuery().get("testParam1"));
assertEquals("testParamValue2", request.getQuery().get("testParam2"));
}
Expand All @@ -39,14 +42,14 @@ public void testEmptyRequestParameterGetSet()
@Test
public void testRequestRawGetSet()
{
Request request = new Request(null,null,"testRaw=testRawValue", null);
Request request = new Request(null,"testRaw=testRawValue", null, 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", new ByteArrayInputStream(new byte[0]));
try {
assertEquals("/test/path", request.getPathRaw());
} catch (AssertionError e) {
Expand All @@ -70,15 +73,15 @@ public void testGetPathWithNullPath()
@Test
public void testParseParametersWithoutAnyParameters()
{
Request request = new Request(null,null, null, "/");
Request request = new Request(null, null, "/", null);
try {
assertEquals(0, request.getPath().size());
} catch (AssertionError e) {
System.out.format("Expected: 0 Got: %s", request.getPath().size());
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) {
Expand All @@ -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", new ByteArrayInputStream(new byte[0]));
Map<String, String> params = request.getPath();

try {
Expand All @@ -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", new ByteArrayInputStream(new byte[0]));
Map<String, String> params = request.getPath();

try {
Expand All @@ -126,4 +129,26 @@ public void testParseParametersWithOddParameters()
throw e;
}
}

@Test
public void testRequestGetBodyWorksAsBefore() {
Request request = new Request(null, null, null, new ByteArrayInputStream("Hello, world".getBytes()));
assertEquals("Hello, world", request.getBody());
}

@Test
public void testRequestInputStream() {
try {
String requestBody = "Hello, world";
byte[] originalData = requestBody.getBytes();
Request request = new Request(null, null, null, new ByteArrayInputStream(requestBody.getBytes()));
byte[] requestBodyData = new byte[requestBody.length()];
request.getInputStream().read(requestBodyData);
assertTrue(Arrays.equals(originalData, requestBodyData));
} catch (IOException e) {
throw new AssertionError(e);
}
}


}
16 changes: 16 additions & 0 deletions template/java8/model/src/test/java/ResponseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.openfaas.model.Response;

import java.util.Arrays;

public class ResponseTest {
@Test public void testResponseHeaderSetGetValue() {
Response r = new Response();
Expand Down Expand Up @@ -49,4 +51,18 @@ public class ResponseTest {
r.setStatusCode(404);
assertEquals(404, r.getStatusCode());
}

@Test public void testResponseSetBodyWorksAsBefore() {
Response r = new Response();
r.setBody("Hello, world");
assertEquals(r.getBody(), "Hello, world");
}

@Test public void testResponseSetBodyData() {
Response r = new Response();
r.setBody("Hello, world".getBytes());
assertEquals(r.getBody(), "Hello, world");

assertTrue(Arrays.equals(r.getBodyData(), "Hello, world".getBytes()));
}
}

0 comments on commit d843d6f

Please sign in to comment.