Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8DEBBD70A for ; Wed, 25 Jul 2012 11:52:17 +0000 (UTC) Received: (qmail 92285 invoked by uid 500); 25 Jul 2012 11:52:17 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 92166 invoked by uid 500); 25 Jul 2012 11:52:17 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 92153 invoked by uid 99); 25 Jul 2012 11:52:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Jul 2012 11:52:16 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Jul 2012 11:52:13 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 92D1423888E4; Wed, 25 Jul 2012 11:51:28 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1365540 - in /cxf/branches/2.6.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 11:51:28 -0000 To: commits@cxf.apache.org From: sergeyb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120725115128.92D1423888E4@eris.apache.org> Author: sergeyb Date: Wed Jul 25 11:51:27 2012 New Revision: 1365540 URL: http://svn.apache.org/viewvc?rev=1365540&view=rev Log: 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.6.x-fixes/ (props changed) cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Propchange: cxf/branches/2.6.x-fixes/ ------------------------------------------------------------------------------ Merged /cxf/trunk:r1365536 Propchange: cxf/branches/2.6.x-fixes/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java?rev=1365540&r1=1365539&r2=1365540&view=diff ============================================================================== --- cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java (original) +++ cxf/branches/2.6.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/MediaTypeHeaderProvider.java Wed Jul 25 11:51:27 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 { +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 { + 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.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java?rev=1365540&r1=1365539&r2=1365540&view=diff ============================================================================== --- cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java (original) +++ cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookServer.java Wed Jul 25 11:51:27 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/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java?rev=1365540&r1=1365539&r2=1365540&view=diff ============================================================================== --- cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java (original) +++ cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/BookStore.java Wed Jul 25 11:51:27 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.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java?rev=1365540&r1=1365539&r2=1365540&view=diff ============================================================================== --- cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java (original) +++ cxf/branches/2.6.x-fixes/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerBookTest.java Wed Jul 25 11:51:27 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 { + public ReplaceContentTypeInterceptor() { + super(Phase.READ); + } + + public void handleMessage(Message message) throws Fault { + Map> headers = + CastUtils.cast((Map)message.get(Message.PROTOCOL_HEADERS)); + headers.put(Message.CONTENT_TYPE, Collections.singletonList("text/plain")); + } + } + }