You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2018/08/22 09:24:12 UTC

[2/5] tomee git commit: TOMEE-2221 - Improved compatibility between Johnzon and Jsonb Provider. Added Jsonb PropertyVisibilityStrategy to also map objects annotated with Johnzon annotations. Added TomEE Provider to control CXF Provider order.

TOMEE-2221 - Improved compatibility between Johnzon and Jsonb Provider.
Added Jsonb PropertyVisibilityStrategy to also map objects annotated with Johnzon annotations.
Added TomEE Provider to control CXF Provider order.

(cherry picked from commit a44f123)


Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/82472058
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/82472058
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/82472058

Branch: refs/heads/tomee-7.1.x
Commit: 82472058c8052bbf18874feb732a9b7ad4f41ab6
Parents: 0fa51f4
Author: Roberto Cortez <ra...@yahoo.com>
Authored: Fri Aug 10 18:02:34 2018 +0100
Committer: Roberto Cortez <ra...@yahoo.com>
Committed: Tue Aug 21 12:24:23 2018 +0100

----------------------------------------------------------------------
 .../openejb/server/cxf/rs/CxfRSService.java     |   3 +-
 .../TomEEJsonbPropertyVisibilityStrategy.java   |  88 ++++++++++++++++
 .../cxf/rs/johnzon/TomEEJsonbProvider.java      |  33 ++++++
 .../cxf/rs/johnzon/JsonbJaxrsProviderTest.java  | 103 +++++++++++++++++++
 4 files changed, 225 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tomee/blob/82472058/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
