1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments;
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 import com.eviware.soapui.support.StringUtils;
33 import com.eviware.soapui.support.editor.inspectors.attachments.ContentTypeHandler;
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 if( SchemaUtils.isAnyType( schemaType ))
84 {
85 return new ByteArrayInputStream( content.getBytes() );
86 }
87 else
88 throw new IOException( "Invalid type for XOPPartDataSource; " + schemaType.getName() );
89 }
90 catch( Exception e )
91 {
92 SoapUI.logError( e );
93 throw new IOException( e.toString() );
94 }
95 }
96
97 public String getName()
98 {
99 return String.valueOf( schemaType.getName());
100 }
101
102 public OutputStream getOutputStream() throws IOException
103 {
104 return null;
105 }
106 }