You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@juneau.apache.org by ja...@apache.org on 2017/04/01 23:54:16 UTC

incubator-juneau git commit: @RestMethod.path() annotation should support numeric variable indexes.

Repository: incubator-juneau
Updated Branches:
  refs/heads/master e86b4cda4 -> 9ee67bd02


@RestMethod.path() annotation should support numeric variable indexes.

Project: http://git-wip-us.apache.org/repos/asf/incubator-juneau/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-juneau/commit/9ee67bd0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-juneau/tree/9ee67bd0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-juneau/diff/9ee67bd0

Branch: refs/heads/master
Commit: 9ee67bd02614919980ac8c3cb78a28d1a47d8e41
Parents: e86b4cd
Author: JamesBognar <ja...@apache.org>
Authored: Sat Apr 1 19:54:15 2017 -0400
Committer: JamesBognar <ja...@apache.org>
Committed: Sat Apr 1 19:54:15 2017 -0400

----------------------------------------------------------------------
 .../juneau/rest/test/PathVariablesResource.java | 48 ++++++++++++++++++
 .../java/org/apache/juneau/rest/test/Root.java  |  1 +
 .../juneau/rest/test/PathVariableTest.java      | 51 ++++++++++++++++++++
 .../java/org/apache/juneau/rest/CallMethod.java | 14 +++++-
 .../org/apache/juneau/rest/annotation/Path.java |  9 ++++
 .../juneau/rest/annotation/RestMethod.java      | 11 +++++
 6 files changed, 132 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9ee67bd0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/PathVariablesResource.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/PathVariablesResource.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/PathVariablesResource.java
