1 17 package org.apache.servicemix.components.http; 18 19 import java.io.IOException ; 20 import java.util.Enumeration ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 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 ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.xml.transform.Source ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.stream.StreamResult ; 34 import javax.xml.transform.stream.StreamSource ; 35 36 import org.apache.servicemix.components.util.MarshalerSupport; 37 import org.apache.servicemix.jbi.jaxp.StringSource; 38 39 44 public class HttpMarshaler extends MarshalerSupport { 45 46 public static final String CGI_HEADERS = "cgi.headers"; 47 48 public static final String AUTH_TYPE = "AUTH_TYPE"; 49 public static final String CONTENT_LENGTH = "CONTENT_LENGTH"; 50 public static final String CONTENT_TYPE = "CONTENT_TYPE"; 51 public static final String DOCUMENT_ROOT = "DOCUMENT_ROOT"; 52 public static final String PATH_INFO = "PATH_INFO"; 53 public static final String PATH_TRANSLATED = "PATH_TRANSLATED"; 54 public static final String QUERY_STRING = "QUERY_STRING"; 55 public static final String REMOTE_ADDRESS = "REMOTE_ADDR"; 56 public static final String REMOTE_HOST = "REMOTE_HOST"; 57 public static final String REMOTE_USER = "REMOTE_USER"; 58 public static final String REQUEST_METHOD = "REQUEST_METHOD"; 59 public static final String REQUEST_URI = "REQUEST_URI"; 60 public static final String SCRIPT_NAME = "SCRIPT_NAME"; 61 public static final String SERVER_NAME = "SERVER_NAME"; 62 public static final String SERVER_PORT = "SERVER_PORT"; 63 public static final String SERVER_PROTOCOL = "SERVER_PROTOCOL"; 64 65 protected static final Source EMPTY_CONTENT = new StringSource("<payload/>"); 66 67 private String contentType = "text/xml"; 68 69 public void toNMS(MessageExchange exchange, NormalizedMessage inMessage, HttpServletRequest request) throws IOException , MessagingException { 70 addNmsProperties(inMessage, request); 71 String method = request.getMethod(); 72 if (method != null && method.equalsIgnoreCase("POST")) { 73 97 inMessage.setContent(new StreamSource (request.getInputStream())); 98 } 99 else { 100 Enumeration enumeration = request.getParameterNames(); 101 while (enumeration.hasMoreElements()) { 102 String name = (String ) enumeration.nextElement(); 103 String 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 response) throws IOException , TransformerException { 111 if (message != null) { 112 addHttpHeaders(response, message); 113 } 114 115 response.setContentType(contentType); 116 getTransformer().toResult(message.getContent(), new StreamResult (response.getOutputStream())); 117 } 118 119 public String getContentType() { 122 return contentType; 123 } 124 125 public void setContentType(String contentType) { 126 this.contentType = contentType; 127 } 128 129 protected void addNmsProperties(NormalizedMessage message, HttpServletRequest request) { 132 Enumeration enumeration = request.getHeaderNames(); 133 while (enumeration.hasMoreElements()) { 134 String name = (String ) enumeration.nextElement(); 135 String value = request.getHeader(name); 136 message.setProperty(name, value); 137 } 138 Map cgi = new HashMap (); 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 response, NormalizedMessage normalizedMessage) { 159 for (Iterator iter = normalizedMessage.getPropertyNames().iterator(); iter.hasNext();) { 160 String name = (String ) iter.next(); 161 Object value = normalizedMessage.getProperty(name); 162 if (shouldIncludeHeader(normalizedMessage, name, value)) { 163 response.setHeader(name, value.toString()); 164 } 165 } 166 } 167 168 172 protected boolean shouldIncludeHeader(NormalizedMessage normalizedMessage, String name, Object value) { 173 return value instanceof String && 174 !"Content-Length".equalsIgnoreCase(name) && 175 !"Content-Type".equalsIgnoreCase(name); 176 } 177 178 } 179 | Popular Tags |