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  
19  import javax.servlet.http.HttpServletResponse;
20  
21  import org.mortbay.jetty.HttpFields;
22  
23  import com.eviware.soapui.impl.wsdl.panels.mockoperation.WsdlMockResultMessageExchange;
24  import com.eviware.soapui.impl.wsdl.teststeps.actions.ShowMessageExchangeAction;
25  import com.eviware.soapui.model.mock.MockResult;
26  import com.eviware.soapui.support.action.swing.ActionList;
27  import com.eviware.soapui.support.action.swing.DefaultActionList;
28  import com.eviware.soapui.support.types.StringToStringMap;
29  
30  /***
31   * The result of a handled WsdlMockRequest
32   * 
33   * @author ole.matzura
34   */
35  
36  public class WsdlMockResult implements MockResult
37  {
38  	private WsdlMockResponse mockResponse;
39  	private String responseContent;
40  	private long timeTaken;
41  	private long timestamp;
42  	private DefaultActionList actions;
43  	private StringToStringMap responseHeaders = new StringToStringMap();
44  	private WsdlMockRequest mockRequest;
45  	private HttpServletResponse response;
46  
47  	public WsdlMockResult( WsdlMockRequest request, HttpServletResponse response ) throws Exception
48  	{
49  		this.response = response;
50  		timestamp = System.currentTimeMillis();
51  		mockRequest = request;
52  	}
53  
54  	public WsdlMockRequest getMockRequest()
55  	{
56  		return mockRequest;
57  	}
58  
59  	public ActionList getActions()
60  	{
61  		if( actions == null )
62  		{
63  			actions = new DefaultActionList( "MockResult" );
64  			actions.setDefaultAction( new ShowMessageExchangeAction( new WsdlMockResultMessageExchange( this ), "MockResult") );
65  		}
66  		
67  		return actions;
68  	}
69  
70  	public WsdlMockResponse getMockResponse()
71  	{
72  		return mockResponse;
73  	}
74  	
75  	public String getResponseContent()
76  	{
77  		return responseContent;
78  	}
79  	
80  	public long getTimeTaken()
81  	{
82  		return timeTaken;
83  	}
84  
85  	public long getTimestamp()
86  	{
87  		return timestamp;
88  	}
89  
90  	public void setTimestamp( long timestamp )
91  	{
92  		this.timestamp = timestamp;
93  	}
94  
95  	public void setTimeTaken( long timeTaken )
96  	{
97  		this.timeTaken = timeTaken;
98  	}
99  	
100 	public StringToStringMap getResponseHeaders()
101 	{
102 		return responseHeaders;
103 	}
104 
105 	public void setMockResponse( WsdlMockResponse mockResponse )
106 	{
107 		this.mockResponse = mockResponse;
108 	}
109 
110 	/***
111 	 * @deprecated
112 	 */
113 	
114 	public void setReponseContent( String responseContent )
115 	{
116 		this.responseContent = responseContent;
117 	}
118 	
119 	public void setResponseContent( String responseContent )
120 	{
121 		this.responseContent = responseContent;
122 	}
123 	
124 	@SuppressWarnings("unchecked")
125 	public void finish()
126 	{
127 		HttpFields httpFields = ((org.mortbay.jetty.Response)response).getHttpFields();
128 		
129 		Enumeration<String> e = httpFields.getFieldNames();
130 		while( e.hasMoreElements() )
131 		{
132 			String nextElement = e.nextElement();
133 			responseHeaders.put( nextElement, httpFields.getStringField( nextElement ) );
134 		}
135 		
136 		response = null;
137 	}
138 
139 	public void addHeader( String name, String value )
140 	{
141 		if( response != null )
142 			response.addHeader( name, value );
143 		else
144 			responseHeaders.put( name, value );
145 	}
146 
147 	public boolean isCommitted()
148 	{
149 		return response.isCommitted();
150 	}
151 
152 	public void setContentType( String string )
153 	{
154 		response.setContentType( string );
155 	}
156 
157 	public OutputStream getOutputStream() throws IOException
158 	{
159 		return response.getOutputStream();
160 	}
161 
162 	public void initResponse()
163 	{
164 		response.setStatus( HttpServletResponse.SC_OK );
165 	}
166 
167 	public boolean isDiscarded()
168 	{
169 		return false;
170 	}
171 }