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  
13  package com.eviware.soapui.impl.wsdl.mock;
14  
15  import java.io.IOException;
16  import java.io.OutputStream;
17  import java.util.Enumeration;
18  import java.util.Vector;
19  
20  import javax.servlet.http.HttpServletResponse;
21  
22  import org.mortbay.jetty.HttpFields;
23  
24  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResultMessageExchange;
25  import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowMessageExchangeAction;
26  import com.eviware.soapui.model.mock.MockResult;
27  import com.eviware.soapui.support.action.swing.ActionList;
28  import com.eviware.soapui.support.action.swing.DefaultActionList;
29  import com.eviware.soapui.support.types.StringToStringMap;
30  
31  /***
32   * The result of a handled WsdlMockRequest
33   * 
34   * @author ole.matzura
35   */
36  
37  public class WsdlMockResult implements MockResult
38  {
39  	private WsdlMockResponse mockResponse;
40  	private String responseContent;
41  	private long timeTaken;
42  	private long timestamp;
43  	private DefaultActionList actions;
44  	private StringToStringMap responseHeaders = new StringToStringMap();
45  	private WsdlMockRequest mockRequest;
46  	private byte[] rawResponseData;
47  	private WsdlMockOperation mockOperation;
48  	private String responseContentType;
49  	private int responseStatus = 200;
50  
51  	public WsdlMockResult( WsdlMockRequest request ) throws Exception
52  	{
53  		timestamp = System.currentTimeMillis();
54  		mockRequest = request;
55  	}
56  
57  	public WsdlMockRequest getMockRequest()
58  	{
59  		return mockRequest;
60  	}
61  
62  	public ActionList getActions()
63  	{
64  		if( actions == null )
65  		{
66  			actions = new DefaultActionList( "MockResult" );
67  			actions.setDefaultAction( new ShowMessageExchangeAction(
68  					new WsdlMockResultMessageExchange( this, mockResponse ), "MockResult" ) );
69  		}
70  
71  		return actions;
72  	}
73  
74  	public WsdlMockResponse getMockResponse()
75  	{
76  		return mockResponse;
77  	}
78  
79  	public String getResponseContent()
80  	{
81  		return responseContent;
82  	}
83  
84  	public long getTimeTaken()
85  	{
86  		return timeTaken;
87  	}
88  
89  	public long getTimestamp()
90  	{
91  		return timestamp;
92  	}
93  
94  	public void setTimestamp( long timestamp )
95  	{
96  		this.timestamp = timestamp;
97  	}
98  
99  	public void setTimeTaken( long timeTaken )
100 	{
101 		this.timeTaken = timeTaken;
102 	}
103 
104 	public StringToStringMap getResponseHeaders()
105 	{
106 		return responseHeaders;
107 	}
108 
109 	public void setMockResponse( WsdlMockResponse mockResponse )
110 	{
111 		this.mockResponse = mockResponse;
112 		mockRequest.getRequestContext().setMockResponse( mockResponse );
113 	}
114 
115 	/***
116 	 * @deprecated
117 	 */
118 
119 	public void setReponseContent( String responseContent )
120 	{
121 		this.responseContent = responseContent;
122 	}
123 
124 	public void setResponseContent( String responseContent )
125 	{
126 		this.responseContent = responseContent;
127 	}
128 
129 	@SuppressWarnings( "unchecked" )
130 	public void finish()
131 	{
132 		if( mockRequest.getHttpResponse() instanceof org.mortbay.jetty.Response )
133 		{
134 			HttpFields httpFields = ( ( org.mortbay.jetty.Response )mockRequest.getHttpResponse() ).getHttpFields();
135 
136 			Enumeration<String> e = httpFields.getFieldNames();
137 			while( e.hasMoreElements() )
138 			{
139 				String nextElement = e.nextElement();
140 				responseHeaders.put( nextElement, httpFields.getStringField( nextElement ) );
141 			}
142 		}
143 	}
144 
145 	public void addHeader( String name, String value )
146 	{
147 		if( mockRequest.getHttpResponse() != null )
148 			mockRequest.getHttpResponse().addHeader( name, value );
149 		else
150 			responseHeaders.put( name, value );
151 	}
152 
153 	public boolean isCommitted()
154 	{
155 		return mockRequest.getHttpResponse().isCommitted();
156 	}
157 
158 	public void setContentType( String contentType )
159 	{
160 		mockRequest.getHttpResponse().setContentType( contentType );
161 		responseContentType = contentType;
162 	}
163 
164 	public OutputStream getOutputStream() throws IOException
165 	{
166 		return mockRequest.getHttpResponse().getOutputStream();
167 	}
168 
169 	public void initResponse()
170 	{
171 		mockRequest.getHttpResponse().setStatus( HttpServletResponse.SC_OK );
172 		responseStatus = HttpServletResponse.SC_OK;
173 	}
174 
175 	public boolean isDiscarded()
176 	{
177 		return false;
178 	}
179 
180 	public Vector<?> getRequestWssResult()
181 	{
182 		return mockRequest.getWssResult();
183 	}
184 
185 	public byte[] getRawResponseData()
186 	{
187 		return rawResponseData;
188 	}
189 
190 	public void setRawResponseData( byte[] rawResponseData )
191 	{
192 		this.rawResponseData = rawResponseData;
193 	}
194 
195 	public void writeRawResponseData( byte[] bs ) throws IOException
196 	{
197 		getOutputStream().write( bs );
198 		setRawResponseData( bs );
199 	}
200 
201 	public void setMockOperation( WsdlMockOperation mockOperation )
202 	{
203 		this.mockOperation = mockOperation;
204 	}
205 
206 	public WsdlMockOperation getMockOperation()
207 	{
208 		if( mockOperation != null )
209 			return mockOperation;
210 
211 		return mockResponse == null ? null : mockResponse.getMockOperation();
212 	}
213 
214 	public String getResponseContentType()
215 	{
216 		return responseContentType;
217 	}
218 
219 	public int getResponseStatus()
220 	{
221 		return responseStatus;
222 	}
223 
224 	public void setResponseStatus( int responseStatus )
225 	{
226 		this.responseStatus = responseStatus;
227 	}
228 
229 	public void setResponseContentType( String responseContentType )
230 	{
231 		this.responseContentType = responseContentType;
232 	}
233 }