01: /*
02: * soapUI, copyright (C) 2004-2007 eviware.com
03: *
04: * soapUI is free software; you can redistribute it and/or modify it under the
05: * terms of version 2.1 of the GNU Lesser General Public License as published by
06: * the Free Software Foundation.
07: *
08: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
09: * 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: private String content;
41: private final String contentType;
42: private final SchemaType schemaType;
43: private File source;
44:
45: public XOPPartDataSource(String content, String contentType,
46: SchemaType schemaType) {
47: this .content = content;
48: this .contentType = contentType;
49: this .schemaType = schemaType;
50: }
51:
52: public XOPPartDataSource(File source, String contentType,
53: SchemaType schemaType) {
54: this .source = source;
55: this .contentType = contentType;
56: this .schemaType = schemaType;
57: }
58:
59: public String getContentType() {
60: return contentType;
61: }
62:
63: public InputStream getInputStream() throws IOException {
64: try {
65: if (source != null) {
66: return new FileInputStream(source);
67: }
68: if (SchemaUtils.isInstanceOf(schemaType, XmlHexBinary.type)) {
69: return new ByteArrayInputStream(Hex.decodeHex(content
70: .toCharArray()));
71: } else if (SchemaUtils.isInstanceOf(schemaType,
72: XmlBase64Binary.type)) {
73: return new ByteArrayInputStream(Base64
74: .decodeBase64(content.getBytes()));
75: } else
76: throw new IOException(
77: "Invalid type for XOPPartDataSource; "
78: + schemaType.getName());
79: } catch (Exception e) {
80: SoapUI.logError(e);
81: throw new IOException(e.toString());
82: }
83: }
84:
85: public String getName() {
86: return schemaType.getName().toString();
87: }
88:
89: public OutputStream getOutputStream() throws IOException {
90: return null;
91: }
92: }
|