From ca0bda720ec43da453db0a171f62a065dca78db3 Mon Sep 17 00:00:00 2001 From: Laird Nelson Date: Wed, 30 Jan 2019 12:33:41 -0800 Subject: [PATCH 1/4] Added Jackson support Signed-off-by: Laird Nelson --- media/jackson/common/pom.xml | 58 +++++++++ .../jackson/common/JacksonProcessing.java | 113 ++++++++++++++++++ .../media/jackson/common/package-info.java | 19 +++ .../common/src/main/java9/module-info.java | 31 +++++ media/jackson/pom.xml | 37 ++++++ media/jackson/server/pom.xml | 67 +++++++++++ .../media/jackson/server/JacksonSupport.java | 109 +++++++++++++++++ .../media/jackson/server/package-info.java | 27 +++++ .../server/src/main/java9/module-info.java | 26 ++++ .../webserver/jackson/JacksonSupportTest.java | 77 ++++++++++++ media/pom.xml | 2 +- pom.xml | 18 ++- 12 files changed, 578 insertions(+), 6 deletions(-) create mode 100644 media/jackson/common/pom.xml create mode 100644 media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java create mode 100644 media/jackson/common/src/main/java/io/helidon/media/jackson/common/package-info.java create mode 100644 media/jackson/common/src/main/java9/module-info.java create mode 100644 media/jackson/pom.xml create mode 100644 media/jackson/server/pom.xml create mode 100644 media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java create mode 100644 media/jackson/server/src/main/java/io/helidon/media/jackson/server/package-info.java create mode 100644 media/jackson/server/src/main/java9/module-info.java create mode 100644 media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java diff --git a/media/jackson/common/pom.xml b/media/jackson/common/pom.xml new file mode 100644 index 00000000000..f36827bb5fc --- /dev/null +++ b/media/jackson/common/pom.xml @@ -0,0 +1,58 @@ + + + + + + helidon-media-jackson-project + io.helidon.media.jackson + 0.11.1-SNAPSHOT + + 4.0.0 + + helidon-media-jackson-common + + + + com.fasterxml.jackson.core + jackson-databind + + + io.helidon.common + helidon-common-http + ${project.version} + + + io.helidon.media + helidon-media-common + ${project.version} + + + org.junit.jupiter + junit-jupiter-api + test + + + org.hamcrest + hamcrest-all + test + + + diff --git a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java new file mode 100644 index 00000000000..98fde12228c --- /dev/null +++ b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.media.jackson.common; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Objects; +import java.util.function.Function; + +import io.helidon.common.http.DataChunk; +import io.helidon.common.http.MediaType; +import io.helidon.common.http.Reader; +import io.helidon.common.reactive.Flow; +import io.helidon.media.common.ContentReaders; +import io.helidon.media.common.ContentWriters; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * Media type support for Jackson. + */ +public final class JacksonProcessing { + + private JacksonProcessing() { + super(); + } + + /** + * Check whether the type is supported by Jackson. + * + * @param type media type to check + * @return true if the media type checked is supported by Jackson + */ + public static boolean isSupported(MediaType type) { + // See https://github.com/FasterXML/jackson-jaxrs-providers/blob/jackson-jaxrs-providers-2.9.4/json/src/main/java/com/fasterxml/jackson/jaxrs/json/JacksonJsonProvider.java#L167-L192 + final boolean returnValue; + if (type == null) { + returnValue = true; + } else { + final String subtype = type.subtype(); + if (subtype == null) { + returnValue = false; + } else { + returnValue = "json".equalsIgnoreCase(subtype) + || subtype.endsWith("+json") + || "javascript".equals(subtype) + || "x-javascript".equals(subtype) + || "x-json".equals(subtype); + } + } + return returnValue; + } + + /** + * Returns a {@link Reader} that converts a {@link Flow.Publisher Publisher} of {@link java.nio.ByteBuffer}s to + * a Java object. + * + *

This method is intended for the derivation of other, more specific readers.

+ * + * @param objectMapper the {@link ObjectMapper} to use; must not be {@code null} + * @return the byte array content reader that transforms a publisher of byte buffers to a completion stage that + * might end exceptionally with a {@link RuntimeException} in case of I/O error + * @exception NullPointerException if {@code objectMapper} is {@code null} + */ + public static Reader reader(final ObjectMapper objectMapper) { + Objects.requireNonNull(objectMapper); + return (publisher, cls) -> ContentReaders.byteArrayReader() + .apply(publisher) + .thenApply(bytes -> { + try { + return objectMapper.readValue(bytes, cls); + } catch (final IOException wrapMe) { + throw new RuntimeException(wrapMe.getMessage(), wrapMe); + } + }); + } + + /** + * Returns a function (writer) converting {@link JsonStructure} to the {@link Flow.Publisher Publisher} + * of {@link DataChunk}s. + * + * @param objectMapper the {@link ObjectMapper} to use; must not be {@code null} + * @return created function + * @exception NullPointerException if {@code objectMapper} is {@code null} + */ + public static Function> writer(final ObjectMapper objectMapper) { + Objects.requireNonNull(objectMapper); + return payload -> { + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + objectMapper.writeValue(baos, payload); + } catch (final IOException wrapMe) { + throw new RuntimeException(wrapMe.getMessage(), wrapMe); + } + return ContentWriters.byteArrayWriter(false) + .apply(baos.toByteArray()); + }; + } + +} diff --git a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/package-info.java b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/package-info.java new file mode 100644 index 00000000000..c743c4ab8e8 --- /dev/null +++ b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * Jackson media type support. + */ +package io.helidon.media.jackson.common; diff --git a/media/jackson/common/src/main/java9/module-info.java b/media/jackson/common/src/main/java9/module-info.java new file mode 100644 index 00000000000..241fdab2465 --- /dev/null +++ b/media/jackson/common/src/main/java9/module-info.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Jackson support common classes. + * + * @see io.helidon.media.jackson.common.JacksonProcessing + */ +module io.helidon.media.jackson.common { + + requires com.fasterxml.jackson.databind; + requires io.helidon.common; + requires io.helidon.common.http; + requires io.helidon.common.reactive; + requires io.helidon.media.common; + + exports io.helidon.media.jackson.common; +} diff --git a/media/jackson/pom.xml b/media/jackson/pom.xml new file mode 100644 index 00000000000..6354d8732b7 --- /dev/null +++ b/media/jackson/pom.xml @@ -0,0 +1,37 @@ + + + + + 4.0.0 + + + io.helidon.media + helidon-media-project + 0.11.1-SNAPSHOT + + + pom + io.helidon.media.jackson + helidon-media-jackson-project + + + common + server + + diff --git a/media/jackson/server/pom.xml b/media/jackson/server/pom.xml new file mode 100644 index 00000000000..3fc5feb8689 --- /dev/null +++ b/media/jackson/server/pom.xml @@ -0,0 +1,67 @@ + + + + + + helidon-media-jackson-project + io.helidon.media.jackson + 0.11.1-SNAPSHOT + + 4.0.0 + + helidon-media-jackson-server + Helidon Media Jackson WebServer Support + + + Jackson Support for WebServer + + + + + com.fasterxml.jackson.core + jackson-databind + + + io.helidon.webserver + helidon-webserver + ${project.version} + + + io.helidon.media.jackson + helidon-media-jackson-common + ${project.version} + + + org.junit.jupiter + junit-jupiter-api + test + + + org.hamcrest + hamcrest-all + test + + + io.helidon.webserver + helidon-webserver-test-support + ${project.version} + test + + + diff --git a/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java new file mode 100644 index 00000000000..17faa188f3c --- /dev/null +++ b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.media.jackson.server; + +import java.util.Collection; +import java.util.Objects; +import java.util.function.BiFunction; + +import io.helidon.common.http.MediaType; +import io.helidon.media.jackson.common.JacksonProcessing; +import io.helidon.webserver.Handler; +import io.helidon.webserver.Routing; +import io.helidon.webserver.ServerRequest; +import io.helidon.webserver.ServerResponse; +import io.helidon.webserver.Service; + +import com.fasterxml.jackson.databind.ObjectMapper; + +/** + * A {@link Service} and a {@link Handler} that provides Jackson + * support to Helidon. + */ +public final class JacksonSupport implements Service, Handler { + + private final BiFunction objectMapperProvider; + + /** + * Creates a new {@link JacksonSupport}. + * + * @param objectMapperProvider a {@link BiFunction} that returns + * an {@link ObjectMapper} when given a {@link ServerRequest} and + * a {@link ServerResponse}; must not be {@code null} + * + * @exception NullPointerException if {@code objectMapperProvider} + * is {@code null} + */ + public JacksonSupport(final BiFunction objectMapperProvider) { + super(); + this.objectMapperProvider = Objects.requireNonNull(objectMapperProvider); + } + + @Override + public void update(final Routing.Rules routingRules) { + routingRules.any(this); + } + + @Override + public void accept(final ServerRequest request, final ServerResponse response) { + final ObjectMapper objectMapper = this.objectMapperProvider.apply(request, response); + request.content() + .registerReader(cls -> objectMapper.canDeserialize(objectMapper.constructType(cls)), + JacksonProcessing.reader(objectMapper)); + response.registerWriter(payload -> objectMapper.canSerialize(payload.getClass()) && this.wantsJson(request, response), + JacksonProcessing.writer(objectMapper)); + request.next(); + } + + private static boolean wantsJson(final ServerRequest request, final ServerResponse response) { + final boolean returnValue; + final MediaType outgoingMediaType = response.headers().contentType().orElse(null); + if (outgoingMediaType == null) { + final MediaType preferredType; + final Collection acceptedTypes = request.headers().acceptedTypes(); + if (acceptedTypes == null || acceptedTypes.isEmpty()) { + preferredType = MediaType.APPLICATION_JSON; + } else { + preferredType = acceptedTypes + .stream() + .map(type -> { + if (type.test(MediaType.APPLICATION_JSON)) { + return MediaType.APPLICATION_JSON; + } else if (type.hasSuffix("json")) { + return MediaType.create(type.type(), type.subtype()); + } else { + return null; + } + }) + .filter(Objects::nonNull) + .findFirst() + .orElse(null); + } + if (preferredType == null) { + returnValue = false; + } else { + response.headers().contentType(preferredType); + returnValue = true; + } + } else { + returnValue = MediaType.JSON_PREDICATE.test(outgoingMediaType); + } + return returnValue; + } + +} diff --git a/media/jackson/server/src/main/java/io/helidon/media/jackson/server/package-info.java b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/package-info.java new file mode 100644 index 00000000000..ff0aa693d0b --- /dev/null +++ b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/package-info.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This package contains Jackson support for {@link io.helidon.webserver.WebServer WebServer}'s + * {@link io.helidon.webserver.Routing Routing}. + *

