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

Issue #159 Added support for binary data in request and response #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -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();
erlendv marked this conversation as resolved.
Show resolved Hide resolved

Headers responseHeaders = t.getResponseHeaders();
String contentType = res.getContentType();
Expand All @@ -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.");
}
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 setBodyData(byte[] body);

String getHeader(String key);
void setHeader(String key, String value);
Expand Down
45 changes: 36 additions & 9 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,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 {
Expand All @@ -17,23 +18,45 @@ 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, headers, "", "");
}

public Request(String body, Map<String, String> 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<String, String> headers) {
erlendv marked this conversation as resolved.
Show resolved Hide resolved
this.body = body;
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);
}
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<String, String> getHeaders() {
Expand Down Expand Up @@ -118,4 +141,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,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<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 +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;
}
}
90 changes: 75 additions & 15 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,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) {
Expand All @@ -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) {
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, "/");
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");
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");
Map<String, String> params = request.getPath();

try {
Expand All @@ -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;
}
}
}
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 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()));
}
erlendv marked this conversation as resolved.
Show resolved Hide resolved
}