Skip to content

Commit c09ef8c

Browse files
committed
fixes for review comments
1 parent 70ee4fa commit c09ef8c

File tree

6 files changed

+150
-190
lines changed

6 files changed

+150
-190
lines changed

examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpServer.java renamed to examples/basic-http/src/main/java/io/cloudevents/examples/http/basic/HttpServer.java

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.cloudeventrs.examples.http;
17+
package io.cloudevents.examples.http.basic;
1818

1919
import com.sun.net.httpserver.Headers;
2020
import com.sun.net.httpserver.HttpExchange;
@@ -23,55 +23,61 @@
2323
import io.cloudevents.core.message.MessageWriter;
2424
import io.cloudevents.http.HttpMessageFactory;
2525

26-
import java.io.IOException;
27-
import java.io.UncheckedIOException;
26+
import java.io.*;
2827
import java.net.InetSocketAddress;
2928

3029
public class HttpServer {
3130

3231
public static void main(String[] args) throws IOException {
3332
com.sun.net.httpserver.HttpServer httpServer = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress("localhost", 8080), 0);
34-
httpServer.createContext("/echo", HttpServer::handle);
33+
httpServer.createContext("/echo", HttpServer::echoHandler);
3534
httpServer.start();
3635
}
3736

38-
public static void handle(HttpExchange exchange) throws IOException {
37+
private static void echoHandler(HttpExchange exchange) throws IOException {
3938
if (!"POST".equalsIgnoreCase(exchange.getRequestMethod())) {
4039
exchange.sendResponseHeaders(405, 0);
4140
return;
4241
}
4342
try {
44-
MessageReader messageReader = createReadMessage(exchange);
43+
MessageReader messageReader = createMessageReader(exchange);
4544
CloudEvent cloudEvent = messageReader.toEvent();
4645

4746
System.out.println("Handling event: " + cloudEvent);
4847

49-
MessageWriter messageWriter = createWriteMessage(exchange);
48+
MessageWriter messageWriter = createMessageWriter(exchange);
5049
messageWriter.writeBinary(cloudEvent);
5150
} catch (Throwable t) {
52-
exchange.sendResponseHeaders(500, 0);
51+
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
52+
try (PrintWriter pw = new PrintWriter(byteArrayOutputStream)) {
53+
t.printStackTrace(pw);
54+
}
55+
byte[] body = byteArrayOutputStream.toByteArray();
56+
exchange.sendResponseHeaders(500, body.length);
57+
try (OutputStream outputStream = exchange.getResponseBody()) {
58+
outputStream.write(body);
59+
}
60+
}
5361
}
5462
}
5563

