cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Prototyping a primitive JSON data provider
Date Mon, 01 Dec 2014 13:26:37 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 79156d8a2 -> 25b660324


Prototyping a primitive JSON data provider


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

Branch: refs/heads/master
Commit: 25b66032401abdc53325a9eb8f560806e875e519
Parents: 79156d8
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Mon Dec 1 13:26:18 2014 +0000
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Mon Dec 1 13:26:18 2014 +0000

----------------------------------------------------------------------
 .../cxf/jaxrs/provider/MultipartProvider.java   |   5 +-
 .../cxf/jaxrs/provider/ProviderFactory.java     |   2 +-
 .../provider/json/SimpleTypeJsonProvider.java   | 127 +++++++++++++++++++
 3 files changed, 130 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/25b66032/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
index 43b0c75..8f7c017 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MultipartProvider.java
@@ -77,7 +77,6 @@ import org.apache.cxf.message.MessageUtils;
 public class MultipartProvider extends AbstractConfigurableProvider
     implements MessageBodyReader<Object>, MessageBodyWriter<Object> {
     
-    private static final String ACTIVE_JAXRS_PROVIDER_KEY = "active.jaxrs.provider";
     private static final String SUPPORT_TYPE_AS_MULTIPART = "support.type.as.multipart";
     private static final Logger LOG = LogUtils.getL7dLogger(MultipartProvider.class);
     private static final ResourceBundle BUNDLE = BundleUtils.getBundle(MultipartProvider.class);
@@ -379,13 +378,13 @@ public class MultipartProvider extends AbstractConfigurableProvider
                                             Annotation[] anns,
                                             String mimeType, int id) {
         MediaType mt = JAXRSUtils.toMediaType(mimeType);
-        mc.put(ACTIVE_JAXRS_PROVIDER_KEY, this);
+        mc.put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY, this);
         
         MessageBodyWriter<T> r = null;
         try {
             r = mc.getProviders().getMessageBodyWriter(cls, genericType, anns, mt);
         } finally {
-            mc.put("active.jaxrs.provider", null); 
+            mc.put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY, null); 
         }
         if (r == null) {
             org.apache.cxf.common.i18n.Message message = 

http://git-wip-us.apache.org/repos/asf/cxf/blob/25b66032/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
----------------------------------------------------------------------
diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
index 1fb8abf..934a652 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/ProviderFactory.java
@@ -79,12 +79,12 @@ import org.apache.cxf.message.MessageUtils;
 public abstract class ProviderFactory {
     public static final String DEFAULT_FILTER_NAME_BINDING = "org.apache.cxf.filter.binding";
     public static final String PROVIDER_SELECTION_PROPERTY_CHANGED = "provider.selection.property.changed";
+    public static final String ACTIVE_JAXRS_PROVIDER_KEY = "active.jaxrs.provider";
     
     protected static final String SERVER_FACTORY_NAME = "org.apache.cxf.jaxrs.provider.ServerProviderFactory";
     protected static final String CLIENT_FACTORY_NAME = "org.apache.cxf.jaxrs.client.ClientProviderFactory";
     protected static final String IGNORE_TYPE_VARIABLES = "org.apache.cxf.jaxrs.providers.ignore.typevars";
     
-    private static final String ACTIVE_JAXRS_PROVIDER_KEY = "active.jaxrs.provider";
     private static final Logger LOG = LogUtils.getL7dLogger(ProviderFactory.class);
     
     private static final String JAXB_PROVIDER_NAME = "org.apache.cxf.jaxrs.provider.JAXBElementProvider";

http://git-wip-us.apache.org/repos/asf/cxf/blob/25b66032/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/SimpleTypeJsonProvider.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/SimpleTypeJsonProvider.java
b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/SimpleTypeJsonProvider.java
new file mode 100644
index 0000000..06fdfe0
--- /dev/null
+++ b/rt/rs/extensions/providers/src/main/java/org/apache/cxf/jaxrs/provider/json/SimpleTypeJsonProvider.java
@@ -0,0 +1,127 @@
+/**
+ * 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.cxf.jaxrs.provider.json;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Providers;
+
+import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.provider.AbstractConfigurableProvider;
+import org.apache.cxf.jaxrs.provider.PrimitiveTextProvider;
+import org.apache.cxf.jaxrs.provider.ProviderFactory;
+import org.apache.cxf.jaxrs.utils.InjectionUtils;
+import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+
+@Produces("application/json")
+@Consumes("application/json")
+public class SimpleTypeJsonProvider<T> extends AbstractConfigurableProvider 
+    implements MessageBodyWriter<T>, MessageBodyReader<T> {
+
+    @Context
+    private Providers providers;
+    private boolean supportSimpleTypesOnly;
+    private PrimitiveTextProvider<T> primitiveHelper = new PrimitiveTextProvider<T>();
+    @Override
+    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
+        return !supportSimpleTypesOnly || primitiveHelper.isReadable(type, genericType, annotations,
mediaType);
+    }
+
+    @Override
+    public long getSize(T t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
+        return -1;
+    }
+
+    @Override
+    public void writeTo(T t, Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType,
+                        MultivaluedMap<String, Object> headers, OutputStream os)
+        throws IOException, WebApplicationException {
+        if (!supportSimpleTypesOnly && !InjectionUtils.isPrimitive(type)) {
+            @SuppressWarnings("unchecked")
+            MessageBodyWriter<T> next = 
+                (MessageBodyWriter<T>)providers.getMessageBodyWriter(type, genericType,
annotations, mediaType);
+            JAXRSUtils.getCurrentMessage().put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY,
this);
+            try {
+                next.writeTo(t, type, genericType, annotations, mediaType, headers, os);
+            } finally {
+                JAXRSUtils.getCurrentMessage().put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY,
null); 
+            }
+        } else {
+            os.write(StringUtils.toBytesASCII("{\"" + type.getSimpleName().toLowerCase()
+ "\":"));
+            writeQuote(os, type);
+            primitiveHelper.writeTo(t, type, genericType, annotations, mediaType, headers,
os);
+            writeQuote(os, type);
+            os.write(StringUtils.toBytesASCII("}"));
+        }
+    }
+
+    private void writeQuote(OutputStream os, Class<?> type) throws IOException {
+        if (type == String.class) {
+            os.write(StringUtils.toBytesASCII("\""));        
+        }
+    }
+    public void setSupportSimpleTypesOnly(boolean supportSimpleTypesOnly) {
+        this.supportSimpleTypesOnly = supportSimpleTypesOnly;
+    }
+
+    @Override
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations,
MediaType mediaType) {
+        return isWriteable(type, genericType, annotations, mediaType);
+    }
+
+    @Override
+    public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType
mediaType,
+                      MultivaluedMap<String, String> headers, InputStream is)
+        throws IOException, WebApplicationException {
+        if (!supportSimpleTypesOnly && !InjectionUtils.isPrimitive(type)) {
+            MessageBodyReader<T> next = 
+                (MessageBodyReader<T>)providers.getMessageBodyReader(type, genericType,
annotations, mediaType);
+            JAXRSUtils.getCurrentMessage().put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY,
this);
+            try {
+                return next.readFrom(type, genericType, annotations, mediaType, headers,
is);
+            } finally {
+                JAXRSUtils.getCurrentMessage().put(ProviderFactory.ACTIVE_JAXRS_PROVIDER_KEY,
null); 
+            }
+        } else {
+            String data = IOUtils.toString(is).trim();
+            int index = data.indexOf(':');
+            data = data.substring(index + 1, data.length() - 1).trim();
+            if (data.startsWith("\"")) {
+                data = data.substring(1, data.length() - 1);
+            }
+            return primitiveHelper.readFrom(type, genericType, annotations, mediaType, headers,

+                                            new ByteArrayInputStream(StringUtils.toBytesUTF8(data)));
+        }
+    }
+
+}


Mime
View raw message