Skip to content

Commit 58570cf

Browse files
Spring Javadocs (#323)
* Spring Javadocs Signed-off-by: Francesco Guardiani <[email protected]> * Rebase fix Signed-off-by: Francesco Guardiani <[email protected]>
1 parent 24d108f commit 58570cf

File tree

10 files changed

+211
-210
lines changed

10 files changed

+211
-210
lines changed

pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
<version>3.2.0</version>
159159
<configuration>
160160
<detectLinks/>
161+
<links>
162+
<link>https://docs.spring.io/spring-framework/docs/current/javadoc-api/</link>
163+
</links>
161164
</configuration>
162165
<executions>
163166
<execution>

spring/src/main/java/io/cloudevents/spring/http/CloudEventHttpUtils.java

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,21 @@
1515
*/
1616
package io.cloudevents.spring.http;
1717

18-
import java.util.function.Consumer;
19-
import java.util.function.Supplier;
20-
2118
import io.cloudevents.CloudEvent;
2219
import io.cloudevents.CloudEventContext;
20+
import io.cloudevents.SpecVersion;
2321
import io.cloudevents.core.CloudEventUtils;
2422
import io.cloudevents.core.builder.CloudEventBuilder;
2523
import io.cloudevents.core.message.MessageReader;
2624
import io.cloudevents.http.HttpMessageFactory;
2725
import io.cloudevents.http.impl.HttpMessageWriter;
28-
26+
import io.cloudevents.rw.CloudEventRWException;
2927
import org.springframework.http.HttpHeaders;
3028
import org.springframework.http.ResponseEntity;
3129

30+
import java.util.function.Consumer;
31+
import java.util.function.Supplier;
32+
3233
/**
3334
* Miscellaneous utility methods to assist with Cloud Events in the context of Spring Web
3435
* frameworks. Primarily intended for the internal use within Spring-based frameworks or
@@ -42,54 +43,58 @@ public class CloudEventHttpUtils {
4243
private CloudEventHttpUtils() {
4344
}
4445

45-
/**
46-
* Create a {@link MessageReader} to assist in conversion of an HTTP request to a
47-
* {@link CloudEvent}.
48-
* @param headers the HTTP request headers
49-
* @param body the HTTP request body as a byte array
50-
* @return a {@link MessageReader} representing the {@link CloudEvent}
51-
*/
52-
public static MessageReader toReader(HttpHeaders headers, Supplier<byte[]> body) {
53-
return HttpMessageFactory.createReaderFromMultimap(headers, body.get());
54-
}
46+
/**
47+
* Create a {@link MessageReader} to convert an HTTP request to a {@link CloudEvent}.
48+
*
49+
* @param headers the HTTP request headers
50+
* @param body the HTTP request body as a byte array
51+
* @return a {@link MessageReader} representing the {@link CloudEvent}
52+
* @throws CloudEventRWException if something goes wrong while resolving the {@link SpecVersion} or if the message has unknown encoding
53+
*/
54+
public static MessageReader toReader(HttpHeaders headers, Supplier<byte[]> body) throws CloudEventRWException {
55+
return HttpMessageFactory.createReaderFromMultimap(headers, body.get());
56+
}
5557

5658
/**
5759
* Create an {@link HttpMessageWriter} that can hand off a {@link CloudEvent} to an
5860
* HTTP response. Mainly useful in a blocking (not async) setting because the response
5961
* body has to be consumed directly.
62+
*
6063
* @param headers the response headers (will be mutated)
6164
* @param sendBody a consumer for the response body that puts the bytes on the wire
6265
*/
6366
public static HttpMessageWriter toWriter(HttpHeaders headers, Consumer<byte[]> sendBody) {
6467
return HttpMessageFactory.createWriter(headers::set, sendBody);
65-
}
68+
}
6669

67-
/**
68-
* Helper method for extracting {@link HttpHeaders} from a {@link CloudEvent}. Can,
69-
* for instance, be used in a <code>&#64;RequestMapping</code> to return a
70-
* {@link ResponseEntity} that has headers copied from a {@link CloudEvent}.
71-
* @param event the input {@link CloudEvent}
72-
* @return the response headers represented by the event
73-
*/
74-
public static HttpHeaders toHttp(CloudEventContext event) {
75-
HttpHeaders headers = new HttpHeaders();
76-
CloudEventUtils.toReader(CloudEventBuilder.fromContext(event).build()).read(toWriter(headers, bytes -> {
77-
}));
78-
return headers;
79-
}
70+
/**
71+
* Helper method for extracting {@link HttpHeaders} from a {@link CloudEvent}. Can,
72+
* for instance, be used in a {@link org.springframework.web.bind.annotation.RequestMapping} to return a
73+
* {@link ResponseEntity} that has headers copied from a {@link CloudEvent}.
74+
*
75+
* @param event the input {@link CloudEvent}
76+
* @return the response headers represented by the event
77+
* @throws CloudEventRWException if something goes wrong while writing the context to the http headers
78+
*/
79+
public static HttpHeaders toHttp(CloudEventContext event) throws CloudEventRWException {
80+
HttpHeaders headers = new HttpHeaders();
81+
CloudEventUtils.toReader(CloudEventBuilder.fromContext(event).build()).read(toWriter(headers, bytes -> {
82+
}));
83+
return headers;
84+
}
8085

