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