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.model.iface.Attachment;
28 import com.eviware.soapui.settings.WsdlSettings;
29 import com.eviware.soapui.support.StringUtils;
30 import com.eviware.soapui.support.Tools;
31 import com.eviware.soapui.support.xml.XmlUtils;
32
33 public class MultipartMessageSupport
34 {
35 private final List<Attachment> attachments = new ArrayList<Attachment>();
36 private Attachment rootPart;
37 private MimeMessage message;
38 private String responseContent;
39
40 public MultipartMessageSupport( DataSource dataSource, String rootPartId ) throws MessagingException
41 {
42 MimeMultipart mp = new MimeMultipart( dataSource);
43 message = new MimeMessage( AttachmentUtils.JAVAMAIL_SESSION );
44 message.setContent( mp );
45
46 for( int c = 0; c < mp.getCount(); c++ )
47 {
48 BodyPart bodyPart = mp.getBodyPart( c );
49
50 if( bodyPart.getContentType().toUpperCase().startsWith( "MULTIPART/"))
51 {
52 MimeMultipart mp2 = new MimeMultipart( new BodyPartDataSource( bodyPart ));
53 for( int i = 0; i < mp2.getCount(); i++ )
54 {
55 attachments.add( new BodyPartAttachment( mp2.getBodyPart(i)));
56 }
57 }
58 else
59 {
60 BodyPartAttachment attachment = new BodyPartAttachment( bodyPart);
61
62 String[] contentIdHeaders = bodyPart.getHeader( "Content-ID");
63 if( contentIdHeaders != null && contentIdHeaders.length > 0 && contentIdHeaders[0].equals( rootPartId ))
64 {
65 rootPart = attachment;
66 }
67 else
68 attachments.add( attachment );
69 }
70 }
71
72
73 if( rootPart == null )
74 rootPart = attachments.remove( 0 );
75 }
76
77 public Attachment[] getAttachments()
78 {
79 return attachments.toArray( new Attachment[attachments.size()]);
80 }
81
82 public Attachment getRootPart()
83 {
84 return rootPart;
85 }
86
87 public Attachment[] getAttachmentsForPart(String partName)
88 {
89 List<Attachment> results = new ArrayList<Attachment>();
90
91 for( Attachment attachment : attachments )
92 {
93 if( attachment.getPart().equals( partName ))
94 results.add( attachment );
95 }
96
97 return results.toArray( new Attachment[results.size()]);
98 }
99
100 public String getContentAsString()
101 {
102 if( rootPart == null )
103 return null;
104
105 if( responseContent == null )
106 {
107 try
108 {
109 InputStream in = rootPart.getInputStream();
110 ByteArrayOutputStream data = Tools.readAll( in, Tools.READ_ALL );
111
112 String contentType = rootPart.getContentType();
113 if( contentType != null && contentType.indexOf( "charset=" ) > 0 )
114 {
115 try
116 {
117 int ix = contentType.indexOf( "charset=" );
118 int ix2 = contentType.indexOf( ";", ix );
119
120 String charset = ix2 == -1 ? contentType.substring( ix+8 ) :
121 contentType.substring( ix+8, ix2 );
122
123 responseContent = data.toString( StringUtils.unquote( charset ) );
124 }
125 catch( Throwable e )
126 {
127 e.printStackTrace();
128 }
129 }
130
131 if( responseContent == null )
132 {
133 responseContent = data.toString();
134 }
135
136 if( SoapUI.getSettings().getBoolean( WsdlSettings.PRETTY_PRINT_RESPONSE_MESSAGES ))
137 {
138 responseContent = XmlUtils.prettyPrintXml( responseContent );
139 }
140
141 return responseContent;
142 }
143 catch (Exception e)
144 {
145 e.printStackTrace();
146 }
147 }
148
149 return responseContent;
150 }
151
152 public void setResponseContent(String responseContent)
153 {
154 this.responseContent = responseContent;
155 }
156
157 public Attachment getAttachmentWithContentId( String contentId )
158 {
159 for( Attachment attachment : attachments)
160 if( contentId.equals( attachment.getContentID() ))
161 return attachment;
162
163 return null;
164 }
165 }