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