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.ByteArrayOutputStream;
016: import java.io.InputStream;
017: import java.util.ArrayList;
018: import java.util.List;
019:
020: import javax.activation.DataSource;
021: import javax.mail.BodyPart;
022: import javax.mail.MessagingException;
023: import javax.mail.internet.MimeMessage;
024: import javax.mail.internet.MimeMultipart;
025:
026: import com.eviware.soapui.SoapUI;
027: import com.eviware.soapui.impl.wsdl.WsdlOperation;
028: import com.eviware.soapui.model.iface.Attachment;
029: import com.eviware.soapui.settings.WsdlSettings;
030: import com.eviware.soapui.support.StringUtils;
031: import com.eviware.soapui.support.Tools;
032: import com.eviware.soapui.support.xml.XmlUtils;
033:
034: /**
035: * Utility class for managing large MultiParts
036: *
037: * @author ole.matzura
038: */
039:
040: public class MultipartMessageSupport {
041: private final List<BodyPartAttachment> attachments = new ArrayList<BodyPartAttachment>();
042: private Attachment rootPart;
043: private MimeMessage message;
044: private String responseContent;
045:
046: public MultipartMessageSupport(DataSource dataSource,
047: String rootPartId, WsdlOperation operation,
048: boolean isRequest) throws MessagingException {
049: MimeMultipart mp = new MimeMultipart(dataSource);
050: message = new MimeMessage(AttachmentUtils.JAVAMAIL_SESSION);
051: message.setContent(mp);
052:
053: for (int c = 0; c < mp.getCount(); c++) {
054: BodyPart bodyPart = mp.getBodyPart(c);
055:
056: if (bodyPart.getContentType().toUpperCase().startsWith(
057: "MULTIPART/")) {
058: MimeMultipart mp2 = new MimeMultipart(
059: new BodyPartDataSource(bodyPart));
060: for (int i = 0; i < mp2.getCount(); i++) {
061: attachments.add(new BodyPartAttachment(mp2
062: .getBodyPart(i), operation, isRequest));
063: }
064: } else {
065: BodyPartAttachment attachment = new BodyPartAttachment(
066: bodyPart, operation, isRequest);
067:
068: String[] contentIdHeaders = bodyPart
069: .getHeader("Content-ID");
070: if (contentIdHeaders != null
071: && contentIdHeaders.length > 0
072: && contentIdHeaders[0].equals(rootPartId)) {
073: rootPart = attachment;
074: } else
075: attachments.add(attachment);
076: }
077: }
078:
079: // if no explicit root part has been set, use the first one in the result
080: if (rootPart == null)
081: rootPart = attachments.remove(0);
082: }
083:
084: public void setOperation(WsdlOperation operation) {
085: for (BodyPartAttachment attachment : attachments) {
086: attachment.setOperation(operation);
087: }
088: }
089:
090: public Attachment[] getAttachments() {
091: return attachments.toArray(new Attachment[attachments.size()]);
092: }
093:
094: public Attachment getRootPart() {
095: return rootPart;
096: }
097:
098: public Attachment[] getAttachmentsForPart(String partName) {
099: List<Attachment> results = new ArrayList<Attachment>();
100:
101: for (Attachment attachment : attachments) {
102: if (attachment.getPart().equals(partName))
103: results.add(attachment);
104: }
105:
106: return results.toArray(new Attachment[results.size()]);
107: }
108:
109: public String getContentAsString() {
110: if (rootPart == null)
111: return null;
112:
113: if (responseContent == null) {
114: try {
115: InputStream in = rootPart.getInputStream();
116: ByteArrayOutputStream data = Tools.readAll(in,
117: Tools.READ_ALL);
118:
119: String contentType = rootPart.getContentType();
120: if (contentType != null
121: && contentType.indexOf("charset=") > 0) {
122: try {
123: int ix = contentType.indexOf("charset=");
124: int ix2 = contentType.indexOf(";", ix);
125:
126: String charset = ix2 == -1 ? contentType
127: .substring(ix + 8) : contentType
128: .substring(ix + 8, ix2);
129:
130: responseContent = data.toString(StringUtils
131: .unquote(charset));
132: } catch (Throwable e) {
133: SoapUI.logError(e);
134: }
135: }
136:
137: if (responseContent == null) {
138: responseContent = data.toString();
139: }
140:
141: if (SoapUI.getSettings().getBoolean(
142: WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES)) {
143: responseContent = XmlUtils
144: .prettyPrintXml(responseContent);
145: }
146:
147: return responseContent;
148: } catch (Exception e) {
149: SoapUI.logError(e);
150: }
151: }
152:
153: return responseContent;
154: }
155:
156: public void setResponseContent(String responseContent) {
157: this .responseContent = responseContent;
158: }
159:
160: public Attachment getAttachmentWithContentId(String contentId) {
161: for (Attachment attachment : attachments)
162: if (contentId.equals(attachment.getContentID()))
163: return attachment;
164:
165: return null;
166: }
167: }
|