diff options
author | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2011-10-07 02:13:40 +0000 |
---|---|---|
committer | lresende <lresende@13f79535-47bb-0310-9956-ffa450edef68> | 2011-10-07 02:13:40 +0000 |
commit | ab778d147b1ce734484c13ece3ddb1d1ff464e42 (patch) | |
tree | bded6a93fe99c964dea09fd98283f9f6b95eb205 | |
parent | abd09eeffe29cfadddb46f973954da76f97436f1 (diff) |
Adding support for ?wadl to resources exposed trough the rest binding
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1179923 13f79535-47bb-0310-9956-ffa450edef68
3 files changed, 150 insertions, 2 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml b/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml index 030ff87336..1a7b938325 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml @@ -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> diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java index 266d038b3b..e47f5243f9 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/TuscanyRESTServlet.java @@ -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.http.ThreadHTTPContext; 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.servlet.RestServlet; * */ 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 RestServlet { 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 RestServlet { 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 diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.java new file mode 100644 index 0000000000..46f06b3a06 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestWADLGenerator.java @@ -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()); + } +} |