View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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;
14  
15  import java.io.ByteArrayInputStream;
16  import java.io.File;
17  import java.io.FileInputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  
22  import javax.activation.DataSource;
23  
24  import org.apache.commons.codec.binary.Base64;
25  import org.apache.commons.codec.binary.Hex;
26  import org.apache.xmlbeans.SchemaType;
27  import org.apache.xmlbeans.XmlBase64Binary;
28  import org.apache.xmlbeans.XmlHexBinary;
29  
30  import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
31  
32  public final class XOPPartDataSource implements DataSource
33  {
34  	private String content;
35  	private final String contentType;
36  	private final SchemaType schemaType;
37  	private File source;
38  
39  	public XOPPartDataSource(String content, String contentType, SchemaType schemaType)
40  	{
41  		this.content = content;
42  		this.contentType = contentType;
43  		this.schemaType = schemaType;
44  	}
45  	
46  	public XOPPartDataSource(File source, String contentType, SchemaType schemaType)
47  	{
48  		this.source = source;
49  		this.contentType = contentType;
50  		this.schemaType = schemaType;
51  	}
52  
53  	public String getContentType()
54  	{
55  		return contentType;
56  	}
57  
58  	public InputStream getInputStream() throws IOException
59  	{
60  		try
61  		{
62  			if( source != null )
63  			{
64  				return new FileInputStream( source );
65  			}
66  			if (SchemaUtils.isInstanceOf( schemaType, XmlHexBinary.type ))
67  			{
68  				return new ByteArrayInputStream(Hex.decodeHex(content.toCharArray()));
69  			}
70  			else if (SchemaUtils.isInstanceOf( schemaType, XmlBase64Binary.type ))
71  			{
72  				return new ByteArrayInputStream( Base64.decodeBase64( content.getBytes() ));
73  			}
74  			else throw new IOException( "Invalid type for XOPPartDataSource; " + schemaType.getName() );
75  		}
76  		catch (Exception e)
77  		{
78  			e.printStackTrace();
79  			throw new IOException( e.toString() );
80  		}			
81  	}
82  
83  	public String getName()
84  	{
85  		return schemaType.getName().toString();
86  	}
87  
88  	public OutputStream getOutputStream() throws IOException
89  	{
90  		return null;
91  	}
92  }