Author: sergeyb
Date: Tue May 4 17:02:29 2010
New Revision: 940958
URL: http://svn.apache.org/viewvc?rev=940958&view=rev
Log:
Merged revisions 940950 via svnmerge from
https://svn.apache.org/repos/asf/cxf/trunk
........
r940950 | sergeyb | 2010-05-04 17:57:52 +0100 (Tue, 04 May 2010) | 1 line
CXF2784 : updating UriBuilderImpl to support queries with templates
........
Modified:
cxf/branches/2.2.x-fixes/ (props changed)
cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue May 4 17:02:29 2010
@@ -1 +1 @@
-/cxf/trunk:935945,935995,936318,937409,938804,939012,939079,939664,939697,939719
+/cxf/trunk:935945,935995,936318,937409,938804,939012,939079,939664,939697,939719,940950
Propchange: cxf/branches/2.2.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.
Modified:
cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java?rev=940958&r1=940957&r2=940958&view=diff
==============================================================================
---
cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
(original)
+++
cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java
Tue May 4 17:02:29 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/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
URL:
http://svn.apache.org/viewvc/cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java?rev=940958&r1=940957&r2=940958&view=diff
==============================================================================
---
cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
(original)
+++
cxf/branches/2.2.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
Tue May 4 17:02:29 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);