001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.submit.transports.http;
014:
015: import java.io.BufferedOutputStream;
016: import java.io.ByteArrayInputStream;
017: import java.io.File;
018: import java.io.FileOutputStream;
019: import java.io.InputStream;
020: import java.io.OutputStream;
021: import java.net.MalformedURLException;
022:
023: import javax.mail.BodyPart;
024: import javax.mail.MessagingException;
025:
026: import org.apache.commons.codec.binary.Base64;
027: import org.apache.commons.codec.binary.Hex;
028:
029: import com.eviware.soapui.SoapUI;
030: import com.eviware.soapui.impl.wsdl.WsdlOperation;
031: import com.eviware.soapui.model.iface.Attachment;
032: import com.eviware.soapui.support.Tools;
033:
034: /**
035: * Attachment for a BodyPart
036: *
037: * @author ole.matzura
038: */
039:
040: public class BodyPartAttachment implements Attachment {
041: private final BodyPart bodyPart;
042: private File tempFile;
043: private WsdlOperation operation;
044: private final boolean isRequest;
045: private byte[] data;
046:
047: public BodyPartAttachment(BodyPart bodyPart,
048: WsdlOperation operation, boolean isRequest) {
049: this .bodyPart = bodyPart;
050: this .operation = operation;
051: this .isRequest = isRequest;
052: }
053:
054: public String getContentType() {
055: try {
056: return bodyPart.getContentType();
057: } catch (MessagingException e) {
058: SoapUI.logError(e);
059: return null;
060: }
061: }
062:
063: public AttachmentEncoding getEncoding() {
064: return AttachmentUtils.getAttachmentEncoding(operation,
065: getPart(), !isRequest);
066: }
067:
068: public InputStream getInputStream() throws Exception {
069: if (data != null)
070: return new ByteArrayInputStream(data);
071:
072: AttachmentEncoding encoding = getEncoding();
073: if (encoding == AttachmentEncoding.NONE)
074: return bodyPart.getInputStream();
075:
076: data = Tools.readAll(bodyPart.getInputStream(), Tools.READ_ALL)
077: .toByteArray();
078:
079: if (encoding == AttachmentEncoding.BASE64) {
080: if (Base64.isArrayByteBase64(data))
081: data = Tools.readAll(
082: new ByteArrayInputStream(Base64
083: .decodeBase64(data)), Tools.READ_ALL)
084: .toByteArray();
085: else
086: throw new Exception("Attachment content for part ["
087: + getPart() + "] is not base64 encoded");
088: } else if (encoding == AttachmentEncoding.HEX) {
089: data = Hex.decodeHex(new String(data).toCharArray());
090: }
091:
092: return new ByteArrayInputStream(data);
093: }
094:
095: public String getName() {
096: try {
097: String[] header = bodyPart.getHeader("Content-Id");
098: if (header == null || header.length == 0)
099: return "<missing name>";
100:
101: if (header[0].startsWith("<") && header[0].endsWith(">"))
102: header[0] = header[0].substring(1,
103: header[0].length() - 1);
104:
105: return header[0];
106: } catch (MessagingException e) {
107: SoapUI.logError(e);
108: return null;
109: }
110: }
111:
112: public String getPart() {
113: String name = getName();
114: int ix = name.indexOf('=');
115: if (ix > 0) {
116: name = name.substring(0, ix);
117: }
118:
119: return name;
120: }
121:
122: public long getSize() {
123: try {
124: getInputStream();
125: return data == null ? bodyPart.getSize() : data.length;
126: } catch (Exception e) {
127: SoapUI.logError(e);
128: return -1;
129: }
130: }
131:
132: public String getUrl() {
133: if (tempFile == null) {
134: String contentType = getContentType();
135: int ix = contentType.lastIndexOf('/');
136: int iy = -1;
137: if (ix != -1)
138: iy = contentType.indexOf(';', ix);
139:
140: try {
141: tempFile = File.createTempFile("response-attachment",
142: (ix == -1 ? ".dat" : "."
143: + (iy == -1 ? contentType
144: .substring(ix + 1)
145: : contentType.substring(ix + 1,
146: iy))));
147:
148: OutputStream out = new BufferedOutputStream(
149: new FileOutputStream(tempFile));
150: InputStream inputStream = getInputStream();
151: out.write(Tools.readAll(inputStream, 0).toByteArray());
152: out.flush();
153: out.close();
154:
155: inputStream.reset();
156: } catch (Exception e) {
157: SoapUI.logError(e);
158: }
159: }
160:
161: try {
162: return tempFile.toURL().toString();
163: } catch (MalformedURLException e) {
164: SoapUI.logError(e);
165: return null;
166: }
167: }
168:
169: public void setContentType(String contentType) {
170: }
171:
172: public void setPart(String part) {
173: }
174:
175: public boolean isCached() {
176: return true;
177: }
178:
179: public AttachmentType getAttachmentType() {
180: return AttachmentType.UNKNOWN;
181: }
182:
183: public void release() {
184: operation = null;
185: }
186:
187: public String getContentID() {
188: try {
189: String[] header = bodyPart.getHeader("Content-ID");
190: if (header != null && header.length > 0)
191: return header[0];
192: } catch (MessagingException e) {
193: SoapUI.logError(e);
194: }
195:
196: return null;
197: }
198:
199: public void setOperation(WsdlOperation operation) {
200: this.operation = operation;
201: }
202: }
|