You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by bl...@apache.org on 2009/09/16 03:52:39 UTC

svn commit: r815586 - in /incubator/wink/trunk/wink-common/src: main/java/org/apache/wink/common/internal/providers/entity/ main/java/org/apache/wink/common/internal/registry/ main/java/org/apache/wink/common/internal/registry/metadata/ main/java/org/a...

Author: bluk
Date: Wed Sep 16 01:52:38 2009
New Revision: 815586

URL: http://svn.apache.org/viewvc?rev=815586&view=rev
Log:
Consumes/Produces can contain , separated values

Thanks Mike Rheinheimer for the contribution.

See [WINK-186]

Added:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/AnnotationUtils.java   (with props)
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/MyProvider.java   (with props)
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ProvidersRegistry11Test.java   (with props)
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/metadata/
    incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollectorTest.java   (with props)
Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/AssetProvider.java
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/AssetProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/AssetProvider.java?rev=815586&r1=815585&r2=815586&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/AssetProvider.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/entity/AssetProvider.java Wed Sep 16 01:52:38 2009
@@ -54,6 +54,7 @@
 import org.apache.wink.common.internal.registry.InjectableFactory;
 import org.apache.wink.common.internal.registry.Injectable.ParamType;
 import org.apache.wink.common.internal.runtime.RuntimeContextTLS;
+import org.apache.wink.common.internal.utils.AnnotationUtils;
 import org.apache.wink.common.internal.utils.GenericsUtils;
 import org.apache.wink.common.internal.utils.MediaTypeUtils;
 
