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.support.attachments;
14  
15  import java.io.ByteArrayInputStream;
16  import java.io.IOException;
17  import java.io.InputStream;
18  import java.io.OutputStream;
19  
20  import javax.activation.DataSource;
21  
22  import com.eviware.soapui.SoapUI;
23  import com.eviware.soapui.impl.wsdl.submit.transports.http.ExtendedHttpMethod;
24  import com.eviware.soapui.impl.wsdl.support.CompressionSupport;
25  import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport;
26  import com.eviware.soapui.settings.HttpSettings;
27  
28  /***
29   * DataSource for a standard POST response
30   * 
31   * @author ole.matzura
32   */
33  
34  public class PostResponseDataSource implements DataSource
35  {
36  	private final ExtendedHttpMethod postMethod;
37  	private byte[] data;
38  
39  	public PostResponseDataSource( ExtendedHttpMethod postMethod )
40  	{
41  		this.postMethod = postMethod;
42  
43  		try
44  		{
45  			data = postMethod.getResponseBody(); // Tools.readAll(
46  																// postMethod.getResponseBodyAsStream(),
47  																// 0 ).toByteArray();
48  
49  			if( !SoapUI.getSettings().getBoolean( HttpSettings.DISABLE_RESPONSE_DECOMPRESSION ) )
50  			{
51  				String compressionAlg = HttpClientSupport.getResponseCompressionType( postMethod );
52  				if( compressionAlg != null )
53  					data = CompressionSupport.decompress( compressionAlg, data );
54  			}
55  		}
56  		catch( Exception e )
57  		{
58  			SoapUI.logError( e );
59  		}
60  	}
61  
62  	public long getDataSize()
63  	{
64  		return data == null ? -1 : data.length;
65  	}
66  
67  	public String getContentType()
68  	{
69  		return postMethod.getResponseHeader( "Content-Type" ).getValue();
70  	}
71  
72  	public InputStream getInputStream() throws IOException
73  	{
74  		return new ByteArrayInputStream( data );
75  	}
76  
77  	public String getName()
78  	{
79  		return postMethod.getName() + " response for " + postMethod.getPath().toString();
80  	}
81  
82  	public OutputStream getOutputStream() throws IOException
83  	{
84  		return null;
85  	}
86  
87  	public byte[] getData()
88  	{
89  		return data;
90  	}
91  }