cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r940950 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
Date Tue, 04 May 2010 16:57:53 GMT
Author: sergeyb
Date: Tue May  4 16:57:52 2010
New Revision: 940950

URL: http://svn.apache.org/viewvc?rev=940950&view=rev
Log:
CXF2784 : updating UriBuilderImpl to support queries with templates

Modified:
    cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
    cxf/trunk/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.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=940950&r1=940949&r2=940950&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
Tue May  4 16:57:52 2010
@@ -77,18 +77,28 @@ public class UriBuilderImpl extends UriB
     private URI doBuild(boolean fromEncoded, Object... values) {
         try {
             String thePath = buildPath(fromEncoded);
-            thePath = substituteVarargs(thePath, values);
-            return buildURI(fromEncoded, thePath);
+            URITemplate pathTempl = new URITemplate(thePath);
+            thePath = substituteVarargs(pathTempl, values, 0);
+            
+            String theQuery = buildQuery(fromEncoded);
+            if (theQuery != null) {
+                URITemplate queryTempl = new URITemplate(theQuery);
+                int lengthDiff = values.length - pathTempl.getVariables().size(); 
+                if (lengthDiff > 0) {
+                    theQuery = substituteVarargs(queryTempl, values, values.length - lengthDiff);
+                }
+            }
+            
+            return buildURI(fromEncoded, thePath, theQuery);
         } catch (URISyntaxException ex) {
             throw new UriBuilderException("URI can not be built", ex);
         }
     }
     
-    private URI buildURI(boolean fromEncoded, String thePath) throws URISyntaxException {
-        String theQuery = buildQuery(fromEncoded);
+    private URI buildURI(boolean fromEncoded, String thePath, String theQuery) throws URISyntaxException
{
         // TODO : do encodePartiallyEncoded only once here, do not do it inside buildPath()
         // buildFromEncoded and buildFromEncodedMap - we'll need to be careful such that

-        // path '/' seperators are not encoded so probably we'll need to create PathSegments
+        // path '/' separators are not encoded so probably we'll need to create PathSegments
         // again if fromEncoded is set
         if (fromEncoded) {
             StringBuilder b = new StringBuilder();
@@ -126,9 +136,9 @@ public class UriBuilderImpl extends UriB
         return schemeSpecificPart != null;
     }
     
-    private String substituteVarargs(String path, Object... values) {
+    private String substituteVarargs(URITemplate templ, Object[] values, int ind) {
         Map<String, String> varValueMap = new HashMap<String, String>();
-        URITemplate templ = new URITemplate(path);
+        
         // vars in set are properly ordered due to linking in hash set
         Set<String> uniqueVars = new LinkedHashSet<String>(templ.getVariables());
         if (values.length < uniqueVars.size()) {
@@ -136,7 +146,7 @@ public class UriBuilderImpl extends UriB
                                                + " value(s) given for " + uniqueVars.size()
                                                + " unique variable(s)");
         }
-        int idx = 0;
+        int idx = ind;
         for (String var : uniqueVars) {
             Object oval = values[idx++];
             varValueMap.put(var, oval.toString());
@@ -170,14 +180,22 @@ public class UriBuilderImpl extends UriB
         try {
             String thePath = buildPath(fromEncoded);
             thePath = substituteMapped(thePath, map);
-            return buildURI(fromEncoded, thePath);
+            
+            String theQuery = buildQuery(fromEncoded);
+            if (theQuery != null) {
+                theQuery = substituteMapped(theQuery, map);
+            }
+            
+            return buildURI(fromEncoded, thePath, theQuery);
         } 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());
         if (varValueMap.size() < uniqueVars.size()) {
             throw new IllegalArgumentException("Unresolved variables; only " + varValueMap.size()

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=940950&r1=940949&r2=940950&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
Tue May  4 16:57:52 2010
@@ -38,6 +38,63 @@ import org.junit.Test;
 
 public class UriBuilderImplTest extends Assert {
 
+    @Test
+    public void testQueryParamWithTemplateValues() {
+        URI uri;
+        uri = UriBuilder.fromPath("/index.jsp").queryParam("a", "{a}").queryParam("b", "{b}")
+            .build("valueA", "valueB");
+        assertEquals("/index.jsp?a=valueA&b=valueB", uri.toString());        
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testQueryParamWithMissingTemplateValues() {
+        UriBuilder.fromPath("/index.jsp").queryParam("a", "{a}").queryParam("b", "{b}")
+            .build("valueA");
+    }
+
+    @Test
+    public void testPathAndQueryParamWithTemplateValues() {
+        URI uri;
+        uri = UriBuilder.fromPath("/index{ind}.jsp").queryParam("a", "{a}").queryParam("b",
"{b}")
+            .build("1", "valueA", "valueB");
+        assertEquals("/index1.jsp?a=valueA&b=valueB", uri.toString());        
+    }
+
+    @Test
+    public void testReplaceQueryStringWithTemplateValues() {
+        URI uri;
+        uri = UriBuilder.fromUri("/index.jsp").replaceQuery("a={a}&b={b}")
+            .build("valueA", "valueB");
+        assertEquals("/index.jsp?a=valueA&b=valueB", uri.toString());        
+    }
+
+    @Test
+    public void testQueryParamUsingMapWithTemplateValues() {
+        Map<String, String> values = new HashMap<String, String>();
+        values.put("a", "valueA");
+        values.put("b", "valueB");
+        URI uri;
+        uri = UriBuilder.fromPath("/index.jsp")
+            .queryParam("a", "{a}")
+            .queryParam("b", "{b}")
+            .buildFromMap(values);
+        assertEquals("/index.jsp?a=valueA&b=valueB", uri.toString());        
+    }
+
+    @Test
+    public void testPathAndQueryParamUsingMapWithTemplateValues() {
+        Map<String, String> values = new HashMap<String, String>();
+        values.put("a", "valueA");
+        values.put("b", "valueB");
+        values.put("ind", "1");
+        URI uri;
+        uri = UriBuilder.fromPath("/index{ind}.jsp")
+            .queryParam("a", "{a}")
+            .queryParam("b", "{b}")
+            .buildFromMap(values);
+        assertEquals("/index1.jsp?a=valueA&b=valueB", uri.toString());        
+    }
+
     @Test(expected = IllegalArgumentException.class)
     public void testCtorNull() throws Exception {
         new UriBuilderImpl(null);



Mime
View raw message