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