1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.support;
14
15 import java.io.BufferedInputStream;
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.zip.ZipEntry;
25 import java.util.zip.ZipInputStream;
26 import java.util.zip.ZipOutputStream;
27
28 import org.apache.commons.codec.binary.Base64;
29 import org.apache.commons.codec.binary.Hex;
30 import org.apache.log4j.Logger;
31
32 import com.eviware.soapui.SoapUI;
33 import com.eviware.soapui.config.AttachmentConfig;
34 import com.eviware.soapui.impl.wsdl.panels.request.components.editor.inspectors.attachments.ContentTypeHandler;
35 import com.eviware.soapui.support.Tools;
36
37 /***
38 * Attachments cached locally for each request
39 *
40 * @author Ole.Matzura
41 */
42
43 public abstract class FileAttachment implements WsdlAttachment
44 {
45 private AttachmentConfig config;
46 private final static Logger log = Logger.getLogger(FileAttachment.class);
47
48 public FileAttachment( AttachmentConfig config )
49 {
50 this.config = config;
51
52 if( config.getTempFilename() != null )
53 {
54 try
55 {
56 log.info( "Moving locally cached file [" + config.getTempFilename() + "] to internal cache.." );
57 File tempFile = new File( config.getTempFilename() );
58 cacheFileLocally( tempFile);
59 }
60 catch (IOException e)
61 {
62 if( !config.isSetData() )
63 {
64 config.setData( new byte[0] );
65 config.setSize( 0 );
66 }
67
68 SoapUI.logError( e );
69 }
70 }
71
72 if( isCached() )
73 {
74 if( config.isSetTempFilename())
75 config.unsetTempFilename();
76
77 if( config.isSetUrl() )
78 config.unsetUrl();
79 }
80 }
81
82 public FileAttachment( File file, boolean cache, AttachmentConfig config ) throws IOException
83 {
84 this( config );
85
86 config.setName( file.getName() );
87 config.setContentType( ContentTypeHandler.getContentTypeFromFilename( file.getName() ) );
88
89
90 if( cache )
91 {
92 cacheFileLocally( file );
93 }
94 else
95 {
96 config.setUrl( file.getPath() );
97 }
98 }
99
100 private void cacheFileLocally(File file) throws FileNotFoundException, IOException
101 {
102
103 ByteArrayOutputStream data = new ByteArrayOutputStream();
104 ZipOutputStream out = new ZipOutputStream( data );
105 out.putNextEntry( new ZipEntry( config.getName() ));
106
107 InputStream in = new FileInputStream( file );
108 long sz = file.length();
109 config.setSize( sz );
110
111 Tools.writeAll( out, in );
112
113 in.close();
114 out.closeEntry();
115 out.finish();
116 out.close();
117 data.close();
118
119 config.setData( data.toByteArray() );
120 }
121
122 public String getContentType()
123 {
124 return config.getContentType();
125 }
126
127 public InputStream getInputStream() throws IOException
128 {
129 BufferedInputStream inputStream = null;
130
131 if( isCached() )
132 {
133 ZipInputStream zipInputStream = new ZipInputStream( new ByteArrayInputStream( config.getData() ));
134 zipInputStream.getNextEntry();
135 inputStream = new BufferedInputStream( zipInputStream );
136 }
137 else
138 {
139 inputStream = new BufferedInputStream( new FileInputStream( config.getUrl() ));
140 }
141
142 AttachmentEncoding encoding = getEncoding();
143 if( encoding == AttachmentEncoding.BASE64 )
144 {
145 ByteArrayOutputStream data = Tools.readAll( inputStream, Tools.READ_ALL );
146 return new ByteArrayInputStream( Base64.encodeBase64( data.toByteArray() ));
147 }
148 else if( encoding == AttachmentEncoding.HEX )
149 {
150 ByteArrayOutputStream data = Tools.readAll( inputStream, Tools.READ_ALL );
151 return new ByteArrayInputStream( new String( Hex.encodeHex( data.toByteArray() )).getBytes() );
152 }
153
154 return inputStream;
155 }
156
157 public String getName()
158 {
159 return config.getName();
160 }
161
162 public long getSize()
163 {
164 if( isCached() )
165 return config.getSize();
166 else
167 return new File( config.getUrl() ).length();
168 }
169
170 public void release()
171 {
172 if( isCached() )
173 new File( config.getTempFilename() ).delete();
174 }
175
176 public String getPart()
177 {
178 return config.getPart();
179 }
180
181 public void setContentType(String contentType)
182 {
183 config.setContentType( contentType );
184 }
185
186 public void setPart(String part)
187 {
188 config.setPart( part );
189 }
190
191 public String getUrl()
192 {
193 if( isCached() )
194 {
195 String name = config.getName();
196 int ix = name.lastIndexOf( "." );
197
198 try
199 {
200 File tempFile = File.createTempFile( "attachment-" + name.substring( 0, ix), name.substring(ix) );
201 FileOutputStream out = new FileOutputStream( tempFile );
202 InputStream in = getInputStream();
203
204 Tools.writeAll( out, in );
205
206 out.close();
207 in.close();
208
209 return tempFile.getAbsoluteFile().toURI().toURL().toString();
210 }
211 catch (IOException e)
212 {
213 SoapUI.logError( e );
214 }
215 }
216 else
217 {
218 return config.getUrl();
219 }
220
221 return null;
222 }
223
224 public boolean isCached()
225 {
226 return config.isSetData();
227 }
228
229 abstract public AttachmentType getAttachmentType();
230
231 public void updateConfig(AttachmentConfig config)
232 {
233 this.config = config;
234 }
235
236 public AttachmentConfig getConfig()
237 {
238 return config;
239 }
240
241 public void setContentID( String contentID )
242 {
243 if( (contentID == null || contentID.length() == 0) && config.isSetContentId() )
244 config.unsetContentId();
245 else
246 config.setContentId( contentID );
247 }
248
249 public String getContentID()
250 {
251 return config.getContentId();
252 }
253 }