Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 6A76ED243 for ; Mon, 22 Oct 2012 16:26:43 +0000 (UTC) Received: (qmail 34251 invoked by uid 500); 22 Oct 2012 16:26:43 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 34117 invoked by uid 500); 22 Oct 2012 16:26:41 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 34102 invoked by uid 99); 22 Oct 2012 16:26:41 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Oct 2012 16:26:41 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 22 Oct 2012 16:26:37 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id CFFBB23889B8; Mon, 22 Oct 2012 16:25:52 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1400939 - in /cxf/trunk/rt/frontend/jaxrs/src: main/java/org/apache/cxf/jaxrs/impl/UriBuilderImpl.java main/java/org/apache/cxf/jaxrs/model/URITemplate.java test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java Date: Mon, 22 Oct 2012 16:25:52 -0000 To: commits@cxf.apache.org From: sergeyb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121022162552.CFFBB23889B8@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sergeyb Date: Mon Oct 22 16:25:52 2012 New Revision: 1400939 URL: http://svn.apache.org/viewvc?rev=1400939&view=rev Log: Prototyping new UriBuilder method implementations 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/model/URITemplate.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=1400939&r1=1400938&r2=1400939&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 Mon Oct 22 16:25:52 2012 @@ -72,14 +72,14 @@ public class UriBuilderImpl extends UriB @Override public URI build(Object... values) throws IllegalArgumentException, UriBuilderException { - return doBuild(false, false, values); + return doBuild(false, true, values); } private URI doBuild(boolean fromEncoded, boolean encodePathSlash, Object... values) { String thePath = buildPath(fromEncoded); URITemplate pathTempl = new URITemplate(thePath); - thePath = substituteVarargs(pathTempl, values, 0); + thePath = substituteVarargs(pathTempl, values, 0, encodePathSlash); String theQuery = buildQuery(fromEncoded); int queryTemplateVarsSize = 0; @@ -88,7 +88,7 @@ public class UriBuilderImpl extends UriB int lengthDiff = values.length - pathTempl.getVariables().size(); if (lengthDiff > 0) { queryTemplateVarsSize = queryTempl.getVariables().size(); - theQuery = substituteVarargs(queryTempl, values, values.length - lengthDiff); + theQuery = substituteVarargs(queryTempl, values, values.length - lengthDiff, false); } } @@ -97,7 +97,7 @@ public class UriBuilderImpl extends UriB URITemplate fragmentTempl = new URITemplate(theFragment); int lengthDiff = values.length - pathTempl.getVariables().size() - queryTemplateVarsSize; if (lengthDiff > 0) { - theFragment = substituteVarargs(fragmentTempl, values, values.length - lengthDiff); + theFragment = substituteVarargs(fragmentTempl, values, values.length - lengthDiff, false); } } @@ -140,6 +140,10 @@ public class UriBuilderImpl extends UriB private URI buildURIFromEncoded(String thePath, String theQuery, String theFragment) throws URISyntaxException { + return new URI(buildUriString(thePath, theQuery, theFragment)); + } + + private String buildUriString(String thePath, String theQuery, String theFragment) { StringBuilder b = new StringBuilder(); if (scheme != null) { b.append(scheme).append(":"); @@ -169,7 +173,7 @@ public class UriBuilderImpl extends UriB if (theFragment != null) { b.append('#').append(theFragment); } - return new URI(b.toString()); + return b.toString(); } private boolean isSchemeOpaque() { @@ -200,7 +204,7 @@ public class UriBuilderImpl extends UriB @Override public URI buildFromMap(Map map) throws IllegalArgumentException, UriBuilderException { - return doBuildFromMap(map, false, false); + return doBuildFromMap(map, false, true); } private URI doBuildFromMap(Map map, boolean fromEncoded, @@ -208,14 +212,15 @@ public class UriBuilderImpl extends UriB throws IllegalArgumentException, UriBuilderException { try { String thePath = buildPath(fromEncoded); - thePath = substituteMapped(thePath, map); + thePath = substituteMapped(thePath, map, encodePathSlash); String theQuery = buildQuery(fromEncoded); if (theQuery != null) { - theQuery = substituteMapped(theQuery, map); + theQuery = substituteMapped(theQuery, map, false); } - String theFragment = fragment == null ? null : substituteMapped(fragment, map); + String theFragment = fragment == null + ? null : substituteMapped(fragment, map, encodePathSlash); return buildURI(fromEncoded, thePath, theQuery, theFragment); } catch (URISyntaxException ex) { @@ -223,7 +228,10 @@ public class UriBuilderImpl extends UriB } } - private String substituteVarargs(URITemplate templ, Object[] values, int ind) { + private String substituteVarargs(URITemplate templ, + Object[] values, + int ind, + boolean encodePathSlash) { Map varValueMap = new HashMap(); // vars in set are properly ordered due to linking in hash set @@ -239,12 +247,15 @@ public class UriBuilderImpl extends UriB if (oval == null) { throw new IllegalArgumentException("No object for " + var); } - varValueMap.put(var, oval.toString()); + String value = oval.toString(); + varValueMap.put(var, value); } - return templ.substitute(varValueMap); + return templ.substitute(varValueMap, encodePathSlash); } - private String substituteMapped(String path, Map varValueMap) { + private String substituteMapped(String path, + Map varValueMap, + boolean encodePathSlash) { URITemplate templ = new URITemplate(path); @@ -254,7 +265,7 @@ public class UriBuilderImpl extends UriB + " value(s) given for " + uniqueVars.size() + " unique variable(s)"); } - return templ.substitute(varValueMap); + return templ.substitute(varValueMap, encodePathSlash); } @Override @@ -402,7 +413,8 @@ public class UriBuilderImpl extends UriB segments = JAXRSUtils.getPathSegments(path, false, false); } else { segments = new ArrayList(); - segments.add(new PathSegmentImpl(path.replaceAll("/", "%2F"), false)); + path = path.replaceAll("/", "%2F"); + segments.add(new PathSegmentImpl(path, false)); } if (!paths.isEmpty() && !matrix.isEmpty()) { PathSegment ps = paths.remove(paths.size() - 1); @@ -745,8 +757,39 @@ public class UriBuilderImpl extends UriB return doBuildFromMap(map, false, encodePathSlash); } + @Override public String toTemplate() { + final String thePath = buildPath(true); + final String theQuery = buildQuery(true); + return buildUriString(thePath, theQuery, fragment); + } + + public UriBuilder resolveTemplate(String name, Object value) throws IllegalArgumentException { + return resolveTemplate(name, value, true); + } + + public UriBuilder resolveTemplate(String name, Object value, boolean encodePathSlash) + throws IllegalArgumentException { + throw new UnsupportedOperationException(); + } + + public UriBuilder resolveTemplateFromEncoded(String name, Object value) throws IllegalArgumentException { throw new UnsupportedOperationException(); } + + public UriBuilder resolveTemplates(Map values) throws IllegalArgumentException { + return resolveTemplates(values, true); + } + + public UriBuilder resolveTemplates(Map values, boolean encodePathSlash) + throws IllegalArgumentException { + throw new UnsupportedOperationException(); + } + + public UriBuilder resolveTemplatesFromEncoded(Map values) + throws IllegalArgumentException { + throw new UnsupportedOperationException(); + } + } Modified: cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java?rev=1400939&r1=1400938&r2=1400939&view=diff ============================================================================== --- cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java (original) +++ cxf/trunk/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/URITemplate.java Mon Oct 22 16:25:52 2012 @@ -248,6 +248,10 @@ public final class URITemplate { return sb.toString(); } + public String substitute(Map valuesMap) throws IllegalArgumentException { + return this.substitute(valuesMap, false); + } + /** * Substitutes template variables with mapped values. Variables are mapped to values; if not all variables * are bound result will still contain variables. Note that all variables with the same name are replaced @@ -260,7 +264,8 @@ public final class URITemplate { * @param valuesMap map variables to their values; on each value Object.toString() is called. * @return template with bound variables. */ - public String substitute(Map valuesMap) throws IllegalArgumentException { + public String substitute(Map valuesMap, + boolean encodePathSlash) throws IllegalArgumentException { if (valuesMap == null) { throw new IllegalArgumentException("valuesMap is null"); } @@ -276,7 +281,10 @@ public final class URITemplate { + var.getName() + " with pattern " + var.getPattern()); } - sb.append(value); + if (encodePathSlash) { + sval = sval.replaceAll("/", "%2F"); + } + sb.append(sval); } else { throw new IllegalArgumentException("Template variable " + var.getName() + " has no matching 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=1400939&r1=1400938&r2=1400939&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 Mon Oct 22 16:25:52 2012 @@ -327,7 +327,7 @@ public class UriBuilderImplTest extends public void testBuildValuesPct() throws Exception { URI uri = new URI("http://zzz"); URI newUri = new UriBuilderImpl(uri).path("/{a}").build("foo%25/bar%"); - assertEquals("URI is not built correctly", new URI("http://zzz/foo%2525/bar%25"), newUri); + assertEquals("URI is not built correctly", new URI("http://zzz/foo%2525%2Fbar%25"), newUri); } @Test @@ -375,7 +375,8 @@ public class UriBuilderImplTest extends map.put("a", "foo%25/bar%"); Map immutable = Collections.unmodifiableMap(map); URI newUri = new UriBuilderImpl(uri).path("/{a}").buildFromMap(immutable); - assertEquals("URI is not built correctly", new URI("http://zzz/foo%2525/bar%25"), newUri); + assertEquals("URI is not built correctly", + new URI("http://zzz/foo%2525%2Fbar%25"), newUri); } @Test @@ -936,7 +937,54 @@ public class UriBuilderImplTest extends assertEquals(expected, uri.toString()); } + @Test + public void testSegments3() { + String path1 = "ab"; + String[] path2 = {"a1", "{xy}", "3b "}; + String expected = "ab/a1/x%2Fy/3b%20"; + + URI uri = UriBuilder.fromPath(path1).segment(path2).build("x/y"); + assertEquals(uri.toString(), expected); + } + + public void testToTemplate() { + String path1 = "ab"; + String[] path2 = {"a1", "{xy}", "3b "}; + String expected = "ab/a1/{xy}/3b "; + + String template = UriBuilder.fromPath(path1).segment(path2).toTemplate(); + assertEquals(template, expected); + } + @Test + public void testSegments4() { + String path1 = "ab"; + String[] path2 = {"a1", "{xy}", "3b "}; + String expected = "ab/a1/x/y/3b%20"; + + URI uri = UriBuilder.fromPath(path1).segment(path2).build(new Object[]{"x/y"}, false); + assertEquals(uri.toString(), expected); + } + + @Test + public void testPathEncodedSlash() { + String path1 = "ab"; + String path2 = "{xy}"; + String expected = "ab/x%2Fy"; + + URI uri = UriBuilder.fromPath(path1).path(path2).build(new Object[]{"x/y"}, true); + assertEquals(uri.toString(), expected); + } + + @Test + public void testPathEncodedSlashNot() { + String path1 = "ab"; + String path2 = "{xy}"; + String expected = "ab/x/y"; + + URI uri = UriBuilder.fromPath(path1).path(path2).build(new Object[]{"x/y"}, false); + assertEquals(uri.toString(), expected); + } @Test public void testNullSegment() {