new file mode 100644
index 0000000..1a72297
--- /dev/null
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/PathVariablesResource.java
@@ -0,0 +1,48 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.test;
+
+import org.apache.juneau.rest.*;
+import org.apache.juneau.rest.annotation.*;
+import org.apache.juneau.utils.*;
+
+/**
+ * JUnit automated testcase resource.
+ * Tests the <code>@RestMethod.path()</code> annotation.
+ */
+@RestResource(
+	path="/testPathVariables"
+)
+public class PathVariablesResource extends RestServletDefault {
+	private static final long serialVersionUID = 1L;
+
+	@RestMethod(name="GET", path="/test1/{x}/foo/{y}/bar/{z}/*")
+	public StringMessage test1(@Path String x, @Path int y, @Path boolean z) {
+		return new StringMessage("x={0},y={1},z={2}", x, y, z);
+	}
+
+	@RestMethod(name="GET", path="/test2/{z}/foo/{y}/bar/{x}/*")
+	public StringMessage test2(@Path("x") String x, @Path("y") int y, @Path("z") boolean z) {
+		return new StringMessage("x={0},y={1},z={2}", x, y, z);
+	}
+
+	@RestMethod(name="GET", path="/test3/{0}/foo/{1}/bar/{2}/*")
+	public StringMessage test3(@Path String x, @Path int y, @Path boolean z) {
+		return new StringMessage("x={0},y={1},z={2}", x, y, z);
+	}
+
+	@RestMethod(name="GET", path="/test4/{2}/foo/{1}/bar/{0}/*")
+	public StringMessage test4(@Path String x, @Path int y, @Path boolean z) {
+		return new StringMessage("x={0},y={1},z={2}", x, y, z);
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9ee67bd0/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
index 74834a0..10bbb18 100644
--- a/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
+++ b/juneau-rest-test/src/main/java/org/apache/juneau/rest/test/Root.java
@@ -54,6 +54,7 @@ import org.apache.juneau.rest.labels.*;
 		ParsersResource.class,
 		PathResource.class,
 		PathsResource.class,
+		PathVariablesResource.class,
 		PropertiesResource.class,
 		RestClient2Resource.class,
 		SerializersResource.class,

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9ee67bd0/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/PathVariableTest.java
----------------------------------------------------------------------
diff --git a/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/PathVariableTest.java b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/PathVariableTest.java
new file mode 100644
index 0000000..3f20146
--- /dev/null
+++ b/juneau-rest-test/src/test/java/org/apache/juneau/rest/test/PathVariableTest.java
@@ -0,0 +1,51 @@
+// ***************************************************************************************************************************
+// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
+// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
+// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
+// * with the License.  You may obtain a copy of the License at                                                              *
+// *                                                                                                                         *
+// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
+// *                                                                                                                         *
+// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
+// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
+// * specific language governing permissions and limitations under the License.                                              *
+// ***************************************************************************************************************************
+package org.apache.juneau.rest.test;
+
+import static org.junit.Assert.*;
+
+import org.apache.juneau.rest.client.*;
+import org.junit.*;
+
+/**
+ * Tests the <code>@RestMethod.path()</code> annotation.
+ */
+public class PathVariableTest extends RestTestcase {
+
+	private static String URL = "/testPathVariables";
+	RestClient client = TestMicroservice.DEFAULT_CLIENT;
+
+	@Test
+	public void test1() throws Exception {
+		String r = client.doGet(URL + "/test1/xxx/foo/123/bar/true").getResponseAsString();
+		assertEquals("x=xxx,y=123,z=true", r);
+	}
+
+	@Test
+	public void test2() throws Exception {
+		String r = client.doGet(URL + "/test2/true/foo/123/bar/xxx").getResponseAsString();
+		assertEquals("x=xxx,y=123,z=true", r);
+	}
+
+	@Test
+	public void test3() throws Exception {
+		String r = client.doGet(URL + "/test3/xxx/foo/123/bar/true").getResponseAsString();
+		assertEquals("x=xxx,y=123,z=true", r);
+	}
+
+	@Test
+	public void test4() throws Exception {
+		String r = client.doGet(URL + "/test4/true/foo/123/bar/xxx").getResponseAsString();
+		assertEquals("x=xxx,y=123,z=true", r);
+	}
+}

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9ee67bd0/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java b/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
index 8125cdc..c26e53c 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/CallMethod.java
@@ -382,9 +382,19 @@ class CallMethod implements Comparable<CallMethod>  {
 				_paramType = PATH;
 
 			if (_paramType == PATH && _name.isEmpty()) {
-				if (pathPattern.getVars().length <= attrIdx)
+				int idx = attrIdx++;
+				String[] vars = pathPattern.getVars();
+				if (vars.length <= idx)
 					throw new RestServletException("Number of attribute parameters in method ''{0}'' exceeds the number of URL pattern variables.", method.getName());
-				_name = pathPattern.getVars()[attrIdx++];
+
+				// Check for {#} variables.
+				String idxs = String.valueOf(idx);
+				for (int i = 0; i < vars.length; i++)
+					if (StringUtils.isNumeric(vars[i]) && vars[i].equals(idxs))
+						_name = vars[i];
+
+				if (_name.isEmpty())
+					_name = pathPattern.getVars()[idx];
 			}
 
 			this.paramType = _paramType;

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9ee67bd0/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/Path.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/Path.java b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/Path.java
index d9bdff9..d3c5c89 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/Path.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/Path.java
@@ -56,6 +56,15 @@ import java.lang.annotation.*;
  * 		...
  * 	}
  * </p>
+ * You can also use <code>{#}</code> notation to specify path parameters without specifying names.
+ * <p>
+ * <p class='bcode'>
+ * 	<ja>@RestMethod</ja>(name=<js>"GET"</js>, path=<js>"/myurl/{0}/{1}/{2}/*"</js>)
+ * 	<jk>public void</jk> doGet(RestRequest req, RestResponse res,
+ * 			<ja>@Path</ja> String foo, <ja>@Path</ja> <jk>int</jk> bar, <ja>@Path</ja> UUID baz) {
+ * 		...
+ * 	}
+ * </p>
  */
 @Documented
 @Target(PARAMETER)

http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/9ee67bd0/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java
----------------------------------------------------------------------
diff --git a/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java
index 986aa74..1720066 100644
--- a/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java
+++ b/juneau-rest/src/main/java/org/apache/juneau/rest/annotation/RestMethod.java
@@ -69,6 +69,17 @@ public @interface RestMethod {
 	 * Appending <js>"/*"</js> to the end of the path pattern will make it match any remainder too.<br>
 	 * Not appending <js>"/*"</js> to the end of the pattern will cause a 404 (Not found) error to occur
 	 * 	if the exact pattern is not found.
+	 * <p>
+	 * The path can contain variables that get resolved to {@link Path @Path} parameters:
+	 * <p class='bcode'>
+	 * 	<jc>// Example 1</jc>
+	 * 	<ja>@RestMethod</ja>(name=<js>"GET"</js>, path=<js>"/myurl/{foo}/{bar}/{baz}/*"</js>)
+	 *
+	 * 	<jc>// Example 2</jc>
+	 * 	<ja>@RestMethod</ja>(name=<js>"GET"</js>, path=<js>"/myurl/{0}/{1}/{2}/*"</js>)
+	 * </p>
+	 * <p>
+	 * Refer to {@link Path @Path} on how path variables get resolved.
 	 */
 	String path() default "/*";