KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > http > HttpMarshaler


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.http;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.jbi.messaging.InOut;
26 import javax.jbi.messaging.MessageExchange;
27 import javax.jbi.messaging.MessagingException;
28 import javax.jbi.messaging.NormalizedMessage;
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.TransformerException JavaDoc;
33 import javax.xml.transform.stream.StreamResult JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35
36 import org.apache.servicemix.components.util.MarshalerSupport;
37 import org.apache.servicemix.jbi.jaxp.StringSource;
38
39 /**
40  * A class which marshalls a HTTP request to a NMS message
41  *
42  * @version $Revision: 426415 $
43  */

44 public class HttpMarshaler extends MarshalerSupport {
45
46     public static final String JavaDoc CGI_HEADERS = "cgi.headers";
47     
48     public static final String JavaDoc AUTH_TYPE = "AUTH_TYPE";
49     public static final String JavaDoc CONTENT_LENGTH = "CONTENT_LENGTH";
50     public static final String JavaDoc CONTENT_TYPE = "CONTENT_TYPE";
51     public static final String JavaDoc DOCUMENT_ROOT = "DOCUMENT_ROOT";
52     public static final String JavaDoc PATH_INFO = "PATH_INFO";
53     public static final String JavaDoc PATH_TRANSLATED = "PATH_TRANSLATED";
54     public static final String JavaDoc QUERY_STRING = "QUERY_STRING";
55     public static final String JavaDoc REMOTE_ADDRESS = "REMOTE_ADDR";
56     public static final String JavaDoc REMOTE_HOST = "REMOTE_HOST";
57     public static final String JavaDoc REMOTE_USER = "REMOTE_USER";
58     public static final String JavaDoc REQUEST_METHOD = "REQUEST_METHOD";
59     public static final String JavaDoc REQUEST_URI = "REQUEST_URI";
60     public static final String JavaDoc SCRIPT_NAME = "SCRIPT_NAME";
61     public static final String JavaDoc SERVER_NAME = "SERVER_NAME";
62     public static final String JavaDoc SERVER_PORT = "SERVER_PORT";
63     public static final String JavaDoc SERVER_PROTOCOL = "SERVER_PROTOCOL";
64     
65     protected static final Source JavaDoc EMPTY_CONTENT = new StringSource("<payload/>");
66
67     private String JavaDoc contentType = "text/xml";
68
69     public void toNMS(MessageExchange exchange, NormalizedMessage inMessage, HttpServletRequest JavaDoc request) throws IOException JavaDoc, MessagingException {
70         addNmsProperties(inMessage, request);
71         String JavaDoc method = request.getMethod();
72         if (method != null && method.equalsIgnoreCase("POST")) {
73             /*
74             Source src = null;
75             try {
76                 if (request.getContentType() != null) {
77                     String charset = new MimeType(request.getContentType()).getParameter("charset");
78                     if (charset != null) {
79                         XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(request.getInputStream(), charset);
80                         src = new StaxSource(xr);
81                     }
82                 }
83                 if (src == null) {
84                     XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(request.getInputStream());
85                     src = new StaxSource(xr);
86                 }
87             } catch (Exception e) {
88                 throw new RuntimeException(e);
89             }
90             try {
91                 src = getTransformer().toDOMSource(src);
92             } catch (Exception e) {
93                 throw new RuntimeException(e);
94             }
95             inMessage.setContent(src);
96             */

97             inMessage.setContent(new StreamSource JavaDoc(request.getInputStream()));
98         }
99         else {
100             Enumeration JavaDoc enumeration = request.getParameterNames();
101             while (enumeration.hasMoreElements()) {
102                 String JavaDoc name = (String JavaDoc) enumeration.nextElement();
103                 String JavaDoc value = request.getParameter(name);
104                 inMessage.setProperty(name, value);
105             }
106             inMessage.setContent(EMPTY_CONTENT);
107         }
108     }
109
110     public void toResponse(InOut exchange, NormalizedMessage message, HttpServletResponse JavaDoc response) throws IOException JavaDoc, TransformerException JavaDoc {
111         if (message != null) {
112             addHttpHeaders(response, message);
113         }
114
115         response.setContentType(contentType);
116         getTransformer().toResult(message.getContent(), new StreamResult JavaDoc(response.getOutputStream()));
117     }
118
119     // Properties
120
// -------------------------------------------------------------------------
121
public String JavaDoc getContentType() {
122         return contentType;
123     }
124
125     public void setContentType(String JavaDoc contentType) {
126         this.contentType = contentType;
127     }
128
129     // Implementation methods
130
// -------------------------------------------------------------------------
131
protected void addNmsProperties(NormalizedMessage message, HttpServletRequest JavaDoc request) {
132         Enumeration JavaDoc enumeration = request.getHeaderNames();
133         while (enumeration.hasMoreElements()) {
134             String JavaDoc name = (String JavaDoc) enumeration.nextElement();
135             String JavaDoc value = request.getHeader(name);
136             message.setProperty(name, value);
137         }
138         Map JavaDoc cgi = new HashMap JavaDoc();
139         cgi.put(AUTH_TYPE, request.getAuthType());
140         cgi.put(CONTENT_LENGTH, String.valueOf(request.getContentLength()));
141         cgi.put(CONTENT_TYPE, request.getContentType());
142         cgi.put(DOCUMENT_ROOT, request.getRealPath("/"));
143         cgi.put(PATH_INFO, request.getPathInfo());
144         cgi.put(PATH_TRANSLATED, request.getPathTranslated());
145         cgi.put(QUERY_STRING, request.getQueryString());
146         cgi.put(REMOTE_ADDRESS, request.getRemoteAddr());
147         cgi.put(REMOTE_HOST, request.getRemoteHost());
148         cgi.put(REMOTE_USER, request.getRemoteUser());
149         cgi.put(REQUEST_METHOD, request.getMethod());
150         cgi.put(REQUEST_URI, request.getRequestURL());
151         cgi.put(SCRIPT_NAME, request.getServletPath());
152         cgi.put(SERVER_NAME, request.getServerName());
153         cgi.put(SERVER_PORT, String.valueOf(request.getServerPort()));
154         cgi.put(SERVER_PROTOCOL, request.getProtocol());
155         message.setProperty(CGI_HEADERS, cgi);
156     }
157     
158     protected void addHttpHeaders(HttpServletResponse JavaDoc response, NormalizedMessage normalizedMessage) {
159         for (Iterator JavaDoc iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) {
160             String JavaDoc name = (String JavaDoc) iter.next();
161             Object JavaDoc value = normalizedMessage.getProperty(name);
162             if (shouldIncludeHeader(normalizedMessage, name, value)) {
163                 response.setHeader(name, value.toString());
164             }
165         }
166     }
167
168     /**
169      * Decides whether or not the given header should be included in the JMS
170      * message. By default this includes all suitable typed values
171      */

172     protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String JavaDoc name, Object JavaDoc value) {
173         return value instanceof String JavaDoc &&
174                 !"Content-Length".equalsIgnoreCase(name) &&
175                 !"Content-Type".equalsIgnoreCase(name);
176     }
177
178 }
179
Popular Tags