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