1
2
3
4
5
6
7
8
9
10
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
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
66 e.printStackTrace();
67 }
68 }
69
70 @Override
71 protected void onException(Throwable ex)
72 {
73
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
83 e.printStackTrace();
84 }
85 }
86
87 @Override
88 protected void onRequestCommitted() throws IOException
89 {
90
91 this.capturedMessage.setRequest(((CaptureInputStream) in).getCapturedData());
92 }
93
94 @Override
95 protected void onRequestComplete() throws IOException
96 {
97
98 this.capturedMessage.setRequest(((CaptureInputStream) in).getCapturedData());
99 }
100
101 @Override
102 protected void onResponseComplete() throws IOException
103 {
104
105 continuation.resume();
106 this.capturedMessage.stopCapture();
107 }
108
109 @Override
110 protected void onResponseContent(Buffer content) throws IOException
111 {
112
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);
119 }
120 }
121
122 @Override
123 protected void onResponseHeaderComplete() throws IOException
124 {
125
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 }