cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dk...@apache.org
Subject svn commit: r630317 - in /incubator/cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/provider/ test/java/org/apache/cxf/jaxrs/model/ test/java/org/apache/cxf/jaxrs/provider/
Date Fri, 22 Feb 2008 20:59:48 GMT
Author: dkulp
Date: Fri Feb 22 12:59:45 2008
New Revision: 630317

URL: http://svn.apache.org/viewvc?rev=630317&view=rev
Log:
Forgot to svn add some files

Added:
    incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
  (with props)
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
  (with props)
    incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
  (with props)

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java?rev=630317&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
Fri Feb 22 12:59:45 2008
@@ -0,0 +1,92 @@
+/**
+ * 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;
+
+import java.text.ParseException;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.HeaderProvider;
+
+public class MediaTypeHeaderProvider implements HeaderProvider<MediaType> {
+
+    public MediaType fromString(String mType) throws ParseException {
+        
+        if (mType.equals(MediaType.MEDIA_TYPE_WILDCARD) || mType.startsWith("*;")) {
+            return new MediaType("*", "*");
+        }
+        
+        int i = mType.indexOf('/');
+        if (i == -1) {
+            throw new ParseException("Media type separator is missing", 0);
+        }
+        
+        int paramsStart = mType.indexOf(';', i + 1);
+        int end = paramsStart == -1  ? mType.length() : paramsStart;
+        
+        String type = mType.substring(0, i); 
+        String subtype = mType.substring(i + 1, end);
+        
+        Map<String, String> parameters = null;
+        if (paramsStart != -1) {
+            // Using Pattern.compile might be marginally faster ?
+            // though it's rare when more than one parameter is provided
+            parameters = new LinkedHashMap<String, String>();
+            StringTokenizer st = 
+                new StringTokenizer(mType.substring(paramsStart + 1), ";");
+            while (st.hasMoreTokens()) {
+                String token = st.nextToken();
+                int equalSign = token.indexOf('=');
+                if (equalSign == -1) {
+                    throw new ParseException("Wrong media type  parameter, seperator is missing",
0);
+                }
+                parameters.put(token.substring(0, equalSign).trim().toLowerCase(), 
+                               token.substring(equalSign + 1).trim().toLowerCase());
+            }
+            
+        }
+        
+        return new MediaType(type.trim().toLowerCase(), 
+                             subtype.trim().toLowerCase(), 
+                             parameters);
+    }
+
+    public boolean supports(Class<?> clazz) {
+        return MediaType.class.isAssignableFrom(clazz);
+    }
+
+    public String toString(MediaType type) {
+        StringBuilder sb = new StringBuilder();
+        sb.append(type.getType()).append('/').append(type.getSubtype());
+        
+        for (Iterator<Map.Entry<String, String>> iter = type.getParameters().entrySet().iterator();
+            iter.hasNext();) {
+            Map.Entry<String, String> entry = iter.next();
+            sb.append(';').append(entry.getKey()).append('=').append(entry.getValue());
+        }
+        
+        return sb.toString();
+    }
+
+    
+}

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProvider.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java?rev=630317&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
Fri Feb 22 12:59:45 2008
@@ -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.cxf.jaxrs.model;
+
+import java.util.List;
+
+import javax.ws.rs.ConsumeMime;
+import javax.ws.rs.ProduceMime;
+import javax.ws.rs.core.MediaType;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class OperationResourceInfoTest extends Assert {
+    
+    @ProduceMime("text/xml")
+    @ConsumeMime("application/xml")
+    static class TestClass {
+        @ProduceMime("text/plain")
+        public void doIt() {
+            // empty
+        };
+        @ConsumeMime("application/atom+xml")
+        public void doThat() {
+            // empty
+        };
+        
+    }
+    
+    @Test
+    public void testConsumeTypes() throws Exception {
+        OperationResourceInfo ori1 = new OperationResourceInfo(
+                                 TestClass.class.getMethod("doIt", new Class[]{}), 
+                                 new ClassResourceInfo(TestClass.class));
+        
+        List<MediaType> ctypes = ori1.getConsumeTypes();
+        assertEquals("Single media type expected", 1, ctypes.size());
+        assertEquals("Class resource consume type should be used", 
+                   "application/xml", ctypes.get(0).toString());
+        
+        OperationResourceInfo ori2 = new OperationResourceInfo(
+                                 TestClass.class.getMethod("doThat", new Class[]{}), 
+                                 new ClassResourceInfo(TestClass.class));
+        ctypes = ori2.getConsumeTypes();
+        assertEquals("Single media type expected", 1, ctypes.size());
+        assertEquals("Method consume type should be used", 
+                   "application/atom+xml", ctypes.get(0).toString());
+    }
+    
+    @Test
+    public void testProduceTypes() throws Exception {
+        
+        OperationResourceInfo ori1 = new OperationResourceInfo(
+                                       TestClass.class.getMethod("doIt", new Class[]{}),

+                                       new ClassResourceInfo(TestClass.class));
+        
+        List<MediaType> ctypes = ori1.getProduceTypes();
+        assertEquals("Single media type expected", 1, ctypes.size());
+        assertEquals("Method produce type should be used", 
+                   "text/plain", ctypes.get(0).toString());
+        
+        OperationResourceInfo ori2 = new OperationResourceInfo(
+                                 TestClass.class.getMethod("doThat", new Class[]{}), 
+                                 new ClassResourceInfo(TestClass.class));
+        ctypes = ori2.getProduceTypes();
+        assertEquals("Single media type expected", 1, ctypes.size());
+        assertEquals("Class resource produce type should be used", 
+                     "text/xml", ctypes.get(0).toString());
+    }
+
+}

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/model/OperationResourceInfoTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java?rev=630317&view=auto
==============================================================================
--- incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
(added)
+++ incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
Fri Feb 22 12:59:45 2008
@@ -0,0 +1,125 @@
+/**
+ * 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;
+
+import java.text.ParseException;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.EntityTag;
+import javax.ws.rs.core.MediaType;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class MediaTypeHeaderProviderTest extends Assert {
+    
+    @Test
+    public void testSimpleType() {
+        MediaType m = MediaType.parse("text/html");
+        assertEquals("Media type was not parsed correctly", 
+                     m, new MediaType("text", "html"));
+        assertEquals("Media type was not parsed correctly", 
+                     MediaType.parse("text/html "), new MediaType("text", "html"));
+    }
+    
+    @Test
+    public void testShortWildcard() {
+        MediaType m = MediaType.parse("*");
+        assertEquals("Media type was not parsed correctly", 
+                     m, new MediaType("*", "*"));
+    }
+    
+    @Test
+    public void testShortWildcardWithParameters() {
+        MediaType m = MediaType.parse("*;q=0.2");
+        assertEquals("Media type was not parsed correctly", 
+                     m, new MediaType("*", "*"));
+    }
+    
+    @Test
+    public void testBadType() {
+        try {
+            new MediaTypeHeaderProvider().fromString("texthtml");
+            fail("Parse exception must've been thrown");
+        } catch (ParseException pe) {
+            // expected
+        }
+        
+    }
+    
+    @Test
+    public void testBadParameter() {
+        try {
+            new MediaTypeHeaderProvider().fromString("text/html;*");
+            fail("Parse exception must've been thrown");
+        } catch (ParseException pe) {
+            // expected
+        }
+    }
+    
+    @Test
+    public void testTypeWithParameters() {
+        MediaType m = MediaType.parse("text/html;q=1234;b=4321");
+        
+        Map<String, String> params = new HashMap<String, String>();
+        params.put("q", "1234");
+        params.put("b", "4321");
+        
+        MediaType expected = new MediaType("text", "html", params);
+        
+        assertEquals("Media type was not parsed correctly", expected, m);
+    }
+    
+    @Test
+    public void testSupports() {
+        MediaTypeHeaderProvider provider = 
+            new MediaTypeHeaderProvider();
+        
+        assertTrue(provider.supports(MediaType.class));
+        // I think we should have a single default header provider
+        assertFalse(provider.supports(EntityTag.class));
+    }
+    
+    @Test
+    public void testSimpleToString() {
+        MediaTypeHeaderProvider provider = 
+            new MediaTypeHeaderProvider();
+        
+        assertEquals("simple media type is not serialized", "text/plain",
+                     provider.toString(new MediaType("text", "plain")));
+    }
+    
+    @Test
+    public void testComplexToString() {
+        MediaTypeHeaderProvider provider = 
+            new MediaTypeHeaderProvider();
+        
+        Map<String, String> params = new LinkedHashMap<String, String>();
+        params.put("foo", "bar");
+        params.put("q", "0.2");
+        
+        assertEquals("complex media type is not serialized", "text/plain;foo=bar;q=0.2",
+                     provider.toString(new MediaType("text", "plain", params)));
+        
+    }
+
+}

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/MediaTypeHeaderProviderTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message