View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  package com.eviware.soapui.impl.wsdl.monitor.jettyproxy;
13  
14  import java.io.IOException;
15  import java.io.InputStream;
16  import java.io.OutputStream;
17  import java.net.InetSocketAddress;
18  import java.net.Socket;
19  import java.nio.channels.SocketChannel;
20  
21  import javax.net.ssl.SSLSocket;
22  import javax.net.ssl.SSLSocketFactory;
23  import javax.servlet.ServletException;
24  
25  import org.mortbay.jetty.Request;
26  import org.mortbay.jetty.Response;
27  import org.mortbay.util.IO;
28  
29  public class Server extends org.mortbay.jetty.Server
30  {
31  
32  	@Override
33  	public void handle(final org.mortbay.jetty.HttpConnection connection) throws IOException, ServletException
34  	{
35  		final Request request = connection.getRequest();
36  
37  		if (!request.getMethod().equals("CONNECT"))
38  		{
39  			super.handle(connection);
40  			return;
41  		}
42  
43  		final String uri = request.getUri().toString();
44  
45  		final int c = uri.indexOf(':');
46  		final String port = uri.substring(c + 1);
47  		final String host = uri.substring(0, c);
48  
49  		final InetSocketAddress inetAddress = new InetSocketAddress(host, Integer.parseInt(port));
50  
51  		final Socket clientSocket = connection.getEndPoint().getTransport() instanceof Socket ? (Socket) connection
52  				.getEndPoint().getTransport() : ((SocketChannel) connection.getEndPoint().getTransport()).socket();
53  		final InputStream in = clientSocket.getInputStream();
54  		final OutputStream out = clientSocket.getOutputStream();
55  
56  		final SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket(inetAddress.getAddress(), inetAddress.getPort());
57  
58  		final Response response = connection.getResponse();
59  		response.setStatus(200);
60  //		response.setHeader("Connection", "close");
61  		response.flushBuffer();
62  
63  		IO.copyThread(socket.getInputStream(), out);
64  
65  		IO.copyThread(in, socket.getOutputStream());
66  
67  	}
68  
69  }