summaryrefslogtreecommitdiffstats
path: root/java/sca/modules/binding-http-runtime
diff options
context:
space:
mode:
authorlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2008-11-19 05:27:58 +0000
committerlresende <lresende@13f79535-47bb-0310-9956-ffa450edef68>2008-11-19 05:27:58 +0000
commit5f3869c451e46aadc943d00087d6847877dd1c50 (patch)
treee22baaff1fb9ea42606b7d04af52e032e3bc03bc /java/sca/modules/binding-http-runtime
parent60744a36aae604ac3c4499ed54f1082ab8f5947d (diff)
Merging the 1.x delta on top of the equinox based modules
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@718858 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'java/sca/modules/binding-http-runtime')
-rw-r--r--java/sca/modules/binding-http-runtime/pom.xml6
-rw-r--r--java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java317
-rw-r--r--java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java6
-rw-r--r--java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java60
-rw-r--r--java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/util/HTTPHeadersParser.java50
-rw-r--r--java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java1647
-rw-r--r--java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java10
7 files changed, 1131 insertions, 965 deletions
diff --git a/java/sca/modules/binding-http-runtime/pom.xml b/java/sca/modules/binding-http-runtime/pom.xml
index d007603827..271eef0f89 100644
--- a/java/sca/modules/binding-http-runtime/pom.xml
+++ b/java/sca/modules/binding-http-runtime/pom.xml
@@ -38,6 +38,12 @@
<dependency>
<groupId>org.apache.tuscany.sca</groupId>
+ <artifactId>tuscany-policy-security</artifactId>
+ <version>1.4-SNAPSHOT</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.tuscany.sca</groupId>
<artifactId>tuscany-core-spi</artifactId>
<version>1.4-EQUINOX-SNAPSHOT</version>
</dependency>
diff --git a/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java
index e969749358..084f52026f 100644
--- a/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java
+++ b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPBindingListenerServlet.java
@@ -24,16 +24,25 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.ParseException;
+import java.util.List;
import javax.servlet.ServletException;
+import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.xml.namespace.QName;
-import org.apache.tuscany.sca.binding.http.CacheContext;
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.binding.http.HTTPCacheContext;
+import org.apache.tuscany.sca.binding.http.util.HTTPHeadersParser;
import org.apache.tuscany.sca.invocation.Invoker;
import org.apache.tuscany.sca.invocation.Message;
import org.apache.tuscany.sca.invocation.MessageFactory;
+import org.apache.tuscany.sca.policy.Intent;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+import org.apache.tuscany.sca.policy.authentication.basic.BasicAuthenticationPolicy;
/**
* Servlet responsible for dispatching HTTP requests to the
@@ -44,6 +53,13 @@ import org.apache.tuscany.sca.invocation.MessageFactory;
public class HTTPBindingListenerServlet extends HttpServlet {
private static final long serialVersionUID = 2865466417329430610L;
+ private static final QName AUTEHTICATION_INTENT = new QName("http://www.osoa.org/xmlns/sca/1.0","authentication");
+
+ transient private Binding binding;
+
+ transient private boolean requiresAuthentication = false;
+ transient private BasicAuthenticationPolicy basicAuthenticationPolicy = null;
+
private MessageFactory messageFactory;
private Invoker getInvoker;
private Invoker conditionalGetInvoker;
@@ -57,12 +73,49 @@ public class HTTPBindingListenerServlet extends HttpServlet {
/**
* Constructs a new HTTPServiceListenerServlet.
*/
- public HTTPBindingListenerServlet(MessageFactory messageFactory) {
+ public HTTPBindingListenerServlet(Binding binding, MessageFactory messageFactory) {
+ this.binding = binding;
this.messageFactory = messageFactory;
+
+
+ // find out which policies are active
+ if (binding instanceof PolicySetAttachPoint) {
+ List<Intent> intents = ((PolicySetAttachPoint)binding).getRequiredIntents();
+ for(Intent intent : intents) {
+ if(intent.getName().equals(AUTEHTICATION_INTENT)) {
+ requiresAuthentication = true;
+ }
+ }
+
+
+ List<PolicySet> policySets = ((PolicySetAttachPoint)binding).getApplicablePolicySets();
+ for (PolicySet ps : policySets) {
+ for (Object p : ps.getPolicies()) {
+ if (BasicAuthenticationPolicy.class.isInstance(p)) {
+ basicAuthenticationPolicy = (BasicAuthenticationPolicy)p;
+ } else {
+ // etc. check for other types of policy being present
+ }
+ }
+ }
+ }
}
@Override
+ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+ if(requiresAuthentication) {
+ if(! hasAuthenticationHeader(request, response)) {
+ response.setHeader("WWW-Authenticate", "BASIC realm=\"Tuscany\"");
+ response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+
+ 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");
@@ -75,12 +128,16 @@ public class HTTPBindingListenerServlet extends HttpServlet {
// Invoke the get operation on the service implementation
Message requestMessage = messageFactory.createMessage();
+
+ //store http headers to message
+ requestMessage.getHeaders().addAll(HTTPHeadersParser.getHeaders(request));
+
String id = path.substring(1);
Message responseMessage = null;
- CacheContext cacheContext = null;
+ HTTPCacheContext cacheContext = null;
try {
- cacheContext = CacheContext.getCacheContextFromRequest(request);
+ cacheContext = HTTPCacheContext.getCacheContextFromRequest(request);
} catch (ParseException e) {
}
@@ -144,9 +201,9 @@ public class HTTPBindingListenerServlet extends HttpServlet {
String id = path.substring(1);
Message responseMessage = null;
- CacheContext cacheContext = null;
+ HTTPCacheContext cacheContext = null;
try {
- cacheContext = CacheContext.getCacheContextFromRequest(request);
+ cacheContext = HTTPCacheContext.getCacheContextFromRequest(request);
} catch (ParseException e) {
}
@@ -210,9 +267,9 @@ public class HTTPBindingListenerServlet extends HttpServlet {
String id = path.substring(1);
Message responseMessage = null;
- CacheContext cacheContext = null;
+ HTTPCacheContext cacheContext = null;
try {
- cacheContext = CacheContext.getCacheContextFromRequest(request);
+ cacheContext = HTTPCacheContext.getCacheContextFromRequest(request);
} catch (ParseException e) {
}
@@ -276,9 +333,9 @@ public class HTTPBindingListenerServlet extends HttpServlet {
// String id = path.substring(1);
Message responseMessage = null;
- CacheContext cacheContext = null;
+ HTTPCacheContext cacheContext = null;
try {
- cacheContext = CacheContext.getCacheContextFromRequest(request);
+ cacheContext = HTTPCacheContext.getCacheContextFromRequest(request);
} catch (ParseException e) {
}
@@ -314,9 +371,9 @@ public class HTTPBindingListenerServlet extends HttpServlet {
// Test if the ETag and LastModified are returned as a cache context.
Object body = responseMessage.getBody();
- if ( body.getClass() == CacheContext.class ) {
+ if ( body.getClass() == HTTPCacheContext.class ) {
// Transfer to header if so.
- CacheContext cc = (CacheContext)responseMessage.getBody();
+ HTTPCacheContext cc = (HTTPCacheContext)responseMessage.getBody();
if (( cc != null ) && ( cc.isEnabled() )) {
String eTag = cc.getETag();
if ( eTag != null )
@@ -328,116 +385,132 @@ public class HTTPBindingListenerServlet extends HttpServlet {
}
}
- /**
- * @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 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;
+ }
+
+
+ /**
+ * Utility Methods related to Policy
+ */
- /**
- * @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;
- }
+
+ private boolean hasAuthenticationHeader(HttpServletRequest request, ServletResponse response) {
+ boolean result = false;
+ if(request.getHeader("Authorization") != null) {
+ result = true;
+ }
+
+ return result;
+ }
+
}
diff --git a/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java
index 0a8f74edca..8f91ff4343 100644
--- a/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java
+++ b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceBindingProvider.java
@@ -39,10 +39,10 @@ import org.apache.tuscany.sca.runtime.RuntimeWire;
* @version $Rev$ $Date$
*/
public class HTTPServiceBindingProvider implements ServiceBindingProvider {
-
private RuntimeComponentService service;
private HTTPBinding binding;
private MessageFactory messageFactory;
+
private ServletHost servletHost;
private String servletMapping;
private HTTPBindingListenerServlet bindingListenerServlet;
@@ -63,7 +63,7 @@ public class HTTPServiceBindingProvider implements ServiceBindingProvider {
RuntimeComponentService componentService = (RuntimeComponentService) service;
RuntimeWire wire = componentService.getRuntimeWire(binding);
Servlet servlet = null;
- bindingListenerServlet = new HTTPBindingListenerServlet( messageFactory );
+ bindingListenerServlet = new HTTPBindingListenerServlet(binding, messageFactory );
for (InvocationChain invocationChain : wire.getInvocationChains()) {
Operation operation = invocationChain.getTargetOperation();
String operationName = operation.getName();
@@ -101,7 +101,7 @@ public class HTTPServiceBindingProvider implements ServiceBindingProvider {
servlet = bindingListenerServlet;
} else if (operationName.equals("service")) {
Invoker serviceInvoker = invocationChain.getHeadInvoker();
- servlet = new HTTPServiceListenerServlet(serviceInvoker, messageFactory);
+ servlet = new HTTPServiceListenerServlet(binding, serviceInvoker, messageFactory);
break;
}
}
diff --git a/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java
index 03b91c4db2..493255655d 100644
--- a/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java
+++ b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/provider/HTTPServiceListenerServlet.java
@@ -20,17 +20,25 @@
package org.apache.tuscany.sca.binding.http.provider;
import java.io.IOException;
+import java.util.List;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.xml.namespace.QName;
+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;
+import org.apache.tuscany.sca.policy.Intent;
+import org.apache.tuscany.sca.policy.PolicySet;
+import org.apache.tuscany.sca.policy.PolicySetAttachPoint;
+import org.apache.tuscany.sca.policy.authentication.basic.BasicAuthenticationPolicy;
/**
* Servlet responsible for dispatching HTTP service requests to the
@@ -39,17 +47,45 @@ import org.apache.tuscany.sca.invocation.MessageFactory;
* @version $Rev$ $Date$
*/
public class HTTPServiceListenerServlet implements Servlet {
+ private static final QName AUTEHTICATION_INTENT = new QName("http://www.osoa.org/xmlns/sca/1.0","authentication");
- private ServletConfig config;
- private MessageFactory messageFactory;
- private Invoker serviceInvoker;
+ transient private Binding binding;
+ transient private ServletConfig config;
+ transient private MessageFactory messageFactory;
+ transient private Invoker serviceInvoker;
+ transient private boolean requiresAuthentication = false;
+ transient private BasicAuthenticationPolicy basicAuthenticationPolicy = null;
+
/**
* Constructs a new HTTPServiceListenerServlet.
*/
- public HTTPServiceListenerServlet(Invoker serviceInvoker, MessageFactory messageFactory) {
+ public HTTPServiceListenerServlet(Binding binding, Invoker serviceInvoker, MessageFactory messageFactory) {
+ this.binding = binding;
this.serviceInvoker = serviceInvoker;
this.messageFactory = messageFactory;
+
+ // find out which policies are active
+ if (binding instanceof PolicySetAttachPoint) {
+ List<Intent> intents = ((PolicySetAttachPoint)binding).getRequiredIntents();
+ for(Intent intent : intents) {
+ if(intent.getName().equals(AUTEHTICATION_INTENT)) {
+ requiresAuthentication = true;
+ }
+ }
+
+
+ List<PolicySet> policySets = ((PolicySetAttachPoint)binding).getApplicablePolicySets();
+ for (PolicySet ps : policySets) {
+ for (Object p : ps.getPolicies()) {
+ if (BasicAuthenticationPolicy.class.isInstance(p)) {
+ basicAuthenticationPolicy = (BasicAuthenticationPolicy)p;
+ } else {
+ // etc. check for other types of policy being present
+ }
+ }
+ }
+ }
}
public ServletConfig getServletConfig() {
@@ -65,10 +101,17 @@ public class HTTPServiceListenerServlet implements Servlet {
}
public void destroy() {
+
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
+ if(requiresAuthentication) {
+ if(! hasAuthenticationHeader((HttpServletRequest)request, (HttpServletResponse)response)) {
+ ((HttpServletResponse)response).sendError(HttpServletResponse.SC_UNAUTHORIZED);
+ }
+ }
+
// Dispatch the service interaction to the service invoker
Message requestMessage = messageFactory.createMessage();
requestMessage.setBody(new Object[]{request, response});
@@ -81,4 +124,13 @@ public class HTTPServiceListenerServlet implements Servlet {
}
}
+
+ private boolean hasAuthenticationHeader(HttpServletRequest request, ServletResponse response) {
+ boolean result = false;
+ if(request.getHeader("Authorization") != null) {
+ result = true;
+ }
+
+ return result;
+ }
}
diff --git a/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/util/HTTPHeadersParser.java b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/util/HTTPHeadersParser.java
new file mode 100644
index 0000000000..1fe4600e31
--- /dev/null
+++ b/java/sca/modules/binding-http-runtime/src/main/java/org/apache/tuscany/sca/binding/http/util/HTTPHeadersParser.java
@@ -0,0 +1,50 @@
+/*
+ * 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.util;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.tuscany.sca.binding.http.HTTPHeader;
+
+public class HTTPHeadersParser {
+
+ /**
+ * Parse http request headers to a map
+ *
+ * @param request
+ * @return
+ */
+ public static List<HTTPHeader> getHeaders(HttpServletRequest request) {
+ List<HTTPHeader> headers = new ArrayList<HTTPHeader>();
+
+ Enumeration<?> headerNames = request.getHeaderNames();
+ while (headerNames.hasMoreElements()) {
+ String headerName = (String) headerNames.nextElement();
+ Object headerValue = request.getHeader(headerName);
+ HTTPHeader header = new HTTPHeader(headerName, headerValue);
+ headers.add(header);
+ }
+ return headers;
+ }
+}
diff --git a/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java
index 50022e9663..bb5f6d5a8b 100644
--- a/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java
+++ b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/HTTPBindingCacheTestCase.java
@@ -29,10 +29,7 @@ import java.util.Date;
import junit.framework.TestCase;
-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.apache.tuscany.sca.host.embedded.SCADomain;
/**
* HTTP binding unit tests.
@@ -40,831 +37,819 @@ import org.apache.tuscany.sca.node.NodeFactory;
* @version $Rev$ $Date$
*/
public class HTTPBindingCacheTestCase extends TestCase {
- // 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 Node node;
-
- @Override
- protected void setUp() throws Exception {
- String contribution = ContributionLocationHelper.getContributionLocation(HTTPBindingCacheTestCase.class);
- node = NodeFactory.newInstance().createNode("testCache.composite", new Contribution("test", contribution));
- node.start();
- }
-
- @Override
- protected void tearDown() throws Exception {
- node.stop();
- node.destroy();
- }
-
- /**
- * Test invoking a POJO get method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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);
- assertTrue(document.indexOf("<body><p>item=" + index) != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- assertTrue(document.indexOf("<body><p>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
- */
- 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("<body><p>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
- */
- 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
- assertTrue(document.indexOf("<body><p>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
- */
- 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("<body><p>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
- */
- 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("<body><p>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
- */
- 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
- assertTrue(document.indexOf("<body><p>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
- */
- 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
- assertTrue(document.indexOf("<body><p>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
- */
- 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("<body><p>item=" + index) != -1);
- // Should return code 412 PreconditionFailed.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a POJO get method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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);
- assertTrue(document.indexOf("deleted item=" + index) != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 304") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a POJO get method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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);
- assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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);
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 304") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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.
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a POJO get method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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);
- assertTrue(document.indexOf("updated item=" + index) != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 304") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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.
- assertTrue(document.indexOf("HTTP/1.1 412") != -1);
- }
-
- /**
- * Test invoking a conditional method implementation using the HTTP binding.
- *
- * @throws Exception
- */
- 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
- 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
- */
- 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
- 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
- */
- 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.
- 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();
- }
- }
- }
+ // 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 SCADomain scaDomain;
+
+ @Override
+ protected void setUp() throws Exception {
+ scaDomain = SCADomain.newInstance("testCache.composite");
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ scaDomain.close();
+ }
+
+ /**
+ * Test invoking a POJO get method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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);
+ assertTrue(document.indexOf("<body><p>item=" + index) != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ assertTrue(document.indexOf("<body><p>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
+ */
+ 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("<body><p>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
+ */
+ 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
+ assertTrue(document.indexOf("<body><p>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
+ */
+ 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("<body><p>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
+ */
+ 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("<body><p>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
+ */
+ 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
+ assertTrue(document.indexOf("<body><p>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
+ */
+ 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
+ assertTrue(document.indexOf("<body><p>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
+ */
+ 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("<body><p>item=" + index) != -1);
+ // Should return code 412 PreconditionFailed.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a POJO get method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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);
+ assertTrue(document.indexOf("deleted item=" + index) != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 304") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a POJO get method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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);
+ assertTrue(document.indexOf("HTTP/1.1 200 OK") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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);
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 304") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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.
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a POJO get method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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);
+ assertTrue(document.indexOf("updated item=" + index) != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 304") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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.
+ assertTrue(document.indexOf("HTTP/1.1 412") != -1);
+ }
+
+ /**
+ * Test invoking a conditional method implementation using the HTTP binding.
+ * @throws Exception
+ */
+ 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
+ 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
+ */
+ 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
+ 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
+ */
+ 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.
+ 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/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java
index a3fce376f3..f2456f3175 100644
--- a/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java
+++ b/java/sca/modules/binding-http-runtime/src/test/java/org/apache/tuscany/sca/binding/http/TestBindingCacheImpl.java
@@ -49,7 +49,7 @@ public class TestBindingCacheImpl {
* @param id
* @return
*/
- public InputStream conditionalGet(String id, CacheContext cacheContext)
+ public InputStream conditionalGet(String id, HTTPCacheContext cacheContext)
throws NotModifiedException, PreconditionFailedException {
if (cacheContext != null) {
@@ -99,7 +99,7 @@ public class TestBindingCacheImpl {
* @param id
* @return
*/
- public InputStream conditionalDelete(String id, CacheContext cacheContext)
+ public InputStream conditionalDelete(String id, HTTPCacheContext cacheContext)
throws NotModifiedException, PreconditionFailedException {
if (cacheContext != null) {
@@ -151,7 +151,7 @@ public class TestBindingCacheImpl {
* @param id
* @return
*/
- public CacheContext conditionalPost(CacheContext cacheContext)
+ public HTTPCacheContext conditionalPost(HTTPCacheContext cacheContext)
throws NotModifiedException, PreconditionFailedException {
String id = "" + (new java.util.Random()).nextInt(Integer.MAX_VALUE);
@@ -180,7 +180,7 @@ public class TestBindingCacheImpl {
}
// Return the ETag and LastModfied fields by serialize to a byte array
- CacheContext returnContext = new CacheContext();
+ HTTPCacheContext returnContext = new HTTPCacheContext();
returnContext.setETag( "ETag" + (new java.util.Random()).nextInt(Integer.MAX_VALUE) );
returnContext.setLastModified( new Date() );
return returnContext;
@@ -202,7 +202,7 @@ public class TestBindingCacheImpl {
* @param id
* @return
*/
- public InputStream conditionalPut(String id, CacheContext cacheContext)
+ public InputStream conditionalPut(String id, HTTPCacheContext cacheContext)
throws NotModifiedException, PreconditionFailedException {
if (cacheContext != null) {