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