View Javadoc

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