wink-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r813121 - in /incubator/wink/trunk: wink-common/src/main/java/org/apache/wink/common/internal/providers/header/ wink-server/src/main/java/org/apache/wink/server/internal/contexts/ wink-server/src/test/java/org/apache/wink/server/internal/
Date Wed, 09 Sep 2009 21:15:18 GMT
Author: bluk
Date: Wed Sep  9 21:15:16 2009
New Revision: 813121

URL: http://svn.apache.org/viewvc?rev=813121&view=rev
Log:
Send a 400 when Accept header has invalid types

See [WINK-172]

Modified:
    incubator/wink/trunk/wink-common/src/main/java/org/apache/wink/common/internal/providers/header/MediaTypeHeaderDelegate.java
    incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
    incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java

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=813121&r1=813120&r2=813121&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 21:15:16 2009
@@ -62,6 +62,11 @@
             String main = all[0];
             String[] mainArray = SLASH.split(main);
             type = mainArray[0];
+            if ("".equals(type)) {
+                String errMsg = String.format(Messages.getMessage("mediaTypeWrongFormat"),
value);
+                logger.error(errMsg);
+                throw new IllegalArgumentException(errMsg);
+            }
             if (mainArray.length == 1 && MediaType.MEDIA_TYPE_WILDCARD.equals(type))
{
                 subType = MediaType.MEDIA_TYPE_WILDCARD;
             } else {

Modified: incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java?rev=813121&r1=813120&r2=813121&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
(original)
+++ incubator/wink/trunk/wink-server/src/main/java/org/apache/wink/server/internal/contexts/HttpHeadersImpl.java
Wed Sep  9 21:15:16 2009
@@ -30,6 +30,7 @@
 import java.util.Map;
 
 import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Cookie;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -126,8 +127,12 @@
                 acceptValue = acceptValueTemp.toString();
             }
         }
-        Accept acceptHeader = Accept.valueOf(acceptValue);
-        return acceptHeader;
+        try {
+            Accept acceptHeader = Accept.valueOf(acceptValue);
+            return acceptHeader;
+        } catch (IllegalArgumentException e) {
+            throw new WebApplicationException(e, 400);
+        }
     }
 
     public Map<String, Cookie> getCookies() {

Modified: 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=813121&r1=813120&r2=813121&view=diff
==============================================================================
--- incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java
(original)
+++ incubator/wink/trunk/wink-server/src/test/java/org/apache/wink/server/internal/CommonAcceptHeaderTest.java
Wed Sep  9 21:15:16 2009
@@ -23,6 +23,8 @@
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.HttpHeaders;
 import javax.ws.rs.core.MediaType;
@@ -54,6 +56,18 @@
             // System.out.println(acceptMediaTypes);
             return acceptMediaTypes.size() + "";
         }
+
+        @GET
+        @Path("onlytext")
+        @Produces("text/plain")
+        public String getHelloWorldText(@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 {
@@ -81,16 +95,64 @@
         try {
             invoke(request);
             fail();
-        } catch (IllegalArgumentException e) {
-
+        } catch (WebApplicationException e) {
+            assertEquals(400, e.getResponse().getStatus());
         }
         request = MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes",
"      ");
         try {
             invoke(request);
             fail();
-        } catch (IllegalArgumentException e) {
+        } catch (WebApplicationException e) {
+            assertEquals(400, e.getResponse().getStatus());
+        }
+    }
+
+    public void testIllegalStringAcceptHeader() throws Exception {
+        MockHttpServletRequest request =
+            MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes", "text/");
+        try {
+            invoke(request);
+            fail();
+        } catch (WebApplicationException e) {
+            assertEquals(400, e.getResponse().getStatus());
+        }
+
+        request =
+            MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes", "application/");
+        try {
+            invoke(request);
+            fail();
+        } catch (WebApplicationException e) {
+            assertEquals(400, e.getResponse().getStatus());
+        }
 
+        try {
+            request =
+                MockRequestConstructor.constructMockRequest("GET", "/countaccepttypes", "/xml");
+            invoke(request);
+        } catch (WebApplicationException e) {
+            assertEquals(400, e.getResponse().getStatus());
         }
+
+        /*
+         * while the response type isn't there
+         */
+        request =
+            MockRequestConstructor
+                .constructMockRequest("GET",
+                                      "/onlytext",
+                                      "         text              / plain               
   ");
+        MockHttpServletResponse response = invoke(request);
+        assertEquals(406, response.getStatus());
+        assertEquals("", response.getContentAsString());
+
+        /*
+         * be sure that capitalization is ignore
+         */
+        request = MockRequestConstructor.constructMockRequest("GET", "/onlytext", "TEXT/pLain");
+        response = invoke(request);
+        assertEquals(200, response.getStatus());
+        assertEquals("1", response.getContentAsString());
     }
 
     public void testHttpURLConnectionAcceptHeader() throws Exception {



Mime
View raw message