index 2d7a55f..347f42b 100644
--- a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRSService.java
@@ -224,8 +224,7 @@ public class CxfRSService extends RESTService {
 
             final Collection<Object> defaults = new ArrayList<>();
             for (final String provider : asList(
-                    "org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider",
-                    "org.apache.openejb.server.cxf.rs.johnzon.TomEEJohnzonProvider",
+                    "org.apache.openejb.server.cxf.rs.johnzon.TomEEJsonbProvider",
                     "org.apache.openejb.server.cxf.rs.johnzon.TomEEJsonpProvider")) {
                 if (!isActive(provider)) {
                     continue;

http://git-wip-us.apache.org/repos/asf/tomee/blob/82472058/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbPropertyVisibilityStrategy.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbPropertyVisibilityStrategy.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbPropertyVisibilityStrategy.java
new file mode 100644
index 0000000..dd7bce8
--- /dev/null
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbPropertyVisibilityStrategy.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.openejb.server.cxf.rs.johnzon;
+
+import org.apache.johnzon.mapper.JohnzonProperty;
+
+import javax.json.bind.annotation.JsonbProperty;
+import javax.json.bind.annotation.JsonbVisibility;
+import javax.json.bind.config.PropertyVisibilityStrategy;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+public class TomEEJsonbPropertyVisibilityStrategy implements PropertyVisibilityStrategy {
+    private final ConcurrentMap<Class<?>, PropertyVisibilityStrategy> strategies = new ConcurrentHashMap<>();
+
+    @Override
+    public boolean isVisible(final Field field) {
+        return field.getAnnotation(JsonbProperty.class) != null ||
+               field.getAnnotation(JohnzonProperty.class) != null ||
+               Modifier.isPublic(field.getModifiers()) ||
+               strategies.computeIfAbsent(field.getDeclaringClass(), this::visibilityStrategy).isVisible(field);
+    }
+
+    @Override
+    public boolean isVisible(final Method method) {
+        return method.getAnnotation(JsonbProperty.class) != null ||
+               method.getAnnotation(JohnzonProperty.class) != null ||
+               Modifier.isPublic(method.getModifiers()) ||
+               strategies.computeIfAbsent(method.getDeclaringClass(), this::visibilityStrategy).isVisible(method);
+    }
+
+    private PropertyVisibilityStrategy visibilityStrategy(final Class<?> type) { // can be cached
+        JsonbVisibility visibility = type.getAnnotation(JsonbVisibility.class);
+        if (visibility != null) {
+            try {
+                return visibility.value().newInstance();
+            } catch (final InstantiationException | IllegalAccessException e) {
+                throw new IllegalArgumentException(e);
+            }
+        }
+        Package p = type.getPackage();
+        while (p != null) {
+            visibility = p.getAnnotation(JsonbVisibility.class);
+            if (visibility != null) {
+                try {
+                    return visibility.value().newInstance();
+                } catch (final InstantiationException | IllegalAccessException e) {
+                    throw new IllegalArgumentException(e);
+                }
+            }
+            final String name = p.getName();
+            final int end = name.lastIndexOf('.');
+            if (end < 0) {
+                break;
+            }
+            p = Package.getPackage(name.substring(0, end));
+        }
+
+        return new PropertyVisibilityStrategy() {
+            @Override
+            public boolean isVisible(final Field field) {
+                return false;
+            }
+
+            @Override
+            public boolean isVisible(final Method method) {
+                return false;
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/82472058/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbProvider.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbProvider.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbProvider.java
new file mode 100644
index 0000000..19a8815
--- /dev/null
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/johnzon/TomEEJsonbProvider.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.openejb.server.cxf.rs.johnzon;
+
+import org.apache.johnzon.jaxrs.jsonb.jaxrs.JsonbJaxrsProvider;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.Produces;
+import javax.ws.rs.ext.Provider;
+
+@Provider
+// This will sort the Provider to be after CXF defaults. Check org.apache.cxf.jaxrs.provider.ProviderFactory.sortReaders()
+@Produces({"application/json", "application/*+json"})
+@Consumes({"application/json", "application/*+json"})
+public class TomEEJsonbProvider<T> extends JsonbJaxrsProvider<T> {
+    public TomEEJsonbProvider() {
+        config.withPropertyVisibilityStrategy(new TomEEJsonbPropertyVisibilityStrategy());
+    }
+}

http://git-wip-us.apache.org/repos/asf/tomee/blob/82472058/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/johnzon/JsonbJaxrsProviderTest.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/johnzon/JsonbJaxrsProviderTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/johnzon/JsonbJaxrsProviderTest.java
new file mode 100644
index 0000000..0c4a451
--- /dev/null
+++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/johnzon/JsonbJaxrsProviderTest.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.openejb.server.cxf.rs.johnzon;
+
+import org.apache.johnzon.mapper.JohnzonIgnore;
+import org.apache.openejb.jee.EjbJar;
+import org.apache.openejb.jee.SingletonBean;
+import org.apache.openejb.junit.ApplicationComposer;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.testing.EnableServices;
+import org.apache.openejb.testing.Module;
+import org.apache.openejb.testing.RandomPort;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import javax.json.bind.annotation.JsonbTransient;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import java.io.IOException;
+import java.net.URL;
+
+import static org.junit.Assert.assertEquals;
+
+@EnableServices("jax-rs")
+@RunWith(ApplicationComposer.class)
+public class JsonbJaxrsProviderTest {
+    @RandomPort("http")
+    private URL base;
+
+    @Module
+    public EjbJar beans() {
+        final EjbJar ejbJar = new EjbJar();
+        ejbJar.addEnterpriseBean(new SingletonBean(Endpoint.class));
+        return ejbJar;
+    }
+
+    @Test
+    public void run() throws IOException {
+        assertEquals("{\"value\":\"value\"}",
+                     IO.slurp(new URL(base.toExternalForm() + getClass().getSimpleName() + "/test")));
+    }
+
+    @Path("test")
+    public static class Endpoint {
+        @GET
+        @Produces(MediaType.APPLICATION_JSON)
+        public Model get() {
+            final Model model = new Model();
+            model.setValue("value");
+            model.setIgnoreJohnzon("ignoreJohnzon");
+            model.setIgnoreJsonb("ignoreJsonb");
+            return model;
+        }
+    }
+
+    public static class Model {
+        private String value;
+        @JohnzonIgnore
+        private String ignoreJohnzon;
+        @JsonbTransient
+        private String ignoreJsonb;
+
+        public String getValue() {
+            return value;
+        }
+
+        public void setValue(final String value) {
+            this.value = value;
+        }
+
+        public String getIgnoreJohnzon() {
+            return ignoreJohnzon;
+        }
+
+        public void setIgnoreJohnzon(final String ignoreJohnzon) {
+            this.ignoreJohnzon = ignoreJohnzon;
+        }
+
+        public String getIgnoreJsonb() {
+            return ignoreJsonb;
+        }
+
+        public void setIgnoreJsonb(final String ignoreJsonb) {
+            this.ignoreJsonb = ignoreJsonb;
+        }
+    }
+}