You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by mi...@apache.org on 2010/02/17 18:46:56 UTC

svn commit: r911111 - in /incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs: ./ org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/ org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/ org.apac...

Author: mir
Date: Wed Feb 17 17:46:55 2010
New Revision: 911111

URL: http://svn.apache.org/viewvc?rev=911111&view=rev
Log:
CLEREZZA-129: resolved

Added:
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/
      - copied from r910456, incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.triaxrs/
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/FormParamTest.java
Modified:
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/JaxRsHandler.java
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/RootResourceDescriptor.java
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/TemplateEncoder.java
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/URITemplate.java
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/ResourcePathTest.java
    incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/util/TestTemplateEncoder.java

Modified: incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/JaxRsHandler.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/JaxRsHandler.java?rev=911111&r1=910456&r2=911111&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/JaxRsHandler.java (original)
+++ incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/JaxRsHandler.java Wed Feb 17 17:46:55 2010
@@ -383,23 +383,15 @@
 			String pathPrefix) {
 		Path path = clazz.getAnnotation(Path.class);
 		if (path != null) {
-			// URI encode the template, ignoring URI template variable
-			// specifications.
-			String encodedPathTemplate;
-			try {
-				encodedPathTemplate = TemplateEncoder.encode(pathPrefix
-						+ path.value(), "utf-8");
-			} catch (UnsupportedEncodingException e) {
-				throw new RuntimeException(e);
-			}
 			RootResourceDescriptor rootResourceDescriptor;
 			collectHttpMethods(clazz);
+			String completePath = pathPrefix + path.value();
 			if (instance == null) {
 				rootResourceDescriptor = new RootResourceDescriptor(clazz,
-						encodedPathTemplate);
+						completePath);
 			} else {
 				rootResourceDescriptor = new RootResourceDescriptor(clazz,
-						instance, encodedPathTemplate, providers);
+						instance, completePath, providers);
 			}
 			rootResources.add(rootResourceDescriptor);
 			applicationProvidedDescriptors.add(rootResourceDescriptor);

Modified: incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/RootResourceDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/RootResourceDescriptor.java?rev=911111&r1=910456&r2=911111&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/RootResourceDescriptor.java (original)
+++ incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/RootResourceDescriptor.java Wed Feb 17 17:46:55 2010
@@ -37,18 +37,18 @@
 	private Object instance;
 	private Logger logger = LoggerFactory.getLogger(RootResourceDescriptor.class);
 