@@ -274,7 +275,8 @@
         for (Method method : methods) {
             Produces annotation = method.getAnnotation(Produces.class);
             if (annotation != null) {
-                String[] producesArray = annotation.value();
+                String[] producesArray =
+                    AnnotationUtils.parseConsumesProducesValues(annotation.value());
                 List<MediaType> produces = toSortedMediaTypes(producesArray);
                 for (MediaType mt : produces) {
                     if (mt.isCompatible(mediaType)) {
@@ -296,7 +298,8 @@
         for (Method method : methods) {
             Consumes annotation = method.getAnnotation(Consumes.class);
             if (annotation != null) {
-                String[] producesArray = annotation.value();
+                String[] producesArray =
+                    AnnotationUtils.parseConsumesProducesValues(annotation.value());
                 List<MediaType> produces = toSortedMediaTypes(producesArray);
                 for (MediaType mt : produces) {
                     if (mt.isCompatible(mediaType)) {

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java?rev=815586&r1=815585&r2=815586&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/ProvidersRegistry.java Wed Sep 16 01:52:38 2009
@@ -55,6 +55,7 @@
 import org.apache.wink.common.internal.i18n.Messages;
 import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
 import org.apache.wink.common.internal.lifecycle.ObjectFactory;
+import org.apache.wink.common.internal.utils.AnnotationUtils;
 import org.apache.wink.common.internal.utils.GenericsUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -361,7 +362,7 @@
                 Consumes consumes = factory.getInstanceClass().getAnnotation(Consumes.class);
                 String[] values = null;
                 if (consumes != null) {
-                    values = consumes.value();
+                    values = AnnotationUtils.parseConsumesProducesValues(consumes.value());
                 } else {
                     values = new String[] {MediaType.WILDCARD};
                 }
@@ -397,7 +398,7 @@
             if (produces == null) {
                 put(MediaType.WILDCARD_TYPE, objectFactory);
             } else {
-                String[] values = produces.value();
+                String[] values = AnnotationUtils.parseConsumesProducesValues(produces.value());
                 for (String val : values) {
                     put(MediaType.valueOf(val), objectFactory);
                 }
@@ -416,7 +417,7 @@
             if (consumes == null) {
                 put(MediaType.WILDCARD_TYPE, objectFactory);
             } else {
-                String[] values = consumes.value();
+                String[] values = AnnotationUtils.parseConsumesProducesValues(consumes.value());
                 for (String val : values) {
                     put(MediaType.valueOf(val), objectFactory);
                 }

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java?rev=815586&r1=815585&r2=815586&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java (original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollector.java Wed Sep 16 01:52:38 2009
@@ -44,6 +44,7 @@
 import org.apache.wink.common.internal.i18n.Messages;
 import org.apache.wink.common.internal.registry.Injectable;
 import org.apache.wink.common.internal.registry.InjectableFactory;
+import org.apache.wink.common.internal.utils.AnnotationUtils;
 
 /**
  * Collects ClassMetadata from JAX-RS Resource classes
@@ -323,7 +324,7 @@
     private String[] getConsumes(AnnotatedElement element) {
         Consumes consumes = element.getAnnotation(Consumes.class);
         if (consumes != null) {
-            return consumes.value();
+            return AnnotationUtils.parseConsumesProducesValues(consumes.value());
         }
         return new String[] {};
     }
@@ -331,7 +332,7 @@
     private String[] getProduces(AnnotatedElement element) {
         Produces produces = element.getAnnotation(Produces.class);
         if (produces != null) {
-            return produces.value();
+            return AnnotationUtils.parseConsumesProducesValues(produces.value());
         }
         return new String[] {};
     }

Added: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/AnnotationUtils.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/AnnotationUtils.java?rev=815586&view=auto
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/AnnotationUtils.java (added)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/AnnotationUtils.java Wed Sep 16 01:52:38 2009
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * 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.wink.common.internal.utils;
+
+import java.util.ArrayList;
+import java.util.StringTokenizer;
+
+public class AnnotationUtils {
+
+    /**
+     * Utility method to parse the values array returned from the @Consumes or @Produces
+     * annotation. JAX-RS 1.1 E012 allows example syntax:
+     * 
+     * @Consumes({"text/xml, application/xml", "text/plain"}) The annotation
+     *                       value() method will give an array with two strings,
+     *                       not the desired three, hence the need for this
+     *                       utility method.
+     * @param values
+     * @return String[] representing the media type values declared in the
+     *         annotation
+     */
+    public static String[] parseConsumesProducesValues(String[] values) {
+        ArrayList<String> strings = new ArrayList<String>();
+        for (String v : values) {
+            StringTokenizer tokenizer = new StringTokenizer(v, ",");
+            while (tokenizer.hasMoreTokens()) {
+                strings.add(tokenizer.nextToken().trim());
+            }
+        }
+        return (String[])strings.toArray(new String[] {});
+    }
+
+}

Propchange: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/utils/AnnotationUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/MyProvider.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/MyProvider.java?rev=815586&view=auto
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/MyProvider.java (added)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/MyProvider.java Wed Sep 16 01:52:38 2009
@@ -0,0 +1,69 @@
+/*******************************************************************************
+ * 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.wink.common.internal.registry;
+
+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.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyReader;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+
+@Provider
+@Consumes( { "abcd/efg, hijk/lmn", "opqr/stu" })  // testing permitted syntax from JAX-RS 1.1 E012
+@Produces( { "abcd/efg, hijk/lmn", "opqr/stu" })  // testing permitted syntax from JAX-RS 1.1 E012
+public class MyProvider implements MessageBodyReader<String>, MessageBodyWriter<String> {
+
+    public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public String readFrom(Class<String> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
+            throws IOException, WebApplicationException {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    public long getSize(String t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        // TODO Auto-generated method stub
+        return 0;
+    }
+
+    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
+        // TODO Auto-generated method stub
+        return false;
+    }
+
+    public void writeTo(String t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
+            throws IOException, WebApplicationException {
+        // TODO Auto-generated method stub
+
+    }
+
+}
\ No newline at end of file

Propchange: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/MyProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ProvidersRegistry11Test.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ProvidersRegistry11Test.java?rev=815586&view=auto
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ProvidersRegistry11Test.java (added)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ProvidersRegistry11Test.java Wed Sep 16 01:52:38 2009
@@ -0,0 +1,78 @@
+/*******************************************************************************
+ * 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.wink.common.internal.registry;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map.Entry;
+
+import junit.framework.TestCase;
+
+import org.apache.wink.common.internal.application.ApplicationValidator;
+import org.apache.wink.common.internal.lifecycle.LifecycleManagersRegistry;
+
+/**
+ * specifically testing JAX-RS 1.1
+ */
+public class ProvidersRegistry11Test extends TestCase {
+
+    /**
+     * JAX-RS 1.1 allows syntax such as:
+     * 
+     * @Consumes( { "abcd/efg, hijk/lmn", "opqr/stu" })
+     * @throws Exception
+     */
+    public void testConsumesAnnotationParsing() throws Exception {
+        ProvidersRegistry providersRegistry =
+            new ProvidersRegistry(new LifecycleManagersRegistry(), new ApplicationValidator());
+        providersRegistry.addProvider(MyProvider.class);
+        Field field = providersRegistry.getClass().getDeclaredField("messageBodyReaders");
+        field.setAccessible(true);
+        Object messageBodyReaders = field.get(providersRegistry);
+        Field field2 = messageBodyReaders.getClass().getSuperclass().getDeclaredField("data");
+        field2.setAccessible(true);
+        HashMap data = (HashMap)field2.get(messageBodyReaders);
+        assertEquals(3, data.size());
+
+    }
+
+    /**
+     * JAX-RS 1.1 allows syntax such as:
+     * 
+     * @Produces( { "abcd/efg, hijk/lmn", "opqr/stu" })
+     * @throws Exception
+     */
+    public void testProvidesAnnotationParsing() throws Exception {
+        ProvidersRegistry providersRegistry =
+            new ProvidersRegistry(new LifecycleManagersRegistry(), new ApplicationValidator());
+        providersRegistry.addProvider(MyProvider.class);
+        Field field = providersRegistry.getClass().getDeclaredField("messageBodyWriters");
+        field.setAccessible(true);
+        Object messageBodyWriters = field.get(providersRegistry);
+        Field field2 = messageBodyWriters.getClass().getSuperclass().getDeclaredField("data");
+        field2.setAccessible(true);
+        HashMap data = (HashMap)field2.get(messageBodyWriters);
+        assertEquals(3, data.size());
+
+    }
+
+}

Propchange: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/ProvidersRegistry11Test.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollectorTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollectorTest.java?rev=815586&view=auto
==============================================================================
--- incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollectorTest.java (added)
+++ incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollectorTest.java Wed Sep 16 01:52:38 2009
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * 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.wink.common.internal.registry.metadata;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+
+import junit.framework.TestCase;
+
+import org.apache.wink.common.internal.registry.metadata.ClassMetadata;
+import org.apache.wink.common.internal.registry.metadata.ResourceMetadataCollector;
+
+public class ResourceMetadataCollectorTest extends TestCase {
+
+    @Path("/myresource")
+    public class MyResource {
+
+        @GET
+        @Consumes( {"abcd/efg, hijk/lmn", "opqr/stu"})
+        // testing permitted syntax from JAX-RS 1.1 E012
+        @Produces( {"abcd/efg, hijk/lmn", "opqr/stu"})
+        // testing permitted syntax from JAX-RS 1.1 E012
+        public String getString() {
+            return "blahblah";
+        }
+    }
+
+    /**
+     * JAX-RS 1.1 allows syntax such as:
+     * 
+     * @Consumes( { "abcd/efg, hijk/lmn", "opqr/stu" })
+     * @throws Exception
+     */
+    public void testConsumesAnnotationParsing() throws Exception {
+        ClassMetadata classMetadata = ResourceMetadataCollector.collectMetadata(MyResource.class);
+        Set<MediaType> mediaTypes = classMetadata.getResourceMethods().get(0).getConsumes();
+        assertEquals(3, mediaTypes.size());
+
+        HashSet<MediaType> values = new HashSet<MediaType>(3);
+        for (Iterator<MediaType> it = mediaTypes.iterator(); it.hasNext();) {
+            values.add((MediaType)it.next());
+        }
+
+        HashSet<MediaType> expected = new HashSet<MediaType>(3);
+        expected.add(new MediaType("abcd", "efg"));
+        expected.add(new MediaType("hijk", "lmn")); // make sure whitespace is
+                                                    // ignored
+        expected.add(new MediaType("opqr", "stu"));
+
+        assertEquals(expected, values);
+    }
+
+    /**
+     * JAX-RS 1.1 allows syntax such as:
+     * 
+     * @Produces( { "abcd/efg, hijk/lmn", "opqr/stu" })
+     * @throws Exception
+     */
+    public void testProducesAnnotationParsing() throws Exception {
+        ClassMetadata classMetadata = ResourceMetadataCollector.collectMetadata(MyResource.class);
+        Set<MediaType> mediaTypes = classMetadata.getResourceMethods().get(0).getProduces();
+        assertEquals(3, mediaTypes.size());
+    }
+}

Propchange: incubator/wink/trunk/wink-common/src/test/java/org/apache/wink/common/internal/registry/metadata/ResourceMetadataCollectorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native