You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2006/11/10 07:44:45 UTC
svn commit: r473207 -
/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/
Author: gnodet
Date: Thu Nov 9 22:44:44 2006
New Revision: 473207
URL: http://svn.apache.org/viewvc?view=rev&rev=473207
Log:
Commit work in progress on refactored endpoints in servicemix-http to allow custom marshalers to be used on endpoints (for rest support for example)
Added:
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/DefaultHttpConsumerMarshaler.java
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerEndpoint.java
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerMarshaler.java
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderEndpoint.java
incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderMarshaler.java
Added: incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/DefaultHttpConsumerMarshaler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/DefaultHttpConsumerMarshaler.java?view=auto&rev=473207
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/DefaultHttpConsumerMarshaler.java (added)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/DefaultHttpConsumerMarshaler.java Thu Nov 9 22:44:44 2006
@@ -0,0 +1,68 @@
+/*
+ * 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.servicemix.http.endpoints;
+
+import java.net.URI;
+
+import javax.jbi.component.ComponentContext;
+import javax.jbi.messaging.Fault;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.servicemix.jbi.FaultException;
+import org.apache.servicemix.jbi.messaging.MessageExchangeSupport;
+
+public class DefaultHttpConsumerMarshaler implements HttpConsumerMarshaler {
+
+ private URI defaultMep = MessageExchangeSupport.IN_OUT;
+
+ public URI getDefaultMep() {
+ return defaultMep;
+ }
+
+ public void setDefaultMep(URI defaultMep) {
+ this.defaultMep = defaultMep;
+ }
+
+ public MessageExchange createExchange(HttpServletRequest request, ComponentContext context) throws Exception {
+ MessageExchange me;
+ me = context.getDeliveryChannel().createExchangeFactory().createExchange(getDefaultMep());
+ NormalizedMessage in = me.createMessage();
+ me.setMessage(in, "in");
+ return me;
+ }
+
+ public void sendOut(MessageExchange exchange, NormalizedMessage outMsg, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+ public void sendFault(MessageExchange exchange, Fault fault, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ throw new FaultException("Fault occured", exchange, fault);
+ }
+
+ public void sendError(MessageExchange exchange, Exception error, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ throw error;
+ }
+
+ public void sendAccepted(MessageExchange exchange, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ response.setStatus(HttpServletResponse.SC_ACCEPTED);
+ }
+
+}
Added: incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerEndpoint.java?view=auto&rev=473207
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerEndpoint.java (added)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerEndpoint.java Thu Nov 9 22:44:44 2006
@@ -0,0 +1,341 @@
+/*
+ * 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.servicemix.http.endpoints;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.jbi.messaging.ExchangeStatus;
+import javax.jbi.messaging.Fault;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.xml.namespace.QName;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.stream.StreamResult;
+
+import org.apache.servicemix.common.DefaultComponent;
+import org.apache.servicemix.common.ExchangeProcessor;
+import org.apache.servicemix.common.ServiceUnit;
+import org.apache.servicemix.common.endpoints.ConsumerEndpoint;
+import org.apache.servicemix.http.ContextManager;
+import org.apache.servicemix.http.HttpComponent;
+import org.apache.servicemix.http.HttpProcessor;
+import org.apache.servicemix.http.SslParameters;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.mortbay.jetty.RetryRequest;
+import org.mortbay.util.ajax.Continuation;
+import org.mortbay.util.ajax.ContinuationSupport;
+import org.w3c.dom.Node;
+
+/**
+ *
+ * @author gnodet
+ * @org.apache.xbean.XBean element="consumer"
+ */
+public class HttpConsumerEndpoint extends ConsumerEndpoint implements ExchangeProcessor, HttpProcessor {
+
+ private String authMethod;
+ private SslParameters ssl;
+ private String locationUri;
+ private HttpConsumerMarshaler marshaler = new DefaultHttpConsumerMarshaler();
+ private long timeout = 0; // 60s
+
+ private Map resources = new HashMap();
+ private Map locks;
+ private Map exchanges;
+ private Object httpContext;
+
+ public HttpConsumerEndpoint() {
+ super();
+ }
+
+ public HttpConsumerEndpoint(DefaultComponent component, ServiceEndpoint endpoint) {
+ super(component, endpoint);
+ }
+
+ public HttpConsumerEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
+ super(serviceUnit, service, endpoint);
+ }
+
+ /**
+ * @return the locationUri
+ */
+ public String getLocationURI() {
+ return locationUri;
+ }
+
+ /**
+ * @param locationUri the locationUri to set
+ */
+ public void setLocationURI(String locationUri) {
+ this.locationUri = locationUri;
+ }
+
+ /**
+ * @return the timeout
+ */
+ public long getTimeout() {
+ return timeout;
+ }
+
+ /**
+ * @param timeout the timeout to set
+ */
+ public void setTimeout(long timeout) {
+ this.timeout = timeout;
+ }
+
+ /**
+ * @return the marshaler
+ */
+ public HttpConsumerMarshaler getMarshaler() {
+ return marshaler;
+ }
+
+ /**
+ * @param marshaler the marshaler to set
+ */
+ public void setMarshaler(HttpConsumerMarshaler marshaler) {
+ this.marshaler = marshaler;
+ }
+
+ /**
+ * @return the authMethod
+ */
+ public String getAuthMethod() {
+ return authMethod;
+ }
+
+ /**
+ * @param authMethod the authMethod to set
+ */
+ public void setAuthMethod(String authMethod) {
+ this.authMethod = authMethod;
+ }
+
+ /**
+ * @return the sslParameters
+ */
+ public SslParameters getSsl() {
+ return ssl;
+ }
+
+ /**
+ * @param ssl the sslParameters to set
+ */
+ public void setSsl(SslParameters ssl) {
+ this.ssl = ssl;
+ }
+
+ public void start() throws Exception {
+ super.start();
+ loadStaticResources();
+ httpContext = getServerManager().createContext(locationUri, this);
+ }
+
+ public void stop() throws Exception {
+ getServerManager().remove(httpContext);
+ httpContext = null;
+ super.stop();
+ }
+
+ public void process(MessageExchange exchange) throws Exception {
+ Continuation cont = (Continuation) locks.remove(exchange.getExchangeId());
+ if (cont == null) {
+ throw new Exception("HTTP request has timed out");
+ }
+ synchronized (cont) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Resuming continuation for exchange: " + exchange.getExchangeId());
+ }
+ exchanges.put(exchange.getExchangeId(), exchange);
+ cont.resume();
+ }
+ }
+
+ public void process(HttpServletRequest request, HttpServletResponse response) throws Exception {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Receiving HTTP request: " + request);
+ }
+ MessageExchange exchange = null;
+ try {
+ // Handle WSDLs, XSDs
+ if (handleStaticResource(request, response)) {
+ return;
+ }
+ // Not giving a specific mutex will synchronize on the contination itself
+ Continuation cont = ContinuationSupport.getContinuation(request, null);
+ // If the continuation is not a retry
+ if (!cont.isPending()) {
+ exchange = createExchange(request);
+ locks.put(exchange.getExchangeId(), cont);
+ request.setAttribute(MessageExchange.class.getName(), exchange.getExchangeId());
+ synchronized (cont) {
+ send(exchange);
+ if (exchanges.remove(exchange.getExchangeId()) == null) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("Suspending continuation for exchange: " + exchange.getExchangeId());
+ }
+ long timeout = this.timeout;
+ if (timeout == 0) {
+ timeout = ((HttpComponent) getServiceUnit().getComponent()).getConfiguration().getConsumerProcessorSuspendTime();
+ }
+ boolean result = cont.suspend(timeout);
+ if (!result) {
+ locks.remove(exchange.getExchangeId());
+ throw new Exception("Exchange timed out");
+ }
+ }
+ request.removeAttribute(MessageExchange.class.getName());
+ }
+ return;
+ } else {
+ String id = (String) request.getAttribute(MessageExchange.class.getName());
+ locks.remove(id);
+ exchange = (MessageExchange) exchanges.remove(id);
+ request.removeAttribute(MessageExchange.class.getName());
+ boolean result = cont.suspend(0);
+ // Check if this is a timeout
+ if (exchange == null) {
+ throw new IllegalStateException("Exchange not found");
+ }
+ if (!result) {
+ throw new Exception("Timeout");
+ }
+ }
+ if (exchange.getStatus() == ExchangeStatus.ERROR) {
+ Exception e = exchange.getError();
+ if (e == null) {
+ e = new Exception("Unkown error (exchange aborted ?)");
+ }
+ throw e;
+ } else if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
+ try {
+ Fault fault = exchange.getFault();
+ if (fault != null) {
+ sendFault(exchange, fault, request, response);
+ } else {
+ NormalizedMessage outMsg = exchange.getMessage("out");
+ if (outMsg != null) {
+ sendOut(exchange, outMsg, request, response);
+ }
+ }
+ exchange.setStatus(ExchangeStatus.DONE);
+ send(exchange);
+ } catch (Exception e) {
+ exchange.setError(e);
+ send(exchange);
+ }
+ } else if (exchange.getStatus() == ExchangeStatus.DONE) {
+ // This happens when there is no response to send back
+ sendAccepted(exchange, request, response);
+ }
+ } catch (RetryRequest e) {
+ throw e;
+ } catch (Exception e) {
+ sendError(exchange, e, request, response);
+ }
+ }
+
+ protected void loadStaticResources() {
+ // TODO: load wsdl
+ }
+
+ /**
+ * Handle static resources
+ *
+ * @param request the http request
+ * @param response the http response
+ * @return <code>true</code> if the request has been handled
+ * @throws IOException
+ * @throws ServletException
+ */
+ protected boolean handleStaticResource(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
+ if (!"GET".equals(request.getMethod())) {
+ return false;
+ }
+ String query = request.getQueryString();
+ if (query != null && query.trim().equalsIgnoreCase("wsdl") && getResource("main.wsdl") != null) {
+ String uri = request.getRequestURI();
+ if (!uri.endsWith("/")) {
+ uri += "/";
+ }
+ uri += "main.wsdl";
+ response.sendRedirect(uri);
+ return true;
+ }
+ String path = request.getPathInfo();
+ if (path.lastIndexOf('/') >= 0) {
+ path = path.substring(path.lastIndexOf('/') + 1);
+ }
+ Object res = getResource(path);
+ if (res == null) {
+ return false;
+ }
+ if (res instanceof Node) {
+ response.setStatus(200);
+ response.setContentType("text/xml");
+ try {
+ new SourceTransformer().toResult(new DOMSource((Node) res), new StreamResult(response.getOutputStream()));
+ } catch (TransformerException e) {
+ throw new ServletException("Error while sending xml resource", e);
+ }
+ } else if (res != null) {
+ // TODO: handle other static resources ...
+ throw new ServletException("Unable to serialize resource");
+ } else {
+ return false;
+ }
+ return true;
+ }
+
+ protected Object getResource(String path) {
+ return resources.get(path);
+ }
+
+ protected ContextManager getServerManager() {
+ HttpComponent comp = (HttpComponent) getServiceUnit().getComponent();
+ return comp.getServer();
+ }
+
+ public MessageExchange createExchange(HttpServletRequest request) throws Exception {
+ return marshaler.createExchange(request, getContext());
+ }
+
+ public void sendAccepted(MessageExchange exchange, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ marshaler.sendAccepted(exchange, request, response);
+ }
+
+ public void sendError(MessageExchange exchange, Exception error, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ marshaler.sendError(exchange, error, request, response);
+ }
+
+ public void sendFault(MessageExchange exchange, Fault fault, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ marshaler.sendFault(exchange, fault, request, response);
+ }
+
+ public void sendOut(MessageExchange exchange, NormalizedMessage outMsg, HttpServletRequest request, HttpServletResponse response) throws Exception {
+ marshaler.sendOut(exchange, outMsg, request, response);
+ }
+
+}
Added: incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerMarshaler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerMarshaler.java?view=auto&rev=473207
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerMarshaler.java (added)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpConsumerMarshaler.java Thu Nov 9 22:44:44 2006
@@ -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.servicemix.http.endpoints;
+
+import javax.jbi.component.ComponentContext;
+import javax.jbi.messaging.Fault;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+public interface HttpConsumerMarshaler {
+
+ MessageExchange createExchange(HttpServletRequest request,
+ ComponentContext context) throws Exception;
+
+ void sendOut(MessageExchange exchange,
+ NormalizedMessage outMsg,
+ HttpServletRequest request,
+ HttpServletResponse response) throws Exception;
+
+ void sendFault(MessageExchange exchange,
+ Fault fault,
+ HttpServletRequest request,
+ HttpServletResponse response) throws Exception;
+
+ void sendError(MessageExchange exchange,
+ Exception error,
+ HttpServletRequest request,
+ HttpServletResponse response) throws Exception;
+
+ void sendAccepted(MessageExchange exchange,
+ HttpServletRequest request,
+ HttpServletResponse response) throws Exception;
+
+}
Added: incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderEndpoint.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderEndpoint.java?view=auto&rev=473207
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderEndpoint.java (added)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderEndpoint.java Thu Nov 9 22:44:44 2006
@@ -0,0 +1,63 @@
+/*
+ * 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.servicemix.http.endpoints;
+
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.servicedesc.ServiceEndpoint;
+import javax.xml.namespace.QName;
+
+import org.apache.servicemix.common.DefaultComponent;
+import org.apache.servicemix.common.ExchangeProcessor;
+import org.apache.servicemix.common.ServiceUnit;
+import org.apache.servicemix.common.endpoints.ProviderEndpoint;
+import org.apache.servicemix.http.BasicAuthCredentials;
+
+/**
+ *
+ * @author gnodet
+ * @org.apache.xbean.XBean element="provider"
+ */
+public class HttpProviderEndpoint extends ProviderEndpoint implements ExchangeProcessor {
+
+ private BasicAuthCredentials basicAuthentication;
+
+ public HttpProviderEndpoint() {
+ super();
+ }
+
+ public HttpProviderEndpoint(DefaultComponent component, ServiceEndpoint endpoint) {
+ super(component, endpoint);
+ }
+
+ public HttpProviderEndpoint(ServiceUnit serviceUnit, QName service, String endpoint) {
+ super(serviceUnit, service, endpoint);
+ }
+
+ public BasicAuthCredentials getBasicAuthentication() {
+ return basicAuthentication;
+ }
+
+ public void setBasicAuthentication(BasicAuthCredentials basicAuthentication) {
+ this.basicAuthentication = basicAuthentication;
+ }
+
+ public void process(MessageExchange exchange) throws Exception {
+ // TODO Auto-generated method stub
+
+ }
+
+}
Added: incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderMarshaler.java
URL: http://svn.apache.org/viewvc/incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderMarshaler.java?view=auto&rev=473207
==============================================================================
--- incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderMarshaler.java (added)
+++ incubator/servicemix/trunk/deployables/bindingcomponents/servicemix-http/src/main/java/org/apache/servicemix/http/endpoints/HttpProviderMarshaler.java Thu Nov 9 22:44:44 2006
@@ -0,0 +1,21 @@
+/*
+ * 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.servicemix.http.endpoints;
+
+public interface HttpProviderMarshaler {
+
+}