From fea3c0ecbc1cec4fc04c30658615d0ab88e2561f Mon Sep 17 00:00:00 2001 From: lresende Date: Sun, 25 Apr 2010 17:51:29 +0000 Subject: Refactoring packages and other references from HTTP to REST git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@937843 13f79535-47bb-0310-9956-ffa450edef68 --- .../http/provider/HTTPBindingListenerServlet.java | 461 ----------- .../http/provider/HTTPBindingProviderFactory.java | 64 -- .../http/provider/HTTPGetListenerServlet.java | 94 --- .../http/provider/HTTPServiceBindingProvider.java | 208 ----- .../http/provider/HTTPServiceListenerServlet.java | 85 -- .../rest/provider/RESTBindingListenerServlet.java | 461 +++++++++++ .../rest/provider/RESTBindingProviderFactory.java | 64 ++ .../rest/provider/RESTGetListenerServlet.java | 94 +++ .../rest/provider/RESTServiceBindingProvider.java | 208 +++++ .../rest/provider/RESTServiceListenerServlet.java | 85 ++ ...che.tuscany.sca.provider.BindingProviderFactory | 3 +- .../sca/binding/http/HTTPBindingCacheTestCase.java | 903 --------------------- .../sca/binding/http/HTTPBindingTestCase.java | 163 ---- .../sca/binding/http/NotModifiedException.java | 44 - .../binding/http/PreconditionFailedException.java | 44 - .../sca/binding/http/TestBindingCacheImpl.java | 241 ------ .../tuscany/sca/binding/http/TestGetImpl.java | 37 - .../sca/binding/http/TestServiceCacheImpl.java | 60 -- .../tuscany/sca/binding/http/TestServiceImpl.java | 60 -- .../sca/binding/rest/NotModifiedException.java | 44 + .../binding/rest/PreconditionFailedException.java | 44 + .../sca/binding/rest/RESTBindingCacheTestCase.java | 903 +++++++++++++++++++++ .../sca/binding/rest/RESTBindingTestCase.java | 163 ++++ .../sca/binding/rest/TestBindingCacheImpl.java | 241 ++++++ .../tuscany/sca/binding/rest/TestGetImpl.java | 37 + .../sca/binding/rest/TestServiceCacheImpl.java | 60 ++ .../tuscany/sca/binding/rest/TestServiceImpl.java | 60 ++ .../src/test/resources/test.composite | 10 +- .../src/test/resources/testCache.composite | 4 +- 29 files changed, 2472 insertions(+), 2473 deletions(-) delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPGetListenerServlet.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingProviderFactory.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingTestCase.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/NotModifiedException.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/PreconditionFailedException.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestGetImpl.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceCacheImpl.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/NotModifiedException.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/PreconditionFailedException.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingTestCase.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestBindingCacheImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestGetImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceCacheImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceImpl.java (limited to 'sca-java-2.x/trunk/modules/binding-rest-runtime/src') diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java deleted file mode 100644 index 3e8e396927..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java +++ /dev/null @@ -1,461 +0,0 @@ -/* - * 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.http.provider; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URLDecoder; -import java.text.ParseException; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.tuscany.sca.assembly.Binding; -import org.apache.tuscany.sca.binding.http.HTTPCacheContext; -import org.apache.tuscany.sca.common.http.HTTPContentTypeMapper; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.invocation.MessageFactory; - -/** - * Servlet responsible for dispatching HTTP requests to the - * target component implementation. - * - * @version $Rev$ $Date$ - */ -public class HTTPBindingListenerServlet extends HttpServlet { - private static final long serialVersionUID = 2865466417329430610L; - - transient private Binding binding; - - private MessageFactory messageFactory; - private Invoker getInvoker; - private Invoker conditionalGetInvoker; - private Invoker putInvoker; - private Invoker conditionalPutInvoker; - private Invoker postInvoker; - private Invoker conditionalPostInvoker; - private Invoker deleteInvoker; - private Invoker conditionalDeleteInvoker; - - /** - * Constructs a new HTTPServiceListenerServlet. - */ - public HTTPBindingListenerServlet(Binding binding, MessageFactory messageFactory) { - this.binding = binding; - this.messageFactory = messageFactory; - } - - - @Override - protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - super.service(request, response); - } - - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // Get the request path - String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); - if (path.length() ==0) { - // Redirect to a URL ending with / to make relative hrefs work - // relative to the served resource. - response.sendRedirect(request.getRequestURL().append('/').toString()); - return; - } - - // Invoke the get operation on the service implementation - Message requestMessage = messageFactory.createMessage(); - - String id = path.substring(1); - - Message responseMessage = null; - HTTPCacheContext cacheContext = null; - try { - cacheContext = HTTPCacheContext.getCacheContextFromRequest(request); - } catch (ParseException e) { - } - - // Route message based on availability of cache info and cache methods - if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalGetInvoker != null )) { - requestMessage.setBody(new Object[] {id, cacheContext}); - responseMessage = conditionalGetInvoker.invoke(requestMessage); - } else { - requestMessage.setBody(new Object[] {id}); - responseMessage = getInvoker.invoke(requestMessage); - } - if (responseMessage.isFault()) { - Object body = responseMessage.getBody(); - - int index = -1; - if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); - return; - } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); - return; - } - - throw new ServletException((Throwable)responseMessage.getBody()); - } - - if(response.getContentType() == null || response.getContentType().length() == 0){ - // Calculate content-type based on extension - String contentType = HTTPContentTypeMapper.getContentType(id); - if(contentType != null && contentType.length() >0) { - response.setContentType(contentType); - } - } - - // Write the response from the service implementation to the response - // output stream - InputStream is = (InputStream)responseMessage.getBody(); - OutputStream os = response.getOutputStream(); - byte[] buffer = new byte[2048]; - for (;;) { - int n = is.read(buffer); - if (n <= 0) - break; - os.write(buffer, 0, n); - } - os.flush(); - os.close(); - } - - @Override - protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // Get the request path - String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); - if (path.length() ==0) { - // Redirect to a URL ending with / to make relative hrefs work - // relative to the served resource. - response.sendRedirect(request.getRequestURL().append('/').toString()); - return; - } - - // Invoke the get operation on the service implementation - Message requestMessage = messageFactory.createMessage(); - String id = path.substring(1); - - Message responseMessage = null; - HTTPCacheContext cacheContext = null; - try { - cacheContext = HTTPCacheContext.getCacheContextFromRequest(request); - } catch (ParseException e) { - } - - // Route message based on availability of cache info and cache methods - if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalDeleteInvoker != null )) { - requestMessage.setBody(new Object[] {id, cacheContext}); - responseMessage = conditionalDeleteInvoker.invoke(requestMessage); - } else { - requestMessage.setBody(new Object[] {id}); - responseMessage = deleteInvoker.invoke(requestMessage); - } - if (responseMessage.isFault()) { - Object body = responseMessage.getBody(); - - int index = -1; - if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); - return; - } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); - return; - } - - throw new ServletException((Throwable)responseMessage.getBody()); - } - - // Write the response from the service implementation to the response - // output stream - InputStream is = (InputStream)responseMessage.getBody(); - OutputStream os = response.getOutputStream(); - byte[] buffer = new byte[2048]; - for (;;) { - int n = is.read(buffer); - if (n <= 0) - break; - os.write(buffer, 0, n); - } - os.flush(); - os.close(); - } - - @Override - protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // Get the request path - String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); - if (path.length() ==0) { - // Redirect to a URL ending with / to make relative hrefs work - // relative to the served resource. - response.sendRedirect(request.getRequestURL().append('/').toString()); - return; - } - - // Invoke the get operation on the service implementation - Message requestMessage = messageFactory.createMessage(); - String id = path.substring(1); - - Message responseMessage = null; - HTTPCacheContext cacheContext = null; - try { - cacheContext = HTTPCacheContext.getCacheContextFromRequest(request); - } catch (ParseException e) { - } - - // Route message based on availability of cache info and cache methods - if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalPutInvoker != null )) { - requestMessage.setBody(new Object[] {id, cacheContext}); - responseMessage = conditionalPutInvoker.invoke(requestMessage); - } else { - requestMessage.setBody(new Object[] {id}); - responseMessage = putInvoker.invoke(requestMessage); - } - if (responseMessage.isFault()) { - Object body = responseMessage.getBody(); - - int index = -1; - if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); - return; - } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); - return; - } - - throw new ServletException((Throwable)responseMessage.getBody()); - } - - // Write the response from the service implementation to the response - // output stream - InputStream is = (InputStream)responseMessage.getBody(); - OutputStream os = response.getOutputStream(); - byte[] buffer = new byte[2048]; - for (;;) { - int n = is.read(buffer); - if (n <= 0) - break; - os.write(buffer, 0, n); - } - os.flush(); - os.close(); - } - - @Override - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - // Get the request path - String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); - if (path.length() ==0) { - // Redirect to a URL ending with / to make relative hrefs work - // relative to the served resource. - response.sendRedirect(request.getRequestURL().append('/').toString()); - return; - } - - // Invoke the get operation on the service implementation - Message requestMessage = messageFactory.createMessage(); - // String id = path.substring(1); - - Message responseMessage = null; - HTTPCacheContext cacheContext = null; - try { - cacheContext = HTTPCacheContext.getCacheContextFromRequest(request); - } catch (ParseException e) { - } - - // Route message based on availability of cache info and cache methods - if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalPostInvoker != null )) { - requestMessage.setBody(new Object[] {cacheContext}); - responseMessage = conditionalPostInvoker.invoke(requestMessage); - } else { - requestMessage.setBody(new Object[] {}); - responseMessage = postInvoker.invoke(requestMessage); - } - if (responseMessage.isFault()) { - Object body = responseMessage.getBody(); - - int index = -1; - if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); - return; - } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { - if ( index > -1 ) - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); - else - response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); - return; - } - - throw new ServletException((Throwable)responseMessage.getBody()); - } - - - // Test if the ETag and LastModified are returned as a cache context. - Object body = responseMessage.getBody(); - if ( body.getClass() == HTTPCacheContext.class ) { - // Transfer to header if so. - HTTPCacheContext cc = (HTTPCacheContext)responseMessage.getBody(); - if (( cc != null ) && ( cc.isEnabled() )) { - String eTag = cc.getETag(); - if ( eTag != null ) - response.setHeader( "ETag", cc.getETag() ); - String lastModified = cc.getLastModified(); - if ( lastModified != null) - response.setHeader( "LastModified", cc.getLastModified() ); - } - } - } - - /** - * @return the getInvoker - */ - public Invoker getGetInvoker() { - return getInvoker; - } - - /** - * @param getInvoker the getInvoker to set - */ - public void setGetInvoker(Invoker getInvoker) { - this.getInvoker = getInvoker; - } - - /** - * @return the conditionalGetInvoker - */ - public Invoker getConditionalGetInvoker() { - return conditionalGetInvoker; - } - - /** - * @param conditionalGetInvoker the conditionalGetInvoker to set - */ - public void setConditionalGetInvoker(Invoker conditionalGetInvoker) { - this.conditionalGetInvoker = conditionalGetInvoker; - } - - /** - * @return the putInvoker - */ - public Invoker getPutInvoker() { - return putInvoker; - } - - /** - * @param putInvoker the putInvoker to set - */ - public void setPutInvoker(Invoker putInvoker) { - this.putInvoker = putInvoker; - } - - /** - * @return the conditionalPutInvoker - */ - public Invoker getConditionalPutInvoker() { - return conditionalPutInvoker; - } - - /** - * @param conditionalPutInvoker the conditionalPutInvoker to set - */ - public void setConditionalPutInvoker(Invoker conditionalPutInvoker) { - this.conditionalPutInvoker = conditionalPutInvoker; - } - - /** - * @return the postInvoker - */ - public Invoker getPostInvoker() { - return postInvoker; - } - - /** - * @param postInvoker the postInvoker to set - */ - public void setPostInvoker(Invoker postInvoker) { - this.postInvoker = postInvoker; - } - - /** - * @return the conditionalPostInvoker - */ - public Invoker getConditionalPostInvoker() { - return conditionalPostInvoker; - } - - /** - * @param conditionalPostInvoker the conditionalPostInvoker to set - */ - public void setConditionalPostInvoker(Invoker conditionalPostInvoker) { - this.conditionalPostInvoker = conditionalPostInvoker; - } - - /** - * @return the deleteInvoker - */ - public Invoker getDeleteInvoker() { - return deleteInvoker; - } - - /** - * @param deleteInvoker the deleteInvoker to set - */ - public void setDeleteInvoker(Invoker deleteInvoker) { - this.deleteInvoker = deleteInvoker; - } - - /** - * @return the conditionalDeleteInvoker - */ - public Invoker getConditionalDeleteInvoker() { - return conditionalDeleteInvoker; - } - - /** - * @param conditionalDeleteInvoker the conditionalDeleteInvoker to set - */ - public void setConditionalDeleteInvoker(Invoker conditionalDeleteInvoker) { - this.conditionalDeleteInvoker = conditionalDeleteInvoker; - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java deleted file mode 100644 index 118a2c3bc4..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingProviderFactory.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.http.provider; - -import org.apache.tuscany.sca.binding.http.HTTPBinding; -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.core.FactoryExtensionPoint; -import org.apache.tuscany.sca.host.http.ServletHost; -import org.apache.tuscany.sca.host.http.ServletHostHelper; -import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.provider.BindingProviderFactory; -import org.apache.tuscany.sca.provider.ReferenceBindingProvider; -import org.apache.tuscany.sca.provider.ServiceBindingProvider; -import org.apache.tuscany.sca.runtime.RuntimeEndpoint; -import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; - - -/** - * Factory for HTTP binding providers. - * - * @version $Rev$ $Date$ - */ -public class HTTPBindingProviderFactory implements BindingProviderFactory { - private ExtensionPointRegistry extensionPoints; - private MessageFactory messageFactory; - private ServletHost servletHost; - - public HTTPBindingProviderFactory(ExtensionPointRegistry extensionPoints) { - this.extensionPoints = extensionPoints; - this.servletHost = ServletHostHelper.getServletHost(extensionPoints); - FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); - messageFactory = modelFactories.getFactory(MessageFactory.class); - } - - public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpointReference) { - // Binding HTTP is currently NOT supporting References - return null; - } - - public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { - return new HTTPServiceBindingProvider(endpoint, extensionPoints, messageFactory, servletHost); - } - - public Class getModelType() { - return HTTPBinding.class; - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPGetListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPGetListenerServlet.java deleted file mode 100644 index e20d420d49..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPGetListenerServlet.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.http.provider; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URLDecoder; - -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.invocation.MessageFactory; - -/** - * Servlet responsible for dispatching HTTP GET requests to the - * target component implementation. - * - * @version $Rev$ $Date$ - */ -public class HTTPGetListenerServlet extends HttpServlet { - private static final long serialVersionUID = 2865466417329430610L; - - private MessageFactory messageFactory; - private Invoker getInvoker; - - /** - * Constructs a new HTTPServiceListenerServlet. - */ - public HTTPGetListenerServlet(Invoker getInvoker, MessageFactory messageFactory) { - this.getInvoker = getInvoker; - this.messageFactory = messageFactory; - } - - @Override - protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - - // Get the request path - String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); - if (path.length() ==0) { - - // Redirect to a URL ending with / to make relative hrefs work - // relative to the served resource. - response.sendRedirect(request.getRequestURL().append('/').toString()); - return; - } - - // Invoke the get operation on the service implementation - Message requestMessage = messageFactory.createMessage(); - String id = path.substring(1); - requestMessage.setBody(new Object[] {id}); - Message responseMessage = getInvoker.invoke(requestMessage); - if (responseMessage.isFault()) { - throw new ServletException((Throwable)responseMessage.getBody()); - } - - // Write the response from the service implementation to the response - // output stream - InputStream is = (InputStream)responseMessage.getBody(); - OutputStream os = response.getOutputStream(); - byte[] buffer = new byte[2048]; - for (;;) { - int n = is.read(buffer); - if (n <= 0) - break; - os.write(buffer, 0, n); - } - os.flush(); - os.close(); - - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java deleted file mode 100644 index 41981d2db5..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * 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.http.provider; - -import javax.servlet.Servlet; - -import org.apache.tuscany.sca.binding.http.HTTPBinding; -import org.apache.tuscany.sca.core.ExtensionPointRegistry; -import org.apache.tuscany.sca.host.http.ServletHost; -import org.apache.tuscany.sca.interfacedef.InterfaceContract; -import org.apache.tuscany.sca.interfacedef.Operation; -import org.apache.tuscany.sca.invocation.InvocationChain; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.MessageFactory; -import org.apache.tuscany.sca.invocation.Phase; -import org.apache.tuscany.sca.provider.EndpointProvider; -import org.apache.tuscany.sca.provider.OperationSelectorProvider; -import org.apache.tuscany.sca.provider.OperationSelectorProviderFactory; -import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; -import org.apache.tuscany.sca.provider.WireFormatProvider; -import org.apache.tuscany.sca.provider.WireFormatProviderFactory; -import org.apache.tuscany.sca.runtime.RuntimeComponent; -import org.apache.tuscany.sca.runtime.RuntimeComponentService; -import org.apache.tuscany.sca.runtime.RuntimeEndpoint; - -/** - * Implementation of an HTTP binding provider. - * - * @version $Rev$ $Date$ - */ -public class HTTPServiceBindingProvider implements EndpointProvider { - private ExtensionPointRegistry extensionPoints; - - private RuntimeEndpoint endpoint; - private RuntimeComponent component; - private RuntimeComponentService service; - private InterfaceContract serviceContract; - private HTTPBinding binding; - private MessageFactory messageFactory; - - private OperationSelectorProvider osProvider; - private WireFormatProvider wfProvider; - - private ServletHost servletHost; - private String servletMapping; - private HTTPBindingListenerServlet bindingListenerServlet; - - public HTTPServiceBindingProvider(RuntimeEndpoint endpoint, - ExtensionPointRegistry extensionPoints, - MessageFactory messageFactory, - ServletHost servletHost) { - - this.endpoint = endpoint; - this.component = (RuntimeComponent)endpoint.getComponent(); - this.service = (RuntimeComponentService)endpoint.getService(); - this.binding = (HTTPBinding)endpoint.getBinding(); - - this.extensionPoints = extensionPoints; - this.messageFactory = messageFactory; - this.servletHost = servletHost; - - // retrieve operation selector and wire format service providers - - ProviderFactoryExtensionPoint providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); - - - if (binding.getOperationSelector() != null) { - // Configure the interceptors for operation selection - OperationSelectorProviderFactory osProviderFactory = (OperationSelectorProviderFactory) providerFactories.getProviderFactory(binding.getOperationSelector().getClass()); - if (osProviderFactory != null) { - this.osProvider = osProviderFactory.createServiceOperationSelectorProvider(endpoint); - } - } - - if (binding.getRequestWireFormat() != null && binding.getResponseWireFormat() != null) { - // Configure the interceptors for wire format - WireFormatProviderFactory wfProviderFactory = (WireFormatProviderFactory) providerFactories.getProviderFactory(binding.getRequestWireFormat().getClass()); - if (wfProviderFactory != null) { - this.wfProvider = wfProviderFactory.createServiceWireFormatProvider(endpoint); - } - } - - - - //clone the service contract to avoid databinding issues - try { - this.serviceContract = (InterfaceContract) service.getInterfaceContract().clone(); - - // configure data binding - if (this.wfProvider != null) { - wfProvider.configureWireFormatInterfaceContract(service.getInterfaceContract()); - } - } catch(CloneNotSupportedException e) { - this.serviceContract = service.getInterfaceContract(); - } - - } - - public void start() { - // Get the invokers for the supported operations - Servlet servlet = null; - bindingListenerServlet = new HTTPBindingListenerServlet(binding, messageFactory ); - for (InvocationChain invocationChain : endpoint.getInvocationChains()) { - Operation operation = invocationChain.getTargetOperation(); - String operationName = operation.getName(); - if (operationName.equals("get")) { - Invoker getInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setGetInvoker(getInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("conditionalGet")) { - Invoker conditionalGetInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setConditionalGetInvoker(conditionalGetInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("delete")) { - Invoker deleteInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setDeleteInvoker(deleteInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("conditionalDelete")) { - Invoker conditionalDeleteInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setConditionalDeleteInvoker(conditionalDeleteInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("put")) { - Invoker putInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setPutInvoker(putInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("conditionalPut")) { - Invoker conditionalPutInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setConditionalPutInvoker(conditionalPutInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("post")) { - Invoker postInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setPostInvoker(postInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("conditionalPost")) { - Invoker conditionalPostInvoker = invocationChain.getHeadInvoker(); - bindingListenerServlet.setConditionalPostInvoker(conditionalPostInvoker); - servlet = bindingListenerServlet; - } else if (operationName.equals("service")) { - Invoker serviceInvoker = invocationChain.getHeadInvoker(); - servlet = new HTTPServiceListenerServlet(binding, serviceInvoker, messageFactory); - break; - } - } - if (servlet == null) { - throw new IllegalStateException("No get or service method found on the service"); - } - - // Create our HTTP service listener Servlet and register it with the - // Servlet host - servletMapping = binding.getURI(); - if (!servletMapping.endsWith("/")) { - servletMapping += "/"; - } - if (!servletMapping.endsWith("*")) { - servletMapping += "*"; - } - - servletHost.addServletMapping(servletMapping, servlet); - } - - public void stop() { - // Unregister the Servlet from the Servlet host - servletHost.removeServletMapping(servletMapping); - } - - public InterfaceContract getBindingInterfaceContract() { - return service.getInterfaceContract(); - } - - public boolean supportsOneWayInvocation() { - return false; - } - - /** - * Add specific http interceptor to invocation chain - */ - public void configure() { - - InvocationChain bindingChain = endpoint.getBindingInvocationChain(); - - if(osProvider != null) { - bindingChain.addInterceptor(Phase.SERVICE_BINDING_OPERATION_SELECTOR, osProvider.createInterceptor()); - } - - if (wfProvider != null) { - bindingChain.addInterceptor(Phase.SERVICE_BINDING_WIREFORMAT, wfProvider.createInterceptor()); - } - - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java deleted file mode 100644 index f752dce3e1..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.http.provider; - -import java.io.IOException; - -import javax.servlet.Servlet; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletResponse; - -import org.apache.tuscany.sca.assembly.Binding; -import org.apache.tuscany.sca.invocation.Invoker; -import org.apache.tuscany.sca.invocation.Message; -import org.apache.tuscany.sca.invocation.MessageFactory; - -/** - * Servlet responsible for dispatching HTTP service requests to the - * target component implementation. - * - * @version $Rev$ $Date$ - */ -public class HTTPServiceListenerServlet implements Servlet { - transient private Binding binding; - transient private ServletConfig config; - transient private MessageFactory messageFactory; - transient private Invoker serviceInvoker; - - /** - * Constructs a new HTTPServiceListenerServlet. - */ - public HTTPServiceListenerServlet(Binding binding, Invoker serviceInvoker, MessageFactory messageFactory) { - this.binding = binding; - this.serviceInvoker = serviceInvoker; - this.messageFactory = messageFactory; - } - - public ServletConfig getServletConfig() { - return config; - } - - public String getServletInfo() { - return ""; - } - - public void init(ServletConfig config) throws ServletException { - this.config = config; - } - - public void destroy() { - - } - - public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { - // Dispatch the service interaction to the service invoker - Message requestMessage = messageFactory.createMessage(); - requestMessage.setBody(new Object[]{request, response}); - Message responseMessage = serviceInvoker.invoke(requestMessage); - if (responseMessage.isFault()) { - // Turn a fault into an exception - //throw new ServletException((Throwable)responseMessage.getBody()); - Throwable e = (Throwable)responseMessage.getBody(); - ((HttpServletResponse)response).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); - } - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java new file mode 100644 index 0000000000..5739fbea77 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingListenerServlet.java @@ -0,0 +1,461 @@ +/* + * 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.provider; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URLDecoder; +import java.text.ParseException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.binding.rest.RESTCacheContext; +import org.apache.tuscany.sca.common.http.HTTPContentTypeMapper; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; + +/** + * Servlet responsible for dispatching HTTP requests to the + * target component implementation. + * + * @version $Rev$ $Date$ + */ +public class RESTBindingListenerServlet extends HttpServlet { + private static final long serialVersionUID = 2865466417329430610L; + + transient private Binding binding; + + private MessageFactory messageFactory; + private Invoker getInvoker; + private Invoker conditionalGetInvoker; + private Invoker putInvoker; + private Invoker conditionalPutInvoker; + private Invoker postInvoker; + private Invoker conditionalPostInvoker; + private Invoker deleteInvoker; + private Invoker conditionalDeleteInvoker; + + /** + * Constructs a new RESTServiceListenerServlet. + */ + public RESTBindingListenerServlet(Binding binding, MessageFactory messageFactory) { + this.binding = binding; + this.messageFactory = messageFactory; + } + + + @Override + protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + super.service(request, response); + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + if (path.length() ==0) { + // Redirect to a URL ending with / to make relative hrefs work + // relative to the served resource. + response.sendRedirect(request.getRequestURL().append('/').toString()); + return; + } + + // Invoke the get operation on the service implementation + Message requestMessage = messageFactory.createMessage(); + + String id = path.substring(1); + + Message responseMessage = null; + RESTCacheContext cacheContext = null; + try { + cacheContext = RESTCacheContext.getCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // Route message based on availability of cache info and cache methods + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalGetInvoker != null )) { + requestMessage.setBody(new Object[] {id, cacheContext}); + responseMessage = conditionalGetInvoker.invoke(requestMessage); + } else { + requestMessage.setBody(new Object[] {id}); + responseMessage = getInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + throw new ServletException((Throwable)responseMessage.getBody()); + } + + if(response.getContentType() == null || response.getContentType().length() == 0){ + // Calculate content-type based on extension + String contentType = HTTPContentTypeMapper.getContentType(id); + if(contentType != null && contentType.length() >0) { + response.setContentType(contentType); + } + } + + // Write the response from the service implementation to the response + // output stream + InputStream is = (InputStream)responseMessage.getBody(); + OutputStream os = response.getOutputStream(); + byte[] buffer = new byte[2048]; + for (;;) { + int n = is.read(buffer); + if (n <= 0) + break; + os.write(buffer, 0, n); + } + os.flush(); + os.close(); + } + + @Override + protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + if (path.length() ==0) { + // Redirect to a URL ending with / to make relative hrefs work + // relative to the served resource. + response.sendRedirect(request.getRequestURL().append('/').toString()); + return; + } + + // Invoke the get operation on the service implementation + Message requestMessage = messageFactory.createMessage(); + String id = path.substring(1); + + Message responseMessage = null; + RESTCacheContext cacheContext = null; + try { + cacheContext = RESTCacheContext.getCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // Route message based on availability of cache info and cache methods + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalDeleteInvoker != null )) { + requestMessage.setBody(new Object[] {id, cacheContext}); + responseMessage = conditionalDeleteInvoker.invoke(requestMessage); + } else { + requestMessage.setBody(new Object[] {id}); + responseMessage = deleteInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + throw new ServletException((Throwable)responseMessage.getBody()); + } + + // Write the response from the service implementation to the response + // output stream + InputStream is = (InputStream)responseMessage.getBody(); + OutputStream os = response.getOutputStream(); + byte[] buffer = new byte[2048]; + for (;;) { + int n = is.read(buffer); + if (n <= 0) + break; + os.write(buffer, 0, n); + } + os.flush(); + os.close(); + } + + @Override + protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + if (path.length() ==0) { + // Redirect to a URL ending with / to make relative hrefs work + // relative to the served resource. + response.sendRedirect(request.getRequestURL().append('/').toString()); + return; + } + + // Invoke the get operation on the service implementation + Message requestMessage = messageFactory.createMessage(); + String id = path.substring(1); + + Message responseMessage = null; + RESTCacheContext cacheContext = null; + try { + cacheContext = RESTCacheContext.getCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // Route message based on availability of cache info and cache methods + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalPutInvoker != null )) { + requestMessage.setBody(new Object[] {id, cacheContext}); + responseMessage = conditionalPutInvoker.invoke(requestMessage); + } else { + requestMessage.setBody(new Object[] {id}); + responseMessage = putInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + throw new ServletException((Throwable)responseMessage.getBody()); + } + + // Write the response from the service implementation to the response + // output stream + InputStream is = (InputStream)responseMessage.getBody(); + OutputStream os = response.getOutputStream(); + byte[] buffer = new byte[2048]; + for (;;) { + int n = is.read(buffer); + if (n <= 0) + break; + os.write(buffer, 0, n); + } + os.flush(); + os.close(); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + if (path.length() ==0) { + // Redirect to a URL ending with / to make relative hrefs work + // relative to the served resource. + response.sendRedirect(request.getRequestURL().append('/').toString()); + return; + } + + // Invoke the get operation on the service implementation + Message requestMessage = messageFactory.createMessage(); + // String id = path.substring(1); + + Message responseMessage = null; + RESTCacheContext cacheContext = null; + try { + cacheContext = RESTCacheContext.getCacheContextFromRequest(request); + } catch (ParseException e) { + } + + // Route message based on availability of cache info and cache methods + if (( cacheContext != null ) && (cacheContext.isEnabled()) && (conditionalPostInvoker != null )) { + requestMessage.setBody(new Object[] {cacheContext}); + responseMessage = conditionalPostInvoker.invoke(requestMessage); + } else { + requestMessage.setBody(new Object[] {}); + responseMessage = postInvoker.invoke(requestMessage); + } + if (responseMessage.isFault()) { + Object body = responseMessage.getBody(); + + int index = -1; + if ( -1 < (index = body.getClass().getName().indexOf( "NotModifiedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_NOT_MODIFIED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_NOT_MODIFIED ); + return; + } else if ( -1 < (index = body.getClass().getName().indexOf( "PreconditionFailedException")) ) { + if ( index > -1 ) + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED, body.toString().substring( index )); + else + response.sendError( HttpServletResponse.SC_PRECONDITION_FAILED ); + return; + } + + throw new ServletException((Throwable)responseMessage.getBody()); + } + + + // Test if the ETag and LastModified are returned as a cache context. + Object body = responseMessage.getBody(); + if ( body.getClass() == RESTCacheContext.class ) { + // Transfer to header if so. + RESTCacheContext cc = (RESTCacheContext)responseMessage.getBody(); + if (( cc != null ) && ( cc.isEnabled() )) { + String eTag = cc.getETag(); + if ( eTag != null ) + response.setHeader( "ETag", cc.getETag() ); + String lastModified = cc.getLastModified(); + if ( lastModified != null) + response.setHeader( "LastModified", cc.getLastModified() ); + } + } + } + + /** + * @return the getInvoker + */ + public Invoker getGetInvoker() { + return getInvoker; + } + + /** + * @param getInvoker the getInvoker to set + */ + public void setGetInvoker(Invoker getInvoker) { + this.getInvoker = getInvoker; + } + + /** + * @return the conditionalGetInvoker + */ + public Invoker getConditionalGetInvoker() { + return conditionalGetInvoker; + } + + /** + * @param conditionalGetInvoker the conditionalGetInvoker to set + */ + public void setConditionalGetInvoker(Invoker conditionalGetInvoker) { + this.conditionalGetInvoker = conditionalGetInvoker; + } + + /** + * @return the putInvoker + */ + public Invoker getPutInvoker() { + return putInvoker; + } + + /** + * @param putInvoker the putInvoker to set + */ + public void setPutInvoker(Invoker putInvoker) { + this.putInvoker = putInvoker; + } + + /** + * @return the conditionalPutInvoker + */ + public Invoker getConditionalPutInvoker() { + return conditionalPutInvoker; + } + + /** + * @param conditionalPutInvoker the conditionalPutInvoker to set + */ + public void setConditionalPutInvoker(Invoker conditionalPutInvoker) { + this.conditionalPutInvoker = conditionalPutInvoker; + } + + /** + * @return the postInvoker + */ + public Invoker getPostInvoker() { + return postInvoker; + } + + /** + * @param postInvoker the postInvoker to set + */ + public void setPostInvoker(Invoker postInvoker) { + this.postInvoker = postInvoker; + } + + /** + * @return the conditionalPostInvoker + */ + public Invoker getConditionalPostInvoker() { + return conditionalPostInvoker; + } + + /** + * @param conditionalPostInvoker the conditionalPostInvoker to set + */ + public void setConditionalPostInvoker(Invoker conditionalPostInvoker) { + this.conditionalPostInvoker = conditionalPostInvoker; + } + + /** + * @return the deleteInvoker + */ + public Invoker getDeleteInvoker() { + return deleteInvoker; + } + + /** + * @param deleteInvoker the deleteInvoker to set + */ + public void setDeleteInvoker(Invoker deleteInvoker) { + this.deleteInvoker = deleteInvoker; + } + + /** + * @return the conditionalDeleteInvoker + */ + public Invoker getConditionalDeleteInvoker() { + return conditionalDeleteInvoker; + } + + /** + * @param conditionalDeleteInvoker the conditionalDeleteInvoker to set + */ + public void setConditionalDeleteInvoker(Invoker conditionalDeleteInvoker) { + this.conditionalDeleteInvoker = conditionalDeleteInvoker; + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingProviderFactory.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingProviderFactory.java new file mode 100644 index 0000000000..23012f4a6b --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTBindingProviderFactory.java @@ -0,0 +1,64 @@ +/* + * 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.provider; + +import org.apache.tuscany.sca.binding.rest.RESTBinding; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.host.http.ServletHostHelper; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.provider.BindingProviderFactory; +import org.apache.tuscany.sca.provider.ReferenceBindingProvider; +import org.apache.tuscany.sca.provider.ServiceBindingProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + + +/** + * Factory for REST binding providers. + * + * @version $Rev$ $Date$ + */ +public class RESTBindingProviderFactory implements BindingProviderFactory { + private ExtensionPointRegistry extensionPoints; + private MessageFactory messageFactory; + private ServletHost servletHost; + + public RESTBindingProviderFactory(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + this.servletHost = ServletHostHelper.getServletHost(extensionPoints); + FactoryExtensionPoint modelFactories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + messageFactory = modelFactories.getFactory(MessageFactory.class); + } + + public ReferenceBindingProvider createReferenceBindingProvider(RuntimeEndpointReference endpointReference) { + // Binding REST is currently NOT supporting References + return null; + } + + public ServiceBindingProvider createServiceBindingProvider(RuntimeEndpoint endpoint) { + return new RESTServiceBindingProvider(endpoint, extensionPoints, messageFactory, servletHost); + } + + public Class getModelType() { + return RESTBinding.class; + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java new file mode 100644 index 0000000000..b08fa4b201 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTGetListenerServlet.java @@ -0,0 +1,94 @@ +/* + * 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.provider; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URLDecoder; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; + +/** + * Servlet responsible for dispatching HTTP GET requests to the + * target component implementation. + * + * @version $Rev$ $Date$ + */ +public class RESTGetListenerServlet extends HttpServlet { + private static final long serialVersionUID = 2865466417329430610L; + + private MessageFactory messageFactory; + private Invoker getInvoker; + + /** + * Constructs a new HTTPServiceListenerServlet. + */ + public RESTGetListenerServlet(Invoker getInvoker, MessageFactory messageFactory) { + this.getInvoker = getInvoker; + this.messageFactory = messageFactory; + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + // Get the request path + String path = URLDecoder.decode(request.getRequestURI().substring(request.getServletPath().length()), "UTF-8"); + if (path.length() ==0) { + + // Redirect to a URL ending with / to make relative hrefs work + // relative to the served resource. + response.sendRedirect(request.getRequestURL().append('/').toString()); + return; + } + + // Invoke the get operation on the service implementation + Message requestMessage = messageFactory.createMessage(); + String id = path.substring(1); + requestMessage.setBody(new Object[] {id}); + Message responseMessage = getInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + throw new ServletException((Throwable)responseMessage.getBody()); + } + + // Write the response from the service implementation to the response + // output stream + InputStream is = (InputStream)responseMessage.getBody(); + OutputStream os = response.getOutputStream(); + byte[] buffer = new byte[2048]; + for (;;) { + int n = is.read(buffer); + if (n <= 0) + break; + os.write(buffer, 0, n); + } + os.flush(); + os.close(); + + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java new file mode 100644 index 0000000000..e70aed8680 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceBindingProvider.java @@ -0,0 +1,208 @@ +/* + * 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.provider; + +import javax.servlet.Servlet; + +import org.apache.tuscany.sca.binding.rest.RESTBinding; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.host.http.ServletHost; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.InvocationChain; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.EndpointProvider; +import org.apache.tuscany.sca.provider.OperationSelectorProvider; +import org.apache.tuscany.sca.provider.OperationSelectorProviderFactory; +import org.apache.tuscany.sca.provider.ProviderFactoryExtensionPoint; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.provider.WireFormatProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * Implementation of an HTTP binding provider. + * + * @version $Rev$ $Date$ + */ +public class RESTServiceBindingProvider implements EndpointProvider { + private ExtensionPointRegistry extensionPoints; + + private RuntimeEndpoint endpoint; + private RuntimeComponent component; + private RuntimeComponentService service; + private InterfaceContract serviceContract; + private RESTBinding binding; + private MessageFactory messageFactory; + + private OperationSelectorProvider osProvider; + private WireFormatProvider wfProvider; + + private ServletHost servletHost; + private String servletMapping; + private RESTBindingListenerServlet bindingListenerServlet; + + public RESTServiceBindingProvider(RuntimeEndpoint endpoint, + ExtensionPointRegistry extensionPoints, + MessageFactory messageFactory, + ServletHost servletHost) { + + this.endpoint = endpoint; + this.component = (RuntimeComponent)endpoint.getComponent(); + this.service = (RuntimeComponentService)endpoint.getService(); + this.binding = (RESTBinding)endpoint.getBinding(); + + this.extensionPoints = extensionPoints; + this.messageFactory = messageFactory; + this.servletHost = servletHost; + + // retrieve operation selector and wire format service providers + + ProviderFactoryExtensionPoint providerFactories = extensionPoints.getExtensionPoint(ProviderFactoryExtensionPoint.class); + + + if (binding.getOperationSelector() != null) { + // Configure the interceptors for operation selection + OperationSelectorProviderFactory osProviderFactory = (OperationSelectorProviderFactory) providerFactories.getProviderFactory(binding.getOperationSelector().getClass()); + if (osProviderFactory != null) { + this.osProvider = osProviderFactory.createServiceOperationSelectorProvider(endpoint); + } + } + + if (binding.getRequestWireFormat() != null && binding.getResponseWireFormat() != null) { + // Configure the interceptors for wire format + WireFormatProviderFactory wfProviderFactory = (WireFormatProviderFactory) providerFactories.getProviderFactory(binding.getRequestWireFormat().getClass()); + if (wfProviderFactory != null) { + this.wfProvider = wfProviderFactory.createServiceWireFormatProvider(endpoint); + } + } + + + + //clone the service contract to avoid databinding issues + try { + this.serviceContract = (InterfaceContract) service.getInterfaceContract().clone(); + + // configure data binding + if (this.wfProvider != null) { + wfProvider.configureWireFormatInterfaceContract(service.getInterfaceContract()); + } + } catch(CloneNotSupportedException e) { + this.serviceContract = service.getInterfaceContract(); + } + + } + + public void start() { + // Get the invokers for the supported operations + Servlet servlet = null; + bindingListenerServlet = new RESTBindingListenerServlet(binding, messageFactory ); + for (InvocationChain invocationChain : endpoint.getInvocationChains()) { + Operation operation = invocationChain.getTargetOperation(); + String operationName = operation.getName(); + if (operationName.equals("get")) { + Invoker getInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setGetInvoker(getInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalGet")) { + Invoker conditionalGetInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalGetInvoker(conditionalGetInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("delete")) { + Invoker deleteInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setDeleteInvoker(deleteInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalDelete")) { + Invoker conditionalDeleteInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalDeleteInvoker(conditionalDeleteInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("put")) { + Invoker putInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setPutInvoker(putInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalPut")) { + Invoker conditionalPutInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalPutInvoker(conditionalPutInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("post")) { + Invoker postInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setPostInvoker(postInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("conditionalPost")) { + Invoker conditionalPostInvoker = invocationChain.getHeadInvoker(); + bindingListenerServlet.setConditionalPostInvoker(conditionalPostInvoker); + servlet = bindingListenerServlet; + } else if (operationName.equals("service")) { + Invoker serviceInvoker = invocationChain.getHeadInvoker(); + servlet = new RESTServiceListenerServlet(binding, serviceInvoker, messageFactory); + break; + } + } + if (servlet == null) { + throw new IllegalStateException("No get or service method found on the service"); + } + + // Create our HTTP service listener Servlet and register it with the + // Servlet host + servletMapping = binding.getURI(); + if (!servletMapping.endsWith("/")) { + servletMapping += "/"; + } + if (!servletMapping.endsWith("*")) { + servletMapping += "*"; + } + + servletHost.addServletMapping(servletMapping, servlet); + } + + public void stop() { + // Unregister the Servlet from the Servlet host + servletHost.removeServletMapping(servletMapping); + } + + public InterfaceContract getBindingInterfaceContract() { + return service.getInterfaceContract(); + } + + public boolean supportsOneWayInvocation() { + return false; + } + + /** + * Add specific rest interceptor to invocation chain + */ + public void configure() { + + InvocationChain bindingChain = endpoint.getBindingInvocationChain(); + + if(osProvider != null) { + bindingChain.addInterceptor(Phase.SERVICE_BINDING_OPERATION_SELECTOR, osProvider.createInterceptor()); + } + + if (wfProvider != null) { + bindingChain.addInterceptor(Phase.SERVICE_BINDING_WIREFORMAT, wfProvider.createInterceptor()); + } + + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java new file mode 100644 index 0000000000..183a0e599b --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/provider/RESTServiceListenerServlet.java @@ -0,0 +1,85 @@ +/* + * 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.provider; + +import java.io.IOException; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.invocation.MessageFactory; + +/** + * Servlet responsible for dispatching HTTP service requests to the + * target component implementation. + * + * @version $Rev$ $Date$ + */ +public class RESTServiceListenerServlet implements Servlet { + transient private Binding binding; + transient private ServletConfig config; + transient private MessageFactory messageFactory; + transient private Invoker serviceInvoker; + + /** + * Constructs a new HTTPServiceListenerServlet. + */ + public RESTServiceListenerServlet(Binding binding, Invoker serviceInvoker, MessageFactory messageFactory) { + this.binding = binding; + this.serviceInvoker = serviceInvoker; + this.messageFactory = messageFactory; + } + + public ServletConfig getServletConfig() { + return config; + } + + public String getServletInfo() { + return ""; + } + + public void init(ServletConfig config) throws ServletException { + this.config = config; + } + + public void destroy() { + + } + + public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { + // Dispatch the service interaction to the service invoker + Message requestMessage = messageFactory.createMessage(); + requestMessage.setBody(new Object[]{request, response}); + Message responseMessage = serviceInvoker.invoke(requestMessage); + if (responseMessage.isFault()) { + // Turn a fault into an exception + //throw new ServletException((Throwable)responseMessage.getBody()); + Throwable e = (Throwable)responseMessage.getBody(); + ((HttpServletResponse)response).sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); + } + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory index 15b0e51b74..4a682c1cb6 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.BindingProviderFactory @@ -16,5 +16,4 @@ # under the License. # Implementation class for the binding extension -org.apache.tuscany.sca.binding.http.provider.HTTPBindingProviderFactory;model=org.apache.tuscany.sca.binding.http.HTTPBinding - +org.apache.tuscany.sca.binding.rest.provider.RESTBindingProviderFactory;model=org.apache.tuscany.sca.binding.rest.RESTBinding diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java deleted file mode 100644 index eed7975bc6..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java +++ /dev/null @@ -1,903 +0,0 @@ -/* - * 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.http; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.Socket; -import java.text.MessageFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -import junit.framework.Assert; - -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.BeforeClass; -import org.junit.Test; - -/** - * HTTP binding unit tests. - * - * @version $Rev$ $Date$ - */ -public class HTTPBindingCacheTestCase { - // RFC 822 date time - protected static final SimpleDateFormat dateFormat = new SimpleDateFormat( - "EEE, dd MMM yyyy HH:mm:ss Z"); - - // Request with no predicates in header. - private static final String REQUEST1 = "{0} /httpbinding/{1} HTTP/1.0\n" - + "Host: localhost\n" + "Content-Type: text/xml\n" - + "Connection: close\n" + "Content-Length: {2}" + "\n\n{3}"; - - // Request with predicates in header - private static final String REQUEST2 = "{0} /httpbinding/{1} HTTP/1.0\n" - + "Host: localhost\n" + "Content-Type: text/xml\n" + "{2}: {3}\n" // predicate (If-Match, If-None-Match, If-Modified-Since, If-NotModified-Since): value (date or ETag) - + "Connection: close\n" + "Content-Length: {4}" + "\n\n{5}"; - - private static final int HTTP_PORT = 8085; - - private static Node node; - - @BeforeClass - public static void setUp() throws Exception { - try { - String contribution = ContributionLocationHelper.getContributionLocation(HTTPBindingCacheTestCase.class); - node = NodeFactory.newInstance().createNode("testCache.composite", new Contribution("test", contribution)); - node.start(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @AfterClass - public static void tearDown() throws Exception { - node.stop(); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testGet() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "GET", index, content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - Assert.assertTrue(document.indexOf("

