1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments;
14
15 import com.eviware.soapui.SoapUI;
16 import com.eviware.soapui.impl.support.AbstractHttpOperation;
17 import com.eviware.soapui.impl.wsdl.WsdlOperation;
18 import com.eviware.soapui.model.iface.Attachment;
19 import com.eviware.soapui.support.Tools;
20 import org.apache.commons.codec.binary.Base64;
21 import org.apache.commons.codec.binary.Hex;
22
23 import javax.mail.BodyPart;
24 import javax.mail.MessagingException;
25 import java.io.*;
26 import java.net.MalformedURLException;
27
28 /***
29 * Attachment for a BodyPart
30 *
31 * @author ole.matzura
32 */
33
34 public class BodyPartAttachment implements Attachment
35 {
36 private final BodyPart bodyPart;
37 private File tempFile;
38 private AbstractHttpOperation operation;
39 private final boolean isRequest;
40 private byte[] data;
41 private AttachmentType attachmentType;
42
43 public BodyPartAttachment(BodyPart bodyPart, AbstractHttpOperation operation, boolean isRequest, AttachmentType attachmentType )
44 {
45 this.bodyPart = bodyPart;
46 this.operation = operation;
47 this.isRequest = isRequest;
48 this.attachmentType = attachmentType;
49 }
50
51 public String getContentType()
52 {
53 try
54 {
55 return bodyPart.getContentType();
56 }
57 catch (MessagingException e)
58 {
59 SoapUI.logError( e );
60 return null;
61 }
62 }
63
64 public AttachmentEncoding getEncoding()
65 {
66 return operation == null ? AttachmentEncoding.NONE : operation.getAttachmentEncoding( getPart(), !isRequest );
67 }
68
69 public InputStream getInputStream() throws Exception
70 {
71 if( data != null )
72 return new ByteArrayInputStream( data );
73
74 AttachmentEncoding encoding = getEncoding();
75 if( encoding == AttachmentEncoding.NONE )
76 return bodyPart.getInputStream();
77
78 data = Tools.readAll( bodyPart.getInputStream(), Tools.READ_ALL ).toByteArray();
79
80 if( encoding == AttachmentEncoding.BASE64 )
81 {
82 if( Base64.isArrayByteBase64( data ))
83 data = Tools.readAll( new ByteArrayInputStream( Base64.decodeBase64( data )), Tools.READ_ALL ).toByteArray();
84 else
85 throw new Exception( "Attachment content for part [" + getPart() + "] is not base64 encoded");
86 }
87 else if(encoding == AttachmentEncoding.HEX )
88 {
89 data = Hex.decodeHex( new String( data ).toCharArray() );
90 }
91
92 return new ByteArrayInputStream( data );
93 }
94
95 public String getName()
96 {
97 try
98 {
99 String[] header = bodyPart.getHeader( "Content-Id" );
100 if( header == null || header.length == 0 )
101 return "<missing name>";
102
103 if( header[0].startsWith( "<" ) && header[0].endsWith( ">" ))
104 header[0] = header[0].substring( 1, header[0].length()-1 );
105
106 return header[0];
107 }
108 catch (MessagingException e)
109 {
110 SoapUI.logError( e );
111 return null;
112 }
113 }
114
115 public String getPart()
116 {
117 String name = getName();
118 int ix = name.indexOf( '=' );
119 if( ix > 0)
120 {
121 name = name.substring( 0,ix );
122 }
123
124 return name;
125 }
126
127 public long getSize()
128 {
129 try
130 {
131 getInputStream();
132 return data == null ? bodyPart.getSize() : data.length;
133 }
134 catch (Exception e)
135 {
136 SoapUI.logError( e );
137 return -1;
138 }
139 }
140
141 public String getUrl()
142 {
143 if( tempFile == null )
144 {
145 String contentType = getContentType();
146 int ix = contentType.lastIndexOf( '/' );
147 int iy = -1;
148 if (ix != -1)
149 iy = contentType.indexOf(';', ix);
150
151 try
152 {
153 tempFile = File.createTempFile( "response-attachment", (ix == -1 ? ".dat" : "." + (iy == -1 ?
154 contentType.substring(ix+1) : contentType.substring(ix+1, iy))));
155
156 OutputStream out = new BufferedOutputStream( new FileOutputStream( tempFile ));
157 InputStream inputStream = getInputStream();
158 out.write( Tools.readAll( inputStream, 0 ).toByteArray() );
159 out.flush();
160 out.close();
161
162 inputStream.reset();
163 }
164 catch (Exception e)
165 {
166 SoapUI.logError( e );
167 }
168 }
169
170 try
171 {
172 return tempFile.toURI().toURL().toString();
173 }
174 catch (MalformedURLException e)
175 {
176 SoapUI.logError( e );
177 return null;
178 }
179 }
180
181 public void setContentType(String contentType)
182 {
183 }
184
185 public void setPart(String part)
186 {
187 }
188
189 public boolean isCached()
190 {
191 return true;
192 }
193
194 public AttachmentType getAttachmentType()
195 {
196 return attachmentType == null ? AttachmentType.UNKNOWN : attachmentType;
197 }
198
199 public void release()
200 {
201 operation = null;
202 }
203
204 public String getContentID()
205 {
206 try
207 {
208 String[] header = bodyPart.getHeader( "Content-ID" );
209 if( header != null && header.length > 0 )
210 return header[0];
211 }
212 catch( MessagingException e )
213 {
214 SoapUI.logError( e );
215 }
216
217 return null;
218 }
219
220 public void setOperation( WsdlOperation operation )
221 {
222 this.operation = operation;
223 }
224
225 public void setAttachmentType(AttachmentType attachmentType)
226 {
227 this.attachmentType = attachmentType;}
228 }