1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.submit.transports.http;
14
15 import java.io.ByteArrayOutputStream;
16 import java.io.InputStream;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import javax.activation.DataSource;
21 import javax.mail.BodyPart;
22 import javax.mail.MessagingException;
23 import javax.mail.internet.MimeMessage;
24 import javax.mail.internet.MimeMultipart;
25
26 import com.eviware.soapui.SoapUI;
27 import com.eviware.soapui.impl.wsdl.WsdlOperation;
28 import com.eviware.soapui.model.iface.Attachment;
29 import com.eviware.soapui.support.StringUtils;
30 import com.eviware.soapui.support.Tools;
31 import com.eviware.soapui.support.xml.XmlUtils;
32
33 /***
34 * Utility class for managing large MultiParts
35 *
36 * @author ole.matzura
37 */
38
39 public class MultipartMessageSupport
40 {
41 private final List<BodyPartAttachment> attachments = new ArrayList<BodyPartAttachment>();
42 private Attachment rootPart;
43 private MimeMessage message;
44 private String responseContent;
45 private final boolean prettyPrint;
46
47 public MultipartMessageSupport( DataSource dataSource, String rootPartId, WsdlOperation operation, boolean isRequest, boolean prettyPrint ) throws MessagingException
48 {
49 this.prettyPrint = prettyPrint;
50 MimeMultipart mp = new MimeMultipart( dataSource);
51 message = new MimeMessage( AttachmentUtils.JAVAMAIL_SESSION );
52 message.setContent( mp );
53
54 for( int c = 0; c < mp.getCount(); c++ )
55 {
56 BodyPart bodyPart = mp.getBodyPart( c );
57
58 if( bodyPart.getContentType().toUpperCase().startsWith( "MULTIPART/"))
59 {
60 MimeMultipart mp2 = new MimeMultipart( new BodyPartDataSource( bodyPart ));
61 for( int i = 0; i < mp2.getCount(); i++ )
62 {
63 attachments.add( new BodyPartAttachment( mp2.getBodyPart(i), operation, isRequest ));
64 }
65 }
66 else
67 {
68 BodyPartAttachment attachment = new BodyPartAttachment( bodyPart, operation, isRequest );
69
70 String[] contentIdHeaders = bodyPart.getHeader( "Content-ID");
71 if( contentIdHeaders != null && contentIdHeaders.length > 0 && contentIdHeaders[0].equals( rootPartId ))
72 {
73 rootPart = attachment;
74 }
75 else
76 attachments.add( attachment );
77 }
78 }
79
80
81 if( rootPart == null )
82 rootPart = attachments.remove( 0 );
83 }
84
85 public void setOperation( WsdlOperation operation )
86 {
87 for( BodyPartAttachment attachment : attachments )
88 {
89 attachment.setOperation( operation );
90 }
91 }
92
93 public Attachment[] getAttachments()
94 {
95 return attachments.toArray( new Attachment[attachments.size()]);
96 }
97
98 public Attachment getRootPart()
99 {
100 return rootPart;
101 }
102
103 public Attachment[] getAttachmentsForPart(String partName)
104 {
105 List<Attachment> results = new ArrayList<Attachment>();
106
107 for( Attachment attachment : attachments )
108 {
109 if( attachment.getPart().equals( partName ))
110 results.add( attachment );
111 }
112
113 return results.toArray( new Attachment[results.size()]);
114 }
115
116 public String getContentAsString()
117 {
118 if( rootPart == null )
119 return null;
120
121 if( responseContent == null )
122 {
123 try
124 {
125 InputStream in = rootPart.getInputStream();
126 ByteArrayOutputStream data = Tools.readAll( in, Tools.READ_ALL );
127
128 String contentType = rootPart.getContentType();
129 if( contentType != null && contentType.indexOf( "charset=" ) > 0 )
130 {
131 try
132 {
133 int ix = contentType.indexOf( "charset=" );
134 int ix2 = contentType.indexOf( ";", ix );
135
136 String charset = ix2 == -1 ? contentType.substring( ix+8 ) :
137 contentType.substring( ix+8, ix2 );
138
139 responseContent = data.toString( StringUtils.unquote( charset ) );
140 }
141 catch( Throwable e )
142 {
143 SoapUI.logError( e );
144 }
145 }
146
147 if( responseContent == null )
148 {
149 responseContent = data.toString();
150 }
151
152 if( prettyPrint )
153 {
154 responseContent = XmlUtils.prettyPrintXml( responseContent );
155 }
156
157 return responseContent;
158 }
159 catch (Exception e)
160 {
161 SoapUI.logError( e );
162 }
163 }
164
165 return responseContent;
166 }
167
168 public void setResponseContent(String responseContent)
169 {
170 this.responseContent = responseContent;
171 }
172
173 public Attachment getAttachmentWithContentId( String contentId )
174 {
175 for( Attachment attachment : attachments)
176 if( contentId.equals( attachment.getContentID() ))
177 return attachment;
178
179 return null;
180 }
181 }