Author: heiko.braun(a)jboss.com
Date: 2007-11-19 10:23:28 -0500 (Mon, 19 Nov 2007)
New Revision: 5066
Added:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/PresetInvocationTarget.java
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java
stack/native/branches/rest/src/main/java/org/jboss/rs/media/DefaultContentNegotiation.java
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ParameterBinding.java
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceLocator.java
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceModelParser.java
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceResolver.java
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/DefaultInvocationHandler.java
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/Invocation.java
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/RuntimeContext.java
stack/native/branches/rest/src/test/java/org/jboss/test/rs/Widget.java
stack/native/branches/rest/src/test/java/org/jboss/test/rs/deployment/DeploymentTestCase.java
stack/native/branches/rest/src/test/java/org/jboss/test/rs/invocation/InvocationBuilderTestCase.java
stack/native/branches/rest/src/test/java/org/jboss/test/rs/model/ResolverTestCase.java
Log:
Cleanup test cases
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/deployment/ResourceServlet.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -28,12 +28,8 @@
import org.jboss.rs.model.ResourceMethod;
import org.jboss.rs.model.ResourceModel;
import org.jboss.rs.model.ResourceResolver;
-import org.jboss.rs.runtime.DefaultInvocationBuilder;
-import org.jboss.rs.runtime.DefaultInvocationHandler;
-import org.jboss.rs.runtime.Invocation;
-import org.jboss.rs.runtime.InvocationBuilder;
-import org.jboss.rs.runtime.RuntimeContext;
-import org.jboss.rs.runtime.InvocationHandler;
+import org.jboss.rs.model.ResourceLocator;
+import org.jboss.rs.runtime.*;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@@ -45,6 +41,7 @@
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
+import java.util.Stack;
/**
* @author Heiko.Braun(a)jboss.com
@@ -102,8 +99,29 @@
ResourceResolver resolver = ResourceResolver.newInstance(rt);
ResourceMethod resourceMethod = resolver.resolve();
+ // evaluate locator stack
+ Object subResourceInstance = null;
+ Stack<ResourceLocator> visitedLocators = resolver.getVisitedLocator();
+ while(!visitedLocators.isEmpty())
+ {
+ ResourceLocator loc = visitedLocators.pop();
+
+ InvocationBuilder builder = new DefaultInvocationBuilder();
+ builder.addInvocationModel(loc.getParameterBinding());
+ builder.addInvocationModel(loc.getOperationBinding());
+ Invocation locatorInvocation = builder.build(rt);
+
+ InvocationHandler bridgeInvoker = new DefaultInvocationHandler();
+ subResourceInstance = bridgeInvoker.invoke(locatorInvocation);
+
+ }
+
// create an Invocation instance
InvocationBuilder builder = new DefaultInvocationBuilder();
+
+ if(subResourceInstance!=null)
+ builder.addInvocationModel(new PresetInvocationTarget(subResourceInstance));
+
builder.addInvocationModel( resourceMethod.getParameterBinding() );
builder.addInvocationModel( resourceMethod.getOperationBinding() );
Invocation invocation = builder.build(rt);
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/media/DefaultContentNegotiation.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/media/DefaultContentNegotiation.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/media/DefaultContentNegotiation.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -26,6 +26,7 @@
import org.jboss.rs.model.NoMethodException;
import org.jboss.rs.runtime.RuntimeContext;
import org.jboss.rs.MethodHTTP;
+import org.jboss.logging.Logger;
import javax.activation.MimeType;
import java.util.List;
@@ -54,6 +55,8 @@
public class DefaultContentNegotiation implements ContentNegotiation
{
+ private static Logger log = Logger.getLogger(DefaultContentNegotiation.class);
+
public ResourceMethod match(RuntimeContext context,
List<ResourceMatch<ResourceMethod>> candidates)
throws NoMethodException
{
@@ -67,6 +70,7 @@
matches = matchByOutputMime(context, matches);
// TODO: sort results, requires abstracting of RegexQualifier to a more general
Qualifier
+ log.warn("Conneg sort algo is missing");
match = matches.get(0);
}
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ParameterBinding.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ParameterBinding.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ParameterBinding.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -51,7 +51,7 @@
*/
public class ParameterBinding implements InvocationModel
{
- /* Pattenern of the owning ResourceMethod */
+ /* Pattern of the owning ResourceMethod */
private final Pattern regex;
/* Pattern to strip prefix from runtime path */
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceLocator.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceLocator.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceLocator.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -21,20 +21,27 @@
*/
package org.jboss.rs.model;
+import java.lang.reflect.Method;
+import java.util.Map;
+import java.util.HashMap;
+
/**
* @author Heiko.Braun(a)jboss.com
* @version $Revision$
*/
public class ResourceLocator extends AbstractRegexResolveable
{
- ResourceModel target;
+ private ResourceModel link;
private String uriTemplate;
+ private Method invocationTarget;
+ private ParameterBinding parameterBinding;
- ResourceLocator(ResourceModel target)
+ ResourceLocator(Method invocationTarget, ResourceModel link)
{
- this.uriTemplate = target.getUriTemplate();
- this.target = target;
+ this.invocationTarget = invocationTarget;
+ this.uriTemplate = link.getUriTemplate();
+ this.link = link;
}
public String getUriTemplate()
@@ -44,7 +51,7 @@
public ResourceModel field()
{
- return target;
+ return link;
}
boolean hasChildren()
@@ -55,6 +62,42 @@
void freeze()
{
- initFromUriTemplate(this.uriTemplate, null);
+ // We need to know which param belongs to what regex group
+ final Map<String, Integer> regexInfo = new HashMap<String, Integer>();
+ UriParamHandler collectRegexInfo = new UriParamHandler()
+ {
+ public void newUriParam(int regexGroup, String paramName)
+ {
+ regexInfo.put(paramName, regexGroup);
+ }
+ };
+
+ initFromUriTemplate(this.uriTemplate, collectRegexInfo);
+
+ // Create ParameterBindig
+ this.parameterBinding = new ParameterBinding(this.regexPattern);
+
+ // Annotations on method parameters
+ this.parameterBinding.registerParameterAnnotations(invocationTarget);
+
+ // Additional info abpout the regex binding
+ for(String paramName : regexInfo.keySet())
+ {
+ int group = regexInfo.get(paramName);
+ this.parameterBinding.registerRegexGroupForParam(group, paramName);
+ }
}
+
+ public ParameterBinding getParameterBinding() {
+ return parameterBinding;
+ }
+
+ public Method getInvocationTarget() {
+ return invocationTarget;
+ }
+
+ public OperationBinding getOperationBinding()
+ {
+ return new OperationBinding(this.invocationTarget);
+ }
}
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceModelParser.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceModelParser.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceModelParser.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -118,9 +118,8 @@
// subresource locator
if(null == resourceMethod)
{
- // locator
ResourceModel subResource = new ResourceModel(resource, uri.value(),
method.getReturnType());
- ResourceLocator locator = new ResourceLocator(subResource);
+ ResourceLocator locator = new ResourceLocator(method, subResource);
locator.freeze();
resource.addSubResourceLocator(locator);
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceResolver.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceResolver.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/model/ResourceResolver.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -25,10 +25,7 @@
import org.jboss.rs.media.DefaultContentNegotiation;
import org.jboss.rs.runtime.RuntimeContext;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
/**
* Resolves resource methods from {@link
org.jboss.rs.runtime.RuntimeContext#getPath()}.<br>
@@ -47,6 +44,8 @@
// pluggable content negotitation
private ContentNegotiation connegPlugin;
+ private Stack<ResourceLocator> visitedLocator = new
Stack<ResourceLocator>();
+
/**
* Provides a resolver with the default content negotitation.
*
@@ -105,7 +104,7 @@
/**
* Recursive scan for resource methods.
- * Inspect a resource match for methods and it fails try the locators.
+ * Inspect a resource match for methods and if it fails try the locators.
*
* @param dfsEntry
* @return
@@ -122,27 +121,27 @@
// root didn't match, so recurse locators to find a resource
if(null == resourceMethod)
{
- ResourceMatch subResource = resolveByLocator(dfsEntry);
+ ResourceMatch<ResourceModel> subResource = resolveByLocator(dfsEntry);
if(subResource!=null)
- resourceMethod = dfsResourceMatch(subResource);
+ resourceMethod = dfsResourceMatch(subResource);
}
return resourceMethod;
}
- private ResourceMatch resolveByLocator(ResourceMatch<ResourceModel>
resourceMatch)
+ private ResourceMatch<ResourceModel>
resolveByLocator(ResourceMatch<ResourceModel> resourceMatch)
throws NoResourceException
{
- ResourceMatch match = null;
+ ResourceMatch<ResourceModel> match = null;
- List<ResourceMatch> weightedResults = new ArrayList<ResourceMatch>();
+ List<ResourceMatch<ResourceModel>> weightedResults = new
ArrayList<ResourceMatch<ResourceModel>>();
Iterator<ResourceLocator> locators =
resourceMatch.model.getResourceLocator().iterator();
while(locators.hasNext())
{
ResourceLocator bridge = locators.next();
RegexQualifier qualifier =
bridge.resolve(resourceMatch.qualifier.nextUriToken);
if(qualifier!=null)
- weightedResults.add( new ResourceMatch( bridge.field(), qualifier) );
+ weightedResults.add( new ResourceMatch<ResourceModel>( bridge.field(),
qualifier) );
}
if(!weightedResults.isEmpty())
@@ -151,6 +150,14 @@
match = weightedResults.get(0);
}
+ // TODO: cleanup locator matching after sort
+ for(ResourceLocator loc : resourceMatch.model.getResourceLocator())
+ {
+ if(match.model == loc.field())
+ visitedLocator.add(0, loc);
+
+ }
+
return match;
}
@@ -205,4 +212,8 @@
return connegPlugin.match(context, matches);
}
+
+ public Stack<ResourceLocator> getVisitedLocator() {
+ return visitedLocator;
+ }
}
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/DefaultInvocationHandler.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/DefaultInvocationHandler.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/DefaultInvocationHandler.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -32,7 +32,9 @@
{
try
{
- Object targetInstance = invocation.getTargetBean().newInstance();
+ Object targetInstance = invocation.getTargetInstance() == null ?
+ invocation.getTargetBean().newInstance() :
+ invocation.getTargetInstance();
Object result;
Modified: stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/Invocation.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/Invocation.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/Invocation.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -45,7 +45,9 @@
// invocation metadata about the bean
private Class targetBean;
-
+
+ private Object targetInstance;
+
public Invocation(RuntimeContext context)
{
this.context = context;
@@ -97,4 +99,12 @@
{
return targetBean;
}
+
+ public Object getTargetInstance() {
+ return targetInstance;
+ }
+
+ public void setTargetInstance(Object targetInstance) {
+ this.targetInstance = targetInstance;
+ }
}
Added:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/PresetInvocationTarget.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/PresetInvocationTarget.java
(rev 0)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/PresetInvocationTarget.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -0,0 +1,17 @@
+package org.jboss.rs.runtime;
+
+public class PresetInvocationTarget implements InvocationModel
+{
+ private Object invocationInstance;
+
+
+ public PresetInvocationTarget(Object invocationInstance) {
+ assert invocationInstance!=null;
+ this.invocationInstance = invocationInstance;
+ }
+
+ public void accept(Invocation invocation) {
+
+ invocation.setTargetInstance(invocationInstance);
+ }
+}
Property changes on:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/PresetInvocationTarget.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified:
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/RuntimeContext.java
===================================================================
---
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/RuntimeContext.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/main/java/org/jboss/rs/runtime/RuntimeContext.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -31,6 +31,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
+import java.util.Stack;
/**
* @author Heiko.Braun(a)jboss.com
@@ -102,6 +103,7 @@
public void parseAcceptHeader(String headerValue)
{
+ assert headerValue!=null;
consumeMimeTypes.addAll( Convert.mimeStringToMimeTypes(headerValue) );
}
@@ -145,6 +147,4 @@
{
return path;
}
-
-
}
Modified: stack/native/branches/rest/src/test/java/org/jboss/test/rs/Widget.java
===================================================================
--- stack/native/branches/rest/src/test/java/org/jboss/test/rs/Widget.java 2007-11-19
12:52:37 UTC (rev 5065)
+++ stack/native/branches/rest/src/test/java/org/jboss/test/rs/Widget.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -24,6 +24,7 @@
import javax.ws.rs.GET;
import javax.ws.rs.UriParam;
import javax.ws.rs.UriTemplate;
+import javax.ws.rs.ProduceMime;
/**
* @author Heiko.Braun(a)jboss.com
@@ -31,9 +32,8 @@
*/
public class Widget
{
- String id;
+ String id = "";
-
public Widget() {
}
@@ -43,13 +43,20 @@
}
@GET
+ @UriTemplate("id")
+ @ProduceMime("text/plain")
+ public String getId() {
+ return this.id;
+ }
+
+ @GET
@UriTemplate("spec")
public Specification[] getSpecification() {
return new Specification[]{ new Specification() };
}
@GET
- @UriTemplate("spec/{name}")
+ @UriTemplate("spec/{name}")
public Specification getSpecByName(@UriParam("name")String name)
{
return new Specification(name);
Modified:
stack/native/branches/rest/src/test/java/org/jboss/test/rs/deployment/DeploymentTestCase.java
===================================================================
---
stack/native/branches/rest/src/test/java/org/jboss/test/rs/deployment/DeploymentTestCase.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/test/java/org/jboss/test/rs/deployment/DeploymentTestCase.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -27,6 +27,7 @@
import java.net.URL;
import java.net.URLConnection;
+import java.net.HttpURLConnection;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@@ -42,7 +43,10 @@
return new JBossWSTestSetup(DeploymentTestCase.class,
"jbossrs-deployment.war");
}
-
+ /**
+ * Invoke a root resource
+ * @throws Exception
+ */
public void testRequest1() throws Exception
{
URL url = new URL("http://localhost:8080/jbossrs-deployment/widgets");
@@ -51,10 +55,24 @@
assertEquals("A widgetlist", response);
}
+ /**
+ * Invoke a subresource
+ *
+ * @throws Exception
+ */
+ public void testRequest2() throws Exception
+ {
+ URL url = new
URL("http://localhost:8080/jbossrs-deployment/widgets/123/id");
+ String response = doTextPlainRequest(url, null);
+ assertNotNull(response);
+ assertEquals("123", response);
+ }
+
private String doTextPlainRequest(URL url, String data) throws Exception
{
- URLConnection conn = url.openConnection();
+ HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoOutput( data!=null );
+ conn.setRequestProperty("accept", "text/*");
if(data !=null)
{
Modified:
stack/native/branches/rest/src/test/java/org/jboss/test/rs/invocation/InvocationBuilderTestCase.java
===================================================================
---
stack/native/branches/rest/src/test/java/org/jboss/test/rs/invocation/InvocationBuilderTestCase.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/test/java/org/jboss/test/rs/invocation/InvocationBuilderTestCase.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -58,7 +58,7 @@
public void testUriParamBinding() throws Exception
{
- URI uri = new
URI("http://jboss.com/widgets/Foo/spec/Bar");
+ URI uri = new URI("/rest/widgets/Foo/spec/Bar");
RuntimeContext context = defaultRuntimeContext(MethodHTTP.GET, uri);
ResourceResolver resolver = ResourceResolver.newInstance(context);
@@ -81,7 +81,7 @@
public void testHttpContextParamBinding() throws Exception
{
- URI uri = new
URI("http://jboss.com/widgets/special");
+ URI uri = new URI("/rest/widgets/special");
RuntimeContext context = defaultRuntimeContext(MethodHTTP.POST, uri);
context.parseContentTypeHeader("text/xml");
ResourceResolver resolver = ResourceResolver.newInstance(context);
Modified:
stack/native/branches/rest/src/test/java/org/jboss/test/rs/model/ResolverTestCase.java
===================================================================
---
stack/native/branches/rest/src/test/java/org/jboss/test/rs/model/ResolverTestCase.java 2007-11-19
12:52:37 UTC (rev 5065)
+++
stack/native/branches/rest/src/test/java/org/jboss/test/rs/model/ResolverTestCase.java 2007-11-19
15:23:28 UTC (rev 5066)
@@ -55,7 +55,7 @@
public void testRegexResolver1() throws Exception
{
- URI uri = new
URI("http://jboss.com/widgets/Id/spec");
+ URI uri = new URI("/rest/widgets/Id/spec");
RuntimeContext context = defaultRuntimeContext(MethodHTTP.GET, uri);
ResourceResolver resolver = ResourceResolver.newInstance(context);
ResourceMethod method = resolver.resolve();
@@ -66,7 +66,7 @@
public void testRegexResolver2() throws Exception
{
- URI uri = new
URI("http://jboss.com/widgets/special");
+ URI uri = new URI("/rest/widgets/special");
RuntimeContext context = defaultRuntimeContext(MethodHTTP.POST, uri);
context.parseContentTypeHeader("text/xml");
@@ -80,7 +80,7 @@
public void testRegexResolver3() throws Exception
{
- URI uri = new
URI("http://jboss.com/widgets/offers");
+ URI uri = new URI("/rest/widgets/offers");
RuntimeContext context = defaultRuntimeContext(MethodHTTP.GET, uri);
ResourceResolver resolver = ResourceResolver.newInstance(context);
@@ -92,7 +92,7 @@
public void testRegexResolver4() throws Exception
{
- URI uri = new
URI("http://jboss.com/widgets/Id/spec/SpecName");
+ URI uri = new URI("/rest/widgets/Id/spec/SpecName");
RuntimeContext context = defaultRuntimeContext(MethodHTTP.GET, uri);
ResourceResolver resolver = ResourceResolver.newInstance(context);
@@ -104,7 +104,7 @@
public void testRegexResolver5() throws Exception
{
- URI uri = new
URI("http://jboss.com/widgets");
+ URI uri = new URI("/rest/widgets");
RuntimeContext context = defaultRuntimeContext(MethodHTTP.GET, uri);
context.parseAcceptHeader("text/plain");
@@ -120,7 +120,7 @@
private RuntimeContext defaultRuntimeContext(MethodHTTP method, URI uri)
{
RuntimeContext context = new RuntimeContext(method, uri, rootModels );
- context.parseAcceptHeader("*/*");
+ context.parseAcceptHeader("text/html, image/gif, image/jpeg, *; q=.2, */*;
q=.2");
return context;
}