1 10 11 package org.mule.providers.http; 12 13 import org.apache.commons.httpclient.ChunkedOutputStream; 14 import org.apache.commons.httpclient.Header; 15 import org.apache.commons.httpclient.HttpParser; 16 import org.apache.commons.httpclient.StatusLine; 17 import org.apache.commons.io.IOUtils; 18 import org.mule.MuleManager; 19 20 import java.io.DataOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.UnsupportedEncodingException ; 25 import java.net.Socket ; 26 import java.net.SocketException ; 27 import java.util.Iterator ; 28 29 32 public class HttpServerConnection 33 { 34 private Socket socket = null; 35 private InputStream in = null; 36 private OutputStream out = null; 37 private boolean keepAlive = false; 38 private String encoding; 39 40 public HttpServerConnection(final Socket socket) throws IOException 41 { 42 this(socket, MuleManager.getConfiguration().getEncoding()); 43 } 44 45 public HttpServerConnection(final Socket socket, String encoding) throws IOException 46 { 47 super(); 48 if (socket == null) 49 { 50 throw new IllegalArgumentException ("Socket may not be null"); 51 } 52 this.socket = socket; 53 this.socket.setTcpNoDelay(true); 54 this.in = socket.getInputStream(); 55 this.out = new DataOutputStream (socket.getOutputStream()); 56 this.encoding = encoding; 57 } 58 59 public synchronized void close() 60 { 61 try 62 { 63 if (socket != null) 64 { 65 socket.close(); 66 } 67 } 68 catch (IOException e) 69 { 70 } 72 finally 73 { 74 socket = null; 75 } 76 } 77 78 public synchronized boolean isOpen() 79 { 80 return this.socket != null; 81 } 82 83 public void setKeepAlive(boolean b) 84 { 85 this.keepAlive = b; 86 } 87 88 public boolean isKeepAlive() 89 { 90 return this.keepAlive; 91 } 92 93 public InputStream getInputStream() 94 { 95 return this.in; 96 } 97 98 public OutputStream getOutputStream() 99 { 100 return this.out; 101 } 102 103 108 public ResponseWriter getWriter() throws UnsupportedEncodingException 109 { 110 return new ResponseWriter(out); 111 } 112 113 public HttpRequest readRequest() throws IOException 114 { 115 String line = null; 116 try 117 { 118 do 119 { 120 line = HttpParser.readLine(in, encoding); 121 } 122 while (line != null && line.length() == 0); 123 124 if (line == null) 125 { 126 setKeepAlive(false); 127 return null; 128 } 129 HttpRequest request = new HttpRequest(RequestLine.parseLine(line), HttpParser.parseHeaders( 130 this.in, encoding), this.in); 131 return request; 132 } 133 catch (IOException e) 134 { 135 close(); 136 throw e; 137 } 138 } 139 140 public HttpResponse readResponse() throws IOException 141 { 142 try 143 { 144 String line = null; 145 do 146 { 147 line = HttpParser.readLine(in, encoding); 148 } 149 while (line != null && line.length() == 0); 150 151 if (line == null) 152 { 153 setKeepAlive(false); 154 return null; 155 } 156 HttpResponse response = new HttpResponse(new StatusLine(line), HttpParser.parseHeaders(this.in, 157 encoding), this.in); 158 return response; 159 } 160 catch (IOException e) 161 { 162 close(); 163 throw e; 164 } 165 } 166 167 public void writeRequest(final HttpRequest request) throws IOException 168 { 169 if (request == null) 170 { 171 return; 172 } 173 ResponseWriter writer = new ResponseWriter(this.out, encoding); 174 writer.println(request.getRequestLine().toString()); 175 Iterator item = request.getHeaderIterator(); 176 while (item.hasNext()) 177 { 178 Header header = (Header)item.next(); 179 writer.print(header.toExternalForm()); 180 } 181 writer.println(); 182 writer.flush(); 183 184 OutputStream outstream = this.out; 185 InputStream content = request.getBody(); 186 if (content != null) 187 { 188 Header transferenc = request.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); 189 if (transferenc != null) 190 { 191 request.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH); 192 if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) 193 { 194 outstream = new ChunkedOutputStream(outstream); 195 } 196 } 197 198 IOUtils.copy(content, outstream); 199 200 if (outstream instanceof ChunkedOutputStream) 201 { 202 ((ChunkedOutputStream)outstream).finish(); 203 } 204 } 205 206 outstream.flush(); 207 } 208 209 public void writeResponse(final HttpResponse response) throws IOException 210 { 211 if (response == null) 212 { 213 return; 214 } 215 setKeepAlive(response.isKeepAlive()); 216 ResponseWriter writer = new ResponseWriter(this.out, encoding); 217 OutputStream outstream = this.out; 218 219 writer.println(response.getStatusLine()); 220 Iterator item = response.getHeaderIterator(); 221 while (item.hasNext()) 222 { 223 Header header = (Header)item.next(); 224 writer.print(header.toExternalForm()); 225 226 } 227 writer.println(); 228 writer.flush(); 229 230 InputStream content = response.getBody(); 231 if (content != null) 232 { 233 Header transferenc = response.getFirstHeader(HttpConstants.HEADER_TRANSFER_ENCODING); 234 if (transferenc != null) 235 { 236 response.removeHeaders(HttpConstants.HEADER_CONTENT_LENGTH); 237 if (transferenc.getValue().indexOf(HttpConstants.TRANSFER_ENCODING_CHUNKED) != -1) 238 { 239 outstream = new ChunkedOutputStream(outstream); 240 } 241 } 242 243 IOUtils.copy(content, outstream); 244 245 if (outstream instanceof ChunkedOutputStream) 246 { 247 ((ChunkedOutputStream)outstream).finish(); 248 } 249 } 250 251 outstream.flush(); 252 } 253 254 public int getSocketTimeout() throws SocketException 255 { 256 return this.socket.getSoTimeout(); 257 } 258 259 public void setSocketTimeout(int timeout) throws SocketException 260 { 261 this.socket.setSoTimeout(timeout); 262 } 263 } 264 | Popular Tags |