cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1365551 - in /cxf/branches/2.5.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/
Date Wed, 25 Jul 2012 12:25:08 GMT
Author: sergeyb
Date: Wed Jul 25 12:25:07 2012
New Revision: 1365551

URL: http://svn.apache.org/viewvc?rev=1365551&view=rev
Log:
Merged revisions 1365540 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.6.x-fixes

................
  r1365540 | sergeyb | 2012-07-25 12:51:27 +0100 (Wed, 25 Jul 2012) | 9 lines
  
  Merged revisions 1365536 via svnmerge from 
  https://svn.apache.org/repos/asf/cxf/trunk
  
  ........
    r1365536 | sergeyb | 2012-07-25 12:47:51 +0100 (Wed, 25 Jul 2012) | 1 line
    
    [CXF-4443] Relaxing the control of malformed media types
  ........
................

Modified:
    cxf/branches/2.5.x-fixes/   (props changed)
    cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
    cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
    cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
    cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1365536
  Merged /cxf/branches/2.6.x-fixes:r1365540

Propchange: cxf/branches/2.5.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java?rev=1365551&r1=1365550&r2=1365551&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
(original)
+++ cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
Wed Jul 25 12:25:07 2012
@@ -24,14 +24,22 @@ import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.StringTokenizer;
+import java.util.logging.Logger;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.ext.RuntimeDelegate.HeaderDelegate;
 
-public class MediaTypeHeaderProvider implements HeaderDelegate<MediaType> {
+import org.apache.cxf.common.logging.LogUtils;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
+import org.apache.cxf.phase.PhaseInterceptorChain;
 
+public class MediaTypeHeaderProvider implements HeaderDelegate<MediaType> {
+    private static final Logger LOG = LogUtils.getL7dLogger(MediaTypeHeaderProvider.class);
+    private static final String STRICT_MEDIA_TYPE_CHECK = 
+        "org.apache.cxf.jaxrs.mediaTypeCheck.strict";
     private static final Pattern COMPLEX_PARAMETERS = 
         Pattern.compile("(([\\w-]+=\"[^\"]*\")|([\\w-]+=[\\w-/]+))");
     
@@ -43,14 +51,7 @@ public class MediaTypeHeaderProvider imp
         
         int i = mType.indexOf('/');
         if (i == -1) {
-            mType = mType.trim();
-            if (mType.startsWith(MediaType.MEDIA_TYPE_WILDCARD)) {
-                char next = mType.length() == 1 ? ' ' : mType.charAt(1);
-                if (next == ' ' || next == ';') {
-                    return new MediaType("*", "*");
-                }
-            }
-            throw new IllegalArgumentException("Media type separator is missing");
+            return handleMediaTypeWithoutSubtype(mType.trim());
         }
         
         int paramsStart = mType.indexOf(';', i + 1);
@@ -109,5 +110,28 @@ public class MediaTypeHeaderProvider imp
         return sb.toString();
     }
 
-    
+    private MediaType handleMediaTypeWithoutSubtype(String mType) {
+        if (mType.startsWith(MediaType.MEDIA_TYPE_WILDCARD)) {
+            char next = mType.length() == 1 ? ' ' : mType.charAt(1);
+            if (next == ' ' || next == ';') {
+                return MediaType.WILDCARD_TYPE;
+            }
+        }
+        Message message = PhaseInterceptorChain.getCurrentMessage();
+        if (message != null 
+            && !MessageUtils.isTrue(message.getContextualProperty(STRICT_MEDIA_TYPE_CHECK)))
{
+            MediaType mt = null;
+            if (mType.equals(MediaType.TEXT_PLAIN_TYPE.getType())) {
+                mt = MediaType.TEXT_PLAIN_TYPE;
+            } else if (mType.equals(MediaType.APPLICATION_XML_TYPE.getSubtype())) {
+                mt = MediaType.APPLICATION_XML_TYPE;
+            } else {
+                mt = MediaType.WILDCARD_TYPE;
+            }
+            LOG.fine("Converting a malformed media type '" + mType + "' to '" + mt.toString()
+ "'");
+            return mt;
+        } else {
+            throw new IllegalArgumentException("Media type separator is missing");
+        }
+    }
 }