81-
/**
82-
* Helper method for converting {@link HttpHeaders} to a {@link CloudEvent}. The input
83-
* headers must represent a valid event in "binary" form, i.e. it must have headers
84-
* "ce-id", "ce-specversion" etc.
85-
* @param headers the input request headers
86-
* @return a {@link CloudEventBuilder} that can be used to create a new
87-
* {@link CloudEvent}
88-
*
89-
*/
90-
public static CloudEventBuilder fromHttp(HttpHeaders headers) {
91-
return CloudEventBuilder
92-
.fromContext(CloudEventUtils.toEvent(CloudEventHttpUtils.toReader(headers, () -> null)));
86+
/**
87+
* Helper method for converting {@link HttpHeaders} to a {@link CloudEvent}. The input
88+
* headers <b>must</b> represent a valid event in "binary" form, i.e. it must have headers
89+
* {@code ce-id}, {@code ce-specversion} etc.
90+
*
91+
* @param headers the input request headers
92+
* @return a {@link CloudEventBuilder} that can be used to create a new {@link CloudEvent}
93+
* @throws CloudEventRWException if something goes wrong while reading the context from the http headers
94+
*/
95+
public static CloudEventBuilder fromHttp(HttpHeaders headers) throws CloudEventRWException {
96+
return CloudEventBuilder
97+
.fromContext(CloudEventUtils.toEvent(CloudEventHttpUtils.toReader(headers, () -> null)));
9398
}
9499

95100
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2019-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.cloudevents.spring.messaging;
17+
18+
import io.cloudevents.CloudEvent;
19+
import io.cloudevents.CloudEventContext;
20+
import io.cloudevents.SpecVersion;
21+
import io.cloudevents.core.CloudEventUtils;
22+
import io.cloudevents.rw.CloudEventRWException;
23+
import org.springframework.messaging.MessageHeaders;
24+
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
import static io.cloudevents.spring.messaging.CloudEventsHeaders.CE_PREFIX;
29+
30+
/**
31+
* Utility class for copying message headers to and from {@link CloudEventContext}.
32+
*
33+
* @author Dave Syer
34+
*/
35+
public class CloudEventContextUtils {
36+
37+
/**
38+
* Helper method for converting {@link MessageHeaders} to a {@link CloudEventContext}.
39+
* The input headers <b>must</b> represent a valid event in "binary" form, i.e. it must have headers
40+
* {@code ce-id}, {@code ce-specversion} etc.
41+
*
42+
* @param headers the input message headers
43+
* @return a {@link CloudEventContext} that can be used to create a new {@link CloudEvent}
44+
* @throws CloudEventRWException if something goes wrong while converting the headers to {@link CloudEventContext}
45+
*/
46+
public static CloudEventContext fromMap(Map<String, Object> headers) throws CloudEventRWException {
47+
Object value = headers.get(CloudEventsHeaders.SPEC_VERSION);
48+
SpecVersion version = value == null ? SpecVersion.V1 : SpecVersion.parse(value.toString());
49+
return CloudEventUtils.toEvent(new MessageBinaryMessageReader(version, headers));
50+
}
51+
52+
/**
53+
* Helper method for extracting {@link MessageHeaders} from a {@link CloudEventContext}. The
54+
* result will contain headers canonicalized with a {@code ce-} prefix, analogous to the
55+
* "binary" message format in Cloud Events.
56+
*
57+
* @param context the input {@link CloudEventContext}
58+
* @return the response headers represented by the event
59+
* @throws CloudEventRWException if something goes wrong while converting the {@link CloudEventContext} to headers
60+
*/
61+
public static Map<String, Object> toMap(CloudEventContext context) throws CloudEventRWException {
62+
Map<String, Object> headers = new HashMap<>();
63+
// Probably this should be done in CloudEventContextReaderAdapter
64+
headers.put(CE_PREFIX + "specversion", context.getSpecVersion().toString());
65+
MessageBuilderMessageWriter writer = new MessageBuilderMessageWriter(headers);
66+
CloudEventUtils.toContextReader(context).readContext(writer);
67+
return writer.end().getHeaders();
68+
}
69+
70+
}

spring/src/main/java/io/cloudevents/spring/messaging/CloudEventHeaderUtils.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

spring/src/main/java/io/cloudevents/spring/messaging/CloudEventMessageConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package io.cloudevents.spring.messaging;
1717

18-
import java.nio.charset.Charset;
19-
2018
import io.cloudevents.CloudEvent;
2119
import io.cloudevents.CloudEventContext;
2220
import io.cloudevents.SpecVersion;
@@ -25,19 +23,19 @@
2523
import io.cloudevents.core.message.MessageReader;
2624
import io.cloudevents.core.message.impl.GenericStructuredMessageReader;
2725
import io.cloudevents.core.message.impl.MessageUtils;
28-
2926
import org.springframework.messaging.Message;
3027
import org.springframework.messaging.MessageHeaders;
3128
import org.springframework.messaging.converter.MessageConverter;
3229

30+
import java.nio.charset.Charset;
31+
3332
/**
3433
* A {@link MessageConverter} that can translate to and from a {@link Message
3534
* Message&lt;byte[]>} or {@link Message Message&lt;String>} and a {@link CloudEvent}. The
36-
* {@link CloudEventContext} is canonicalized, with key names given a "ce-" prefix in the
35+
* {@link CloudEventContext} is canonicalized, with key names given a {@code ce-} prefix in the
3736
* {@link MessageHeaders}.
38-
*
39-
* @author Dave Syer
4037
*
38+
* @author Dave Syer
4139
*/
4240
public class CloudEventMessageConverter implements MessageConverter {
4341

spring/src/main/java/io/cloudevents/spring/mvc/CloudEventHttpMessageConverter.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@
1515
*/
1616
package io.cloudevents.spring.mvc;
1717

18-
import java.io.IOException;
19-
2018
import io.cloudevents.CloudEvent;
2119
import io.cloudevents.core.CloudEventUtils;
2220
import io.cloudevents.spring.http.CloudEventHttpUtils;
23-
2421
import org.springframework.http.HttpInputMessage;
2522
import org.springframework.http.HttpOutputMessage;
2623
import org.springframework.http.MediaType;
@@ -30,13 +27,14 @@
3027
import org.springframework.http.converter.HttpMessageNotWritableException;
3128
import org.springframework.util.StreamUtils;
3229

30+
import java.io.IOException;
31+
3332
/**
3433
* An {@link HttpMessageConverter} for {@link CloudEvent CloudEvents}. Supports the use of
35-
* {@link CloudEvent} in a <code>&#64;RequestMapping</code> as either a method parameter
34+
* {@link CloudEvent} in a {@link org.springframework.web.bind.annotation.RequestMapping} as either a method parameter
3635
* or a return value.
37-
*
38-
* @author Dave Syer
3936
*
37+
* @author Dave Syer
4038
*/
4139
public class CloudEventHttpMessageConverter extends AbstractHttpMessageConverter<CloudEvent> {
4240

spring/src/main/java/io/cloudevents/spring/webflux/CloudEventHttpMessageReader.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,29 @@
1515
*/
1616
package io.cloudevents.spring.webflux;
1717

18-
import java.util.Arrays;
19-
import java.util.List;
20-
import java.util.Map;
21-
2218
import io.cloudevents.CloudEvent;
2319
import io.cloudevents.core.message.MessageReader;
2420
import io.cloudevents.spring.http.CloudEventHttpUtils;
25-
import reactor.core.publisher.Flux;
26-
import reactor.core.publisher.Mono;
27-
2821
import org.springframework.core.ResolvableType;
2922
import org.springframework.core.io.buffer.DataBufferUtils;
3023
import org.springframework.http.HttpHeaders;
3124
import org.springframework.http.MediaType;
3225
import org.springframework.http.ReactiveHttpInputMessage;
3326
import org.springframework.http.codec.HttpMessageReader;
3427
import org.springframework.util.StreamUtils;
28+
import reactor.core.publisher.Flux;
29+
import reactor.core.publisher.Mono;
30+
31+
import java.util.Arrays;
32+
import java.util.List;
33+
import java.util.Map;
3534

3635
/**
3736
* A reactive {@link HttpMessageReader} for {@link CloudEvent CloudEvents}, converting
38-
* from an HTTP request to a cloud event. Supports the use of {@link CloudEvent} as an
37+
* from an HTTP request to a CloudEvent. Supports the use of {@link CloudEvent} as an
3938
* input to a reactive endpoint.
40-
*
41-
* @author Dave Syer
4239
*
40+
* @author Dave Syer
4341
*/
4442
public class CloudEventHttpMessageReader implements HttpMessageReader<CloudEvent> {
4543

@@ -74,4 +72,4 @@ public Mono<CloudEvent> readMono(ResolvableType elementType, ReactiveHttpInputMe
7472
return body.map(bytes -> CloudEventHttpUtils.toReader(headers, () -> bytes)).map(MessageReader::toEvent);
7573
}
7674

77-
}
75+
}

spring/src/main/java/io/cloudevents/spring/webflux/CloudEventHttpMessageWriter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@
4040

4141
/**
4242
* A reactive {@link HttpMessageWriter} for {@link CloudEvent CloudEvents}, converting
43-
* from a cloud event to an HTTP response. Supports the use of {@link CloudEvent} as an
43+
* from a CloudEvent to an HTTP response. Supports the use of {@link CloudEvent} as an
4444
* output from a reactive endpoint.
4545
*
4646
* @author Dave Syer
47-
*
4847
*/
4948
public class CloudEventHttpMessageWriter implements HttpMessageWriter<CloudEvent> {
5049

0 commit comments

Comments
 (0)