56-
private static MessageReader createReadMessage(HttpExchange httpExchange) throws IOException {
64+
private static MessageReader createMessageReader(HttpExchange httpExchange) throws IOException {
5765
Headers headers = httpExchange.getRequestHeaders();
58-
byte[] body;
59-
String contentLength = headers.getFirst("Content-Length");
60-
if (contentLength != null) {
61-
body = IOUtils.toByteArray(httpExchange.getRequestBody(), Integer.parseInt(contentLength));
62-
} else {
63-
body = IOUtils.toByteArray(httpExchange.getRequestBody());
64-
}
66+
byte[] body = IOUtils.toByteArray(httpExchange.getRequestBody());
6567
return HttpMessageFactory.createReaderFromMultiMap(headers, body);
6668
}
6769

68-
private static MessageWriter createWriteMessage(HttpExchange httpExchange) {
70+
private static MessageWriter createMessageWriter(HttpExchange httpExchange) {
6971
return HttpMessageFactory.createWriter(
7072
httpExchange.getResponseHeaders()::add,
7173
body -> {
7274
try {
73-
httpExchange.sendResponseHeaders(200, body.length);
74-
httpExchange.getResponseBody().write(body);
75+
if (body != null) {
76+
httpExchange.sendResponseHeaders(200, body.length);
77+
httpExchange.getResponseBody().write(body);
78+
} else {
79+
httpExchange.sendResponseHeaders(204, 0);
80+
}
7581
} catch (IOException t) {
7682
throw new UncheckedIOException(t);
7783
}

examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpURLConnectionClient.java renamed to examples/basic-http/src/main/java/io/cloudevents/examples/http/basic/HttpURLConnectionClient.java

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.cloudeventrs.examples.http;
17+
package io.cloudevents.examples.http.basic;
1818

1919
import io.cloudevents.CloudEvent;
2020
import io.cloudevents.core.message.MessageReader;
@@ -50,34 +50,23 @@ public static void main(String[] args) throws IOException {
5050
httpUrlConnection.setDoOutput(true);
5151
httpUrlConnection.setDoInput(true);
5252

53-
MessageWriter messageWriter = createWriteMessage(httpUrlConnection);
53+
MessageWriter messageWriter = createMessageWriter(httpUrlConnection);
5454
messageWriter.writeBinary(ceToSend);
5555

56-
MessageReader messageReader = createReadMessage(httpUrlConnection);
56+
MessageReader messageReader = createMessageReader(httpUrlConnection);
5757
CloudEvent receivedCE = messageReader.toEvent();
5858

5959
System.out.println("CloudEvent: " + receivedCE);
6060
System.out.println("Data: " + new String(receivedCE.getData(), StandardCharsets.UTF_8));
6161
}
6262

63-
private static MessageReader createReadMessage(HttpURLConnection httpUrlConnection) throws IOException {
64-
63+
private static MessageReader createMessageReader(HttpURLConnection httpUrlConnection) throws IOException {
6564
Map<String, List<String>> headers = httpUrlConnection.getHeaderFields();
66-
byte[] body;
67-
String contentLength = null;
68-
if (headers.containsKey("Content-Length")) {
69-
contentLength = headers.get("Content-Length").get(0);
70-
}
71-
if (contentLength != null) {
72-
body = IOUtils.toByteArray(httpUrlConnection.getInputStream(), Integer.parseInt(contentLength));
73-
} else {
74-
body = IOUtils.toByteArray(httpUrlConnection.getInputStream());
75-
}
76-
65+
byte[] body = IOUtils.toByteArray(httpUrlConnection.getInputStream());
7766
return HttpMessageFactory.createReaderFromMultiMap(headers, body);
7867
}
7968

80-
private static MessageWriter createWriteMessage(HttpURLConnection httpUrlConnection) {
69+
private static MessageWriter createMessageWriter(HttpURLConnection httpUrlConnection) {
8170
return HttpMessageFactory.createWriter(
8271
httpUrlConnection::setRequestProperty,
8372
body -> {

examples/basic-http/src/main/java/io/cloudeventrs/examples/http/IOUtils.java renamed to examples/basic-http/src/main/java/io/cloudevents/examples/http/basic/IOUtils.java

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.cloudeventrs.examples.http;
17+
package io.cloudevents.examples.http.basic;
1818

1919
import java.io.ByteArrayOutputStream;
2020
import java.io.IOException;
@@ -27,26 +27,6 @@
2727

2828
public class IOUtils {
2929

30-
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
31-
32-
public static byte[] toByteArray(InputStream body, int size) throws IOException {
33-
if (size < 0) throw new IllegalArgumentException("size cannot be negative.");
34-
if (size > 0) {
35-
byte[] data = new byte[size];
36-
int offset = 0;
37-
int read;
38-
while (offset < size && (read = body.read(data, offset, size - offset)) != -1) {
39-
offset += read;
40-
}
41-
if (offset != size) {
42-
throw new IOException("unexpected size, current: " + offset + ", expected: " + size);
43-
}
44-
return data;
45-
} else {
46-
return EMPTY_BYTE_ARRAY;
47-
}
48-
}
49-
5030
public static byte[] toByteArray(InputStream body) throws IOException {
5131
try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
5232
byte[] buff = new byte[(1<<10) * 8];

examples/basic-http/src/main/java/io/cloudeventrs/examples/http/JettyServer.java renamed to examples/basic-http/src/main/java/io/cloudevents/examples/http/basic/JettyServer.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package io.cloudeventrs.examples.http;
17+
package io.cloudevents.examples.http.basic;
1818

1919
import javax.servlet.ServletOutputStream;
2020
import javax.servlet.http.HttpServletRequest;
@@ -70,15 +70,7 @@ private static MessageReader createMessageReader(HttpServletRequest httpServletR
7070

7171
}
7272
};
73-
74-
byte[] body;
75-
int contentLength = httpServletRequest.getContentLength();
76-
if (contentLength != -1) {
77-
body = IOUtils.toByteArray(httpServletRequest.getInputStream(), contentLength);
78-
} else {
79-
body = IOUtils.toByteArray(httpServletRequest.getInputStream());
80-
}
81-
73+
byte[] body = IOUtils.toByteArray(httpServletRequest.getInputStream());
8274
return HttpMessageFactory.createReader(forEachHeader, body);
8375
}
8476

http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.net.URLConnection;
3333
import java.util.List;
3434
import java.util.Map;
35+
import java.util.concurrent.atomic.AtomicReference;
3536
import java.util.function.BiConsumer;
3637
import java.util.function.Consumer;
3738

@@ -59,13 +60,7 @@ private HttpMessageFactory() {}
5960
*
6061
* }
6162
* };
62-
* byte[] body;
63-
* int contentLength = httpServletRequest.getContentLength();
64-
* if (contentLength != -1) {
65-
* body = httpServletRequest.getInputStream().readNBytes(contentLength);
66-
* } else {
67-
* body = httpServletRequest.getInputStream().readAllBytes();
68-
* }
63+
* byte[] body = httpServletRequest.getInputStream().readAllBytes();
6964
* HttpMessageFactory.createReader(forEachHeader, body);
7065
* }
7166
* </pre>
@@ -75,23 +70,21 @@ private HttpMessageFactory() {}
7570
* @throws IllegalArgumentException If, in case of binary mode, the spec version is invalid
7671
*/
7772
public static MessageReader createReader(Consumer<BiConsumer<String, String>> forEachHeader, byte[] body) {
78-
final String[] hdrs = {null, null};
73+
final AtomicReference<String> contentType = new AtomicReference<>();
74+
final AtomicReference<String> specVersion = new AtomicReference<>();
7975

8076
forEachHeader.accept((k, v) -> {
8177
if (CONTENT_TYPE.equalsIgnoreCase(k)) {
82-
hdrs[0] = v;
78+
contentType.set(v);
8379
} else if (CloudEventsHeaders.SPEC_VERSION.equalsIgnoreCase(k)) {
84-
hdrs[1] = v;
80+
specVersion.set(v);
8581
}
8682
});
8783

88-
final String contentType = hdrs[0];
89-
final String specversion = hdrs[1];
90-
9184
return MessageUtils.parseStructuredOrBinaryMessage(
92-
() -> contentType,
85+
contentType::get,
9386
format -> new GenericStructuredMessageReader(format, body),
94-
() -> specversion,
87+
specVersion::get,
9588
sv -> new HttpMessageReader(sv, forEachHeader, body),
9689
UnknownEncodingMessageReader::new
9790
);

0 commit comments

Comments
 (0)