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.submit.transports.http;
14  
15  import java.io.UnsupportedEncodingException;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.impl.support.AbstractHttpRequest;
19  import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
20  import com.eviware.soapui.impl.wsdl.WsdlRequest;
21  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
22  import com.eviware.soapui.settings.WsdlSettings;
23  import com.eviware.soapui.support.StringUtils;
24  import com.eviware.soapui.support.xml.XmlUtils;
25  
26  /***
27   * Simple response to a request
28   * 
29   * @author ole.matzura
30   */
31  
32  public class SinglePartHttpResponse extends BaseHttpResponse
33  {
34  	private String responseContent;
35  	private String requestContent;
36  	private boolean prettyPrint;
37  	private long responseSize;
38  
39  	public SinglePartHttpResponse( AbstractHttpRequestInterface<?> httpRequest, ExtendedHttpMethod httpMethod,
40  			String requestContent, PropertyExpansionContext context )
41  	{
42  		super( httpMethod, httpRequest );
43  		
44  		if( getRequestContent() == null || !getRequestContent().equals( requestContent ))
45  			this.requestContent = requestContent;
46  
47  		try
48  		{
49  			byte[] responseBody = httpMethod.getResponseBody();
50  			int contentOffset = 0;
51  			if( responseBody == null )
52  				responseBody = new byte[0];
53  
54  			responseSize = responseBody.length;
55  
56  			String contentType = httpMethod.getResponseContentType();
57  			String charset = httpMethod.getResponseCharSet();
58  
59  			if( contentType != null && contentType.toLowerCase().endsWith( "xml" ) )
60  			{
61  				if( responseSize > 3 && responseBody[0] == ( byte )239 && responseBody[1] == ( byte )187
62  						&& responseBody[2] == ( byte )191 )
63  				{
64  					charset = "UTF-8";
65  					contentOffset = 3;
66  				}
67  			}
68  
69  			if( charset == null )
70  				charset = httpRequest.getEncoding();
71  
72  			charset = StringUtils.unquote( charset );
73  
74  			try
75  			{
76  				responseContent = responseBody.length == 0 ? null : charset == null ? new String( responseBody,
77  						contentOffset, ( int )( responseSize - contentOffset ) ) : new String( responseBody, contentOffset,
78  						( int )( responseSize - contentOffset ), charset );
79  			}
80  			catch( UnsupportedEncodingException e )
81  			{
82  				SoapUI.getErrorLog().warn( e.toString() );
83  				responseContent = new String( responseBody, contentOffset, ( int )( responseSize - contentOffset ) );
84  			}
85  
86  			prettyPrint = httpRequest.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES );
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 		( ( AbstractHttpRequest<?> )getRequest() ).notifyPropertyChanged( WsdlRequest.RESPONSE_CONTENT_PROPERTY,
126 				oldContent, responseContent );
127 	}
128 
129 	// public byte[] getRawRequestData()
130 	// {
131 	// return requestData;
132 	// }
133 	//
134 	// public byte[] getRawResponseData()
135 	// {
136 	// return responseBody;
137 	// }
138 
139 }