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