cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1004016 - in /cxf/trunk: rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/ rt/frontend/jaxrs/src/test/java/or...
Date Sun, 03 Oct 2010 18:01:57 GMT
Author: sergeyb
Date: Sun Oct  3 18:01:56 2010
New Revision: 1004016

URL: http://svn.apache.org/viewvc?rev=1004016&view=rev
Log:
Fixing last ignored UriBuilder tests

Modified:
    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/utils/HttpUtils.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.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/UriBuilderImpl.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java?rev=1004016&r1=1004015&r2=1004016&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
Sun Oct  3 18:01:56 2010
@@ -117,6 +117,13 @@ public class UriBuilderImpl extends UriB
                 && thePath.length() != 0 && !thePath.startsWith("/")) {
                 thePath = "/" + thePath;
             }
+            if (theQuery != null && HttpUtils.isPartiallyEncoded(theQuery)) {
+                try {
+                    return buildURIFromEncoded(thePath, theQuery, theFragment);
+                } catch (Exception ex) {
+                    // lets try the option below
+                }
+            }
             URI uri = new URI(scheme, userInfo, host, port, 
                            thePath, theQuery, theFragment);
             if (thePath.contains("%2F")) {
@@ -579,7 +586,13 @@ public class UriBuilderImpl extends UriB
 
     @Override
     public UriBuilder replaceQuery(String queryValue) throws IllegalArgumentException {
-        query = JAXRSUtils.getStructuredParams(queryValue, "&", true, false);
+        if (queryValue != null) {
+            // workaround to do with a conflicting and confusing requirement where spaces

+            // passed as part of replaceQuery are encoded as %20 while those passed as part

+            // of quertyParam are encoded as '+'
+            queryValue = queryValue.replace(" ", "%20");
+        }
+        query = JAXRSUtils.getStructuredParams(queryValue, "&", false, false);
         return this;
     }
 
@@ -650,6 +663,8 @@ public class UriBuilderImpl extends UriB
                 String val = sit.next();
                 if (fromEncoded) {
                     val = HttpUtils.encodePartiallyEncoded(val, isQuery);
+                } else if (isQuery && !val.startsWith("{") && !val.endsWith("}"))
{ 
+                    val = HttpUtils.encodePartiallyEncoded(val, isQuery);
                 }
                 b.append(entry.getKey());
                 if (val.length() != 0) {

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=1004016&r1=1004015&r2=1004016&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 Sun
Oct  3 18:01:56 2010
@@ -61,8 +61,10 @@ public final class HttpUtils {
     private static final String LOCAL_HOST = "localhost";
     private static final Pattern ENCODE_PATTERN = Pattern.compile("%[0-9a-fA-F][0-9a-fA-F]");
     private static final String CHARSET_PARAMETER = "charset";
+    
     // there are more of such characters, ex, '*' but '*' is not affected by UrlEncode
     private static final String PATH_RESERVED_CHARACTERS = "=@";
+    private static final String QUERY_RESERVED_CHARACTERS = "?/+";
     
     private HttpUtils() {
     }
@@ -75,25 +77,14 @@ public final class HttpUtils {
         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) {
+    private static String componentEncode(String reservedChars, String value) {
         
         StringBuilder buffer = new StringBuilder();
         StringBuilder bufferToEncode = new StringBuilder();
         
         for (int i = 0; i < value.length(); i++) {
             char currentChar = value.charAt(i);
-            if (PATH_RESERVED_CHARACTERS.indexOf(currentChar) != -1) {
+            if (reservedChars.indexOf(currentChar) != -1) {
                 if (bufferToEncode.length() > 0) {
                     buffer.append(urlEncode(bufferToEncode.toString()));
                     bufferToEncode.setLength(0);
@@ -108,7 +99,28 @@ public final class HttpUtils {
             buffer.append(urlEncode(bufferToEncode.toString()));
         }
         
-        String result = buffer.toString();
+        return buffer.toString();
+    }
+    
+    public static String queryEncode(String value) {
+        
+        return componentEncode(QUERY_RESERVED_CHARACTERS, 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 = componentEncode(PATH_RESERVED_CHARACTERS, value);
         // URLEncoder will encode '+' to %2B but will turn ' ' into '+'
         // We need to retain '+' and encode ' ' as %20
         if (result.indexOf('+') != -1) {
@@ -121,6 +133,9 @@ public final class HttpUtils {
         return result;
     }
     
+    public static boolean isPartiallyEncoded(String value) {
+        return ENCODE_PATTERN.matcher(value).find();
+    }
        
     /**
      * Encodes partially encoded string. Encode all values but those matching pattern 
@@ -138,12 +153,12 @@ public final class HttpUtils {
         int i = 0;
         while (m.find()) {
             String before = encoded.substring(i, m.start());
-            sb.append(query ? HttpUtils.urlEncode(before) : HttpUtils.pathEncode(before));
+            sb.append(query ? HttpUtils.queryEncode(before) : HttpUtils.pathEncode(before));
             sb.append(m.group());
             i = m.end();            
         }
         String tail = encoded.substring(i, encoded.length());
-        sb.append(query ? HttpUtils.urlEncode(tail) : HttpUtils.pathEncode(tail));
+        sb.append(query ? HttpUtils.queryEncode(tail) : HttpUtils.pathEncode(tail));
         return sb.toString();
     }
     

Modified: cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java?rev=1004016&r1=1004015&r2=1004016&view=diff
==============================================================================
--- cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
(original)
+++ cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/client/WebClientTest.java
Sun Oct  3 18:01:56 2010
@@ -34,7 +34,7 @@ public class WebClientTest extends Asser
     public void testEncoding() {
         URI u = WebClient.create("http://foo").path("bar+ %2B").matrix("a", "value+ ")
             .query("b", "bv+ ").getCurrentURI();
-        assertEquals("http://foo/bar+%20%2B;a=value+%20?b=bv%2B+", u.toString());
+        assertEquals("http://foo/bar+%20%2B;a=value+%20?b=bv++", u.toString());
     }
     
     @Test

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=1004016&r1=1004015&r2=1004016&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
Sun Oct  3 18:01:56 2010
@@ -34,7 +34,6 @@ import org.apache.cxf.jaxrs.resources.Ur
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 
 import org.junit.Assert;
-import org.junit.Ignore;
 import org.junit.Test;
 
 public class UriBuilderImplTest extends Assert {
@@ -168,7 +167,7 @@ public class UriBuilderImplTest extends 
             .matrixParam("m", "m1 ", "m2+%20")
             .queryParam("q", "q1 ", "q2+q3%20").clone().buildFromEncoded("a+ ", "b%2B%20
");   
         assertEquals("URI is not built correctly", 
-                     "http://bar/a+%20/b%2B%20%20;m=m1%20;m=m2+%20?q=q1+&q=q2%2Bq3%20",

+                     "http://bar/a+%20/b%2B%20%20;m=m1%20;m=m2+%20?q=q1+&q=q2+q3%20",

                      newUri.toString());
     }
     
@@ -177,7 +176,7 @@ public class UriBuilderImplTest extends 
         URI uri = new URI("http://bar/foo+%20%2B?q=a+b%20%2B");
         URI newUri = new UriBuilderImpl(uri).buildFromEncoded();   
         assertEquals("URI is not built correctly", 
-                     "http://bar/foo+%20%2B?q=a%2Bb%20%2B", newUri.toString());
+                     "http://bar/foo+%20%2B?q=a+b%20%2B", newUri.toString());
     }
     
     @Test
@@ -216,7 +215,7 @@ public class UriBuilderImplTest extends 
     public void testEncodedAddedQuery() throws Exception {
         URI uri = new URI("http://bar");
         URI newUri = new UriBuilderImpl(uri).queryParam("q", "a+b%20%2B").buildFromEncoded();
  
-        assertEquals("URI is not built correctly", "http://bar?q=a%2Bb%20%2B", newUri.toString());
+        assertEquals("URI is not built correctly", "http://bar?q=a+b%20%2B", newUri.toString());
     }
     
     @Test
@@ -1091,7 +1090,6 @@ public class UriBuilderImplTest extends 
     }
     
     @Test
-    @Ignore("QueryParamTest5")
     public void testEncodingQueryParamFromBuild() throws Exception {
         String expectedValue =
                 "http://localhost:8080?name=x%3D&name=y?&name=x+y&name=%26";
@@ -1101,7 +1099,6 @@ public class UriBuilderImplTest extends 
     }
     
     @Test
-    @Ignore("ReplaceQueryParamTest3")
     public void testReplaceParamAndEncodeQueryParamFromBuild() throws Exception {
         String expectedValue =
                 "http://localhost:8080?name=x&name=y&name=y+x&name=x%25y&name=%20";
@@ -1112,7 +1109,6 @@ public class UriBuilderImplTest extends 
     }
 
     @Test
-    // ReplaceQuery3
     public void testReplaceStringAndEncodeQueryParamFromBuild() {
         String expected = "http://localhost:8080?name1=x&name2=%20&name3=x+y&name4=23&name5=x%20y";
 

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=1004016&r1=1004015&r2=1004016&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
Sun Oct  3 18:01:56 2010
@@ -57,7 +57,7 @@ public class JAXRSClientServerBookTest e
     @BeforeClass
     public static void startServers() throws Exception {
         assertTrue("server did not launch correctly",
-                   launchServer(BookServer.class, false));
+                   launchServer(BookServer.class));
     }
     
     
@@ -496,6 +496,16 @@ public class JAXRSClientServerBookTest e
     }
     
     @Test
+    public void testSearchBook123WithWebClient() throws Exception {
+        String address = "http://localhost:" + PORT + "/bookstore/books/search";
+                          
+        WebClient client = WebClient.create(address);
+        Book b = client.query("_s", "name==CXF*;id=ge=123;id=lt=124").get(Book.class);
+        assertEquals(b.getId(), 123L);
+        
+    }
+    
+    @Test
     public void testGetBook123() throws Exception {
         getAndCompareAsStrings("http://localhost:" + PORT + "/bookstore/books/123",
                                "resources/expected_get_book123.txt",



Mime
View raw message