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