| Commit in servicemix/base/src/main/java/org/servicemix/components/http on MAIN | |||
| SpringBindingServlet.java | +66 | added 1.1 | |
| BindingServlet.java | +2 | -2 | 1.3 -> 1.4 |
| HttpMarshaler.java | +32 | -7 | 1.3 -> 1.4 |
| +100 | -9 | ||
* switched to using application/xml content type by default for Http responses. * added a SpringBindingServlet to make it easy to deploy ServiceMix HTTP Binding in a WAR
servicemix/base/src/main/java/org/servicemix/components/http
SpringBindingServlet.java added at 1.1
diff -N SpringBindingServlet.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ SpringBindingServlet.java 20 Sep 2005 14:39:29 -0000 1.1 @@ -0,0 +1,66 @@
+/**
+ *
+ * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
+ *
+ * Licensed 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.servicemix.components.http;
+
+import org.servicemix.jbi.container.SpringJBIContainer;
+import org.springframework.context.ApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+
+/**
+ * A Spring based HTTP Binding Servlet for ServiceMix which will
+ * lookup the JBI container and the HttpBinding to use from the Spring
+ * [EMAIL PROTECTED] ApplicationContext}
+ *
+ * @version $Revision$
+ */
+public class SpringBindingServlet extends BindingServlet {
+
+ protected HttpBinding createHttpBinding(ServletConfig config) throws ServletException {
+ ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
+
+ String jbiName = config.getInitParameter("jbi");
+ if (jbiName == null) {
+ jbiName = "jbi";
+ }
+ String endpointName = config.getInitParameter("endpoint");
+ if (endpointName == null) {
+ throw new ServletException("You must configure a servlet config parameter of endpointRef");
+ }
+
+ SpringJBIContainer jbi = (SpringJBIContainer) applicationContext.getBean(jbiName);
+ if (jbi == null) {
+ throw new ServletException("Could not find the JBIContainer in the Spring application context for name: " + jbiName);
+ }
+
+ Object value = jbi.getBean(endpointName);
+ if (value == null) {
+ throw new ServletException("Could not find bean in the SpringJBIContainer for id: " + endpointName);
+ }
+ if (value instanceof HttpBinding) {
+ return (HttpBinding) value;
+ }
+ else {
+ throw new ServletException("The endpoint is not a HttpBinding: " + value);
+ }
+ }
+
+
+}
servicemix/base/src/main/java/org/servicemix/components/http
diff -u -r1.3 -r1.4 --- BindingServlet.java 20 Sep 2005 13:23:44 -0000 1.3 +++ BindingServlet.java 20 Sep 2005 14:39:29 -0000 1.4 @@ -29,7 +29,7 @@
/** * A Servlet which dispatches requests into the JBI container and returns the result. *
- * @version $Revision: 1.3 $
+ * @version $Revision: 1.4 $
*/
public class BindingServlet extends HttpServlet {
@@ -81,7 +81,7 @@
}
}
- protected HttpBinding createHttpBinding(ServletConfig config) {
+ protected HttpBinding createHttpBinding(ServletConfig config) throws ServletException {
// lets default to in/out
return new HttpInOutBinding();
}
servicemix/base/src/main/java/org/servicemix/components/http
diff -u -r1.3 -r1.4 --- HttpMarshaler.java 19 Aug 2005 10:36:30 -0000 1.3 +++ HttpMarshaler.java 20 Sep 2005 14:39:29 -0000 1.4 @@ -18,6 +18,7 @@
package org.servicemix.components.http; import org.servicemix.components.util.MarshalerSupport;
+import org.servicemix.jbi.jaxp.StringSource;
import javax.jbi.messaging.InOut; import javax.jbi.messaging.MessageExchange;
@@ -25,6 +26,7 @@
import javax.jbi.messaging.NormalizedMessage; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
+import javax.xml.transform.Source;
import javax.xml.transform.TransformerException; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource;
@@ -34,14 +36,25 @@
/** * A class which marshalls a HTTP request to a NMS message
- * - * @version $Revision: 1.3 $
+ * + * @version $Revision: 1.4 $
*/
public class HttpMarshaler extends MarshalerSupport {
+ protected static final Source EMPTY_CONTENT = new StringSource("<payload/>");
+
+ // could be "application/soap+xml"
+ private String contentType = "application/xml";
+
public void toNMS(MessageExchange exchange, NormalizedMessage inMessage, HttpServletRequest request) throws IOException, MessagingException {
addNmsProperties(inMessage, request);
- inMessage.setContent(new StreamSource(request.getInputStream()));
+ String method = request.getMethod();
+ if (method != null && method.equalsIgnoreCase("POST")) {
+ inMessage.setContent(new StreamSource(request.getInputStream()));
+ }
+ else {
+ inMessage.setContent(EMPTY_CONTENT);
+ }
}
public void toResponse(InOut exchange, NormalizedMessage message, HttpServletResponse response) throws IOException, TransformerException {
@@ -49,10 +62,22 @@
addHttpHeaders(response, message);
}
- response.setContentType("application/soap+xml");
+ response.setContentType(contentType);
getTransformer().toResult(message.getContent(), new StreamResult(response.getOutputStream()));
}
+ // Properties
+ // -------------------------------------------------------------------------
+ public String getContentType() {
+ return contentType;
+ }
+
+ public void setContentType(String contentType) {
+ this.contentType = contentType;
+ }
+
+ // Implementation methods
+ // -------------------------------------------------------------------------
protected void addNmsProperties(NormalizedMessage message, HttpServletRequest request) {
Enumeration enumeration = request.getHeaderNames();
while (enumeration.hasMoreElements()) {
@@ -72,12 +97,12 @@
}
}
-
/**
- * Decides whether or not the given header should be included in the JMS message. - * By default this includes all suitable typed values
+ * Decides whether or not the given header should be included in the JMS + * message. By default this includes all suitable typed values
*/
protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) {
return value instanceof String;
}
+
}
