1 10 11 package org.mule.providers.http; 12 13 import org.apache.commons.httpclient.Header; 14 import org.apache.commons.httpclient.HeaderElement; 15 import org.apache.commons.httpclient.NameValuePair; 16 import org.mule.MuleManager; 17 import org.mule.providers.AbstractMessageAdapter; 18 import org.mule.transformers.simple.SerializableToByteArray; 19 import org.mule.umo.MessagingException; 20 import org.mule.umo.provider.MessageTypeNotSupportedException; 21 import org.mule.umo.transformer.UMOTransformer; 22 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 30 public class HttpMessageAdapter extends AbstractMessageAdapter 31 { 32 35 private static final long serialVersionUID = -1544495479333000422L; 36 37 private static final UMOTransformer transformer = new SerializableToByteArray(); 38 39 private final Object message; 40 private boolean http11 = true; 41 42 public HttpMessageAdapter(Object message) throws MessagingException 43 { 44 if (message instanceof Object []) 45 { 46 this.message = ((Object [])message)[0]; 47 if (((Object [])message).length > 1) 48 { 49 Map props = (Map )((Object [])message)[1]; 50 for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) 51 { 52 Map.Entry e = (Map.Entry )iterator.next(); 53 String key = (String )e.getKey(); 54 Object value = e.getValue(); 55 if (value != null) 57 { 58 setProperty(key, value); 59 } 60 } 61 } 62 } 63 else if (message instanceof byte[]) 64 { 65 this.message = message; 66 } 69 else if (message instanceof HttpResponse) 70 { 71 this.message = message; 72 return; 73 } 74 else 75 { 76 throw new MessageTypeNotSupportedException(message, getClass()); 77 } 78 String temp = getStringProperty(HttpConnector.HTTP_VERSION_PROPERTY, null); 79 if (HttpConstants.HTTP10.equalsIgnoreCase(temp)) 80 { 81 http11 = false; 82 } 83 84 String charset = null; 86 Header contenttype = getHeader(HttpConstants.HEADER_CONTENT_TYPE); 87 if (contenttype != null) 88 { 89 HeaderElement values[] = contenttype.getElements(); 90 if (values.length == 1) 91 { 92 NameValuePair param = values[0].getParameterByName("charset"); 93 if (param != null) 94 { 95 charset = param.getValue(); 96 } 97 } 98 } 99 if (charset != null) 100 { 101 encoding = charset; 102 } 103 else 104 { 105 encoding = MuleManager.getConfiguration().getEncoding(); 106 } 107 } 108 109 114 public Object getPayload() 115 { 116 return message; 117 } 118 119 124 public byte[] getPayloadAsBytes() throws Exception 125 { 126 if (message instanceof byte[]) 127 { 128 return (byte[])message; 129 } 130 else if (message instanceof String ) 131 { 132 return message.toString().getBytes(); 133 } 134 else 135 { 136 return (byte[])transformer.transform(message); 137 } 138 } 139 140 146 public String getPayloadAsString(String encoding) throws Exception 147 { 148 if (message instanceof byte[]) 149 { 150 if (encoding != null) 151 { 152 return new String ((byte[])message, encoding); 153 } 154 else 155 { 156 return new String ((byte[])message); 157 } 158 } 159 else 160 { 161 return message.toString(); 162 } 163 } 164 165 170 public Object getProperty(String key) 171 { 172 if (HttpConstants.HEADER_KEEP_ALIVE.equals(key) || HttpConstants.HEADER_CONNECTION.equals(key)) 173 { 174 if (!http11) 175 { 176 String connection = super.getStringProperty(HttpConstants.HEADER_CONNECTION, null); 177 if (connection != null && connection.equalsIgnoreCase("close")) 178 { 179 return "false"; 180 } 181 else 182 { 183 return "true"; 184 } 185 } 186 else 187 { 188 return (super.getProperty(HttpConstants.HEADER_CONNECTION) != null ? "true" : "false"); 189 } 190 } 191 else 192 { 193 return super.getProperty(key); 194 } 195 } 196 197 public Header getHeader(String name) 198 { 199 String value = getStringProperty(name, null); 200 if (value == null) 201 { 202 return null; 203 } 204 return new Header(name, value); 205 } 206 } 207 | Popular Tags |