View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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 HttpServletResponse response;
47  	private byte[] rawResponseData;
48  
49  	public WsdlMockResult( WsdlMockRequest request, HttpServletResponse response ) throws Exception
50  	{
51  		this.response = response;
52  		timestamp = System.currentTimeMillis();
53  		mockRequest = request;
54  	}
55  
56  	public WsdlMockRequest getMockRequest()
57  	{
58  		return mockRequest;
59  	}
60  
61  	public ActionList getActions()
62  	{
63  		if( actions == null )
64  		{
65  			actions = new DefaultActionList( "MockResult" );
66  			actions.setDefaultAction( new ShowMessageExchangeAction( new WsdlMockResultMessageExchange( this, mockResponse ), "MockResult") );
67  		}
68  		
69  		return actions;
70  	}
71  
72  	public WsdlMockResponse getMockResponse()
73  	{
74  		return mockResponse;
75  	}
76  	
77  	public String getResponseContent()
78  	{
79  		return responseContent;
80  	}
81  	
82  	public long getTimeTaken()
83  	{
84  		return timeTaken;
85  	}
86  
87  	public long getTimestamp()
88  	{
89  		return timestamp;
90  	}
91  
92  	public void setTimestamp( long timestamp )
93  	{
94  		this.timestamp = timestamp;
95  	}
96  
97  	public void setTimeTaken( long timeTaken )
98  	{
99  		this.timeTaken = timeTaken;
100 	}
101 	
102 	public StringToStringMap getResponseHeaders()
103 	{
104 		return responseHeaders;
105 	}
106 
107 	public void setMockResponse( WsdlMockResponse mockResponse )
108 	{
109 		this.mockResponse = mockResponse;
110 		mockRequest.getRequestContext().setMockResponse( mockResponse );
111 	}
112 
113 	/***
114 	 * @deprecated
115 	 */
116 	
117 	public void setReponseContent( String responseContent )
118 	{
119 		this.responseContent = responseContent;
120 	}
121 	
122 	public void setResponseContent( String responseContent )
123 	{
124 		this.responseContent = responseContent;
125 	}
126 	
127 	@SuppressWarnings("unchecked")
128 	public void finish()
129 	{
130 		HttpFields httpFields = ((org.mortbay.jetty.Response)response).getHttpFields();
131 		
132 		Enumeration<String> e = httpFields.getFieldNames();
133 		while( e.hasMoreElements() )
134 		{
135 			String nextElement = e.nextElement();
136 			responseHeaders.put( nextElement, httpFields.getStringField( nextElement ) );
137 		}
138 		
139 		response = null;
140 	}
141 
142 	public void addHeader( String name, String value )
143 	{
144 		if( response != null )
145 			response.addHeader( name, value );
146 		else
147 			responseHeaders.put( name, value );
148 	}
149 
150 	public boolean isCommitted()
151 	{
152 		return response.isCommitted();
153 	}
154 
155 	public void setContentType( String string )
156 	{
157 		response.setContentType( string );
158 	}
159 
160 	public OutputStream getOutputStream() throws IOException
161 	{
162 		return response.getOutputStream();
163 	}
164 
165 	public void initResponse()
166 	{
167 		response.setStatus( HttpServletResponse.SC_OK );
168 	}
169 
170 	public boolean isDiscarded()
171 	{
172 		return false;
173 	}
174 
175 	public Vector getRequestWssResult()
176 	{
177 		return mockRequest.getWssResult();
178 	}
179 
180 	public byte [] getRawResponseData()
181 	{
182 		return rawResponseData;
183 	}
184 
185 	public void setRawResponseData( byte[] rawResponseData )
186 	{
187 		this.rawResponseData = rawResponseData;
188 	}
189 
190 	public void writeRawResponseData( byte[] bs ) throws IOException
191 	{
192 		getOutputStream().write( bs );
193 		setRawResponseData( bs );
194 	}
195 }