-
Notifications
You must be signed in to change notification settings - Fork 166
Generic HTTP bindings #225
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
Conversation
http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/RestfulWSMessageWriter.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
|
relates to #212 |
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
slinkydeveloper
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw you did some refactoring of restful-ws impl inside this PR, can you push it in a separate PR?
http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/impl/RestfulWSMessageWriter.java
Outdated
Show resolved
Hide resolved
|
@slinkydeveloper Sorry for mixing up things: |
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
| ); | ||
| } | ||
|
|
||
| public static MessageWriter createWriter(BiConsumer<String, String> putHeader, Consumer<byte[]> putBody) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a cool idea!
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
For demonstration purpose, can you add a snippet to the module readme? (also with the tests, the usage should be pretty clear) |
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for putting this together! It's very close to what I was imagining.
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/HttpMessageReader.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/HttpMessageReader.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/HttpMessageWriter.java
Outdated
Show resolved
Hide resolved
http/restful-ws/src/main/java/io/cloudevents/http/restful/ws/CloudEventsProvider.java
Outdated
Show resolved
Hide resolved
...l-ws/src/main/java/io/cloudevents/http/restful/ws/impl/BinaryRestfulWSMessageReaderImpl.java
Outdated
Show resolved
Hide resolved
...l-ws/src/main/java/io/cloudevents/http/restful/ws/impl/BinaryRestfulWSMessageReaderImpl.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
|
I added examples using |
|
Speaking of header: I must not forget about their case-insensitivity. |
On the write side, i guess you should just take a convention and use it... Then the internal representation is up to the single http libraries |
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
| } | ||
| } | ||
|
|
||
| public static Stream<Arguments> binaryTestArguments() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is already copypasted with little changes at multiple places. We should refactor that latter (not in this PR) if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a todo to the code itself?
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
|
I added README and some examples |
|
TODO: add Javadocs |
|
Should I call it |
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
|
For me the name of the module should be |
|
@eamonnmcmanus pleas give it another look. |
eamonnmcmanus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking good. Thanks for putting in all that work!
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpURLConnectionClient.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/IOUtils.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/JettyServer.java
Outdated
Show resolved
Hide resolved
| * Example of usage with <a href="https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html">HttpServletRequest</a>: | ||
| * {@code | ||
| * Consumer<BiConsumer<String,String>> forEachHeader = processHeader -> { | ||
| * Enumeration<String> headerNames = httpServletRequest.getHeaderNames(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (String name : Collections.list(httpServletRequest.getHeaderNames())) {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely would work, but why allocate new List just to iterate over it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collections.list was introduced specifically to get the legacy type Enumeration out of the way as quickly as possible. For me this is the idiomatic way to iterate over the Enumeration<String> type that the ancient Servlet API imposes. It's also a lot more readable for the example code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just found out that there is asIterator() then I could call forEachRemaining unfortunately the asIterator() method is available since Java 9+ 😞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that
for (String name : Collections.list(httpServletRequest.getHeaderNames())) {}is probably more readable.
But I find ArrayList allocation (and possible internal reallocation on its growth) unnecessary.
I specifically chosen that obsolete type to show that it works well with Consumer<BiConsumer<String,String>> forEachHeader. @slinkydeveloper wdyt?
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
| * @return a message reader implementation with potentially an unknown encoding | ||
| * @throws IllegalArgumentException If, in case of binary mode, the spec version is invalid | ||
| */ | ||
| public static MessageReader createReaderFromMultiMap(Map<String, List<String>> headers, byte[] body) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the spelling Multimap rather than MultiMap is more usual.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer MultiMap because to me it's a reasoning like TreeMap (first part of the name tells you the impl detail, the second one the interface it implements), but it's a matter of taste
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A TreeMap is just a kind of Map, but a Multimap is a different sort of thing. As words we would write "tree map" but "multimap". https://en.wikipedia.org/wiki/Multimap
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
http/basic/src/main/java/io/cloudevents/http/impl/CloudEventsHeaders.java
Outdated
Show resolved
Hide resolved
slinkydeveloper
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a first pass, on monday i'll do another one 😄
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpServer.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpServer.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpURLConnectionClient.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpURLConnectionClient.java
Outdated
Show resolved
Hide resolved
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
|
|
||
| // based on apache commons io |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a link to the source code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better now?
slinkydeveloper
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the basic http example contains examples with sun http server, http url connection and jetty server, maybe you should add a basic readme that explains the three different samples here?
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpServer.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpServer.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpServer.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudeventrs/examples/http/HttpServer.java
Outdated
Show resolved
Hide resolved
| httpExchange.sendResponseHeaders(200, body.length); | ||
| httpExchange.getResponseBody().write(body); | ||
| } catch (IOException t) { | ||
| throw new UncheckedIOException(t); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
waaaaat, i didn't knew it 😍
| * } | ||
| * }; | ||
| * byte[] body; | ||
| * int contentLength = httpServletRequest.getContentLength(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this
http/basic/src/main/java/io/cloudevents/http/HttpMessageFactory.java
Outdated
Show resolved
Hide resolved
http/basic/src/test/java/io/cloudevents/http/HttpMessageReaderWriterTest.java
Outdated
Show resolved
Hide resolved
http/basic/src/test/java/io/cloudevents/http/HttpMessageReaderWriterTest.java
Show resolved
Hide resolved
|
@eamonnmcmanus please give it another look. |
examples/basic-http/src/main/java/io/cloudevents/examples/http/basic/HttpServer.java
Outdated
Show resolved
Hide resolved
examples/basic-http/src/main/java/io/cloudevents/examples/http/basic/IOUtils.java
Show resolved
Hide resolved
| // V03 | ||
| Arguments.of( | ||
| V03_MIN, | ||
| new HashMap<String,String>() {{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eamonnmcmanus Originally I used something different: c09ef8c#diff-da38f4f0dad4b560cd509239f96a762eL241 was it better?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't realize it uses anon classes. Gee couldn't they just already add proper literals to the language.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course this is only a test, so the kittens God is killing are only test kittens. Still, I think I preferred the way you had it before, which was at least as readable without triggering the kitten-killing fear in seasoned Java developers.
For literals, I think List.of(...) and Set.of(...), added in Java 9, are good enough for those types without needing a special syntax. There's also Map.of(...), which you could have used here except for the desire to work on Java 8. But that one is a bit dicey anyway since there's no obvious syntactic difference between keys and values, and it arbitrarily stops at 10 key-value pairs.
|
@matejvasek can you fix the dco? |
slinkydeveloper
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last change and we're ready to go
examples/basic-http/pom.xml
Outdated
| <artifactId>cloudevents-json-jackson</artifactId> | ||
| <version>${project.version}</version> | ||
| </dependency> | ||
| <dependency> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need that, because it's brought in by cloudevents-json-jackson
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also should be in cloudevents-http-basic
Signed-off-by: Matej Vasek <mvasek@redhat.com>
|
Thanks @matejvasek for the great work with this PR and thanks to @eamonnmcmanus for the fantastic job with the reviews! |

resolves #212