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.SoapUI;
31  import com.eviware.soapui.impl.wsdl.support.xsd.SchemaUtils;
32  
33  /***
34   * DataSource for XOP/MTOM attachments
35   * 
36   * @author ole.matzura
37   */
38  
39  public final class XOPPartDataSource implements DataSource
40  {
41  	private String content;
42  	private final String contentType;
43  	private final SchemaType schemaType;
44  	private File source;
45  
46  	public XOPPartDataSource(String content, String contentType, SchemaType schemaType)
47  	{
48  		this.content = content;
49  		this.contentType = contentType;
50  		this.schemaType = schemaType;
51  	}
52  	
53  	public XOPPartDataSource(File source, String contentType, SchemaType schemaType)
54  	{
55  		this.source = source;
56  		this.contentType = contentType;
57  		this.schemaType = schemaType;
58  	}
59  
60  	public String getContentType()
61  	{
62  		return contentType;
63  	}
64  
65  	public InputStream getInputStream() throws IOException
66  	{
67  		try
68  		{
69  			if( source != null )
70  			{
71  				return new FileInputStream( source );
72  			}
73  			if (SchemaUtils.isInstanceOf( schemaType, XmlHexBinary.type ))
74  			{
75  				return new ByteArrayInputStream(Hex.decodeHex(content.toCharArray()));
76  			}
77  			else if (SchemaUtils.isInstanceOf( schemaType, XmlBase64Binary.type ))
78  			{
79  				return new ByteArrayInputStream( Base64.decodeBase64( content.getBytes() ));
80  			}
81  			else throw new IOException( "Invalid type for XOPPartDataSource; " + schemaType.getName() );
82  		}
83  		catch (Exception e)
84  		{
85  			SoapUI.logError( e );
86  			throw new IOException( e.toString() );
87  		}			
88  	}
89  
90  	public String getName()
91  	{
92  		return schemaType.getName().toString();
93  	}
94  
95  	public OutputStream getOutputStream() throws IOException
96  	{
97  		return null;
98  	}
99  }