wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r812753 - in /incubator/wink/trunk: wink-common/src/main/java/org/apache/wink/common/internal/providers/header/ wink-common/src/main/resources/org/apache/wink/common/internal/i18n/ wink-server/src/test/java/org/apache/wink/server/internal/
Date Wed, 09 Sep 2009 02:57:14 GMT
Author: bluk
Date: Wed Sep  9 02:57:12 2009
New Revision: 812753

URL: http://svn.apache.org/viewvc?rev=812753&view=rev
Log:
Tolerate "*" Accept header value

See [WINK-172]

Added:
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java
  (with props)
Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
    incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties

Modified: incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java?rev=812753&r1=812752&r2=812753&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
(original)
+++ incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
Wed Sep  9 02:57:12 2009
@@ -46,12 +46,12 @@
         if (value == null) {
             throw new IllegalArgumentException("MediaType header is null");
         }
-        
+
         MediaType cached = cache.get(value);
         if (cached != null) {
             return cached;
         }
-        
+
         String type = "*";
         String subType = "*";
         Map<String, String> paramsMap = null;
@@ -62,7 +62,11 @@
             String main = all[0];
             String[] mainArray = SLASH.split(main);
             type = mainArray[0];
-            subType = mainArray[1];
+            if (mainArray.length == 1 && MediaType.MEDIA_TYPE_WILDCARD.equals(type))
{
+                subType = MediaType.MEDIA_TYPE_WILDCARD;
+            } else {
+                subType = mainArray[1];
+            }
 
             // parameters
             if (all.length > 1) {
@@ -74,7 +78,7 @@
             }
         } catch (ArrayIndexOutOfBoundsException e) {
             String errMsg = String.format(Messages.getMessage("mediaTypeWrongFormat"), value);
-            logger.error(errMsg);
+            logger.error(errMsg, e);
             throw new IllegalArgumentException(errMsg, e);
         }
 

Modified: incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties?rev=812753&r1=812752&r2=812753&view=diff
==============================================================================
--- incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
(original)
+++ incubator/wink/trunk/wink-common/src/main/resources/org/apache/wink/common/internal/i18n/resource.properties
Wed Sep  9 02:57:12 2009
@@ -96,7 +96,7 @@
 jaxbCreateDefaultJAXBElement=Creating default JAXBElement for {}
 
 # Failure Messages
-mediaTypeWrongFormat=Wrong MediaType format for MediaType: {}
+mediaTypeWrongFormat=Wrong MediaType format for MediaType: %1$s
 methodDoesNotHandleType=Method doesn't handle {}
 unhandledExceptionToContainer=Unhandled exception
 exceptionOccurredDuringInvocation=%s occured during the handlers chain invocation

Added: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java?rev=812753&view=auto
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java
(added)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java
Wed Sep  9 02:57:12 2009
@@ -0,0 +1,167 @@
+/*
+ * 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.server.internal;
+
+import java.util.List;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+/**
+ * Tests for common accept headers.
+ */
+public class CommonAcceptHeaderTest extends MockServletInvocationTest {
+
+    @Override
+    protected Class<?>[] getClasses() {
+        return new Class<?>[] {EmptyRoot.class};
+    }
+
+    @Path("/")
+    public static class EmptyRoot {
+        @GET
+        @Path("countaccepttypes")
+        public String getHelloWorld(@Context HttpHeaders requestHeaders) {
+            List<MediaType> acceptMediaTypes = requestHeaders.getAcceptableMediaTypes();
+            if (acceptMediaTypes == null || acceptMediaTypes.isEmpty()) {
+                return "0";
+            }
+            // System.out.println(acceptMediaTypes);
+            return acceptMediaTypes.size() + "";
+        }
+    }
+
+    public void testWildcardOnly() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes", "*");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("1", response.getContentAsString());
+
+        request =
+            MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes", "*/;q=0.8");
+        response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("1", response.getContentAsString());
+
+        request = MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes",
"*/");
+        response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("1", response.getContentAsString());
+    }
+
+    public void testEmptyStringAcceptHeader() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes", "");
+        try {
+            invoke(request);
+            fail();
+        } catch (IllegalArgumentException e) {
+
+        }
+        request = MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes",
"      ");
+        try {
+            invoke(request);
+            fail();
+        } catch (IllegalArgumentException e) {
+
+        }
+    }
+
+    public void testHttpURLConnectionAcceptHeader() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor
+                .constructMockRequest("GET",
+                                      "/countaccepttypes",
+                                      "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("5", response.getContentAsString());
+    }
+
+    public void testFirefoxAcceptHeader() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor
+                .constructMockRequest("GET",
+                                      "/countaccepttypes",
+                                      "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("4", response.getContentAsString());
+    }
+
+    public void testIE7AcceptHeader() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("GET",
+                                                        "/countaccepttypes",
+                                                        "image/gif, " + "image/jpeg, "
+                                                            + "image/pjpeg, "
+                                                            + "image/pjpeg, "
+                                                            + "application/x-shockwave-flash,
"
+                                                            + "application/x-ms-application,
"
+                                                            + "application/x-ms-xbap, "
+                                                            + "application/vnd.ms-xpsdocument,
"
+                                                            + "application/xaml+xml, "
+                                                            + "*/*");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("10", response.getContentAsString());
+    }
+
+    public void testIEAcceptHeaderWithFlashAndSilverlightAndOfficeAcceptHeader() throws Exception
{
+        MockHttpServletRequest request =
+            MockRequestConstructor
+                .constructMockRequest("GET", "/countaccepttypes", "image/gif, " + "image/jpeg,
"
+                    + "image/pjpeg, "
+                    + "application/x-ms-application, "
+                    + "application/vnd.ms-xpsdocument, "
+                    + "application/xaml+xml, "
+                    + "application/x-ms-xbap, "
+                    + "application/x-shockwave-flash, application/x-silverlight-2-b2, "
+                    + "application/x-silverlight, application/vnd.ms-excel, "
+                    + "application/vnd.ms-powerpoint, "
+                    + "application/msword, */*");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("14", response.getContentAsString());
+    }
+
+    public void testWebKitAcceptHeader() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor
+                .constructMockRequest("GET",
+                                      "/countaccepttypes",
+                                      "application/xml," + "application/xhtml+xml,"
+                                          + "text/html;q=0.9,"
+                                          + "text/plain;q=0.8,"
+                                          + "image/png,*/*;q=0.5");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("6", response.getContentAsString());
+    }
+}

Propchange: incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message