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.util.Date;
15  import java.util.Enumeration;
16  
17  import javax.servlet.http.HttpServletRequest;
18  import javax.servlet.http.HttpServletResponse;
19  
20  public class CapturedExchange
21  {
22  
23  	private boolean startCapture;
24  	private boolean stopCapture;
25  
26  	private long operationStarted;
27  	private long timeTaken;
28  
29  	private String requestHost;
30  	private String targetHost;
31  
32  	private String wsInterface;
33  	private String wsOperation;
34  	private int requestSize;
35  	private int responseSize;
36  	private byte[] request;
37  	private byte[] response;
38  
39  	private String requestHeader;
40  	private String responseHeader;
41  
42  	public boolean isStartCapture()
43  	{
44  		return startCapture;
45  	}
46  
47  	public void startCapture()
48  	{
49  		this.startCapture = true;
50  		this.stopCapture = false;
51  		setOperationStarted( System.currentTimeMillis() );
52  	}
53  
54  	public boolean isStopCapture()
55  	{
56  		return stopCapture;
57  	}
58  
59  	public void stopCapture()
60  	{
61  		this.startCapture = false;
62  		this.stopCapture = true;
63  		setTimeTaken( System.currentTimeMillis() );
64  	}
65  
66  	public long getOperationStarted()
67  	{
68  		return operationStarted;
69  	}
70  
71  	private void setOperationStarted( long operationStarted )
72  	{
73  		this.operationStarted = operationStarted;
74  	}
75  
76  	public String getRequestHost()
77  	{
78  		return requestHost;
79  	}
80  
81  	public void setRequestHost( String requestHost )
82  	{
83  		this.requestHost = requestHost;
84  	}
85  
86  	public String getTargetHost()
87  	{
88  		return targetHost;
89  	}
90  
91  	public void setTargetHost( String targetHost )
92  	{
93  		this.targetHost = targetHost;
94  	}
95  
96  	public String getWsInterface()
97  	{
98  		return wsInterface;
99  	}
100 
101 	public void setWsInterface( String wsInterface )
102 	{
103 		this.wsInterface = wsInterface;
104 	}
105 
106 	public String getWsOperation()
107 	{
108 		return wsOperation;
109 	}
110 
111 	public void setWsOperation( String wsOperation )
112 	{
113 		this.wsOperation = wsOperation;
114 	}
115 
116 	public long getTimeTaken()
117 	{
118 		return timeTaken;
119 	}
120 
121 	private void setTimeTaken( long endTime )
122 	{
123 		this.timeTaken = -this.operationStarted + endTime;
124 	}
125 
126 	public int getRequestSize()
127 	{
128 		return requestSize;
129 	}
130 
131 	private void setRequestSize( int requestSizeInCharacters )
132 	{
133 		this.requestSize = requestSizeInCharacters;
134 	}
135 
136 	public int getResponseSize()
137 	{
138 		return responseSize;
139 	}
140 
141 	private void setResponseSize()
142 	{
143 		int length = this.response.length;
144 		this.responseSize = length;
145 	}
146 
147 	public byte[] getRequest()
148 	{
149 		return request;
150 	}
151 
152 	public void setRequest( byte[] request )
153 	{
154 		// this.request = request;
155 		if( this.request == null )
156 		{
157 			this.request = request;
158 		}
159 		else
160 		{
161 			byte[] newRequest = new byte[this.request.length + request.length];
162 			for( int i = 0; i < this.request.length; i++ )
163 			{
164 				newRequest[i] = this.request[i];
165 			}
166 			for( int i = this.request.length; i < newRequest.length; i++ )
167 			{
168 				newRequest[i] = request[i - this.response.length];
169 			}
170 			this.request = newRequest;
171 		}
172 		this.setRequestSize( this.request.length );
173 	}
174 
175 	public byte[] getResponse()
176 	{
177 		return response;
178 	}
179 
180 	public void setResponse( byte[] response )
181 	{
182 		if( this.response == null )
183 		{
184 			this.response = response;
185 		}
186 		else
187 		{
188 			byte[] newResponse = new byte[this.response.length + response.length];
189 			for( int i = 0; i < this.response.length; i++ )
190 			{
191 				newResponse[i] = this.response[i];
192 			}
193 			for( int i = this.response.length; i < newResponse.length; i++ )
194 			{
195 				newResponse[i] = response[i - this.response.length];
196 			}
197 			this.response = newResponse;
198 
199 		}
200 		this.setResponseSize();
201 	}
202 
203 	@Override
204 	public String toString()
205 	{
206 
207 		String toString = "Request host: " + this.requestHost + "\n";
208 		toString += "Request header : \n" + this.requestHeader + "\n";
209 		toString += "Request: " + this.request + "\n";
210 		toString += "Request size: " + this.requestSize + "\n";
211 		toString += "Response host:" + this.targetHost + "\n";
212 		toString += "Response header: \n" + this.responseHeader + "\n";
213 		toString += "Response: " + this.response + "\n";
214 		toString += "Response size:" + this.responseSize + "\n";
215 		toString += "Started: " + new Date( this.operationStarted ) + "\n";
216 		toString += "Time Taken: " + this.timeTaken + "ms\n";
217 		return toString;
218 
219 	}
220 
221 	@SuppressWarnings( "unchecked" )
222 	public void setRequestHeader( HttpServletRequest httpRequest, HttpServletResponse httpResponse )
223 	{
224 
225 		String headerValue = null;
226 		Enumeration<String> headerNames = httpRequest.getHeaderNames();
227 		while( headerNames.hasMoreElements() )
228 		{
229 			String name = headerNames.nextElement();
230 
231 			if( ProxyServlet.dontProxyHeaders.contains( name.toLowerCase() ) )
232 			{
233 				continue;
234 			}
235 
236 			headerValue = name + "::";
237 			Enumeration<String> header = httpRequest.getHeaders( name );
238 			while( header.hasMoreElements() )
239 			{
240 				String value = header.nextElement();
241 				if( value != null )
242 				{
243 					headerValue += value;
244 				}
245 			}
246 			requestHeader = requestHeader == null ? headerValue : requestHeader + "\n" + headerValue;
247 		}
248 	}
249 
250 	public void addResponseHeader( String responseHeader )
251 	{
252 		this.responseHeader = this.responseHeader == null ? responseHeader : this.responseHeader + "\n" + responseHeader;
253 	}
254 
255 }