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