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.impl.wsdl.mock.WsdlMockResponse;
16  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
17  
18  import javax.activation.DataSource;
19  import java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.OutputStream;
23  
24  /***
25   * DataSource for an existing WsdlMockResponse  
26   * 
27   * @author ole.matzura
28   */
29  
30  public class MockResponseDataSource implements DataSource
31  {
32  	private final String responseContent;
33  	private final boolean isXOP;
34  	private final WsdlMockResponse mockResponse;
35  
36  	public MockResponseDataSource(WsdlMockResponse mockResponse, String responseContent, boolean isXOP)
37  	{
38  		this.mockResponse = mockResponse;
39  		this.responseContent = responseContent;
40  		this.isXOP = isXOP;
41  	}
42  
43  	public String getContentType()
44  	{
45  		SoapVersion soapVersion = mockResponse.getSoapVersion();
46  		
47  		if( isXOP )
48  		{
49  			return AttachmentUtils.buildRootPartContentType( mockResponse.getMockOperation().getOperation().getName(),
50  		   			soapVersion);	
51  		}
52  		else
53  			return soapVersion.getContentType() + "; charset=UTF-8";
54  	}
55  
56  	public InputStream getInputStream() throws IOException
57  	{
58  		byte[] bytes = responseContent.getBytes( "UTF-8");
59  		return new ByteArrayInputStream( bytes);
60  	}
61  
62  	public String getName()
63  	{
64  		return mockResponse.getName();
65  	}
66  
67  	public OutputStream getOutputStream() throws IOException
68  	{
69  		return null;
70  	}
71  }