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  
13  package com.eviware.soapui.impl.wsdl.monitor.jettyproxy;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.OutputStream;
18  
19  import javax.servlet.http.HttpServletRequest;
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.apache.log4j.Logger;
23  import org.mortbay.io.Buffer;
24  import org.mortbay.jetty.client.HttpExchange;
25  import org.mortbay.util.ajax.Continuation;
26  import org.mortbay.util.ajax.ContinuationSupport;
27  
28  import com.eviware.soapui.impl.wsdl.monitor.JProxyServletWsdlMonitorMessageExchange;
29  import com.eviware.soapui.impl.wsdl.monitor.SoapMonitor;
30  
31  public class SoapUIHttpExchange extends HttpExchange
32  {
33  
34  	private Continuation continuation;
35  	private InputStream in;
36  	private OutputStream out;
37  	private HttpServletResponse httpResponse;
38  	private Logger log = Logger.getLogger(SoapUIHttpExchange.class);
39  	private JProxyServletWsdlMonitorMessageExchange capturedMessage;
40  	private SoapMonitor monitor;
41  
42  	public SoapUIHttpExchange(SoapMonitor monitor, HttpServletRequest httpRequest, HttpServletResponse httpResponse,
43  			CaptureInputStream capture, JProxyServletWsdlMonitorMessageExchange capturedData) throws IOException
44  	{
45  		this.httpResponse = httpResponse;
46  		continuation = ContinuationSupport.getContinuation(httpRequest, httpRequest);
47  		in = capture;
48  		out = httpResponse.getOutputStream();
49  		this.capturedMessage = capturedData;
50  		this.monitor = monitor;
51  	}
52  
53  	@Override
54  	protected void onConnectionFailed(Throwable ex)
55  	{
56  //		log.warn("onConnectionFailed");
57  		try
58  		{
59  			httpResponse.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, ex.getMessage());
60  			httpResponse.flushBuffer();
61  			this.capturedMessage.stopCapture();
62  		}
63  		catch (IOException e)
64  		{
65  			// TODO Auto-generated catch block
66  			e.printStackTrace();
67  		}
68  	}
69  
70  	@Override
71  	protected void onException(Throwable ex)
72  	{
73  //		log.error("onException" + ex.getMessage());
74  		try
75  		{
76  			httpResponse.sendError(HttpServletResponse.SC_NOT_FOUND, ex.getMessage());
77  			httpResponse.flushBuffer();
78  			this.capturedMessage.stopCapture();
79  		}
80  		catch (IOException e)
81  		{
82  			// TODO Auto-generated catch block
83  			e.printStackTrace();
84  		}
85  	}
86  
87  	@Override
88  	protected void onRequestCommitted() throws IOException
89  	{
90  //		log.info("onRequestCommitted()");
91  		this.capturedMessage.setRequest(((CaptureInputStream) in).getCapturedData());
92  	}
93  
94  	@Override
95  	protected void onRequestComplete() throws IOException
96  	{
97  //		log.info("onRequestComplete()");
98  		this.capturedMessage.setRequest(((CaptureInputStream) in).getCapturedData());
99  	}
100 
101 	@Override
102 	protected void onResponseComplete() throws IOException
103 	{
104 //		log.info("onResponseComplete()");
105 		continuation.resume();
106 		this.capturedMessage.stopCapture();
107 	}
108 
109 	@Override
110 	protected void onResponseContent(Buffer content) throws IOException
111 	{
112 //		log.info("onResponseContent()");
113 		byte[] buffer = new byte[content.length()];
114 		while (content.hasContent())
115 		{
116 			int len = content.get(buffer, 0, buffer.length);
117 			this.capturedMessage.setResponse(buffer);
118 			out.write(buffer, 0, len); // May block here for a little bit!
119 		}
120 	}
121 
122 	@Override
123 	protected void onResponseHeaderComplete() throws IOException
124 	{
125 //		log.info("onResponseCompleteHeader()");
126 	}
127 
128 	@SuppressWarnings("deprecation")
129 	@Override
130 	protected void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
131 	{
132 		if (reason != null && reason.length() > 0)
133 			httpResponse.setStatus(status, reason.toString());
134 		else
135 			httpResponse.setStatus(status);
136 
137 	}
138 
139 	@Override
140 	protected void onResponseHeader(Buffer name, Buffer value) throws IOException
141 	{
142 		httpResponse.addHeader(name.toString(), value.toString());
143 		capturedMessage.setResponseHeader(name.toString(), value.toString());
144 	}
145 
146 }