Skip to content

Commit ebd1033

Browse files
committed
Added support for binary data in request and response
Signed-off-by: Erlend Valle <[email protected]>
1 parent f498818 commit ebd1033

File tree

7 files changed

+150
-41
lines changed

7 files changed

+150
-41
lines changed

template/java8/entrypoint/src/main/java/com/openfaas/entrypoint/App.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,7 @@ public void handle(HttpExchange t) throws IOException {
4545
String requestBody = "";
4646
String method = t.getRequestMethod();
4747

48-
if (method.equalsIgnoreCase("POST")) {
49-
InputStream inputStream = t.getRequestBody();
50-
ByteArrayOutputStream result = new ByteArrayOutputStream();
51-
byte[] buffer = new byte[1024];
52-
int length;
53-
while ((length = inputStream.read(buffer)) != -1) {
54-
result.write(buffer, 0, length);
55-
}
56-
// StandardCharsets.UTF_8.name() > JDK 7
57-
requestBody = result.toString("UTF-8");
58-
}
48+
5949

6050
// System.out.println(requestBody);
6151
Headers reqHeaders = t.getRequestHeaders();
@@ -72,12 +62,11 @@ public void handle(HttpExchange t) throws IOException {
7262
// System.out.println("Req header " + entry.getKey() + " " + entry.getValue());
7363
// }
7464

75-
IRequest req = new Request(requestBody, reqHeadersMap,t.getRequestURI().getRawQuery(), t.getRequestURI().getPath());
65+
IRequest req = new Request(reqHeadersMap,t.getRequestURI().getRawQuery(), t.getRequestURI().getPath(), t.getRequestBody());
7666

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

79-
String response = res.getBody();
80-
byte[] bytesOut = response.getBytes("UTF-8");
69+
byte[] bytesOut = res.getBodyData();
8170

8271
Headers responseHeaders = t.getResponseHeaders();
8372
String contentType = res.getContentType();
@@ -93,7 +82,7 @@ public void handle(HttpExchange t) throws IOException {
9382

9483
OutputStream os = t.getResponseBody();
9584
os.write(bytesOut);
96-
os.close();
85+
t.close();
9786

9887
System.out.println("Request / " + Integer.toString(bytesOut.length) +" bytes written.");
9988
}

template/java8/model/src/main/java/com/openfaas/model/IRequest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package com.openfaas.model;
55

6+
import java.io.InputStream;
67
import java.util.Map;
78

89
public interface IRequest {
@@ -13,4 +14,5 @@ public interface IRequest {
1314
Map<String, String> getQuery();
1415
String getPathRaw();
1516
Map<String, String> getPath();
17+
InputStream getInputStream();
1618
}

template/java8/model/src/main/java/com/openfaas/model/IResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
public interface IResponse {
99
String getBody();
10+
byte[] getBodyData();
1011
void setBody(String body);
12+
void setBodyData(byte[] body);
1113

1214
String getHeader(String key);
1315
void setHeader(String key, String value);

template/java8/model/src/main/java/com/openfaas/model/Request.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
package com.openfaas.model;
55

6+
import java.io.*;
7+
import java.nio.charset.StandardCharsets;
68
import java.util.Map;
79
import java.util.HashMap;
810
import java.net.URLDecoder;
9-
import java.io.UnsupportedEncodingException;
1011

1112

1213
public class Request implements IRequest {
@@ -17,23 +18,45 @@ public class Request implements IRequest {
1718
private String queryRaw;
1819
private String pathRaw;
1920
private Map<String, String> path;
21+
private InputStream inputStream;
22+
23+
public Request(String body, Map<String, String> headers) {
24+
this(body, headers, "", "");
25+
}
26+
27+
public Request(String body, Map<String, String> headers, String queryRaw, String path) {
28+
this(headers, queryRaw, path, new ByteArrayInputStream(body != null ? body.getBytes(StandardCharsets.UTF_8) : new byte[0]));
2029

21-
public Request(String body, Map<String, String> headers) {
22-
this.body = body;
23-
this.headers = headers;
2430
}
25-
26-
public Request(String body, Map<String, String> headers,String queryRaw, String path) {
27-
this.body = body;
31+
32+
public Request(Map<String, String> headers, String queryRaw, String path, InputStream inputStream) {
2833
this.headers = headers;
2934
this.queryRaw = queryRaw;
30-
this.queryParameters = this.parseQueryParameters();
35+
this.inputStream = inputStream;
36+
this.queryParameters = this.parseQueryParameters();
3137
this.pathRaw = path;
3238
this.path = this.parsePathParameters();
3339
}
3440

3541
public String getBody() {
36-
return this.body;
42+
if (this.body != null) {
43+
return this.body;
44+
}
45+
try {
46+
ByteArrayOutputStream result = new ByteArrayOutputStream();
47+
byte[] buffer = new byte[1024];
48+
int length;
49+
while ((length = inputStream.read(buffer)) != -1) {
50+
result.write(buffer, 0, length);
51+
}
52+
this.body = result.toString(StandardCharsets.UTF_8.name());
53+
return this.body;
54+
} catch (IOException e) {
55+
// ByteArrayOutputStream.write does not actually throw any IOException,
56+
// but OutputStream.write is declared to throw one.
57+
// To not break getBody(), throw the exception wrapped in a RuntimeException, but it should never be thrown
58+
throw new RuntimeException(e);
59+
}
3760
}
3861

3962
public Map<String, String> getHeaders() {
@@ -118,4 +141,8 @@ private Map<String, String> parseQueryParameters() {
118141
return reqParametersMap;
119142
}
120143

144+
@Override
145+
public InputStream getInputStream() {
146+
return inputStream;
147+
}
121148
}

template/java8/model/src/main/java/com/openfaas/model/Response.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,20 @@
33

44
package com.openfaas.model;
55

6+
import java.io.UnsupportedEncodingException;
7+
import java.nio.charset.StandardCharsets;
68
import java.util.HashMap;
79
import java.util.Map;
810

911
public class Response implements IResponse {
1012

1113
private int statusCode = 200;
12-
private String body;
14+
private byte[] body;
1315
private String contentType;
1416
private Map<String, String> headers;
1517

1618
public Response() {
17-
this.body = "";
19+
this.body = new byte[0];
1820
this.contentType = "";
1921
this.headers = new HashMap<String, String>();
2022
}
@@ -58,10 +60,21 @@ public String getContentType() {
5860
}
5961

6062
public void setBody(String body) {
63+
this.body = body.getBytes(StandardCharsets.UTF_8);
64+
}
65+
66+
67+
@Override
68+
public void setBodyData(byte[] body) {
6169
this.body = body;
6270
}
6371

6472
public String getBody() {
73+
return new String(this.body);
74+
}
75+
76+
@Override
77+
public byte[] getBodyData() {
6578
return this.body;
6679
}
6780
}

template/java8/model/src/test/java/RequestTest.java

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import org.junit.Test;
22
import static org.junit.Assert.*;
33

4+
import java.io.ByteArrayInputStream;
5+
import java.io.IOException;
6+
import java.util.Arrays;
47
import java.util.HashMap;
58
import java.util.Map;
69

@@ -10,43 +13,43 @@ public class RequestTest {
1013
@Test
1114
public void testSingleRequestParameterGetSet()
1215
{
13-
Request request = new Request(null,null,"testParam=testParamValue", null);
16+
Request request = new Request((String)null, null, "testParam=testParamValue", null);
1417
assertEquals("testParamValue", request.getQuery().get("testParam"));
1518
}
16-
19+
1720
@Test
1821
public void testMultipleRequestParametersGetSet()
1922
{
20-
Request request = new Request(null,null,"testParam1=testParamValue1&testParam2=testParamValue2", null);
23+
Request request = new Request("", null,"testParam1=testParamValue1&testParam2=testParamValue2", null);
2124
assertEquals("testParamValue1", request.getQuery().get("testParam1"));
2225
assertEquals("testParamValue2", request.getQuery().get("testParam2"));
2326
}
24-
27+
2528
@Test
2629
public void testNullRequestParameterGetSet()
2730
{
28-
Request request = new Request(null,null,null, null);
31+
Request request = new Request("",null,null, null);
2932
assertEquals(null, request.getQuery().get("testParam"));
3033
}
31-
34+
3235
@Test
3336
public void testEmptyRequestParameterGetSet()
3437
{
35-
Request request = new Request(null,null,"", null);
38+
Request request = new Request("", null, "", null);
3639
assertEquals(null, request.getQuery().get("testParam"));
3740
}
38-
41+
3942
@Test
4043
public void testRequestRawGetSet()
4144
{
42-
Request request = new Request(null,null,"testRaw=testRawValue", null);
45+
Request request = new Request("", null,"testRaw=testRawValue", null);
4346
assertEquals("testRaw=testRawValue", request.getQueryRaw());
4447
}
4548

4649
@Test
4750
public void testGetPath()
4851
{
49-
Request request = new Request(null,null, null, "/test/path");
52+
Request request = new Request("", null, null, "/test/path");
5053
try {
5154
assertEquals("/test/path", request.getPathRaw());
5255
} catch (AssertionError e) {
@@ -58,7 +61,7 @@ public void testGetPath()
5861
@Test
5962
public void testGetPathWithNullPath()
6063
{
61-
Request request = new Request(null,null, null, null);
64+
Request request = new Request("", null,null, null);
6265
try {
6366
assertNull(request.getPathRaw());
6467
} catch (AssertionError e) {
@@ -70,15 +73,15 @@ public void testGetPathWithNullPath()
7073
@Test
7174
public void testParseParametersWithoutAnyParameters()
7275
{
73-
Request request = new Request(null,null, null, "/");
76+
Request request = new Request("", null, null, "/");
7477
try {
7578
assertEquals(0, request.getPath().size());
7679
} catch (AssertionError e) {
7780
System.out.format("Expected: 0 Got: %s", request.getPath().size());
7881
throw e;
7982
}
8083

81-
Request emptyRequest = new Request(null,null, null, "");
84+
Request emptyRequest = new Request(null, null, null, "");
8285
try {
8386
assertEquals(0, emptyRequest.getPath().size());
8487
} catch (AssertionError e) {
@@ -90,7 +93,7 @@ public void testParseParametersWithoutAnyParameters()
9093
@Test
9194
public void testParseParametersWithEvenParameters()
9295
{
93-
Request request = new Request(null,null, null, "/param1/value1/param2/value2");
96+
Request request = new Request("", null, null, "/param1/value1/param2/value2");
9497
Map<String, String> params = request.getPath();
9598

9699
try {
@@ -110,7 +113,7 @@ public void testParseParametersWithEvenParameters()
110113
@Test
111114
public void testParseParametersWithOddParameters()
112115
{
113-
Request request = new Request(null,null, null, "/param1/value1/param2");
116+
Request request = new Request("", null, null, "/param1/value1/param2");
114117
Map<String, String> params = request.getPath();
115118

116119
try {
@@ -126,4 +129,61 @@ public void testParseParametersWithOddParameters()
126129
throw e;
127130
}
128131
}
132+
133+
@Test
134+
public void testRequestGetBodyWithString() {
135+
Request request = new Request(null, null, null, new ByteArrayInputStream("Øl får meg glad".getBytes()));
136+
assertEquals("Øl får meg glad", request.getBody());
137+
}
138+
139+
@Test
140+
public void testRequestInputStream() {
141+
try {
142+
String requestBody = "Øl får meg glad";
143+
byte[] originalData = requestBody.getBytes();
144+
Request request = new Request(null, null, null, new ByteArrayInputStream(requestBody.getBytes()));
145+
byte[] buffer = new byte[64];
146+
int read = request.getInputStream().read(buffer);
147+
byte[] requestBodyData = new byte[read];
148+
System.arraycopy(buffer, 0, requestBodyData, 0, read);
149+
assertTrue(Arrays.equals(originalData, requestBodyData));
150+
} catch (IOException e) {
151+
throw new AssertionError(e);
152+
} catch (AssertionError e) {
153+
System.out.println(e.getMessage());
154+
throw e;
155+
}
156+
}
157+
158+
@Test
159+
public void testAlternateSetBodyGetBodyData() {
160+
try {
161+
String requestBody = "Øl får meg glad";
162+
byte[] originalData = requestBody.getBytes();
163+
Request request = new Request(requestBody, null, null, null);
164+
byte[] buffer = new byte[64];
165+
int read = request.getInputStream().read(buffer);
166+
byte[] requestBodyData = new byte[read];
167+
System.arraycopy(buffer, 0, requestBodyData, 0, read);
168+
assertTrue(Arrays.equals(originalData, requestBodyData));
169+
} catch (IOException e) {
170+
throw new AssertionError(e);
171+
} catch (AssertionError e) {
172+
System.out.println(e.getMessage());
173+
throw e;
174+
}
175+
}
176+
177+
@Test
178+
public void testAlternateSetBodyDataGetBody() {
179+
try {
180+
String originalRequestBody = "Øl får meg glad";
181+
Request request = new Request(null, null, null, new ByteArrayInputStream(originalRequestBody.getBytes()));
182+
String requestBody = request.getBody();
183+
assertEquals(originalRequestBody, requestBody);
184+
} catch (AssertionError e) {
185+
System.out.println(e.getMessage());
186+
throw e;
187+
}
188+
}
129189
}

template/java8/model/src/test/java/ResponseTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import com.openfaas.model.Response;
1010

11+
import java.util.Arrays;
12+
1113
public class ResponseTest {
1214
@Test public void testResponseHeaderSetGetValue() {
1315
Response r = new Response();
@@ -49,4 +51,18 @@ public class ResponseTest {
4951
r.setStatusCode(404);
5052
assertEquals(404, r.getStatusCode());
5153
}
54+
55+
@Test public void testResponseSetBodyWithString() {
56+
Response r = new Response();
57+
r.setBody("Øl får meg glad");
58+
assertEquals(r.getBody(), "Øl får meg glad");
59+
}
60+
61+
@Test public void testResponseSetBodyWithBytes() {
62+
Response r = new Response();
63+
r.setBodyData("Øl får meg glad".getBytes());
64+
assertEquals(r.getBody(), "Øl får meg glad");
65+
66+
assertTrue(Arrays.equals(r.getBodyData(), "Øl får meg glad".getBytes()));
67+
}
5268
}

0 commit comments

Comments
 (0)