Modified: cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=1365551&r1=1365550&r2=1365551&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
(original)
+++ cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
Wed Jul 25 12:25:07 2012
@@ -66,7 +66,9 @@ public class BookServer extends Abstract
                                new SingletonResourceProvider(new BookStore(), true));
         sf.setAddress("http://localhost:" + PORT + "/");
 
-        sf.create();        
+        sf.getProperties(true).put("org.apache.cxf.jaxrs.mediaTypeCheck.strict", true);
+        
+        sf.create();
     }
 
     public static void main(String[] args) {

Modified: cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1365551&r1=1365550&r2=1365551&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++ cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Wed Jul 25 12:25:07 2012
@@ -74,6 +74,7 @@ import javax.xml.transform.dom.DOMSource
 
 import org.apache.cxf.annotations.GZIP;
 import org.apache.cxf.common.util.ProxyHelper;
+import org.apache.cxf.jaxrs.ext.MessageContext;
 import org.apache.cxf.jaxrs.ext.Nullable;
 import org.apache.cxf.jaxrs.ext.Oneway;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
@@ -250,6 +251,15 @@ public class BookStore {
         return books.containsKey(id);
     }
     
+    @GET
+    @Path("books/check/malformedmt/{id}")
+    @Produces("text/plain")
+    public Response checkBookMalformedMT(@PathParam("id") Long id,
+                                         @Context MessageContext mc) {
+        mc.put("org.apache.cxf.jaxrs.mediaTypeCheck.strict", false);
+        return Response.ok(books.containsKey(id)).type("text").build();
+    }
+    
     @POST
     @Path("books/check2")
     @Produces("text/plain")

Modified: cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1365551&r1=1365550&r2=1365551&view=diff
==============================================================================
--- cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++ cxf/branches/2.5.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Wed Jul 25 12:25:07 2012
@@ -31,6 +31,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import javax.xml.bind.JAXBElement;
@@ -44,7 +45,9 @@ import org.apache.commons.httpclient.met
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.PutMethod;
 import org.apache.commons.httpclient.methods.RequestEntity;
+import org.apache.cxf.helpers.CastUtils;
 import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.interceptor.Fault;
 import org.apache.cxf.io.CachedOutputStream;
 import org.apache.cxf.jaxrs.client.ClientWebApplicationException;
 import org.apache.cxf.jaxrs.client.JAXRSClientFactory;
@@ -55,6 +58,9 @@ import org.apache.cxf.jaxrs.client.WebCl
 import org.apache.cxf.jaxrs.ext.xml.XMLSource;
 import org.apache.cxf.jaxrs.provider.JAXBElementProvider;
 import org.apache.cxf.jaxrs.provider.XSLTJaxbProvider;
+import org.apache.cxf.message.Message;
+import org.apache.cxf.phase.AbstractPhaseInterceptor;
+import org.apache.cxf.phase.Phase;
 import org.apache.cxf.systest.jaxrs.BookStore.BookInfo;
 import org.apache.cxf.systest.jaxrs.BookStore.BookInfoInterface;
 import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
@@ -846,6 +852,15 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testBookExistsMalformedMt() throws Exception {
+        WebClient wc = 
+            WebClient.create("http://localhost:" + PORT + "/bookstore/books/check/malformedmt/123");
+        wc.accept(MediaType.TEXT_PLAIN);
+        WebClient.getConfig(wc).getInInterceptors().add(new ReplaceContentTypeInterceptor());
+        assertTrue(wc.get(Boolean.class));
+    }
+    
+    @Test
     public void testBookExists2() throws Exception {
         BookStore proxy = JAXRSClientFactory.create("http://localhost:" + PORT, BookStore.class);
         assertTrue(proxy.checkBook2(123L));
@@ -1817,5 +1832,16 @@ public class JAXRSClientServerBookTest e
         return bos.getOut().toString();        
     }
 
-    
+    public static class ReplaceContentTypeInterceptor extends AbstractPhaseInterceptor<Message>
{
+        public ReplaceContentTypeInterceptor() {
+            super(Phase.READ);
+        }
+
+        public void handleMessage(Message message) throws Fault {
+            Map<String, List<String>> headers = 
+                CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
+            headers.put(Message.CONTENT_TYPE, Collections.singletonList("text/plain"));
+        }
+    }
+
 }



Mime
View raw message