You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by lr...@apache.org on 2011/10/07 04:13:40 UTC

svn commit: r1179923 - in /tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime: pom.xml src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.java

Author: lresende
Date: Fri Oct  7 02:13:40 2011
New Revision: 1179923

URL: http://svn.apache.org/viewvc?rev=1179923&view=rev
Log:
Adding support for ?wadl to resources exposed trough the rest binding

Added:
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.java
Modified:
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml
    tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml
URL: http://svn.apache.org/viewvc/tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml?rev=1179923&r1=1179922&r2=1179923&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml Fri Oct  7 02:13:40 2011
@@ -111,6 +111,12 @@
 
         <dependency>
             <groupId>org.apache.wink</groupId>
+            <artifactId>wink-common</artifactId>
+            <version>1.1.3-incubating</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.wink</groupId>
             <artifactId>wink-server</artifactId>
             <version>1.1.3-incubating</version>
             <exclusions>

Modified: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.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/provider/TuscanyRESTServlet.java?rev=1179923&r1=1179922&r2=1179923&view=diff
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java (original)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java Fri Oct  7 02:13:40 2011
@@ -20,11 +20,14 @@
 package org.apache.tuscany.sca.binding.rest.provider;
 
 import java.io.IOException;
+import java.lang.annotation.Annotation;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Enumeration;
 import java.util.GregorianCalendar;
+import java.util.HashSet;
 import java.util.Properties;
+import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -33,6 +36,8 @@ import javax.servlet.ServletContext;
 import javax.servlet.ServletException;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.ext.MessageBodyWriter;
 
 import org.apache.tuscany.sca.assembly.Binding;
 import org.apache.tuscany.sca.binding.rest.RESTBinding;
@@ -43,8 +48,10 @@ import org.apache.tuscany.sca.common.htt
 import org.apache.tuscany.sca.core.ExtensionPointRegistry;
 import org.apache.tuscany.sca.extensibility.ClassLoaderContext;
 import org.apache.wink.common.internal.registry.ProvidersRegistry;
+import org.apache.wink.common.model.wadl.WADLGenerator;
 import org.apache.wink.server.handlers.HandlersChain;
 import org.apache.wink.server.handlers.MessageContext;
+import org.apache.wink.server.handlers.RequestHandler;
 import org.apache.wink.server.handlers.ResponseHandler;
 import org.apache.wink.server.internal.DeploymentConfiguration;
 import org.apache.wink.server.internal.servlet.RestServlet;
@@ -53,9 +60,13 @@ import org.apache.wink.server.internal.s
  *
  */
 public class TuscanyRESTServlet extends RestServlet {
+    private static final long serialVersionUID = 89997233133964915L;
+    
     private static final Logger logger = Logger.getLogger(TuscanyRESTServlet.class.getName());
+    
+    private static final Annotation[] annotations = new Annotation[0];
 
-    private static final long serialVersionUID = 89997233133964915L;
+    
     private ExtensionPointRegistry registry;
     private RESTBinding binding;
     private Class<?> resourceClass;
@@ -89,7 +100,14 @@ public class TuscanyRESTServlet extends 
         try {
             //store in thread local
             ThreadHTTPContext.setHTTPContext(bindingContext);
-            super.service(request, response);
+            
+            // handle special ?wadl request
+            String query = request.getQueryString();
+            if(query != null && query.indexOf("wadl") >= 0) {
+                handleWadlRequest(request, response);
+            } else {
+                super.service(request, response);
+            }
         } finally {
             //remove
             ThreadHTTPContext.removeHTTPContext();
@@ -165,6 +183,51 @@ public class TuscanyRESTServlet extends 
 
         return config;
     }
+    
+    private void handleWadlRequest(HttpServletRequest request, HttpServletResponse response) {
+        try {
+            org.apache.wink.common.model.wadl.Application wadlDocument = null;
+            WADLGenerator generator = new WADLGenerator();
+            Set<Class<?>> classes = new HashSet<Class<?>>();
+            classes.add(resourceClass);
+            wadlDocument = generator.generate(binding.getURI(), classes);
+            
+            MessageBodyWriter<org.apache.wink.common.model.wadl.Application> writer = 
+                this.getDeploymentConfiguration().getProvidersRegistry().
+                     getMessageBodyWriter(org.apache.wink.common.model.wadl.Application.class, 
+                                          org.apache.wink.common.model.wadl.Application.class,
+                                          annotations, 
+                                          MediaType.APPLICATION_XML_TYPE,
+                                          null);
+            
+            writer.writeTo(wadlDocument, 
+                           org.apache.wink.common.model.wadl.Application.class, 
+                           org.apache.wink.common.model.wadl.Application.class, 
+                           annotations,
+                           MediaType.APPLICATION_XML_TYPE,
+                           null, response.getOutputStream());
+            
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        
+    }
+    
+    class TuscanyWadlRequestHandler implements RequestHandler {
+
+        @Override
+        public void init(Properties properties) {
+            // TODO Auto-generated method stub
+            
+        }
+
+        @Override
+        public void handleRequest(MessageContext messageContext, HandlersChain handlersChain) throws Throwable {
+            // TODO Auto-generated method stub
+            
+        }
+        
+    }
 
     /**
      * TuscanyResponseHandler

Added: tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.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/TestWADLGenerator.java?rev=1179923&view=auto
==============================================================================
--- tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.java (added)
+++ tuscany/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.java Fri Oct  7 02:13:40 2011
@@ -0,0 +1,79 @@
+/*
+ * 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.tuscany.sca.binding.rest;
+
+import java.net.Socket;
+
+import org.apache.tuscany.sca.binding.rest.wireformat.json.CatalogServiceTestCase;
+import org.apache.tuscany.sca.node.Contribution;
+import org.apache.tuscany.sca.node.ContributionLocationHelper;
+import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import com.meterware.httpunit.GetMethodWebRequest;
+import com.meterware.httpunit.WebConversation;
+import com.meterware.httpunit.WebRequest;
+import com.meterware.httpunit.WebResponse;
+
+public class TestWADLGenerator {
+    private static final String SERVICE_URL = "http://localhost:8085/Catalog";
+    
+    private static Node node;
+
+    @BeforeClass
+    public static void init() throws Exception {
+        try {
+            String contribution = ContributionLocationHelper.getContributionLocation(CatalogServiceTestCase.class);
+            node = NodeFactory.newInstance().createNode("store.composite", new Contribution("catalog", contribution));
+            node.start();
+            System.out.println();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @AfterClass
+    public static void destroy() throws Exception {
+        if(node != null) {
+            node.stop();
+        }
+    }
+
+    @Test
+    public void testPing() throws Exception {
+        new Socket("127.0.0.1", 8085);
+        //System.in.read();
+    }
+
+    @Test
+    public void testWadlGeneration() throws Exception {
+        WebConversation wc = new WebConversation();
+        WebRequest request = new GetMethodWebRequest(SERVICE_URL + "/?wadl");
+        WebResponse response = wc.getResource(request);
+
+        Assert.assertEquals(200, response.getResponseCode());
+        Assert.assertNotNull(response.getText());
+        System.out.println(">>>" + response.getText());
+    }
+}