-	public RootResourceDescriptor(Class<?> clazz, String encodedPathTemplate) {
+	public RootResourceDescriptor(Class<?> clazz, String pathTemplate) {
 		super();
-		uriTemplate = new URITemplate(encodedPathTemplate);
+		uriTemplate = new URITemplate(pathTemplate);
 		this.clazz = clazz;
 	//store methods of the class
 	//httpMethodsOfClass = ReflectionUtil.getMethodsOfClass(clazz);
 	}
 
 	public RootResourceDescriptor(Class<?> clazz, Object instance,
-			String encodedPathTemplate, Providers providers) {
+			String pathTemplate, Providers providers) {
 		super();
-		uriTemplate = new URITemplate(encodedPathTemplate);
+		uriTemplate = new URITemplate(pathTemplate);
 		this.clazz = clazz;
 		this.instance = instance;
 		if (instance != null) {

Modified: incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/TemplateEncoder.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/TemplateEncoder.java?rev=911111&r1=910456&r2=911111&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/TemplateEncoder.java (original)
+++ incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/TemplateEncoder.java Wed Feb 17 17:46:55 2010
@@ -35,8 +35,6 @@
  * <li>The special characters &quot;<code>.</code>&quot;, &quot;<code>-</code>
  * &quot;, &quot;<code>*</code>&quot;, and &quot;<code>_</code>&quot; remain the
  * same.
- * <li>The space character &quot;<code>&nbsp;</code>&quot; is converted into a
- * plus sign &quot;<code>+</code>&quot;.
  * <li>All other characters are unsafe and are first converted into one or more
  * bytes using some encoding scheme. Then each byte is represented by the
  * 3-character string &quot;<code>%<i>xy</i></code>&quot;, where <i>xy</i> is
@@ -105,10 +103,7 @@
 		for (i = '0'; i <= '9'; i++) {
 			dontNeedEncoding.set(i);
 		}
-		dontNeedEncoding.set(' '); /*
-									 * encoding a space to a + is done in the
-									 * encode() method
-									 */
+
 		dontNeedEncoding.set('-');
 		dontNeedEncoding.set('_');
 		dontNeedEncoding.set('.');

Modified: incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/URITemplate.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/URITemplate.java?rev=911111&r1=910456&r2=911111&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/URITemplate.java (original)
+++ incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/main/java/org/apache/clerezza/triaxrs/util/URITemplate.java Wed Feb 17 17:46:55 2010
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -67,6 +68,11 @@
 	private String templateString;
 
 	public URITemplate(String rawTemplateString) {
+		try {
+			rawTemplateString = TemplateEncoder.encode(rawTemplateString, "UTF-8");
+		} catch (UnsupportedEncodingException ex) {
+			throw new RuntimeException(ex);
+		}
 		if ((rawTemplateString.length() > 0) && (rawTemplateString.charAt(0) == '/')) {
 			this.templateString = rawTemplateString.substring(1);
 		} else {

Added: incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/FormParamTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/FormParamTest.java?rev=911111&view=auto
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/FormParamTest.java (added)
+++ incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/FormParamTest.java Wed Feb 17 17:46:55 2010
@@ -0,0 +1,97 @@
+/*
+ * 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.clerezza.triaxrs.blackbox;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
+
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.apache.clerezza.triaxrs.JaxRsHandler;
+import org.apache.clerezza.triaxrs.delegate.RuntimeDelegateImpl;
+import org.apache.clerezza.triaxrs.mock.RequestImpl;
+import org.apache.clerezza.triaxrs.mock.RequestURIImpl;
+import org.apache.clerezza.triaxrs.mock.ResponseImpl;
+import org.apache.clerezza.triaxrs.testutils.HandlerCreator;
+import org.wymiwyg.wrhapi.HeaderName;
+import org.wymiwyg.wrhapi.Method;
+import org.wymiwyg.wrhapi.Response;
+import org.wymiwyg.wrhapi.util.MessageBody2Write;
+
+/**
+ *
+ * @author mir
+ */
+public class FormParamTest {
+
+	@Before
+	public void setUp() {
+		RuntimeDelegateImpl.setInstance(new RuntimeDelegateImpl());
+	}
+	
+	@Ignore
+	@Path("/test-resource")
+	public static class TestResourceForDefaultValue {
+
+		static String value;
+	   
+		@POST
+		@Path("setForm")
+		public void setForm(@FormParam(value="value") String value) {
+			
+			TestResourceForDefaultValue.value = value;
+		}
+	}
+	@Test
+	public void testFormParamInjectionIntoMethod() throws Exception {		
+		JaxRsHandler handler = HandlerCreator.getHandler(TestResourceForDefaultValue.class);
+		RequestImpl request = new RequestImpl();
+		RequestURIImpl uri = new RequestURIImpl();
+		uri.setPath("/test-resource/setForm");
+		String[] values = new String[1];
+		values[0] = "application/x-www-form-urlencoded";
+		request.setHeader(HeaderName.CONTENT_TYPE, values);
+		request.setMessageBody(new MessageBody2Write() {
+
+							@Override
+							public void writeTo(WritableByteChannel cout)
+									throws IOException {
+							   
+								OutputStream out = Channels.newOutputStream(cout);
+								String str = new String("value=foo+bar");
+								out.write(str.getBytes());
+							}
+						});		
+		request.setRequestURI(uri);
+		request.setMethod(Method.POST);		
+		Response response = new ResponseImpl();
+		handler.handle(request, response);
+		assertEquals("foo bar", TestResourceForDefaultValue.value);
+	}  
+}
\ No newline at end of file

Modified: incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/ResourcePathTest.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/ResourcePathTest.java?rev=911111&r1=910456&r2=911111&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/ResourcePathTest.java (original)
+++ incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/blackbox/ResourcePathTest.java Wed Feb 17 17:46:55 2010
@@ -18,22 +18,19 @@
  */
 package org.apache.clerezza.triaxrs.blackbox;
 
-import static org.easymock.EasyMock.expect;
-import static org.easymock.EasyMock.replay;
 import static org.junit.Assert.assertTrue;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 
-import org.easymock.EasyMock;
 import org.junit.Test;
 import org.apache.clerezza.triaxrs.JaxRsHandler;
+import org.apache.clerezza.triaxrs.mock.RequestImpl;
+import org.apache.clerezza.triaxrs.mock.RequestURIImpl;
+import org.apache.clerezza.triaxrs.mock.ResponseImpl;
 import org.apache.clerezza.triaxrs.testutils.HandlerCreator;
+import org.junit.Before;
 import org.wymiwyg.wrhapi.Method;
-import org.wymiwyg.wrhapi.Request;
-import org.wymiwyg.wrhapi.RequestURI;
-import org.wymiwyg.wrhapi.Response;
-
 
 public class ResourcePathTest {
 
@@ -46,25 +43,100 @@
 		public void handleGet() {
 			methodInvokedForGet = true;
 		}
+
+		@Path("bar%20foo")
+		@GET
+		public void handleGet2() {
+			methodInvokedForGet = true;
+		}
+
+		@Path("da ja")
+		@GET
+		public void handleGet3() {
+			methodInvokedForGet = true;
+		}
+	}
+
+	@Path("test%20resource")
+	public static class MyResource2 {
+
+		@GET
+		public void handleGet() {
+			methodInvokedForGet = true;
+		}
+	}
+
+	@Path("bla bla")
+	public static class MyResource3 {
+
+		@GET
+		public void handleGet() {
+			methodInvokedForGet = true;
+		}
+	}
+
+	@Before
+	public void reset() {
+		methodInvokedForGet = false;
 	}
 
 	@Test
-	public void testOptions() throws Exception {
+	public void requestOnResourcePathTest() throws Exception {
+		JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
+		RequestImpl requestMock = new RequestImpl();
+		RequestURIImpl requestUri = new RequestURIImpl();
+		requestUri.setPath("/foo");
+		requestMock.setRequestURI(requestUri);
+		requestMock.setMethod(Method.GET);
+		handler.handle(requestMock, new ResponseImpl());
+		assertTrue(methodInvokedForGet);
+	}
+
+	@Test
+	public void requestOnResourcePathContainingEncodedSpaceTest() throws Exception {
+		JaxRsHandler handler = HandlerCreator.getHandler(MyResource2.class);
+		RequestImpl requestMock = new RequestImpl();
+		RequestURIImpl requestUri = new RequestURIImpl();
+		requestUri.setPath("/test%20resource");
+		requestMock.setRequestURI(requestUri);
+		requestMock.setMethod(Method.GET);
+		handler.handle(requestMock, new ResponseImpl());
+		assertTrue(methodInvokedForGet);
+	}
 
+	@Test
+	public void requestOnResourcePathContainingSpaceTest() throws Exception {
+		JaxRsHandler handler = HandlerCreator.getHandler(MyResource3.class);
+		RequestImpl requestMock = new RequestImpl();
+		RequestURIImpl requestUri = new RequestURIImpl();
+		requestUri.setPath("/bla%20bla");
+		requestMock.setRequestURI(requestUri);
+		requestMock.setMethod(Method.GET);
+		handler.handle(requestMock, new ResponseImpl());
+		assertTrue(methodInvokedForGet);
+	}
+
+	@Test
+	public void reqOnResMethodPathContainingEncSpaceTest() throws Exception {
 		JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
-		
-		Request requestMock = EasyMock.createNiceMock(Request.class);
-		Response responseMock = EasyMock.createNiceMock(Response.class);
-		expect(requestMock.getMethod()).andReturn(Method.GET).anyTimes();
-		RequestURI requestURI = EasyMock.createNiceMock(RequestURI.class);
-		expect(requestURI.getPath()).andReturn("/foo");
-		expect(requestMock.getRequestURI()).andReturn(requestURI).anyTimes();
-		replay(requestMock);
-		replay(requestURI);
-		replay(responseMock);
-		handler.handle(requestMock, responseMock);
+		RequestImpl requestMock = new RequestImpl();
+		RequestURIImpl requestUri = new RequestURIImpl();
+		requestUri.setPath("/foo/bar%20foo");
+		requestMock.setRequestURI(requestUri);
+		requestMock.setMethod(Method.GET);
+		handler.handle(requestMock, new ResponseImpl());
 		assertTrue(methodInvokedForGet);
+	}
 
+	@Test
+	public void reqOnResMethodPathContainingSpaceTest() throws Exception {
+		JaxRsHandler handler = HandlerCreator.getHandler(MyResource.class);
+		RequestImpl requestMock = new RequestImpl();
+		RequestURIImpl requestUri = new RequestURIImpl();
+		requestUri.setPath("/foo/da%20ja");
+		requestMock.setRequestURI(requestUri);
+		requestMock.setMethod(Method.GET);
+		handler.handle(requestMock, new ResponseImpl());
+		assertTrue(methodInvokedForGet);
 	}
 }
-

Modified: incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/util/TestTemplateEncoder.java
URL: http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/util/TestTemplateEncoder.java?rev=911111&r1=910456&r2=911111&view=diff
==============================================================================
--- incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/util/TestTemplateEncoder.java (original)
+++ incubator/clerezza/issues/CLEREZZA-129/org.apache.clerezza.triaxrs/org.apache.clerezza.triaxrs/src/test/java/org/apache/clerezza/triaxrs/util/TestTemplateEncoder.java Wed Feb 17 17:46:55 2010
@@ -21,7 +21,6 @@
 import junit.framework.Assert;
 
 import org.junit.Test;
-import org.apache.clerezza.triaxrs.util.TemplateEncoder;
 
 public class TestTemplateEncoder {
 	@Test
@@ -30,5 +29,11 @@
 		String encoded = TemplateEncoder.encode(s, "utf-8");
 		Assert.assertTrue(encoded.indexOf("{blüü}") != -1);
 	}
-	
+
+	@Test
+	public void spaceEncoding() throws Exception {
+		String s = "/foo bar";
+		String encoded = TemplateEncoder.encode(s, "utf-8");
+		Assert.assertEquals("/foo%20bar", encoded);
+	}
 }