1 10 11 package org.mule.providers.http.servlet; 12 13 import java.io.BufferedReader ; 14 import java.io.IOException ; 15 import java.util.Enumeration ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 19 import javax.servlet.http.HttpServletRequest ; 20 import javax.servlet.http.HttpSession ; 21 22 import org.apache.commons.io.IOUtils; 23 import org.apache.commons.io.output.ByteArrayOutputStream; 24 import org.apache.commons.lang.SystemUtils; 25 import org.mule.config.MuleProperties; 26 import org.mule.config.i18n.Message; 27 import org.mule.config.i18n.Messages; 28 import org.mule.providers.AbstractMessageAdapter; 29 import org.mule.providers.http.HttpConstants; 30 import org.mule.umo.MessagingException; 31 import org.mule.umo.provider.MessageTypeNotSupportedException; 32 import org.mule.umo.provider.UniqueIdNotSupportedException; 33 34 38 39 public class HttpRequestMessageAdapter extends AbstractMessageAdapter 40 { 41 44 private static final long serialVersionUID = -4238448252206941125L; 45 46 private Object message = null; 47 48 private HttpServletRequest request; 49 50 public HttpRequestMessageAdapter(Object message) throws MessagingException 51 { 52 if (message instanceof HttpServletRequest ) 53 { 54 setPayload((HttpServletRequest )message); 55 final Map parameterMap = request.getParameterMap(); 56 if (parameterMap != null && parameterMap.size() > 0) 57 { 58 for (Iterator iterator = parameterMap.entrySet().iterator(); iterator.hasNext();) 59 { 60 Map.Entry entry = (Map.Entry )iterator.next(); 61 String key = (String )entry.getKey(); 62 Object value = entry.getValue(); 63 if (value != null) 64 { 65 if (value.getClass().isArray() && ((Object [])value).length == 1) 66 { 67 setProperty(key, ((Object [])value)[0]); 68 } 69 else 70 { 71 setProperty(key, value); 72 } 73 } 74 } 75 } 76 String key; 77 for (Enumeration e = request.getAttributeNames(); e.hasMoreElements();) 78 { 79 key = (String )e.nextElement(); 80 properties.put(key, request.getAttribute(key)); 81 } 82 String realKey; 83 for (Enumeration e = request.getHeaderNames(); e.hasMoreElements();) 84 { 85 key = (String )e.nextElement(); 86 realKey = key; 87 if (key.startsWith(HttpConstants.X_PROPERTY_PREFIX)) 88 { 89 realKey = key.substring(2); 90 } 91 setProperty(realKey, request.getHeader(key)); 92 } 93 } 94 else 95 { 96 throw new MessageTypeNotSupportedException(message, getClass()); 97 } 98 } 99 100 105 public Object getPayload() 106 { 107 return message; 108 } 109 110 public boolean isBinary() 111 { 112 return message instanceof byte[]; 113 } 114 115 120 public byte[] getPayloadAsBytes() throws Exception 121 { 122 if (isBinary()) 123 { 124 return (byte[])message; 125 } 126 else 127 { 128 return ((String )message).getBytes(); 129 } 130 } 131 132 140 public String getPayloadAsString(String encoding) throws Exception 141 { 142 if (isBinary()) 143 { 144 return new String ((byte[])message, encoding); 145 } 146 else 147 { 148 return (String )message; 149 } 150 } 151 152 157 private void setPayload(HttpServletRequest message) throws MessagingException 158 { 159 try 160 { 161 162 request = message; 163 173 String payloadParam = (String )request 176 .getAttribute(AbstractReceiverServlet.PAYLOAD_PARAMETER_NAME); 177 178 if (payloadParam == null) 179 { 180 payloadParam = AbstractReceiverServlet.DEFAULT_PAYLOAD_PARAMETER_NAME; 181 } 182 String payload = request.getParameter(payloadParam); 183 if (payload == null) 184 { 185 if (isText(request.getContentType())) 186 { 187 BufferedReader reader = request.getReader(); 188 StringBuffer buffer = new StringBuffer (8192); 189 String line = reader.readLine(); 190 while (line != null) 191 { 192 buffer.append(line); 193 line = reader.readLine(); 194 if (line != null) buffer.append(SystemUtils.LINE_SEPARATOR); 195 } 196 this.message = buffer.toString(); 197 } 198 else 199 { 200 ByteArrayOutputStream baos = new ByteArrayOutputStream(8192); 201 IOUtils.copy(request.getInputStream(), baos); 202 this.message = baos.toByteArray(); 203 } 204 } 205 else 206 { 207 this.message = payload; 208 } 209 } 210 catch (IOException e) 211 { 212 throw new MessagingException(new Message("servlet", 3, request.getRequestURL().toString()), e); 213 } 214 } 215 216 public HttpServletRequest getRequest() 217 { 218 return request; 219 } 220 221 public String getUniqueId() 222 { 223 HttpSession session = null; 224 225 try 226 { 227 session = getRequest().getSession(); 230 } 231 catch (Exception e) 232 { 233 throw new UniqueIdNotSupportedException(this, new Message(Messages.X_IS_NULL, "Http session")); 234 } 235 if (session == null) 236 { 237 throw new UniqueIdNotSupportedException(this, new Message(Messages.X_IS_NULL, "Http session")); 238 } 239 return session.getId(); 240 } 241 242 protected boolean isText(String contentType) 243 { 244 if (contentType == null) 245 { 246 return true; 247 } 248 return (contentType.startsWith("text/")); 249 } 250 251 259 public void setReplyTo(Object replyTo) 260 { 261 if (replyTo != null && replyTo.toString().startsWith("http")) 262 { 263 setProperty(HttpConstants.HEADER_LOCATION, replyTo); 264 } 265 setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, replyTo); 266 } 267 268 276 public Object getReplyTo() 277 { 278 String replyto = (String )getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); 279 if (replyto == null) 280 { 281 replyto = (String )getProperty(HttpConstants.HEADER_LOCATION); 282 } 283 return replyto; 284 } 285 } 286 | Popular Tags |