View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.submit.transports.http;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
17  import com.eviware.soapui.impl.wsdl.WsdlRequest;
18  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
19  import com.eviware.soapui.settings.WsdlSettings;
20  import com.eviware.soapui.support.StringUtils;
21  import com.eviware.soapui.support.xml.XmlUtils;
22  
23  /***
24   * Simple response to a request
25   * 
26   * @author ole.matzura
27   */
28  
29  public class SinglePartHttpResponse extends BaseHttpResponse
30  {
31  	private String responseContent;
32  	private String requestContent;
33  	private boolean prettyPrint;
34  	private long responseSize;
35  
36  	public SinglePartHttpResponse( AbstractHttpRequestInterface<?> httpRequest, ExtendedHttpMethod httpMethod,
37  			String requestContent, PropertyExpansionContext context )
38  	{
39  		super( httpMethod, httpRequest );
40  
41  		if( getRequestContent() == null )
42  			this.requestContent = requestContent;
43  
44  		try
45  		{
46  			byte[] responseBody = httpMethod.getResponseBody();
47  			int contentOffset = 0;
48  			if( responseBody == null )
49  				responseBody = new byte[0];
50  
51  			responseSize = responseBody.length;
52  
53  			String contentType = httpMethod.getResponseContentType();
54  			String charset = httpMethod.getResponseCharSet();
55  
56  			if( contentType != null && contentType.toLowerCase().endsWith( "xml" ) )
57  			{
58  				if( responseSize > 3 && responseBody[0] == ( byte )239 && responseBody[1] == ( byte )187
59  						&& responseBody[2] == ( byte )191 )
60  				{
61  					charset = "UTF-8";
62  					contentOffset = 3;
63  				}
64  			}
65  
66  			if( charset == null )
67  				charset = httpRequest.getEncoding();
68  
69  			charset = StringUtils.unquote( charset );
70  
71  			responseContent = responseBody.length == 0 ? null : charset == null ? new String( responseBody ) : new String(
72  					responseBody, contentOffset, ( int )( responseSize - contentOffset ), charset );
73  
74  			prettyPrint = httpRequest.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
75  
76  			// RequestEntity requestEntity = httpMethod.getRequestEntity();
77  			// if( requestEntity != null )
78  			// {
79  			// ByteArrayOutputStream out = new ByteArrayOutputStream();
80  			// requestEntity.writeRequest( out );
81  			// requestData = out.toByteArray();
82  			// }
83  			// else if( StringUtils.hasContent( requestContent ))
84  			// {
85  			// requestData = requestContent.getBytes();
86  			// }
87  		}
88  		catch( Exception e )
89  		{
90  			SoapUI.logError( e );
91  		}
92  	}
93  
94  	public String getContentAsString()
95  	{
96  		if( prettyPrint )
97  		{
98  			responseContent = XmlUtils.prettyPrintXml( responseContent );
99  			prettyPrint = false;
100 		}
101 
102 		return responseContent;
103 	}
104 
105 	protected String getResponseContent()
106 	{
107 		return responseContent;
108 	}
109 	
110 	public long getContentLength()
111 	{
112 		return responseSize;
113 	}
114 
115 	public String getRequestContent()
116 	{
117 		return requestContent == null ? super.getRequestContent() : requestContent;
118 	}
119 
120 	public void setResponseContent( String responseContent )
121 	{
122 		String oldContent = this.responseContent;
123 		this.responseContent = responseContent;
124 
125 		getRequest().notifyPropertyChanged( WsdlRequest.RESPONSE_CONTENT_PROPERTY, oldContent, responseContent );
126 	}
127 
128 	// public byte[] getRawRequestData()
129 	// {
130 	// return requestData;
131 	// }
132 	//
133 	// public byte[] getRawResponseData()
134 	// {
135 	// return responseBody;
136 	// }
137 
138 }