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.support.Tools;
17  
18  import javax.activation.DataSource;
19  import javax.servlet.http.HttpServletRequest;
20  import java.io.ByteArrayInputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  /***
26   * DataSource for a MockRequest
27   * 
28   * @author ole.matzura
29   */
30  
31  public class MockRequestDataSource implements DataSource
32  {
33  	private byte[] data;
34  	private String contentType;
35  	private String name;
36  
37  	public MockRequestDataSource(HttpServletRequest request)
38  	{
39  		try
40  		{
41  			data = Tools.readAll( request.getInputStream(), 0 ).toByteArray();
42  			contentType = request.getContentType();
43  			name = "Request for " + request.getPathInfo();
44  		}
45  		catch (Exception e)
46  		{
47  			SoapUI.logError( e );
48  		}		
49  	}
50  
51  	public String getContentType()
52  	{
53  		return contentType;
54  	}
55  
56  	public InputStream getInputStream() throws IOException
57  	{
58  		return new ByteArrayInputStream( data );
59  	}
60  
61  	public String getName()
62  	{
63  		return name;
64  	}
65  
66  	public OutputStream getOutputStream() throws IOException
67  	{
68  		return null;
69  	}
70  
71  	public byte[] getData()
72  	{
73  		return data;
74  	}
75  }