cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r752435 - in /cxf/trunk: common/common/src/main/java/org/apache/cxf/common/util/ common/common/src/test/java/org/apache/cxf/common/util/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/ja...
Date Wed, 11 Mar 2009 13:11:25 GMT
Author: sergeyb
Date: Wed Mar 11 13:11:24 2009
New Revision: 752435

URL: http://svn.apache.org/viewvc?rev=752435&view=rev
Log:
Fix for CXF-2098, update to ServletController for last segment matrix parameters handled correctly

Added:
    cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java   (with
props)
    cxf/trunk/common/common/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java   (with
props)
Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PathSegmentImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
    cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java
    cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java

Added: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java?rev=752435&view=auto
==============================================================================
--- cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java (added)
+++ cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java Wed Mar
11 13:11:24 2009
@@ -0,0 +1,75 @@
+/**
+ * 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.cxf.common.util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URLDecoder;
+import java.util.logging.Logger;
+
+import org.apache.cxf.common.logging.LogUtils;
+
+/**
+ * Utility class for decoding and encoding URLs
+ *
+ */
+public final class UrlUtils {
+    
+    private static final Logger LOG = LogUtils.getL7dLogger(UrlUtils.class);
+    
+    private static final String[] RESERVED_CHARS = {"+"};
+    private static final String[] ENCODED_CHARS = {"%2b"};
+    
+    private UrlUtils() {
+        
+    }
+
+    /**
+     * Decodes using URLDecoder - use when queries or form post values are decoded
+     * @param value value to decode
+     * @return
+     */
+    public static String urlDecode(String value) {
+        try {
+            value = URLDecoder.decode(value, "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            LOG.warning("UTF-8 encoding can not be used to decode " + value);          
+        }
+        return value;
+    }
+    
+    /**
+     * URL path segments may contain '+' symbols which should not be decoded into ' '
+     * This method replaces '+' with %2B and delegates to URLDecoder
+     * @param value value to decode
+     * @return
+     */
+    public static String pathDecode(String value) {
+        // TODO: we actually need to do a proper URI analysis here according to
+        // http://tools.ietf.org/html/rfc3986
+        for (int i = 0; i < RESERVED_CHARS.length; i++) {
+            if (value.indexOf(RESERVED_CHARS[i]) != -1) {
+                value = value.replace(RESERVED_CHARS[i], ENCODED_CHARS[i]);
+            }
+        }
+        
+        return urlDecode(value);
+    }
+    
+}

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/common/common/src/main/java/org/apache/cxf/common/util/UrlUtils.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: cxf/trunk/common/common/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java?rev=752435&view=auto
==============================================================================
--- cxf/trunk/common/common/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java (added)
+++ cxf/trunk/common/common/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java Wed
Mar 11 13:11:24 2009
@@ -0,0 +1,42 @@
+/**
+ * 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.cxf.common.util;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+public class UrlUtilsTest extends Assert {
+    
+    @Test
+    public void testUrlDecode() {
+        assertEquals("+ ", UrlUtils.urlDecode("%2B+"));
+    }
+    
+    @Test
+    public void testUrlDecodeReserved() {
+        assertEquals("!$&'()*,;=", UrlUtils.urlDecode("!$&'()*,;="));
+    }
+    
+    @Test
+    public void testPathDecode() {
+        assertEquals("+++", UrlUtils.pathDecode("+%2B+"));
+    }
+}

Propchange: cxf/trunk/common/common/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/common/common/src/test/java/org/apache/cxf/common/util/UrlUtilsTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/JAXRSInvoker.java Wed Mar
11 13:11:24 2009
@@ -116,9 +116,10 @@
         }
 
         Object result = null;
-        ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();
+        ClassLoader contextLoader = null;
         try {
             if (setServiceLoaderAsContextLoader(exchange.getInMessage())) {
+                contextLoader = Thread.currentThread().getContextClassLoader();
                 Thread.currentThread().setContextClassLoader(resourceObject.getClass().getClassLoader());
             }
             result = invoke(exchange, resourceObject, methodToInvoke, params);
@@ -137,7 +138,9 @@
             }
             return new MessageContentsList(excResponse);
         } finally {
-            Thread.currentThread().setContextClassLoader(contextLoader);
+            if (contextLoader != null) {
+                Thread.currentThread().setContextClassLoader(contextLoader);
+            }
         }
 
         if (ori.isSubResourceLocator()) {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/client/ClientProxyImpl.java
Wed Mar 11 13:11:24 2009
@@ -53,8 +53,8 @@
 import org.apache.cxf.jaxrs.model.OperationResourceInfo;
 import org.apache.cxf.jaxrs.provider.ProviderFactory;
 import org.apache.cxf.jaxrs.utils.AnnotationUtils;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
-import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.jaxrs.utils.ParameterType;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.message.MessageContentsList;
@@ -115,7 +115,7 @@
         handleMatrixes(types, params, builder);
         handleQueries(types, params, builder);
         
-        URI uri = builder.build(pathParams.toArray()).normalize();
+        URI uri = builder.buildFromEncoded(pathParams.toArray()).normalize();
         
         MultivaluedMap<String, String> headers = getHeaders();
         MultivaluedMap<String, String> paramHeaders = new MetadataMap<String, String>();
@@ -251,7 +251,7 @@
         List<Parameter> indexList =  getParameters(map, key);
         List<Object> list = new ArrayList<Object>(indexList.size());
         for (Parameter p : indexList) {
-            list.add(JAXRSUtils.encode(p.isEncoded(), params[p.getIndex()].toString()));
+            list.add(encode(p, params[p.getIndex()].toString()));
         }
         return list;
     }
@@ -268,8 +268,7 @@
         List<Parameter> qs = getParameters(map, ParameterType.QUERY);
         for (Parameter p : qs) {
             if (params[p.getIndex()] != null) {
-                ub.queryParam(p.getValue(), 
-                               JAXRSUtils.encode(p.isEncoded(), params[p.getIndex()].toString()));
+                ub.queryParam(p.getValue(), encode(p, params[p.getIndex()].toString()));
             }
         }
     }
@@ -279,8 +278,7 @@
         List<Parameter> mx = getParameters(map, ParameterType.MATRIX);
         for (Parameter p : mx) {
             if (params[p.getIndex()] != null) {
-                ub.matrixParam(p.getValue(), 
-                                JAXRSUtils.encode(p.isEncoded(), params[p.getIndex()].toString()));
+                ub.matrixParam(p.getValue(), encode(p, params[p.getIndex()].toString()));
             }
         }
     }
@@ -365,6 +363,17 @@
         
     }
     
+    private static String encode(Parameter p, String value) {
+        if (p.isEncoded()) {
+            return value;
+        }
+        if (p.getType() == ParameterType.PATH || p.getType() == ParameterType.MATRIX) {
+            return HttpUtils.pathEncode(value);
+        } else {
+            return HttpUtils.urlEncode(value); 
+        }
+    }
+    
     private Object doChainedInvocation(URI uri, MultivaluedMap<String, String> headers,

                           OperationResourceInfo ori, Object[] params, int bodyIndex, 
                           MultivaluedMap<ParameterType, Parameter> types) throws Throwable
{
@@ -489,5 +498,6 @@
         }
         
     }
+
     
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PathSegmentImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PathSegmentImpl.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PathSegmentImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/PathSegmentImpl.java
Wed Mar 11 13:11:24 2009
@@ -23,6 +23,7 @@
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.ext.Provider;
 
+import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 
 @Provider
@@ -35,7 +36,7 @@
     }
     
     public PathSegmentImpl(String path, boolean decode) {
-        this.path = decode ? JAXRSUtils.uriDecode(path) : path;
+        this.path = decode ? HttpUtils.pathDecode(path) : path;
     }
     
     public MultivaluedMap<String, String> getMatrixParameters() {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
Wed Mar 11 13:11:24 2009
@@ -40,6 +40,7 @@
 import javax.ws.rs.core.UriBuilderException;
 
 import org.apache.cxf.jaxrs.model.URITemplate;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 
 public class UriBuilderImpl extends UriBuilder {
@@ -73,15 +74,19 @@
 
     @Override
     public URI build(Object... values) throws IllegalArgumentException, UriBuilderException
{
+        return doBuild(false, values);
+    }
+
+    private URI doBuild(boolean fromEncoded, Object... values) {
         try {
-            String path = buildPath();
+            String path = buildPath(fromEncoded);
             path = substituteVarargs(path, values);
             return new URI(scheme, userInfo, host, port, path, buildQuery(), fragment);
         } catch (URISyntaxException ex) {
             throw new UriBuilderException("URI can not be built", ex);
         }
     }
-
+    
     private String substituteVarargs(String path, Object... values) {
         Map<String, String> varValueMap = new HashMap<String, String>();
         URITemplate templ = new URITemplate(path);
@@ -108,21 +113,26 @@
         for (int i = 0; i < values.length; i++) {
             values[i] = decodePartiallyEncoded(values[i].toString());
         }
-        return build(values);
+        return doBuild(true, values);
     }
 
     @Override
     public URI buildFromMap(Map<String, ? extends Object> map) throws IllegalArgumentException,
         UriBuilderException {
+        return doBuildFromMap(map, false);
+    }
+
+    private URI doBuildFromMap(Map<String, ? extends Object> map, boolean fromEncoded)

+        throws IllegalArgumentException, UriBuilderException {
         try {
-            String path = buildPath();
+            String path = buildPath(fromEncoded);
             path = substituteMapped(path, map);
             return new URI(scheme, userInfo, host, port, path, buildQuery(), fragment);
         } catch (URISyntaxException ex) {
             throw new UriBuilderException("URI can not be built", ex);
         }
     }
-
+    
     private String substituteMapped(String path, Map<String, ? extends Object> varValueMap)
{
         URITemplate templ = new URITemplate(path);
         Set<String> uniqueVars = new HashSet<String>(templ.getVariables());
@@ -142,7 +152,7 @@
         for (Map.Entry<String, ? extends Object> entry : map.entrySet()) {
             decodedMap.put(entry.getKey(), decodePartiallyEncoded(entry.getValue().toString()));
         }
-        return buildFromMap(decodedMap);
+        return doBuildFromMap(decodedMap, true);
     }
 
     // CHECKSTYLE:OFF
@@ -297,7 +307,7 @@
         }
     }
     
-    private String buildPath() {
+    private String buildPath(boolean fromEncoded) {
         StringBuilder sb = new StringBuilder();
         Iterator<PathSegment> iter = paths.iterator();
         while (iter.hasNext()) {
@@ -309,16 +319,16 @@
                 }
                 sb.append(p);
                 if (iter.hasNext()) {
-                    buildMatrix(sb, ps.getMatrixParameters());
+                    buildMatrix(sb, ps.getMatrixParameters(), fromEncoded);
                 }
             }
         }
-        buildMatrix(sb, matrix);
+        buildMatrix(sb, matrix, fromEncoded);
         return sb.toString();
     }
 
     private String buildQuery() {
-        return buildParams(query, '&');
+        return buildParams(query, '&', false);
     }
 
     @Override
@@ -418,7 +428,7 @@
         StringBuffer sb = new StringBuffer();
         while (m.find()) {
             String found = m.group();
-            m.appendReplacement(sb, JAXRSUtils.uriDecode(found));
+            m.appendReplacement(sb, HttpUtils.pathDecode(found));
         }
         m.appendTail(sb);
         return sb.toString();
@@ -449,14 +459,19 @@
      * 
      * @param map query or matrix multivalued map
      * @param separator params separator, '&' for query ';' for matrix
+     * @param fromEncoded if true then values will be decoded 
      * @return stringified params.
      */
-    private static String buildParams(MultivaluedMap<String, String> map, char separator)
{
+    private String buildParams(MultivaluedMap<String, String> map, char separator,
+                                      boolean fromEncoded) {
         StringBuilder b = new StringBuilder();
         for (Iterator<Map.Entry<String, List<String>>> it = map.entrySet().iterator();
it.hasNext();) {
             Map.Entry<String, List<String>> entry = it.next();
             for (Iterator<String> sit = entry.getValue().iterator(); sit.hasNext();)
{
                 String val = sit.next();
+                if (fromEncoded) {
+                    val = decodePartiallyEncoded(val);
+                }
                 b.append(entry.getKey()).append('=').append(val);
                 if (sit.hasNext() || it.hasNext()) {
                     b.append(separator);
@@ -472,17 +487,18 @@
      * @param sb buffer to add the matrix part to, will get ';' added if map is not empty

      * @param map matrix multivalued map
      */    
-    private static void buildMatrix(StringBuilder sb, MultivaluedMap<String, String>
map) {
+    private void buildMatrix(StringBuilder sb, MultivaluedMap<String, String> map,
+                                    boolean fromEncoded) {
         if (!map.isEmpty()) {
             sb.append(';');
-            sb.append(buildParams(map, ';'));
+            sb.append(buildParams(map, ';', fromEncoded));
         }
     }
     
     private PathSegment replacePathSegment(PathSegment ps) {
         StringBuilder sb = new StringBuilder();
         sb.append(ps.getPath());
-        buildMatrix(sb, matrix);
+        buildMatrix(sb, matrix, false);
         return new PathSegmentImpl(sb.toString());
     }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriInfoImpl.java Wed
Mar 11 13:11:24 2009
@@ -122,7 +122,7 @@
             if (entry.getKey().equals(URITemplate.FINAL_MATCH_GROUP)) {
                 continue;
             }
-            values.add(entry.getKey(), decode ? JAXRSUtils.uriDecode(entry.getValue().get(0))
: entry
+            values.add(entry.getKey(), decode ? HttpUtils.pathDecode(entry.getValue().get(0))
: entry
                 .getValue().get(0));
         }
         return values;
@@ -159,7 +159,7 @@
                 for (Path p : paths) {
                     if (p != null) {
                         String v = p.value();
-                        sum += "/" + (decode ? JAXRSUtils.uriDecode(v) : v);
+                        sum += "/" + (decode ? HttpUtils.pathDecode(v) : v);
                     }
                 }
                 UriBuilder ub = UriBuilder.fromPath(sum);
@@ -174,7 +174,7 @@
 
     private String doGetPath(boolean decode, boolean addSlash) {
         String path = HttpUtils.getPathToMatch(message, addSlash);
-        return decode ? JAXRSUtils.uriDecode(path) : path;
+        return decode ? HttpUtils.pathDecode(path) : path;
     }
 
     private String getAbsolutePathAsString() {

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/provider/FormEncodingProvider.java
Wed Mar 11 13:11:24 2009
@@ -44,7 +44,7 @@
 import org.apache.cxf.jaxrs.impl.MetadataMap;
 import org.apache.cxf.jaxrs.utils.AnnotationUtils;
 import org.apache.cxf.jaxrs.utils.FormUtils;
-import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
 import org.apache.cxf.jaxrs.utils.multipart.AttachmentUtils;
 
 @Produces("application/x-www-form-urlencoded")
@@ -150,7 +150,8 @@
             for (String value : entry.getValue()) {
                 os.write(entry.getKey().getBytes("UTF-8"));
                 os.write('=');
-                os.write(JAXRSUtils.encode(encoded, value).getBytes("UTF-8"));
+                String data = encoded ? value : HttpUtils.urlEncode(value); 
+                os.write(data.getBytes("UTF-8"));
                 if (it.hasNext()) {
                     os.write('&');
                 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/FormUtils.java Wed
Mar 11 13:11:24 2009
@@ -62,7 +62,7 @@
                 if (keyValue.length == 2) {
                     if (decode) {
                         params.add(keyValue[0], 
-                            JAXRSUtils.uriDecode(keyValue[1]));
+                            HttpUtils.urlDecode(keyValue[1]));
                     } else {
                         params.add(keyValue[0], keyValue[1]);
                     }
@@ -86,7 +86,7 @@
             String name = cd.getParameter("name").replace("\"", "").replace("'", "");
             try {
                 String value = IOUtils.toString(a.getDataHandler().getInputStream());
-                params.add(name, decode ? JAXRSUtils.uriDecode(value) : value);
+                params.add(name, decode ? HttpUtils.urlDecode(value) : value);
             } catch (IOException ex) {
                 throw new WebApplicationException(415);
             }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/HttpUtils.java Wed
Mar 11 13:11:24 2009
@@ -19,9 +19,11 @@
 
 package org.apache.cxf.jaxrs.utils;
 
+import java.io.UnsupportedEncodingException;
 import java.net.MalformedURLException;
 import java.net.URI;
 import java.net.URL;
+import java.net.URLEncoder;
 import java.text.SimpleDateFormat;
 import java.util.List;
 import java.util.Locale;
@@ -32,6 +34,7 @@
 import javax.ws.rs.core.PathSegment;
 import javax.ws.rs.core.Response;
 
+import org.apache.cxf.common.util.UrlUtils;
 import org.apache.cxf.jaxrs.impl.PathSegmentImpl;
 import org.apache.cxf.message.Message;
 import org.apache.cxf.transport.Destination;
@@ -39,6 +42,7 @@
 import org.apache.cxf.transport.servlet.ServletDestination;
 
 public final class HttpUtils {
+    
     private static final String LOCAL_IP_ADDRESS = "127.0.0.1";
     private static final String LOCAL_HOST = "localhost";
     
@@ -46,6 +50,35 @@
     private HttpUtils() {
     }
     
+    public static String urlDecode(String value) {
+        return UrlUtils.urlDecode(value);
+    }
+    
+    public static String pathDecode(String value) {
+        return UrlUtils.pathDecode(value);
+    }
+    
+    public static String urlEncode(String value) {
+            
+        try {
+            value = URLEncoder.encode(value, "UTF-8");
+        } catch (UnsupportedEncodingException ex) {
+            // unlikely to happen
+        }
+        
+        return value;
+    }
+    
+    public static String pathEncode(String value) {
+        
+        String result = urlEncode(value);
+        // URLEncoder will encode '+' to %2B but will turn ' ' into '+'
+        if (result.indexOf('+') != -1) {
+            result = result.replace("+", "%20");
+        }
+        return result;
+    }
+    
     public static SimpleDateFormat getHttpDateFormat() {
         SimpleDateFormat dateFormat = 
             new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/InjectionUtils.java
Wed Mar 11 13:11:24 2009
@@ -310,10 +310,9 @@
         values = checkPathSegment(values, realType, pathParam);
         List theValues = new ArrayList();
         for (String r : values) {
-            if (decoded) {
-                r = JAXRSUtils.uriDecode(r);
-            }
-            Object o = InjectionUtils.handleParameter(r, realType, pathParam, basePath);
+            String value = decodeValue(r, decoded, pathParam);
+            
+            Object o = InjectionUtils.handleParameter(value, realType, pathParam, basePath);
             if (o != null) {
                 theValues.add(o);
             }
@@ -334,10 +333,8 @@
         
         Set theValues = sorted ? new TreeSet() : new HashSet();
         for (String r : values) {
-            if (decoded) {
-                r = JAXRSUtils.uriDecode(r);
-            }
-            Object o = InjectionUtils.handleParameter(r, realType, pathParam, basePath);
+            String value = decodeValue(r, decoded, pathParam);
+            Object o = InjectionUtils.handleParameter(value, realType, pathParam, basePath);
             if (o != null) {
                 theValues.add(o);
             }
@@ -403,9 +400,7 @@
                                 : paramValues.get(0);
             }
             if (result != null) {
-                if (decoded) {
-                    result = JAXRSUtils.uriDecode(result);
-                }
+                result = decodeValue(result, decoded, pathParam);
                 return InjectionUtils.handleParameter(result, paramType, pathParam, basePath);
             } else {
                 return null;
@@ -528,4 +523,15 @@
             || Boolean.class.isAssignableFrom(type)
             || String.class == type;
     }
+    
+    private static String decodeValue(String value, boolean decode, ParameterType param)
{
+        if (!decode) {
+            return value;
+        }
+        if (param == ParameterType.PATH || param == ParameterType.MATRIX) {
+            return HttpUtils.pathDecode(value);
+        } else {
+            return HttpUtils.urlDecode(value);
+        }
+    }
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java (original)
+++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java Wed
Mar 11 13:11:24 2009
@@ -21,14 +21,11 @@
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
-import java.net.URLDecoder;
-import java.net.URLEncoder;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
@@ -764,22 +761,21 @@
             List<String> parts = Arrays.asList(query.split(sep));
             for (String part : parts) {
                 String[] values = part.split("=");
-                queries.add(values[0], values.length == 1 ? "" 
-                    : decode ? uriDecode(values[1]) : values[1]);
+                String value = null;
+                if (values.length == 1) {
+                    value = "";
+                } else if (decode) {
+                    value = "&".equals(sep) ? HttpUtils.urlDecode(values[1]) 
+                                            : HttpUtils.pathDecode(values[1]); 
+                } else {
+                    value = values[1];
+                }
+                queries.add(values[0], value);
             }
         }
         return queries;
     }
 
-    public static String uriDecode(String query) {
-        try {
-            query = URLDecoder.decode(query, "UTF-8");
-        } catch (UnsupportedEncodingException e) {
-            //Swallow unsupported decoding exception          
-        }
-        return query;
-    }
-
     @SuppressWarnings("unchecked")
     private static <T> Object readFromMessageBody(Class<T> targetTypeClass,
                                                   Type parameterType,
@@ -975,15 +971,6 @@
         
     }
     
-    public static String encode(boolean encoded, String value) {
-        if (!encoded) {
-            try {
-                value = URLEncoder.encode(value, "UTF-8");
-            } catch (UnsupportedEncodingException ex) {
-                // unlikely to happen
-            }
-        }
-        return value;
-    }
+    
         
 }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
Wed Mar 11 13:11:24 2009
@@ -136,8 +136,9 @@
     @Test
     public void testBuildValuesPctEncoded() throws Exception {
         URI uri = new URI("http://zzz");
-        URI newUri = new UriBuilderImpl(uri).path("/{a}/{b}").buildFromEncoded("foo%25",
"bar%");
-        assertEquals("URI is not built correctly", new URI("http://zzz/foo%25/bar%25"), newUri);
+        URI newUri = new UriBuilderImpl(uri).path("/{a}/{b}/{c}")
+            .buildFromEncoded("foo%25", "bar%", "baz%20");
+        assertEquals("URI is not built correctly", new URI("http://zzz/foo%25/bar%25/baz%20"),
newUri);
     }
 
     @Test
@@ -462,6 +463,13 @@
         URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v2", "v3").build();
         assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1;p1=v2;p1=v3"),
newUri);
     }
+    
+    @Test
+    public void testPctEncodedMatrixParam() throws Exception {
+        URI uri = new URI("http://foo/bar");
+        URI newUri = new UriBuilderImpl(uri).matrixParam("p1", "v1%20").buildFromEncoded();
+        assertEquals("URI is not built correctly", new URI("http://foo/bar;p1=v1%20"), newUri);
+    }
 
     @Test(expected = IllegalArgumentException.class)
     public void testReplaceMatrixParamNameNull() throws Exception {

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/provider/FormEncodingProviderTest.java
Wed Mar 11 13:11:24 2009
@@ -31,7 +31,7 @@
 import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.jaxrs.impl.MetadataMap;
-import org.apache.cxf.jaxrs.utils.JAXRSUtils;
+import org.apache.cxf.jaxrs.utils.HttpUtils;
 
 import org.junit.Assert;
 import org.junit.Before;
@@ -121,7 +121,7 @@
         assertEquals(1,  mvMap.get("baz").size());
         assertEquals("Wrong entry for foo", "1 2", mvMap.getFirst("foo"));
         assertEquals("Wrong entry line for bar", 
-            JAXRSUtils.uriDecode("line1%0D%0Aline+2"), mvMap.get("bar").get(0));
+            HttpUtils.urlDecode("line1%0D%0Aline+2"), mvMap.get("bar").get(0));
         assertEquals("Wrong entry for baz", "4", mvMap.getFirst("baz"));
 
     }

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/utils/HttpUtilsTest.java
Wed Mar 11 13:11:24 2009
@@ -31,6 +31,31 @@
 public class HttpUtilsTest extends Assert {
 
     @Test
+    public void testUrlDecode() {
+        assertEquals("+ ", HttpUtils.urlDecode("%2B+"));
+    }
+    
+    @Test
+    public void testUrlEncode() {
+        assertEquals("%2B+", HttpUtils.urlEncode("+ "));
+    }
+     
+    @Test
+    public void testPathEncode() {
+        assertEquals("%2B%20", HttpUtils.pathEncode("+ "));
+    }
+    
+    @Test
+    public void testUrlDecodeReserved() {
+        assertEquals("!$&'()*,;=", HttpUtils.urlDecode("!$&'()*,;="));
+    }
+    
+    @Test
+    public void testPathDecode() {
+        assertEquals("+++", HttpUtils.pathDecode("+%2B+"));
+    }
+    
+    @Test
     public void testPathToMatch() {
         assertEquals("/", HttpUtils.getPathToMatch("/", "/", true));
         assertEquals("/", HttpUtils.getPathToMatch("/", "/bar", true));

Modified: cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
(original)
+++ cxf/trunk/rt/transports/http/src/main/java/org/apache/cxf/transport/servlet/ServletController.java
Wed Mar 11 13:11:24 2009
@@ -21,7 +21,6 @@
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.URL;
-import java.net.URLDecoder;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.LinkedList;
@@ -39,6 +38,7 @@
 import org.apache.cxf.Bus;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.StringUtils;
+import org.apache.cxf.common.util.UrlUtils;
 import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.service.model.EndpointInfo;
 import org.apache.cxf.service.model.OperationInfo;
@@ -277,15 +277,16 @@
         //fix for CXF-898
         if (!"/".equals(pathInfo) || reqPrefix.endsWith("/")) {
             // needs to be done given that pathInfo is decoded
-            // TODO :
-            // it's unlikely servlet path will contain encoded values so we're most likely
safe
-            // however we need to ensure if it happens then thsi code works propely too
-            try {
-                reqPrefix = URLDecoder.decode(reqPrefix, "UTF-8");
-            } catch (Exception ex) {
-                // unlikey to happen
+            // TODO : it's unlikely servlet path will contain encoded values so we're most

+            // likely safe however we need to ensure if it happens then this code works properly
too
+            reqPrefix = UrlUtils.pathDecode(reqPrefix);
+            // pathInfo drops matrix parameters attached to a last path segment
+            int offset = 0;
+            int index = reqPrefix.lastIndexOf(';');
+            if (index >= pathInfo.length()) {
+                offset = reqPrefix.length() - index;
             }
-            reqPrefix = reqPrefix.substring(0, reqPrefix.length() - pathInfo.length());
+            reqPrefix = reqPrefix.substring(0, reqPrefix.length() - pathInfo.length() - offset);
         }
         return reqPrefix;
     }

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java (original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/BookSubresource.java Wed
Mar 11 13:11:24 2009
@@ -24,6 +24,7 @@
 import javax.ws.rs.FormParam;
 import javax.ws.rs.GET;
 import javax.ws.rs.HeaderParam;
+import javax.ws.rs.MatrixParam;
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
@@ -45,7 +46,7 @@
     @Produces("application/xml")
     Book getTheBook2(@PathParam("n1") String name1,
                      @QueryParam("n2") String name2,
-                     @QueryParam("n3") String name3,
+                     @MatrixParam("n3") String name3,
                      @HeaderParam("N4") String name4,
                      @CookieParam("n5") String name5,
                      String name6) throws BookNotFoundFault;

Modified: cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java?rev=752435&r1=752434&r2=752435&view=diff
==============================================================================
--- cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
(original)
+++ cxf/trunk/systests/src/test/java/org/apache/cxf/systest/jaxrs/JAXRSClientServerSpringBookTest.java
Wed Mar 11 13:11:24 2009
@@ -94,6 +94,13 @@
     }
     
     @Test
+    public void testGetBookWithEncodedPathValue2() throws Exception {
+        String endpointAddress =
+            "http://localhost:9080/the/bookstore/id=12+3"; 
+        getBook(endpointAddress, "resources/expected_get_book123json.txt"); 
+    }
+    
+    @Test
     public void testGetDefaultBook() throws Exception {
         String endpointAddress =
             "http://localhost:9080/the/bookstore"; 



Mime
View raw message