Author: sergeyb
Date: Wed Jul 25 11:47:51 2012
New Revision: 1365536
URL: http://svn.apache.org/viewvc?rev=1365536&view=rev
Log:
[CXF-4443] Relaxing the control of malformed media types
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Modified:
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java?rev=1365536&r1=1365535&r2=1365536&view=diff
==============================================================================
---
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
(original)
+++
cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java
Wed Jul 25 11:47:51 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/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=1365536&r1=1365535&r2=1365536&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java
Wed Jul 25 11:47:51 2012
@@ -75,6 +75,8 @@ public class BookServer extends Abstract
new SingletonResourceProvider(new BookStore(),
true));
sf.setAddress("http://localhost:" + PORT + "/");
+
sf.getProperties(true).put("org.apache.cxf.jaxrs.mediaTypeCheck.strict", true);
+
server = sf.create();
BusFactory.setDefaultBus(null);
BusFactory.setThreadDefaultBus(null);
Modified:
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1365536&r1=1365535&r2=1365536&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java
Wed Jul 25 11:47:51 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/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1365536&r1=1365535&r2=1365536&view=diff
==============================================================================
---
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
(original)
+++
cxf/trunk/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java
Wed Jul 25 11:47:51 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;
@@ -56,6 +59,9 @@ import org.apache.cxf.jaxrs.ext.xml.XMLS
import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
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;
@@ -72,7 +78,7 @@ public class JAXRSClientServerBookTest e
public static void startServers() throws Exception {
AbstractResourceInfo.clearAllMaps();
assertTrue("server did not launch correctly",
- launchServer(BookServer.class));
+ launchServer(BookServer.class, true));
createStaticBus();
}
@@ -881,6 +887,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));
@@ -1852,5 +1867,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"));
+ }
+ }
+
}