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