item=" + index) != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 304 Not Modified. - Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-Unmodified-Since", dateFormat.format(new Date()), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-Unmodified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 precondition failed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 PreconditionFailed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalGetIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "GET", index, - "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("

item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testDelete() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "DELETE", index, - content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 304 Not Modified. - Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-Unmodified-Since", dateFormat.format(new Date()), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-Unmodified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 precondition failed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalDeleteIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "DELETE", index, - "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("deleted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testPost() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "POST", index, content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, - "If-Modified-Since", dateFormat.format(new Date()), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK - // assertTrue(document.indexOf("posted item=" + index) != -1); - Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 304 Not Modified. - Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, - "If-Unmodified-Since", dateFormat.format(new Date()), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK - Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, - "If-Unmodified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, - "If-Match", "eTagMatch", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK. - Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat - .format(REQUEST2, "POST", index, "If-Match", "eTagNoneMatch", - content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, - "If-None-Match", "eTagNoneMatch", content.getBytes().length, - content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return code 200 OK - Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPostIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "POST", index, - "If-None-Match", "eTagMatch", content.getBytes().length, - content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("posted item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testPut() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST1, "PUT", index, content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - Assert.assertTrue(document.indexOf("updated item=" + index) != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfModifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfModifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-Modified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 304 Not Modified. - Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfUnmodifiedNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-Unmodified-Since", dateFormat.format(new Date()), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 304 Not Modified. - // assertTrue(document.indexOf("HTTP/1.1 304") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfUnmodifiedPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-Unmodified-Since", dateFormat.format(new Date(0)), content - .getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 precondition failed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 PreconditionFailed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfNoneMatchNegative() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 1; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - Assert.assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 precondition failed. - // assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Test invoking a conditional method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testConditionalPutIfNoneMatchPositive() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String content = ""; - String request = MessageFormat.format(REQUEST2, "PUT", index, - "If-None-Match", "eTagXXX", content.getBytes().length, content); - os.write(request.getBytes()); - os.flush(); - - String document = read(client); - // Should return item - // assertTrue(document.indexOf("updated item=" + index) != -1); - // Should return code 412 PreconditionFailed. - Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); - } - - /** - * Read response stream from the given socket. - * @param socket - * @return - * @throws IOException - */ - private static String read(Socket socket) throws IOException { - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(socket - .getInputStream())); - StringBuffer sb = new StringBuffer(); - String str; - while ((str = reader.readLine()) != null) { - sb.append(str); - } - return sb.toString(); - } finally { - if (reader != null) { - reader.close(); - } - } - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingTestCase.java deleted file mode 100644 index f314bed909..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingTestCase.java +++ /dev/null @@ -1,163 +0,0 @@ -/* - * 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.http; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.net.Socket; -import java.text.MessageFormat; - -import junit.framework.Assert; - -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.BeforeClass; -import org.junit.Ignore; -import org.junit.Test; - -/** - * HTTP binding unit tests. - * - * @version $Rev$ $Date$ - */ -public class HTTPBindingTestCase { - - private static final String REQUEST1_HEADER = - "GET /httpservice/test HTTP/1.0\n" + "Host: localhost\n" - + "Content-Type: text/xml\n" - + "Connection: close\n" - + "Content-Length: "; - private static final String REQUEST1_CONTENT = ""; - private static final String REQUEST1 = - REQUEST1_HEADER + REQUEST1_CONTENT.getBytes().length + "\n\n" + REQUEST1_CONTENT; - - private static final String REQUEST2_HEADER = - "GET /webcontent/test.html HTTP/1.0\n" + "Host: localhost\n" - + "Content-Type: text/xml\n" - + "Connection: close\n" - + "Content-Length: "; - private static final String REQUEST2_CONTENT = ""; - private static final String REQUEST2 = - REQUEST2_HEADER + REQUEST2_CONTENT.getBytes().length + "\n\n" + REQUEST2_CONTENT; - - private static final String REQUEST3_HEADER = - "GET /httpget/{0} HTTP/1.0\n" + "Host: localhost\n" - + "Content-Type: text/xml\n" - + "Connection: close\n" - + "Content-Length: "; - private static final String REQUEST3_CONTENT = ""; - private static final String REQUEST3 = - REQUEST3_HEADER + REQUEST3_CONTENT.getBytes().length + "\n\n" + REQUEST3_CONTENT; - - private static final int HTTP_PORT = 8085; - - private static Node node; - - @BeforeClass - public static void setUp() throws Exception { - try { - String contribution = ContributionLocationHelper.getContributionLocation(HTTPBindingCacheTestCase.class); - node = NodeFactory.newInstance().createNode("test.composite", new Contribution("test", contribution)); - node.start(); - } catch (Exception e) { - e.printStackTrace(); - } - } - - @AfterClass - public static void tearDown() throws Exception { - node.stop(); - } - - /** - * Test invoking a POJO service implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testServiceImplementation() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - os.write(REQUEST1.getBytes()); - os.flush(); - - String document = read(client); - Assert.assertTrue(document.indexOf("

hey") != -1); - } - - /** - * Test invoking a POJO get method implementation using the HTTP binding. - * @throws Exception - */ - @Test - public void testGetImplementation() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - int index = 0; - String request = MessageFormat.format( REQUEST3, index ); - os.write( request.getBytes()); - os.flush(); - - String document = read(client); - Assert.assertTrue(document.indexOf("

item=" + index) != -1); - } - - /** - * Test getting a static resource provided using the HTTP binding. - * @throws Exception - */ - @Ignore("Implementation resource not available") - public void testStaticResourceImplementation() throws Exception { - Socket client = new Socket("127.0.0.1", HTTP_PORT); - OutputStream os = client.getOutputStream(); - os.write(REQUEST2.getBytes()); - os.flush(); - - String document = read(client); - Assert.assertTrue(document.indexOf("

hello") != -1); - } - - /** - * Read response stream from the given socket. - * @param socket - * @return - * @throws IOException - */ - private static String read(Socket socket) throws IOException { - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); - StringBuffer sb = new StringBuffer(); - String str; - while ((str = reader.readLine()) != null) { - sb.append(str); - } - return sb.toString(); - } finally { - if (reader != null) { - reader.close(); - } - } - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/NotModifiedException.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/NotModifiedException.java deleted file mode 100644 index 1d6efea2a1..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/NotModifiedException.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.http; - -/** - * Indicates that a resource was not modified. - * - * @version $Rev$ $Date$ - */ -public class NotModifiedException extends Exception { - private static final long serialVersionUID = -5046027674128627383L; - - public NotModifiedException() { - } - - public NotModifiedException(String message) { - super(message); - } - - public NotModifiedException(Throwable cause) { - super(cause); - } - - public NotModifiedException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/PreconditionFailedException.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/PreconditionFailedException.java deleted file mode 100644 index 5728899ebe..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/PreconditionFailedException.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.http; - -/** - * Indicates that a resource was not modified. - * - * @version $Rev$ $Date$ - */ -public class PreconditionFailedException extends Exception { - private static final long serialVersionUID = -5046027674128627383L; - - public PreconditionFailedException() { - } - - public PreconditionFailedException(String message) { - super(message); - } - - public PreconditionFailedException(Throwable cause) { - super(cause); - } - - public PreconditionFailedException(String message, Throwable cause) { - super(message, cause); - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java deleted file mode 100644 index f2456f3175..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java +++ /dev/null @@ -1,241 +0,0 @@ -/* - * 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.http; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.util.Date; - -/** - * Test service implementation that implements a various conditional HTTP - * methods. For testing, the id==0 items are very old (Date(0)), not modified, - * and always match ETags and the id==1 items are always brand new (Date()), - * modified, and never match ETags. Using these ids one can test the - * LastModified and ETag headers of the requests. - * - * @version $Rev$ $Date$ - */ -public class TestBindingCacheImpl { - - /** - * Implements the HTTP get method of the collection implementation. - * @param id - * @return - */ - public InputStream get(String id) { - return new ByteArrayInputStream( - ("

item=" + id + "").getBytes()); - } - - /** - * Implements the HTTP conditional get method of the collection implementation. - * @param id - * @return - */ - public InputStream conditionalGet(String id, HTTPCacheContext cacheContext) - throws NotModifiedException, PreconditionFailedException { - - if (cacheContext != null) { - if (cacheContext.ifModifiedSince) { - if ((id.equals("1")) - && (0 > cacheContext.lastModifiedDate - .compareTo(new Date()))) - throw new NotModifiedException("item 1 was modified on " - + new Date()); - } - if (cacheContext.ifUnmodifiedSince) { - if ((id.equals("0")) - && (0 > cacheContext.lastModifiedDate - .compareTo(new Date()))) - throw new PreconditionFailedException( - "item 0 was modified on " + new Date(0)); - } - if (cacheContext.ifMatch) { - if (id.equals("1")) - throw new PreconditionFailedException( - "item 1 eTag does not match " - + cacheContext.getETag()); - } - if (cacheContext.ifNoneMatch) { - if (id.equals("0")) - throw new PreconditionFailedException( - "item 0 eTag matches " + cacheContext.getETag()); - } - } - return new ByteArrayInputStream( - ("

item=" + id + "").getBytes()); - } - - /** - * Implements the HTTP delete method of the collection implementation. - * @param id - * @return - */ - public InputStream delete(String id) { - return new ByteArrayInputStream( - ("

deleted item=" + id + "") - .getBytes()); - } - - /** - * Implements the HTTP conditional delete method of the collection implementation. - * @param id - * @return - */ - public InputStream conditionalDelete(String id, HTTPCacheContext cacheContext) - throws NotModifiedException, PreconditionFailedException { - - if (cacheContext != null) { - if (cacheContext.ifModifiedSince) { - if ((id.equals("1")) - && (0 > cacheContext.lastModifiedDate - .compareTo(new Date()))) - throw new NotModifiedException("item 1 was modified on " - + new Date()); - } - if (cacheContext.ifUnmodifiedSince) { - if ((id.equals("0")) - && (0 > cacheContext.lastModifiedDate - .compareTo(new Date()))) - throw new PreconditionFailedException( - "item 0 was modified on " + new Date(0)); - } - if (cacheContext.ifMatch) { - if (id.equals("1")) - throw new PreconditionFailedException( - "item 1 eTag does not match " - + cacheContext.getETag()); - } - if (cacheContext.ifNoneMatch) { - if (id.equals("0")) - throw new PreconditionFailedException( - "item 0 eTag matches " + cacheContext.getETag()); - } - } - return new ByteArrayInputStream( - ("

deleted item=" + id + "") - .getBytes()); - } - - /** - * Implements the HTTP post method of the collection implementation. - * @param id - * @return - */ - public InputStream post() { - int id = (new java.util.Random()).nextInt(Integer.MAX_VALUE); - return new ByteArrayInputStream( - ("

posted item=" + id + "") - .getBytes()); - } - - /** - * Implements the HTTP conditional post method of the collection implementation. - * @param id - * @return - */ - public HTTPCacheContext conditionalPost(HTTPCacheContext cacheContext) - throws NotModifiedException, PreconditionFailedException { - String id = "" + (new java.util.Random()).nextInt(Integer.MAX_VALUE); - - if (cacheContext != null) { - if (cacheContext.ifModifiedSince) { - if (0 >= cacheContext.lastModifiedDate.compareTo(new Date(0))) - throw new NotModifiedException("item was modified on " - + new Date()); - } - if (cacheContext.ifUnmodifiedSince) { - if ((0 >= cacheContext.lastModifiedDate.compareTo(new Date(0)))) - throw new PreconditionFailedException( - "item was modified on " + new Date(0)); - } - if (cacheContext.ifMatch) { - if (cacheContext.getETag().equalsIgnoreCase("ETagNoneMatch")) - throw new PreconditionFailedException( - "item eTag does not match " - + cacheContext.getETag()); - } - if (cacheContext.ifNoneMatch) { - if (cacheContext.getETag().equalsIgnoreCase("ETagMatch")) - throw new PreconditionFailedException("item eTag matches " - + cacheContext.getETag()); - } - } - - // Return the ETag and LastModfied fields by serialize to a byte array - HTTPCacheContext returnContext = new HTTPCacheContext(); - returnContext.setETag( "ETag" + (new java.util.Random()).nextInt(Integer.MAX_VALUE) ); - returnContext.setLastModified( new Date() ); - return returnContext; - } - - /** - * Implements the HTTP update/put method of the collection implementation. - * @param id - * @return - */ - public InputStream put(String id) { - return new ByteArrayInputStream( - ("

updated item=" + id + "") - .getBytes()); - } - - /** - * Implements the HTTP conditional update/put method of the collection implementation. - * @param id - * @return - */ - public InputStream conditionalPut(String id, HTTPCacheContext cacheContext) - throws NotModifiedException, PreconditionFailedException { - - if (cacheContext != null) { - if (cacheContext.ifModifiedSince) { - if ((id.equals("1")) - && (0 > cacheContext.lastModifiedDate - .compareTo(new Date()))) - throw new NotModifiedException("item 1 was modified on " - + new Date()); - } - if (cacheContext.ifUnmodifiedSince) { - if ((id.equals("0")) - && (0 > cacheContext.lastModifiedDate - .compareTo(new Date()))) - throw new PreconditionFailedException( - "item 0 was modified on " + new Date(0)); - } - if (cacheContext.ifMatch) { - if (id.equals("1")) - throw new PreconditionFailedException( - "item 1 eTag does not match " - + cacheContext.getETag()); - } - if (cacheContext.ifNoneMatch) { - if (id.equals("0")) - throw new PreconditionFailedException( - "item 0 eTag matches " + cacheContext.getETag()); - } - } - - return new ByteArrayInputStream( - ("

updated item=" + id + "") - .getBytes()); - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestGetImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestGetImpl.java deleted file mode 100644 index 333bacbab3..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestGetImpl.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.http; - -import java.io.ByteArrayInputStream; -import java.io.InputStream; - -/** - * Test service implementation that implements a get method. - * - * @version $Rev$ $Date$ - */ -public class TestGetImpl { - - public InputStream get(String id) { - return new ByteArrayInputStream(("

item=" + id + "").getBytes()); - - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceCacheImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceCacheImpl.java deleted file mode 100644 index fec68138a0..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceCacheImpl.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.http; - -import java.io.IOException; - -import javax.servlet.Servlet; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import org.oasisopen.sca.annotation.Service; - -/** - * Test service implementation. - * - * @version $Rev$ $Date$ - */ - -@Service(Servlet.class) -public class TestServiceCacheImpl implements Servlet { - - public void init(ServletConfig config) throws ServletException { - } - - public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { - //HttpServletResponse httpResponse = (HttpServletResponse)response; - response.getOutputStream().print("

hey"); - } - - public void destroy() { - } - - public ServletConfig getServletConfig() { - return null; - } - - public String getServletInfo() { - return null; - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceImpl.java deleted file mode 100644 index b8c1223894..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestServiceImpl.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.http; - -import java.io.IOException; - -import javax.servlet.Servlet; -import javax.servlet.ServletConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import org.oasisopen.sca.annotation.Service; - -/** - * Test service implementation. - * - * @version $Rev$ $Date$ - */ - -@Service(Servlet.class) -public class TestServiceImpl implements Servlet { - - public void init(ServletConfig config) throws ServletException { - } - - public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { - //HttpServletResponse httpResponse = (HttpServletResponse)response; - response.getOutputStream().print("

hey"); - } - - public void destroy() { - } - - public ServletConfig getServletConfig() { - return null; - } - - public String getServletInfo() { - return null; - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/NotModifiedException.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/NotModifiedException.java new file mode 100644 index 0000000000..49e7d67a76 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/NotModifiedException.java @@ -0,0 +1,44 @@ +/* + * 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; + +/** + * Indicates that a resource was not modified. + * + * @version $Rev$ $Date$ + */ +public class NotModifiedException extends Exception { + private static final long serialVersionUID = -5046027674128627383L; + + public NotModifiedException() { + } + + public NotModifiedException(String message) { + super(message); + } + + public NotModifiedException(Throwable cause) { + super(cause); + } + + public NotModifiedException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/PreconditionFailedException.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/PreconditionFailedException.java new file mode 100644 index 0000000000..1463e1a5db --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/PreconditionFailedException.java @@ -0,0 +1,44 @@ +/* + * 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; + +/** + * Indicates that a resource was not modified. + * + * @version $Rev$ $Date$ + */ +public class PreconditionFailedException extends Exception { + private static final long serialVersionUID = -5046027674128627383L; + + public PreconditionFailedException() { + } + + public PreconditionFailedException(String message) { + super(message); + } + + public PreconditionFailedException(Throwable cause) { + super(cause); + } + + public PreconditionFailedException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java new file mode 100644 index 0000000000..e68646c8a5 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingCacheTestCase.java @@ -0,0 +1,903 @@ +/* + * 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.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.Socket; +import java.text.MessageFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import junit.framework.Assert; + +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.BeforeClass; +import org.junit.Test; + +/** + * HTTP binding unit tests. + * + * @version $Rev$ $Date$ + */ +public class RESTBindingCacheTestCase { + // RFC 822 date time + protected static final SimpleDateFormat dateFormat = new SimpleDateFormat( + "EEE, dd MMM yyyy HH:mm:ss Z"); + + // Request with no predicates in header. + private static final String REQUEST1 = "{0} /httpbinding/{1} HTTP/1.0\n" + + "Host: localhost\n" + "Content-Type: text/xml\n" + + "Connection: close\n" + "Content-Length: {2}" + "\n\n{3}"; + + // Request with predicates in header + private static final String REQUEST2 = "{0} /httpbinding/{1} HTTP/1.0\n" + + "Host: localhost\n" + "Content-Type: text/xml\n" + "{2}: {3}\n" // predicate (If-Match, If-None-Match, If-Modified-Since, If-NotModified-Since): value (date or ETag) + + "Connection: close\n" + "Content-Length: {4}" + "\n\n{5}"; + + private static final int HTTP_PORT = 8085; + + private static Node node; + + @BeforeClass + public static void setUp() throws Exception { + try { + String contribution = ContributionLocationHelper.getContributionLocation(RESTBindingCacheTestCase.class); + node = NodeFactory.newInstance().createNode("testCache.composite", new Contribution("test", contribution)); + node.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void tearDown() throws Exception { + node.stop(); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testGet() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "GET", index, content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + Assert.assertTrue(document.indexOf("

item=" + index) != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 304 Not Modified. + Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 precondition failed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 PreconditionFailed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalGetIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "GET", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("

item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testDelete() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "DELETE", index, + content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 304 Not Modified. + Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 precondition failed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalDeleteIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "DELETE", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("deleted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testPost() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "POST", index, content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Modified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK + // assertTrue(document.indexOf("posted item=" + index) != -1); + Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 304 Not Modified. + Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK + Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-Match", "eTagMatch", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK. + Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat + .format(REQUEST2, "POST", index, "If-Match", "eTagNoneMatch", + content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-None-Match", "eTagNoneMatch", content.getBytes().length, + content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return code 200 OK + Assert.assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPostIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "POST", index, + "If-None-Match", "eTagMatch", content.getBytes().length, + content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("posted item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testPut() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST1, "PUT", index, content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + Assert.assertTrue(document.indexOf("updated item=" + index) != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfModifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfModifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Modified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 304 Not Modified. + Assert.assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfUnmodifiedNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Unmodified-Since", dateFormat.format(new Date()), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 304 Not Modified. + // assertTrue(document.indexOf("HTTP/1.1 304") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfUnmodifiedPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Unmodified-Since", dateFormat.format(new Date(0)), content + .getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 precondition failed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 PreconditionFailed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfNoneMatchNegative() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 1; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + Assert.assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 precondition failed. + // assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Test invoking a conditional method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testConditionalPutIfNoneMatchPositive() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String content = ""; + String request = MessageFormat.format(REQUEST2, "PUT", index, + "If-None-Match", "eTagXXX", content.getBytes().length, content); + os.write(request.getBytes()); + os.flush(); + + String document = read(client); + // Should return item + // assertTrue(document.indexOf("updated item=" + index) != -1); + // Should return code 412 PreconditionFailed. + Assert.assertTrue(document.indexOf("HTTP/1.1 412") != -1); + } + + /** + * Read response stream from the given socket. + * @param socket + * @return + * @throws IOException + */ + private static String read(Socket socket) throws IOException { + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(socket + .getInputStream())); + StringBuffer sb = new StringBuffer(); + String str; + while ((str = reader.readLine()) != null) { + sb.append(str); + } + return sb.toString(); + } finally { + if (reader != null) { + reader.close(); + } + } + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingTestCase.java new file mode 100644 index 0000000000..e08069f479 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/RESTBindingTestCase.java @@ -0,0 +1,163 @@ +/* + * 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.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.Socket; +import java.text.MessageFormat; + +import junit.framework.Assert; + +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.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; + +/** + * HTTP binding unit tests. + * + * @version $Rev$ $Date$ + */ +public class RESTBindingTestCase { + + private static final String REQUEST1_HEADER = + "GET /httpservice/test HTTP/1.0\n" + "Host: localhost\n" + + "Content-Type: text/xml\n" + + "Connection: close\n" + + "Content-Length: "; + private static final String REQUEST1_CONTENT = ""; + private static final String REQUEST1 = + REQUEST1_HEADER + REQUEST1_CONTENT.getBytes().length + "\n\n" + REQUEST1_CONTENT; + + private static final String REQUEST2_HEADER = + "GET /webcontent/test.html HTTP/1.0\n" + "Host: localhost\n" + + "Content-Type: text/xml\n" + + "Connection: close\n" + + "Content-Length: "; + private static final String REQUEST2_CONTENT = ""; + private static final String REQUEST2 = + REQUEST2_HEADER + REQUEST2_CONTENT.getBytes().length + "\n\n" + REQUEST2_CONTENT; + + private static final String REQUEST3_HEADER = + "GET /httpget/{0} HTTP/1.0\n" + "Host: localhost\n" + + "Content-Type: text/xml\n" + + "Connection: close\n" + + "Content-Length: "; + private static final String REQUEST3_CONTENT = ""; + private static final String REQUEST3 = + REQUEST3_HEADER + REQUEST3_CONTENT.getBytes().length + "\n\n" + REQUEST3_CONTENT; + + private static final int HTTP_PORT = 8085; + + private static Node node; + + @BeforeClass + public static void setUp() throws Exception { + try { + String contribution = ContributionLocationHelper.getContributionLocation(RESTBindingCacheTestCase.class); + node = NodeFactory.newInstance().createNode("test.composite", new Contribution("test", contribution)); + node.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void tearDown() throws Exception { + node.stop(); + } + + /** + * Test invoking a POJO service implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testServiceImplementation() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + os.write(REQUEST1.getBytes()); + os.flush(); + + String document = read(client); + Assert.assertTrue(document.indexOf("

hey") != -1); + } + + /** + * Test invoking a POJO get method implementation using the HTTP binding. + * @throws Exception + */ + @Test + public void testGetImplementation() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + int index = 0; + String request = MessageFormat.format( REQUEST3, index ); + os.write( request.getBytes()); + os.flush(); + + String document = read(client); + Assert.assertTrue(document.indexOf("

item=" + index) != -1); + } + + /** + * Test getting a static resource provided using the HTTP binding. + * @throws Exception + */ + @Ignore("Implementation resource not available") + public void testStaticResourceImplementation() throws Exception { + Socket client = new Socket("127.0.0.1", HTTP_PORT); + OutputStream os = client.getOutputStream(); + os.write(REQUEST2.getBytes()); + os.flush(); + + String document = read(client); + Assert.assertTrue(document.indexOf("

hello") != -1); + } + + /** + * Read response stream from the given socket. + * @param socket + * @return + * @throws IOException + */ + private static String read(Socket socket) throws IOException { + BufferedReader reader = null; + try { + reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); + StringBuffer sb = new StringBuffer(); + String str; + while ((str = reader.readLine()) != null) { + sb.append(str); + } + return sb.toString(); + } finally { + if (reader != null) { + reader.close(); + } + } + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestBindingCacheImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestBindingCacheImpl.java new file mode 100644 index 0000000000..697f40ae69 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestBindingCacheImpl.java @@ -0,0 +1,241 @@ +/* + * 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.io.ByteArrayInputStream; +import java.io.InputStream; +import java.util.Date; + +/** + * Test service implementation that implements a various conditional HTTP + * methods. For testing, the id==0 items are very old (Date(0)), not modified, + * and always match ETags and the id==1 items are always brand new (Date()), + * modified, and never match ETags. Using these ids one can test the + * LastModified and ETag headers of the requests. + * + * @version $Rev$ $Date$ + */ +public class TestBindingCacheImpl { + + /** + * Implements the HTTP get method of the collection implementation. + * @param id + * @return + */ + public InputStream get(String id) { + return new ByteArrayInputStream( + ("

item=" + id + "").getBytes()); + } + + /** + * Implements the HTTP conditional get method of the collection implementation. + * @param id + * @return + */ + public InputStream conditionalGet(String id, RESTCacheContext cacheContext) + throws NotModifiedException, PreconditionFailedException { + + if (cacheContext != null) { + if (cacheContext.ifModifiedSince) { + if ((id.equals("1")) + && (0 > cacheContext.lastModifiedDate + .compareTo(new Date()))) + throw new NotModifiedException("item 1 was modified on " + + new Date()); + } + if (cacheContext.ifUnmodifiedSince) { + if ((id.equals("0")) + && (0 > cacheContext.lastModifiedDate + .compareTo(new Date()))) + throw new PreconditionFailedException( + "item 0 was modified on " + new Date(0)); + } + if (cacheContext.ifMatch) { + if (id.equals("1")) + throw new PreconditionFailedException( + "item 1 eTag does not match " + + cacheContext.getETag()); + } + if (cacheContext.ifNoneMatch) { + if (id.equals("0")) + throw new PreconditionFailedException( + "item 0 eTag matches " + cacheContext.getETag()); + } + } + return new ByteArrayInputStream( + ("

item=" + id + "").getBytes()); + } + + /** + * Implements the HTTP delete method of the collection implementation. + * @param id + * @return + */ + public InputStream delete(String id) { + return new ByteArrayInputStream( + ("

deleted item=" + id + "") + .getBytes()); + } + + /** + * Implements the HTTP conditional delete method of the collection implementation. + * @param id + * @return + */ + public InputStream conditionalDelete(String id, RESTCacheContext cacheContext) + throws NotModifiedException, PreconditionFailedException { + + if (cacheContext != null) { + if (cacheContext.ifModifiedSince) { + if ((id.equals("1")) + && (0 > cacheContext.lastModifiedDate + .compareTo(new Date()))) + throw new NotModifiedException("item 1 was modified on " + + new Date()); + } + if (cacheContext.ifUnmodifiedSince) { + if ((id.equals("0")) + && (0 > cacheContext.lastModifiedDate + .compareTo(new Date()))) + throw new PreconditionFailedException( + "item 0 was modified on " + new Date(0)); + } + if (cacheContext.ifMatch) { + if (id.equals("1")) + throw new PreconditionFailedException( + "item 1 eTag does not match " + + cacheContext.getETag()); + } + if (cacheContext.ifNoneMatch) { + if (id.equals("0")) + throw new PreconditionFailedException( + "item 0 eTag matches " + cacheContext.getETag()); + } + } + return new ByteArrayInputStream( + ("

deleted item=" + id + "") + .getBytes()); + } + + /** + * Implements the HTTP post method of the collection implementation. + * @param id + * @return + */ + public InputStream post() { + int id = (new java.util.Random()).nextInt(Integer.MAX_VALUE); + return new ByteArrayInputStream( + ("

posted item=" + id + "") + .getBytes()); + } + + /** + * Implements the HTTP conditional post method of the collection implementation. + * @param id + * @return + */ + public RESTCacheContext conditionalPost(RESTCacheContext cacheContext) + throws NotModifiedException, PreconditionFailedException { + String id = "" + (new java.util.Random()).nextInt(Integer.MAX_VALUE); + + if (cacheContext != null) { + if (cacheContext.ifModifiedSince) { + if (0 >= cacheContext.lastModifiedDate.compareTo(new Date(0))) + throw new NotModifiedException("item was modified on " + + new Date()); + } + if (cacheContext.ifUnmodifiedSince) { + if ((0 >= cacheContext.lastModifiedDate.compareTo(new Date(0)))) + throw new PreconditionFailedException( + "item was modified on " + new Date(0)); + } + if (cacheContext.ifMatch) { + if (cacheContext.getETag().equalsIgnoreCase("ETagNoneMatch")) + throw new PreconditionFailedException( + "item eTag does not match " + + cacheContext.getETag()); + } + if (cacheContext.ifNoneMatch) { + if (cacheContext.getETag().equalsIgnoreCase("ETagMatch")) + throw new PreconditionFailedException("item eTag matches " + + cacheContext.getETag()); + } + } + + // Return the ETag and LastModfied fields by serialize to a byte array + RESTCacheContext returnContext = new RESTCacheContext(); + returnContext.setETag( "ETag" + (new java.util.Random()).nextInt(Integer.MAX_VALUE) ); + returnContext.setLastModified( new Date() ); + return returnContext; + } + + /** + * Implements the HTTP update/put method of the collection implementation. + * @param id + * @return + */ + public InputStream put(String id) { + return new ByteArrayInputStream( + ("

updated item=" + id + "") + .getBytes()); + } + + /** + * Implements the HTTP conditional update/put method of the collection implementation. + * @param id + * @return + */ + public InputStream conditionalPut(String id, RESTCacheContext cacheContext) + throws NotModifiedException, PreconditionFailedException { + + if (cacheContext != null) { + if (cacheContext.ifModifiedSince) { + if ((id.equals("1")) + && (0 > cacheContext.lastModifiedDate + .compareTo(new Date()))) + throw new NotModifiedException("item 1 was modified on " + + new Date()); + } + if (cacheContext.ifUnmodifiedSince) { + if ((id.equals("0")) + && (0 > cacheContext.lastModifiedDate + .compareTo(new Date()))) + throw new PreconditionFailedException( + "item 0 was modified on " + new Date(0)); + } + if (cacheContext.ifMatch) { + if (id.equals("1")) + throw new PreconditionFailedException( + "item 1 eTag does not match " + + cacheContext.getETag()); + } + if (cacheContext.ifNoneMatch) { + if (id.equals("0")) + throw new PreconditionFailedException( + "item 0 eTag matches " + cacheContext.getETag()); + } + } + + return new ByteArrayInputStream( + ("

updated item=" + id + "") + .getBytes()); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestGetImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestGetImpl.java new file mode 100644 index 0000000000..4cb1bce73f --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestGetImpl.java @@ -0,0 +1,37 @@ +/* + * 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.io.ByteArrayInputStream; +import java.io.InputStream; + +/** + * Test service implementation that implements a get method. + * + * @version $Rev$ $Date$ + */ +public class TestGetImpl { + + public InputStream get(String id) { + return new ByteArrayInputStream(("

item=" + id + "").getBytes()); + + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceCacheImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceCacheImpl.java new file mode 100644 index 0000000000..e97a107c65 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceCacheImpl.java @@ -0,0 +1,60 @@ +/* + * 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.io.IOException; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import org.oasisopen.sca.annotation.Service; + +/** + * Test service implementation. + * + * @version $Rev$ $Date$ + */ + +@Service(Servlet.class) +public class TestServiceCacheImpl implements Servlet { + + public void init(ServletConfig config) throws ServletException { + } + + public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { + //HttpServletResponse httpResponse = (HttpServletResponse)response; + response.getOutputStream().print("

hey"); + } + + public void destroy() { + } + + public ServletConfig getServletConfig() { + return null; + } + + public String getServletInfo() { + return null; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceImpl.java new file mode 100644 index 0000000000..6b21d6094f --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/TestServiceImpl.java @@ -0,0 +1,60 @@ +/* + * 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.io.IOException; + +import javax.servlet.Servlet; +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import org.oasisopen.sca.annotation.Service; + +/** + * Test service implementation. + * + * @version $Rev$ $Date$ + */ + +@Service(Servlet.class) +public class TestServiceImpl implements Servlet { + + public void init(ServletConfig config) throws ServletException { + } + + public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { + //HttpServletResponse httpResponse = (HttpServletResponse)response; + response.getOutputStream().print("

hey"); + } + + public void destroy() { + } + + public ServletConfig getServletConfig() { + return null; + } + + public String getServletInfo() { + return null; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite index e3525f4afd..9fbce7c427 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/test.composite @@ -26,21 +26,21 @@ - + - + - + - + diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/testCache.composite b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/testCache.composite index 4923cfa74d..3401608c51 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/testCache.composite +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/testCache.composite @@ -24,9 +24,9 @@ name="testCache"> - + - + -- cgit v1.2.3