You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wink.apache.org by ro...@apache.org on 2010/08/12 20:46:18 UTC

svn commit: r984910 - in /incubator/wink/trunk/wink-client: pom.xml src/main/java/org/apache/wink/client/RestClient.java src/test/java/org/apache/wink/client/ClientTest.java

Author: rott
Date: Thu Aug 12 18:46:17 2010
New Revision: 984910

URL: http://svn.apache.org/viewvc?rev=984910&view=rev
Log:
WINK-302: add API call to encode URLs for client when desired

Modified:
    incubator/wink/trunk/wink-client/pom.xml
    incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/RestClient.java
    incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/ClientTest.java

Modified: incubator/wink/trunk/wink-client/pom.xml
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/pom.xml?rev=984910&r1=984909&r2=984910&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client/pom.xml (original)
+++ incubator/wink/trunk/wink-client/pom.xml Thu Aug 12 18:46:17 2010
@@ -33,6 +33,11 @@
     <dependencies>
         <dependency>
             <groupId>org.apache.wink</groupId>
+            <artifactId>wink-component-test-support</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.wink</groupId>
             <artifactId>wink-common</artifactId>
         </dependency>
         <dependency>
@@ -64,11 +69,6 @@
             <artifactId>activation</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.wink</groupId>
-            <artifactId>wink-component-test-support</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
             <groupId>commons-codec</groupId>
             <artifactId>commons-codec</artifactId>
             <version>1.3</version>

Modified: incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/RestClient.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/RestClient.java?rev=984910&r1=984909&r2=984910&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/RestClient.java (original)
+++ incubator/wink/trunk/wink-client/src/main/java/org/apache/wink/client/RestClient.java Thu Aug 12 18:46:17 2010
@@ -20,7 +20,10 @@
 
 package org.apache.wink.client;
 
+import java.net.MalformedURLException;
 import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
 import java.util.Set;
 
 import javax.ws.rs.core.Application;
@@ -108,6 +111,23 @@ public class RestClient {
     public Resource resource(String uri) {
         return resource(URI.create(uri));
     }
+    
+    /**
+     * Create a new {@link Resource} instance
+     * 
+     * @param uri uri of the resource to create
+     * @param httpEncode boolean to declare whether the passed uri needs to be encoded (true) or not (false)
+     * @return a new {@link Resource} instance attached to the specified uri
+     */
+    public Resource resource(String uri, boolean httpEncode) throws MalformedURLException, URISyntaxException {
+        if (httpEncode) {
+            URL url = new URL(uri);
+            // URI.toURL() will escape characters if we use one of the multi-param constructors
+            URI constructedURI = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(),url.getPath(), url.getQuery(), null);
+            return resource(constructedURI);
+        }
+        return resource(URI.create(uri));
+    }
 
     private void initProvidersRegistry() {
         // setup OFFactoryRegistry to support default and scope

Modified: incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/ClientTest.java
URL: http://svn.apache.org/viewvc/incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/ClientTest.java?rev=984910&r1=984909&r2=984910&view=diff
==============================================================================
--- incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/ClientTest.java (original)
+++ incubator/wink/trunk/wink-client/src/test/java/org/apache/wink/client/ClientTest.java Thu Aug 12 18:46:17 2010
@@ -27,8 +27,10 @@ import java.lang.annotation.Annotation;
 import java.lang.reflect.ParameterizedType;
 import java.lang.reflect.Type;
 import java.text.MessageFormat;
+import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.logging.LogRecord;
 
 import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Application;
@@ -41,6 +43,7 @@ import javax.ws.rs.ext.Provider;
 
 import org.apache.wink.client.MockHttpServer.MockHttpServerResponse;
 import org.apache.wink.common.utils.ProviderUtils;
+import org.apache.wink.logging.WinkLogHandler;
 
 public class ClientTest extends BaseTest {
 
@@ -129,6 +132,40 @@ public class ClientTest extends BaseTest
         }));
     }
 
+    public void testRestClientURIEncoded() throws Exception {
+        WinkLogHandler.turnLoggingCaptureOn(WinkLogHandler.LEVEL.TRACE);
+        RestClient client = getRestClient();
+        // I just want to see that the URI got encoded, I don't care if the actual query succeeds; so I'm going to check the logs.
+        Resource resource = client.resource(serviceURL + "/some space", true);
+        try {
+            ClientResponse response = resource.get();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        WinkLogHandler.turnLoggingCaptureOff();
+        ArrayList<LogRecord> logRecords = WinkLogHandler.getRecords();
+        boolean found = false;
+        for(int i = 0; i < logRecords.size(); i++) {
+            if (logRecords.get(i).getMessage().contains("client issued a request")) {
+                if (logRecords.get(i).getMessage().contains(" http://localhost:34568/some/service/some%20space ")) {
+                    found = true;
+                }
+            }
+        }
+        assertTrue("A log record should have contained the encoded URI", found);
+        WinkLogHandler.clearRecords();
+    }
+    
+    public void testRestClientURINotEncoded() throws Exception {
+        RestClient client = getRestClient();
+        // I just want to see that the URI got rejected due to the whitespace
+        try {
+            Resource resource = client.resource(serviceURL + "/some space", false);
+            fail("should have got an IllegalArgumentException due to a space in the URI.");
+        } catch (IllegalArgumentException e) {
+        }
+    }
+    
     public void testResourceGet() {
         MockHttpServerResponse response1 = new MockHttpServerResponse();
         response1.setMockResponseCode(200);