Author: rfeng
Date: Tue Mar 29 23:39:48 2011
New Revision: 1086805

URL: http://svn.apache.org/viewvc?rev=1086805&view=rev
Log:
Allow WebApplicationException to pass back to wink so that the correct response 
code is returned

Modified:
    
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java
    
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java
    
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java
    
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java

Modified: 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java?rev=1086805&r1=1086804&r2=1086805&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/operationselector/jaxrs/provider/JAXRSOperationSelectorInterceptor.java
 Tue Mar 29 23:39:48 2011
@@ -25,6 +25,7 @@ import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
 import java.lang.reflect.Method;
 import java.net.URLDecoder;
 import java.util.List;
@@ -81,51 +82,54 @@ public class JAXRSOperationSelectorInter
     }
 
     public Message invoke(Message msg) {
-        try {
-            HTTPContext bindingContext = (HTTPContext)msg.getBindingContext();
-            
-            // By-pass the operation selector
-            if (bindingContext == null) {
-                return getNext().invoke(msg);
-            }
 
-            String path = 
URLDecoder.decode(HTTPUtils.getRequestPath(bindingContext.getHttpRequest()), 
"UTF-8");
+        HTTPContext bindingContext = (HTTPContext)msg.getBindingContext();
 
-            if (path.startsWith("/")) {
-                path = path.substring(1);
-            }
+        // By-pass the operation selector
+        if (bindingContext == null) {
+            return getNext().invoke(msg);
+        }
 
-            List<Operation> operations =
-                filterOperationsByHttpMethod(interfaceContract, 
bindingContext.getHttpRequest().getMethod());
+        String path = null;
+        try {
+            path = 
URLDecoder.decode(HTTPUtils.getRequestPath(bindingContext.getHttpRequest()), 
"UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new IllegalArgumentException(e);
+        }
 
-            Operation operation = findOperation(path, operations);
+        if (path.startsWith("/")) {
+            path = path.substring(1);
+        }
 
-            final JavaOperation javaOperation = (JavaOperation)operation;
-            final Method method = javaOperation.getJavaMethod();
+        List<Operation> operations =
+            filterOperationsByHttpMethod(interfaceContract, 
bindingContext.getHttpRequest().getMethod());
 
-            if (path != null && path.length() > 0) {
-                if (method.getAnnotation(Path.class) != null) {
-                    msg.setBody(new Object[] {path});
-                }
-            }
+        Operation operation = findOperation(path, operations);
 
-            // FIXME: [rfeng] We should follow JAX-RS rules to identify the 
entity parameter
-            Class<?>[] paramTypes = method.getParameterTypes();
-            if (paramTypes.length == 1) {
-                Class<?> type = paramTypes[0];
-                InputStream is = (InputStream)((Object[])msg.getBody())[0];
-                Object target = convert(is, 
bindingContext.getHttpRequest().getContentType(), type);
-                msg.setBody(new Object[] {target});
-            } else if (paramTypes.length == 0) {
-                msg.setBody(null);
-            }
+        final JavaOperation javaOperation = (JavaOperation)operation;
+        final Method method = javaOperation.getJavaMethod();
 
-            msg.setOperation(operation);
+        if (path != null && path.length() > 0) {
+            if (method.getAnnotation(Path.class) != null) {
+                msg.setBody(new Object[] {path});
+            }
+        }
 
-            return getNext().invoke(msg);
-        } catch (Exception e) {
-            throw new RuntimeException(e);
+        // FIXME: [rfeng] We should follow JAX-RS rules to identify the entity 
parameter
+        Class<?>[] paramTypes = method.getParameterTypes();
+        if (paramTypes.length == 1) {
+            Class<?> type = paramTypes[0];
+            InputStream is = (InputStream)((Object[])msg.getBody())[0];
+            Object target = convert(is, 
bindingContext.getHttpRequest().getContentType(), type);
+            msg.setBody(new Object[] {target});
+        } else if (paramTypes.length == 0) {
+            msg.setBody(null);
         }
+
+        msg.setOperation(operation);
+
+        return getNext().invoke(msg);
+
     }
 
     private Object convert(InputStream content, String contentType, Class<?> 
type) {

Modified: 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java?rev=1086805&r1=1086804&r2=1086805&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java
 Tue Mar 29 23:39:48 2011
@@ -73,17 +73,11 @@ public class CustomerServiceTestCase {
     @Test
     public void testGetInvocation() throws Exception {
         WebConversation wc = new WebConversation();
-        WebRequest request = new GetMethodWebRequest(SERVICE_URL);
+        WebRequest request = new GetMethodWebRequest(SERVICE_URL+"/John");
         request.setHeaderField("Content-Type", "application/xml");
         WebResponse response = wc.getResource(request);
 
         //for debug purposes
-        //list the response headers
-        //for(String headerField : response.getHeaderFieldNames()) {
-        //    System.out.println(">>> Header:" + headerField + " - " + 
response.getHeaderField(headerField));
-        //}
-
-        //for debug purposes
         System.out.println(">>>" + GET_RESPONSE);
         System.out.println(">>>" + response.getText());
 
@@ -93,6 +87,16 @@ public class CustomerServiceTestCase {
         Assert.assertEquals(GET_RESPONSE, response.getText());
 
     }
+    
+    @Test
+    public void testGetInvocationNotFound() throws Exception {
+        WebConversation wc = new WebConversation();
+        WebRequest request = new GetMethodWebRequest(SERVICE_URL+"/Foo");
+        request.setHeaderField("Content-Type", "application/xml");
+        WebResponse response = wc.getResource(request);
+        
+        Assert.assertEquals(404, response.getResponseCode());
+    }
 
     @Test
     public void testPutInvocation() throws Exception {

Modified: 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java?rev=1086805&r1=1086804&r2=1086805&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java
 Tue Mar 29 23:39:48 2011
@@ -23,6 +23,8 @@ import javax.jws.WebResult;
 import javax.ws.rs.GET;
 import javax.ws.rs.POST;
 import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.core.Response;
 
 import org.oasisopen.sca.annotation.Remotable;
@@ -32,7 +34,8 @@ public interface CustomerService {
     
     @GET
     @WebResult(name = "Customer", targetNamespace = "")
-    Customer get();
+    @Path("{name}")
+    Customer get(@PathParam("name") String name);
     
     @GET
     @WebResult(name = "Customer", targetNamespace = "")

Modified: 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java
URL: 
http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java?rev=1086805&r1=1086804&r2=1086805&view=diff
==============================================================================
--- 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java
 (original)
+++ 
tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java
 Tue Mar 29 23:39:48 2011
@@ -23,7 +23,9 @@ import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
 
 import org.oasisopen.sca.annotation.Init;
 import org.oasisopen.sca.annotation.Scope;
@@ -37,21 +39,26 @@ public class CustomerServiceImpl impleme
         customers.put("John", new Customer("John", "John", "[email protected]"));
     }
 
-    public Customer get() {
-        return customers.values().iterator().next();
+    public Customer get(String name) {
+        Customer c = customers.get(name);
+        if (c == null) {
+            // Not found
+            throw new WebApplicationException(Status.NOT_FOUND);
+        }
+        return c;
     }
-    
+
     public Response getResponse() {
-        return Response.ok(get()).build();
+        return Response.ok(get("John")).build();
     }
 
     public Response addCustomer(Customer customer) {
         customers.put(customer.getName(), customer);
         return Response.created(URI.create("/001")).build();
     }
-    
+
     public void updateCustomer(Customer customer) {
-        if(customers.get(customer.getName()) != null) {
+        if (customers.get(customer.getName()) != null) {
             customers.put(customer.getName(), customer);
         }
     }


Reply via email to