+ * For more information see {@link io.helidon.media.jackson.server.JacksonSupport JacksonSupport} documentation. + * + * @see io.helidon.media.jackson.server.JacksonSupport + * @see io.helidon.webserver.Routing + * @see com.fasterxml.jackson.databind.ObjectMapper + */ +package io.helidon.media.jackson.server; diff --git a/media/jackson/server/src/main/java9/module-info.java b/media/jackson/server/src/main/java9/module-info.java new file mode 100644 index 00000000000..bd809534806 --- /dev/null +++ b/media/jackson/server/src/main/java9/module-info.java @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Jackson-based JSON serialization support for webserver. + */ +module io.helidon.media.jackson.server { + requires com.fasterxml.jackson.databind; + requires io.helidon.media.jackson.common; + requires io.helidon.webserver; + + exports io.helidon.media.jackson.server; +} diff --git a/media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java b/media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java new file mode 100644 index 00000000000..0cda2a97109 --- /dev/null +++ b/media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.media.jackson.server; + +import java.util.concurrent.TimeUnit; +import java.util.function.BiFunction; + +import com.fasterxml.jackson.databind.ObjectMapper; +import io.helidon.common.http.Http; +import io.helidon.common.http.MediaType; +import io.helidon.webserver.Handler; +import io.helidon.webserver.Routing; +import io.helidon.webserver.ServerRequest; +import io.helidon.webserver.ServerResponse; +import io.helidon.webserver.testsupport.MediaPublisher; +import io.helidon.webserver.testsupport.TestClient; +import io.helidon.webserver.testsupport.TestResponse; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +/** + * Tests {@link JsonSupport}. + */ +public class JacksonSupportTest { + + private static final BiFunction objectMapperProvider = (req, res) -> new ObjectMapper(); + + @Test + public void pingPong() throws Exception { + final Routing routing = Routing.builder() + .register(new JacksonSupport(objectMapperProvider)) + .post("/foo", Handler.create(Person.class, (req, res, person) -> res.send(person))) + .build(); + final String personJson = "{\"name\":\"Frank\"}"; + final TestResponse response = TestClient.create(routing) + .path("/foo") + .post(MediaPublisher.create(MediaType.APPLICATION_JSON.withCharset("UTF-8"), personJson)); + + assertThat(response.headers().first(Http.Header.CONTENT_TYPE).orElse(null), is(MediaType.APPLICATION_JSON.toString())); + final String json = response.asString().get(10, TimeUnit.SECONDS); + assertThat(json, is(personJson)); + } + + public static final class Person { + + private String name; + + public Person() { + super(); + } + + public String getName() { + return this.name; + } + + public void setName(final String name) { + this.name = name; + } + + } +} diff --git a/media/pom.xml b/media/pom.xml index 320a2257478..4e5e2b8a408 100644 --- a/media/pom.xml +++ b/media/pom.xml @@ -30,7 +30,7 @@ Helidon Media Project - + jackson jsonp common diff --git a/pom.xml b/pom.xml index 713ed731f0f..d72a18c6dda 100644 --- a/pom.xml +++ b/pom.xml @@ -554,6 +554,19 @@ pom import + + com.fasterxml.jackson + jackson-bom + ${version.lib.jackson} + pom + import + + + + com.fasterxml.jackson.core + jackson-annotations + ${version.lib.jackson} + io.projectreactor reactor-core @@ -786,11 +799,6 @@ - - com.fasterxml.jackson.core - jackson-core - ${version.lib.jackson} - com.google.code.findbugs jsr305 From 34a4301c37e9eb38aef408fa6980ee9e52997166 Mon Sep 17 00:00:00 2001 From: Laird Nelson Date: Wed, 30 Jan 2019 12:44:51 -0800 Subject: [PATCH 2/4] Fixed Javadoc error Signed-off-by: Laird Nelson --- .../io/helidon/media/jackson/common/JacksonProcessing.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java index 98fde12228c..83d97b19671 100644 --- a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java +++ b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java @@ -89,8 +89,8 @@ public static Reader reader(final ObjectMapper objectMapper) { } /** - * Returns a function (writer) converting {@link JsonStructure} to the {@link Flow.Publisher Publisher} - * of {@link DataChunk}s. + * Returns a function (writer) converting {@link Object}s to {@link Flow.Publisher Publisher}s + * of {@link DataChunk}s by using the supplied {@link ObjectMapper}. * * @param objectMapper the {@link ObjectMapper} to use; must not be {@code null} * @return created function From bf2c862827fbe771ee465e9e8cf603863c318523 Mon Sep 17 00:00:00 2001 From: Laird Nelson Date: Thu, 7 Feb 2019 09:24:29 -0800 Subject: [PATCH 3/4] Making PR changes per suggestions Signed-off-by: Laird Nelson --- bom/pom.xml | 10 ++++++ .../jackson/common/JacksonProcessing.java | 4 +-- .../common/JacksonRuntimeException.java | 31 +++++++++++++++++++ .../media/jackson/server/JacksonSupport.java | 25 ++++++++++++++- .../webserver/jackson/JacksonSupportTest.java | 2 +- 5 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java diff --git a/bom/pom.xml b/bom/pom.xml index f2f22527208..7d026cd0789 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -58,6 +58,16 @@ helidon-media-common ${project.version} + + io.helidon.media.jackson + helidon-media-jackson-common + ${project.version} + + + io.helidon.media.jackson + helidon-media-jackson-server + ${project.version} + io.helidon.media.jsonp helidon-media-jsonp-common diff --git a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java index 83d97b19671..d3585f04c7f 100644 --- a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java +++ b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonProcessing.java @@ -83,7 +83,7 @@ public static Reader reader(final ObjectMapper objectMapper) { try { return objectMapper.readValue(bytes, cls); } catch (final IOException wrapMe) { - throw new RuntimeException(wrapMe.getMessage(), wrapMe); + throw new JacksonRuntimeException(wrapMe.getMessage(), wrapMe); } }); } @@ -103,7 +103,7 @@ public static Function> writer(final ObjectMap try { objectMapper.writeValue(baos, payload); } catch (final IOException wrapMe) { - throw new RuntimeException(wrapMe.getMessage(), wrapMe); + throw new JacksonRuntimeException(wrapMe.getMessage(), wrapMe); } return ContentWriters.byteArrayWriter(false) .apply(baos.toByteArray()); diff --git a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java new file mode 100644 index 00000000000..52617b38ba1 --- /dev/null +++ b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.media.jackson.common; + +/** + * A {@link RuntimeException} that indicates a problem was encountered + * while performing JSON manipulation with Jackson. + */ +public class JacksonRuntimeException extends RuntimeException { + + /** + * Creates a new {@link JacksonRuntimeException}. + */ + JacksonRuntimeException(final String message, final Throwable cause) { + super(message, cause); + } + +} diff --git a/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java index 17faa188f3c..3e46082e472 100644 --- a/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java +++ b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java @@ -47,7 +47,7 @@ public final class JacksonSupport implements Service, Handler { * @exception NullPointerException if {@code objectMapperProvider} * is {@code null} */ - public JacksonSupport(final BiFunction objectMapperProvider) { super(); @@ -70,6 +70,29 @@ public void accept(final ServerRequest request, final ServerResponse response) { request.next(); } + /** + * Creates a new {@link JacksonSupport}. + */ + public static JacksonSupport create() { + return create((req, res) -> new ObjectMapper()); + } + + /** + * Creates a new {@link JacksonSupport}. + * + * @param objectMapperProvider a {@link BiFunction} that returns + * an {@link ObjectMapper} when given a {@link ServerRequest} and + * a {@link ServerResponse}; must not be {@code null} + * + * @exception NullPointerException if {@code objectMapperProvider} + * is {@code null} + */ + public static JacksonSupport create(final BiFunction objectMapperProvider) { + return new JacksonSupport(objectMapperProvider); + } + private static boolean wantsJson(final ServerRequest request, final ServerResponse response) { final boolean returnValue; final MediaType outgoingMediaType = response.headers().contentType().orElse(null); diff --git a/media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java b/media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java index 0cda2a97109..2a804554191 100644 --- a/media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java +++ b/media/jackson/server/src/test/java/io/helidon/webserver/jackson/JacksonSupportTest.java @@ -44,7 +44,7 @@ public class JacksonSupportTest { @Test public void pingPong() throws Exception { final Routing routing = Routing.builder() - .register(new JacksonSupport(objectMapperProvider)) + .register(JacksonSupport.create()) .post("/foo", Handler.create(Person.class, (req, res, person) -> res.send(person))) .build(); final String personJson = "{\"name\":\"Frank\"}"; From 87533e961daa14d5ab1d26e79a0b446c9d17b1e8 Mon Sep 17 00:00:00 2001 From: Laird Nelson Date: Thu, 7 Feb 2019 09:54:14 -0800 Subject: [PATCH 4/4] Corrected checkstyle errors Signed-off-by: Laird Nelson --- .../media/jackson/common/JacksonRuntimeException.java | 2 +- .../io/helidon/media/jackson/server/JacksonSupport.java | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java index 52617b38ba1..0aa04e92333 100644 --- a/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java +++ b/media/jackson/common/src/main/java/io/helidon/media/jackson/common/JacksonRuntimeException.java @@ -27,5 +27,5 @@ public class JacksonRuntimeException extends RuntimeException { JacksonRuntimeException(final String message, final Throwable cause) { super(message, cause); } - + } diff --git a/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java index 3e46082e472..40e64493dbb 100644 --- a/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java +++ b/media/jackson/server/src/main/java/io/helidon/media/jackson/server/JacksonSupport.java @@ -72,11 +72,13 @@ public void accept(final ServerRequest request, final ServerResponse response) { /** * Creates a new {@link JacksonSupport}. + * + * @return a new {@link JacksonSupport} */ public static JacksonSupport create() { return create((req, res) -> new ObjectMapper()); } - + /** * Creates a new {@link JacksonSupport}. * @@ -84,6 +86,8 @@ public static JacksonSupport create() { * an {@link ObjectMapper} when given a {@link ServerRequest} and * a {@link ServerResponse}; must not be {@code null} * + * @return a new {@link JacksonSupport} + * * @exception NullPointerException if {@code objectMapperProvider} * is {@code null} */