001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.components.http;
018:
019: import java.io.IOException;
020: import java.util.Enumeration;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import javax.jbi.messaging.InOut;
026: import javax.jbi.messaging.MessageExchange;
027: import javax.jbi.messaging.MessagingException;
028: import javax.jbi.messaging.NormalizedMessage;
029: import javax.servlet.http.HttpServletRequest;
030: import javax.servlet.http.HttpServletResponse;
031: import javax.xml.transform.Source;
032: import javax.xml.transform.TransformerException;
033: import javax.xml.transform.stream.StreamResult;
034: import javax.xml.transform.stream.StreamSource;
035:
036: import org.apache.servicemix.components.util.MarshalerSupport;
037: import org.apache.servicemix.jbi.jaxp.StringSource;
038:
039: /**
040: * A class which marshalls a HTTP request to a NMS message
041: *
042: * @version $Revision: 426415 $
043: */
044: public class HttpMarshaler extends MarshalerSupport {
045:
046: public static final String CGI_HEADERS = "cgi.headers";
047:
048: public static final String AUTH_TYPE = "AUTH_TYPE";
049: public static final String CONTENT_LENGTH = "CONTENT_LENGTH";
050: public static final String CONTENT_TYPE = "CONTENT_TYPE";
051: public static final String DOCUMENT_ROOT = "DOCUMENT_ROOT";
052: public static final String PATH_INFO = "PATH_INFO";
053: public static final String PATH_TRANSLATED = "PATH_TRANSLATED";
054: public static final String QUERY_STRING = "QUERY_STRING";
055: public static final String REMOTE_ADDRESS = "REMOTE_ADDR";
056: public static final String REMOTE_HOST = "REMOTE_HOST";
057: public static final String REMOTE_USER = "REMOTE_USER";
058: public static final String REQUEST_METHOD = "REQUEST_METHOD";
059: public static final String REQUEST_URI = "REQUEST_URI";
060: public static final String SCRIPT_NAME = "SCRIPT_NAME";
061: public static final String SERVER_NAME = "SERVER_NAME";
062: public static final String SERVER_PORT = "SERVER_PORT";
063: public static final String SERVER_PROTOCOL = "SERVER_PROTOCOL";
064:
065: protected static final Source EMPTY_CONTENT = new StringSource(
066: "<payload/>");
067:
068: private String contentType = "text/xml";
069:
070: public void toNMS(MessageExchange exchange,
071: NormalizedMessage inMessage, HttpServletRequest request)
072: throws IOException, MessagingException {
073: addNmsProperties(inMessage, request);
074: String method = request.getMethod();
075: if (method != null && method.equalsIgnoreCase("POST")) {
076: /*
077: Source src = null;
078: try {
079: if (request.getContentType() != null) {
080: String charset = new MimeType(request.getContentType()).getParameter("charset");
081: if (charset != null) {
082: XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(request.getInputStream(), charset);
083: src = new StaxSource(xr);
084: }
085: }
086: if (src == null) {
087: XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(request.getInputStream());
088: src = new StaxSource(xr);
089: }
090: } catch (Exception e) {
091: throw new RuntimeException(e);
092: }
093: try {
094: src = getTransformer().toDOMSource(src);
095: } catch (Exception e) {
096: throw new RuntimeException(e);
097: }
098: inMessage.setContent(src);
099: */
100: inMessage.setContent(new StreamSource(request
101: .getInputStream()));
102: } else {
103: Enumeration enumeration = request.getParameterNames();
104: while (enumeration.hasMoreElements()) {
105: String name = (String) enumeration.nextElement();
106: String value = request.getParameter(name);
107: inMessage.setProperty(name, value);
108: }
109: inMessage.setContent(EMPTY_CONTENT);
110: }
111: }
112:
113: public void toResponse(InOut exchange, NormalizedMessage message,
114: HttpServletResponse response) throws IOException,
115: TransformerException {
116: if (message != null) {
117: addHttpHeaders(response, message);
118: }
119:
120: response.setContentType(contentType);
121: getTransformer().toResult(message.getContent(),
122: new StreamResult(response.getOutputStream()));
123: }
124:
125: // Properties
126: // -------------------------------------------------------------------------
127: public String getContentType() {
128: return contentType;
129: }
130:
131: public void setContentType(String contentType) {
132: this .contentType = contentType;
133: }
134:
135: // Implementation methods
136: // -------------------------------------------------------------------------
137: protected void addNmsProperties(NormalizedMessage message,
138: HttpServletRequest request) {
139: Enumeration enumeration = request.getHeaderNames();
140: while (enumeration.hasMoreElements()) {
141: String name = (String) enumeration.nextElement();
142: String value = request.getHeader(name);
143: message.setProperty(name, value);
144: }
145: Map cgi = new HashMap();
146: cgi.put(AUTH_TYPE, request.getAuthType());
147: cgi.put(CONTENT_LENGTH, String.valueOf(request
148: .getContentLength()));
149: cgi.put(CONTENT_TYPE, request.getContentType());
150: cgi.put(DOCUMENT_ROOT, request.getRealPath("/"));
151: cgi.put(PATH_INFO, request.getPathInfo());
152: cgi.put(PATH_TRANSLATED, request.getPathTranslated());
153: cgi.put(QUERY_STRING, request.getQueryString());
154: cgi.put(REMOTE_ADDRESS, request.getRemoteAddr());
155: cgi.put(REMOTE_HOST, request.getRemoteHost());
156: cgi.put(REMOTE_USER, request.getRemoteUser());
157: cgi.put(REQUEST_METHOD, request.getMethod());
158: cgi.put(REQUEST_URI, request.getRequestURL());
159: cgi.put(SCRIPT_NAME, request.getServletPath());
160: cgi.put(SERVER_NAME, request.getServerName());
161: cgi.put(SERVER_PORT, String.valueOf(request.getServerPort()));
162: cgi.put(SERVER_PROTOCOL, request.getProtocol());
163: message.setProperty(CGI_HEADERS, cgi);
164: }
165:
166: protected void addHttpHeaders(HttpServletResponse response,
167: NormalizedMessage normalizedMessage) {
168: for (Iterator iter = normalizedMessage.getPropertyNames()
169: .iterator(); iter.hasNext();) {
170: String name = (String) iter.next();
171: Object value = normalizedMessage.getProperty(name);
172: if (shouldIncludeHeader(normalizedMessage, name, value)) {
173: response.setHeader(name, value.toString());
174: }
175: }
176: }
177:
178: /**
179: * Decides whether or not the given header should be included in the JMS
180: * message. By default this includes all suitable typed values
181: */
182: protected boolean shouldIncludeHeader(
183: NormalizedMessage normalizedMessage, String name,
184: Object value) {
185: return value instanceof String
186: && !"Content-Length".equalsIgnoreCase(name)
187: && !"Content-Type".equalsIgnoreCase(name);
188: }
189:
190: }
|