View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.apache.log4j.Logger;
26  import org.mortbay.jetty.Request;
27  import org.mortbay.jetty.Response;
28  import org.mortbay.util.IO;
29  
30  import com.eviware.soapui.SoapUI;
31  
32  public class Server extends org.mortbay.jetty.Server
33  {
34  
35  	private Logger log = Logger.getLogger( Server.class );
36  
37  	public Server()
38  	{
39  		super();
40  		if( SoapUI.getLogMonitor() == null || SoapUI.getLogMonitor().getLogArea( "jetty log" ) == null )
41  			return;
42  		SoapUI.getLogMonitor().getLogArea( "jetty log" ).addLogger( log.getName(), true );
43  	}
44  
45  	@Override
46  	public void handle( final org.mortbay.jetty.HttpConnection connection ) throws IOException, ServletException
47  	{
48  		final Request request = connection.getRequest();
49  
50  		if( !request.getMethod().equals( "CONNECT" ) )
51  		{
52  			super.handle( connection );
53  			return;
54  		}
55  
56  		final String uri = request.getUri().toString();
57  
58  		final int c = uri.indexOf( ':' );
59  		final String port = uri.substring( c + 1 );
60  		final String host = uri.substring( 0, c );
61  
62  		final InetSocketAddress inetAddress = new InetSocketAddress( host, Integer.parseInt( port ) );
63  
64  		final Socket clientSocket = connection.getEndPoint().getTransport() instanceof Socket ? ( Socket )connection
65  				.getEndPoint().getTransport() : ( ( SocketChannel )connection.getEndPoint().getTransport() ).socket();
66  		final InputStream in = clientSocket.getInputStream();
67  		final OutputStream out = clientSocket.getOutputStream();
68  
69  		final SSLSocket socket = ( SSLSocket )SSLSocketFactory.getDefault().createSocket( inetAddress.getAddress(),
70  				inetAddress.getPort() );
71  
72  		final Response response = connection.getResponse();
73  		response.setStatus( 200 );
74  		// response.setHeader("Connection", "close");
75  		response.flushBuffer();
76  
77  		IO.copyThread( socket.getInputStream(), out );
78  
79  		IO.copyThread( in, socket.getOutputStream() );
80  
81  	}
82  
83  }