View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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.panels.teststeps.amf;
14  
15  import java.io.ByteArrayOutputStream;
16  import java.sql.SQLException;
17  import java.util.ArrayList;
18  
19  import javax.xml.parsers.ParserConfigurationException;
20  import javax.xml.transform.TransformerConfigurationException;
21  import javax.xml.transform.TransformerException;
22  
23  import org.apache.commons.httpclient.Header;
24  
25  import com.eviware.soapui.SoapUI;
26  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.methods.ExtendedPostMethod;
27  import com.eviware.soapui.model.iface.SubmitContext;
28  import com.eviware.soapui.model.support.AbstractResponse;
29  import com.eviware.soapui.support.types.StringToStringMap;
30  
31  import flex.messaging.io.amf.ActionMessage;
32  import flex.messaging.io.amf.MessageHeader;
33  
34  public class AMFResponse extends AbstractResponse<AMFRequest>
35  {
36  
37  	public static final String AMF_POST_METHOD = "AMF_POST_METHOD";
38  	public static final String AMF_RESPONSE_HEADERS = "responseHeaders";
39  	public static final String AMF_RESPONSE_ACTION_MESSAGE = "AMF_RESPONSE_ACTION_MESSAGE";
40  	public static final String AMF_RAW_RESPONSE_BODY = "AMF_RAW_RESPONSE_BODY";
41  
42  	private Object responseContent;
43  	private String responseContentXML = "";
44  	private long timeTaken;
45  	private long timestamp;
46  	private AMFRequest request;
47  	private StringToStringMap requestHeaders;
48  	private StringToStringMap responseHeaders;
49  	private StringToStringMap responseAMFHeaders = new StringToStringMap();
50  	private byte[] rawRequestData;
51  	private byte[] rawResponseData;
52  	private ActionMessage actionMessage;
53  	private byte[] rawResponseBody;
54  
55  	@SuppressWarnings( "unchecked" )
56  	public AMFResponse( AMFRequest request, SubmitContext submitContext, Object responseContent ) throws SQLException,
57  			ParserConfigurationException, TransformerConfigurationException, TransformerException
58  	{
59  		super( request );
60  
61  		this.request = request;
62  		this.responseContent = responseContent;
63  		if( responseContent != null )
64  			setResponseContentXML( new com.thoughtworks.xstream.XStream().toXML( responseContent ) );
65  		this.actionMessage = ( ActionMessage )submitContext.getProperty( AMF_RESPONSE_ACTION_MESSAGE );
66  		this.rawResponseBody = ( byte[] )submitContext.getProperty( AMF_RAW_RESPONSE_BODY );
67  		initHeaders( ( ExtendedPostMethod )submitContext.getProperty( AMF_POST_METHOD ) );
68  
69  	}
70  
71  	public String getContentAsString()
72  	{
73  		return getResponseContentXML();
74  	}
75  
76  	public String getContentType()
77  	{
78  		return "text/xml";
79  	}
80  
81  	public long getContentLength()
82  	{
83  		return rawResponseData != null ? rawResponseData.length : 0;
84  	}
85  
86  	public String getRequestContent()
87  	{
88  		return request.toString();
89  	}
90  
91  	public long getTimeTaken()
92  	{
93  		return timeTaken;
94  	}
95  
96  	public long getTimestamp()
97  	{
98  		return timestamp;
99  	}
100 
101 	public void setContentAsString( String content )
102 	{
103 		responseContent = content;
104 	}
105 
106 	public void setTimeTaken( long timeTaken )
107 	{
108 		this.timeTaken = timeTaken;
109 	}
110 
111 	public void setTimestamp( long timestamp )
112 	{
113 		this.timestamp = timestamp;
114 	}
115 
116 	public void setResponseContentXML( String responseContentXML )
117 	{
118 		this.responseContentXML = responseContentXML;
119 	}
120 
121 	public String getResponseContentXML()
122 	{
123 		return responseContentXML;
124 	}
125 
126 	protected void initHeaders( ExtendedPostMethod postMethod )
127 	{
128 		try
129 		{
130 			ByteArrayOutputStream rawResponse = new ByteArrayOutputStream();
131 			ByteArrayOutputStream rawRequest = new ByteArrayOutputStream();
132 
133 			if( !postMethod.isFailed() )
134 			{
135 				rawResponse.write( String.valueOf( postMethod.getStatusLine() ).getBytes() );
136 				rawResponse.write( "\r\n".getBytes() );
137 			}
138 
139 			rawRequest.write( ( postMethod.getMethod() + " " + postMethod.getURI().toString() + " "
140 					+ postMethod.getParams().getVersion().toString() + "\r\n" ).getBytes() );
141 
142 			requestHeaders = new StringToStringMap();
143 			Header[] headers = postMethod.getRequestHeaders();
144 			for( Header header : headers )
145 			{
146 				requestHeaders.put( header.getName(), header.getValue() );
147 				rawRequest.write( header.toExternalForm().getBytes() );
148 			}
149 
150 			if( !postMethod.isFailed() )
151 			{
152 				responseHeaders = new StringToStringMap();
153 				headers = postMethod.getResponseHeaders();
154 				for( Header header : headers )
155 				{
156 					responseHeaders.put( header.getName(), header.getValue() );
157 					rawResponse.write( header.toExternalForm().getBytes() );
158 				}
159 
160 				responseHeaders.put( "#status#", String.valueOf( postMethod.getStatusLine() ) );
161 			}
162 
163 			if( postMethod.getRequestEntity() != null )
164 			{
165 				rawRequest.write( "\r\n".getBytes() );
166 				if( postMethod.getRequestEntity().isRepeatable() )
167 				{
168 					postMethod.getRequestEntity().writeRequest( rawRequest );
169 				}
170 				else
171 					rawRequest.write( "<request data not available>".getBytes() );
172 			}
173 
174 			if( !postMethod.isFailed() )
175 			{
176 				rawResponse.write( "\r\n".getBytes() );
177 				rawResponse.write( rawResponseBody );
178 			}
179 
180 			rawResponseData = rawResponse.toByteArray();
181 			rawRequestData = rawRequest.toByteArray();
182 
183 			initAMFHeaders( postMethod );
184 
185 		}
186 		catch( Throwable e )
187 		{
188 			SoapUI.logError( e );
189 		}
190 	}
191 
192 	@SuppressWarnings( "unchecked" )
193 	private void initAMFHeaders( ExtendedPostMethod postMethod )
194 	{
195 		if( !postMethod.isFailed() && actionMessage != null )
196 		{
197 			ArrayList<MessageHeader> amfHeaders = actionMessage.getHeaders();
198 
199 			for( MessageHeader header : amfHeaders )
200 			{
201 				responseAMFHeaders.put( header.getName(), header.getData().toString() );
202 			}
203 		}
204 	}
205 
206 	public byte[] getRawRequestData()
207 	{
208 		return rawRequestData;
209 	}
210 
211 	public byte[] getRawResponseData()
212 	{
213 		return rawResponseData;
214 	}
215 
216 	public StringToStringMap getRequestHeaders()
217 	{
218 		return requestHeaders;
219 	}
220 
221 	public StringToStringMap getResponseHeaders()
222 	{
223 		return responseHeaders;
224 	}
225 
226 	public StringToStringMap getResponseAMFHeaders()
227 	{
228 		return responseAMFHeaders;
229 	}
230 
231 }