JsonbConfig injectable by JAX-RS Provider Project: http://git-wip-us.apache.org/repos/asf/johnzon/repo Commit: http://git-wip-us.apache.org/repos/asf/johnzon/commit/46314068 Tree: http://git-wip-us.apache.org/repos/asf/johnzon/tree/46314068 Diff: http://git-wip-us.apache.org/repos/asf/johnzon/diff/46314068
Branch: refs/heads/master Commit: 463140687c354b73846bacebaf9c3cf653357838 Parents: f6f85ed Author: amoscatelli <[email protected]> Authored: Tue Mar 27 11:55:58 2018 +0200 Committer: amoscatelli <[email protected]> Committed: Tue Mar 27 11:55:58 2018 +0200 ---------------------------------------------------------------------- johnzon-jsonb/pom.xml | 188 ++++++++++--------- .../jaxrs/jsonb/jaxrs/JsonbJaxrsProvider.java | 43 +++-- .../johnzon/jsonb/jaxrs/JsonbJaxRsTest.java | 35 +++- 3 files changed, 155 insertions(+), 111 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/johnzon/blob/46314068/johnzon-jsonb/pom.xml ---------------------------------------------------------------------- diff --git a/johnzon-jsonb/pom.xml b/johnzon-jsonb/pom.xml index d63250e..0ba524d 100644 --- a/johnzon-jsonb/pom.xml +++ b/johnzon-jsonb/pom.xml @@ -18,100 +18,108 @@ under the License. --> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <parent> - <artifactId>johnzon</artifactId> - <groupId>org.apache.johnzon</groupId> - <version>1.1.8-SNAPSHOT</version> - </parent> - <modelVersion>4.0.0</modelVersion> + <parent> + <artifactId>johnzon</artifactId> + <groupId>org.apache.johnzon</groupId> + <version>1.1.8-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> - <artifactId>johnzon-jsonb</artifactId> - <name>Johnzon :: JSON-B Implementation</name> - <packaging>bundle</packaging> + <artifactId>johnzon-jsonb</artifactId> + <name>Johnzon :: JSON-B Implementation</name> + <packaging>bundle</packaging> - <properties> - <java-compile.version>1.8</java-compile.version> - <staging.directory>${project.parent.reporting.outputDirectory}</staging.directory> - </properties> + <properties> + <java-compile.version>1.8</java-compile.version> + <staging.directory>${project.parent.reporting.outputDirectory}</staging.directory> + </properties> - <dependencies> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-jaxrs_2.0_spec</artifactId> - <version>1.0-alpha-1</version> - <scope>provided</scope> - <optional>true</optional> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-jcdi_1.1_spec</artifactId> - <version>1.0</version> - <scope>provided</scope> - <optional>true</optional> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-jsonb_1.0_spec</artifactId> - <version>1.0</version> - <scope>provided</scope> - </dependency> + <dependencies> + + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-annotation_1.3_spec</artifactId> + <version>1.0</version> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jaxrs_2.0_spec</artifactId> + <version>1.0-alpha-1</version> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jcdi_1.1_spec</artifactId> + <version>1.0</version> + <scope>provided</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jsonb_1.0_spec</artifactId> + <version>1.0</version> + <scope>provided</scope> + </dependency> - <dependency> - <groupId>org.apache.johnzon</groupId> - <artifactId>johnzon-mapper</artifactId> - <version>${project.version}</version> - </dependency> + <dependency> + <groupId>org.apache.johnzon</groupId> + <artifactId>johnzon-mapper</artifactId> + <version>${project.version}</version> + </dependency> - <dependency> - <groupId>org.apache.cxf</groupId> - <artifactId>cxf-rt-rs-client</artifactId> - <version>${cxf.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.cxf</groupId> - <artifactId>cxf-rt-frontend-jaxrs</artifactId> - <version>${cxf.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.cxf</groupId> - <artifactId>cxf-rt-transports-local</artifactId> - <version>${cxf.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-interceptor_1.2_spec</artifactId> - <version>1.0</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.geronimo.specs</groupId> - <artifactId>geronimo-atinject_1.0_spec</artifactId> - <version>1.0</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.apache.openwebbeans</groupId> - <artifactId>openwebbeans-impl</artifactId> - <version>1.6.2</version> - <scope>test</scope> - </dependency> - </dependencies> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-rs-client</artifactId> + <version>${cxf.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-frontend-jaxrs</artifactId> + <version>${cxf.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.cxf</groupId> + <artifactId>cxf-rt-transports-local</artifactId> + <version>${cxf.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-interceptor_1.2_spec</artifactId> + <version>1.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-atinject_1.0_spec</artifactId> + <version>1.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.openwebbeans</groupId> + <artifactId>openwebbeans-impl</artifactId> + <version>1.6.2</version> + <scope>test</scope> + </dependency> + </dependencies> - <build> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <configuration> - <instructions> - <Require-Capability>osgi.extender;filter:="(osgi.extender=osgi.serviceloader.registrar)"</Require-Capability> - <Provide-Capability>osgi.serviceloader;osgi.serviceloader=javax.json.bind.spi.JsonbProvider</Provide-Capability> - </instructions> - </configuration> - </plugin> - </plugins> - </build> + <build> + <plugins> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + <configuration> + <instructions> + <Require-Capability>osgi.extender;filter:="(osgi.extender=osgi.serviceloader.registrar)"</Require-Capability> + <Provide-Capability>osgi.serviceloader;osgi.serviceloader=javax.json.bind.spi.JsonbProvider</Provide-Capability> + </instructions> + </configuration> + </plugin> + </plugins> + </build> </project> http://git-wip-us.apache.org/repos/asf/johnzon/blob/46314068/johnzon-jsonb/src/main/java/org/apache/johnzon/jaxrs/jsonb/jaxrs/JsonbJaxrsProvider.java ---------------------------------------------------------------------- diff --git a/johnzon-jsonb/src/main/java/org/apache/johnzon/jaxrs/jsonb/jaxrs/JsonbJaxrsProvider.java b/johnzon-jsonb/src/main/java/org/apache/johnzon/jaxrs/jsonb/jaxrs/JsonbJaxrsProvider.java index 49cd334..245d1c1 100644 --- a/johnzon-jsonb/src/main/java/org/apache/johnzon/jaxrs/jsonb/jaxrs/JsonbJaxrsProvider.java +++ b/johnzon-jsonb/src/main/java/org/apache/johnzon/jaxrs/jsonb/jaxrs/JsonbJaxrsProvider.java @@ -43,16 +43,25 @@ import java.lang.reflect.Type; import java.util.Collection; import java.util.Properties; import java.util.concurrent.atomic.AtomicReference; +import javax.annotation.Priority; +import javax.ws.rs.core.Context; +import javax.ws.rs.ext.ContextResolver; +import javax.ws.rs.ext.Providers; // here while we dont compile in java 8 jaxrs module, when migrated we'll merge it with IgnorableTypes hierarchy at least @Provider -@Produces("application/json") -@Consumes("application/json") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +@Priority(value = 4900) public class JsonbJaxrsProvider<T> implements MessageBodyWriter<T>, MessageBodyReader<T> { + protected final Collection<String> ignores; protected final AtomicReference<Jsonb> delegate = new AtomicReference<>(); protected final JsonbConfig config = new JsonbConfig(); + @Context + private Providers providers; + public JsonbJaxrsProvider() { this(null); } @@ -144,26 +153,26 @@ public class JsonbJaxrsProvider<T> implements MessageBodyWriter<T>, MessageBodyR @Override public T readFrom(final Class<T> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, - final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException { - return delegate().fromJson(entityStream, genericType); + final MultivaluedMap<String, String> httpHeaders, final InputStream entityStream) throws IOException, WebApplicationException { + return getJsonb(type).fromJson(entityStream, genericType); } @Override public void writeTo(final T t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, - final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException { - delegate().toJson(t, entityStream); - } - - private Jsonb delegate() { - Jsonb jsonb = delegate.get(); - if (jsonb == null) { - final Jsonb newJsonb = createJsonb(); - if (delegate.compareAndSet(null, newJsonb)) { - jsonb = newJsonb; - } else { - jsonb = delegate.get(); + final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException, WebApplicationException { + getJsonb(type).toJson(t, entityStream); + } + + protected Jsonb getJsonb(Class<?> type) { + ContextResolver<Jsonb> contextResolver = providers.getContextResolver(Jsonb.class, MediaType.APPLICATION_JSON_TYPE); + if (contextResolver != null) { + return contextResolver.getContext(type); + } else { + if (delegate.get() == null) { + delegate.compareAndSet(null, createJsonb()); } + return delegate.get(); } - return jsonb; } + } http://git-wip-us.apache.org/repos/asf/johnzon/blob/46314068/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/jaxrs/JsonbJaxRsTest.java ---------------------------------------------------------------------- diff --git a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/jaxrs/JsonbJaxRsTest.java b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/jaxrs/JsonbJaxRsTest.java index d733447..c01155c 100644 --- a/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/jaxrs/JsonbJaxRsTest.java +++ b/johnzon-jsonb/src/test/java/org/apache/johnzon/jsonb/jaxrs/JsonbJaxRsTest.java @@ -41,22 +41,40 @@ import java.io.IOException; import java.io.OutputStream; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.Arrays; import java.util.List; import static java.util.Arrays.asList; import static java.util.Collections.singletonList; +import javax.json.bind.Jsonb; +import javax.json.bind.JsonbBuilder; +import javax.ws.rs.ext.ContextResolver; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class JsonbJaxRsTest { + private final static String ENDPOINT_ADDRESS = "local://johnzon"; private static Server server; + public static class JsonBindingProvider implements ContextResolver<Jsonb> { + + private final Jsonb jsonb = JsonbBuilder.create(); + public static Boolean called = Boolean.FALSE; + + @Override + public Jsonb getContext(Class<?> type) { + called = Boolean.TRUE; + return jsonb; + } + + } + @BeforeClass public static void bindEndpoint() throws Exception { final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean(); sf.setResourceClasses(JohnzonResource.class); - sf.setProviders(singletonList(new JsonbJaxrsProvider<>())); + sf.setProviders(Arrays.asList(new JsonbJaxrsProvider<>(), new JsonBindingProvider())); sf.setResourceProvider(JohnzonResource.class, new SingletonResourceProvider(new JohnzonResource(), false)); sf.setAddress(ENDPOINT_ADDRESS); server = sf.create(); @@ -75,6 +93,12 @@ public class JsonbJaxRsTest { } @Test + public void jsonbconfigProvider() { + client().path("johnzon").get(String.class); + assertEquals(JsonBindingProvider.called, Boolean.TRUE); + } + + @Test public void object() { final String output = client().path("johnzon").get(String.class); assertEquals("{\"name\":\"johnzon\"}", output); @@ -112,7 +136,7 @@ public class JsonbJaxRsTest { .get(byte[].class); Assert.assertEquals(100, content.length); - for (int i=0; i < 100; i++) { + for (int i = 0; i < 100; i++) { Assert.assertEquals((byte) i, content[i]); } } @@ -155,6 +179,7 @@ public class JsonbJaxRsTest { } public static class Johnzon { + private String name; public Johnzon(final String name) { @@ -176,6 +201,7 @@ public class JsonbJaxRsTest { @Path("johnzon") public static class JohnzonResource { + @GET public Johnzon johnzon() { return new Johnzon("johnzon"); @@ -184,7 +210,7 @@ public class JsonbJaxRsTest { @GET @Path("all1") public Johnzon[] johnzons1() { - return new Johnzon[] { new Johnzon("johnzon1"), new Johnzon("johnzon2") }; + return new Johnzon[]{new Johnzon("johnzon1"), new Johnzon("johnzon2")}; } @GET @@ -221,11 +247,12 @@ public class JsonbJaxRsTest { @Path("mybinary") public byte[] binary() { byte[] content = new byte[100]; - for (int i=0; i < 100; i++) { + for (int i = 0; i < 100; i++) { content[i] = (byte) i; } return content; } } + }
