View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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.log4j.Logger;
29  
30  import com.eviware.soapui.config.AttachmentConfig;
31  import com.eviware.soapui.support.Tools;
32  
33  /***
34   * Attachments cached locally for each request
35   * 
36   * @author Ole.Matzura
37   */
38  
39  public abstract class FileAttachment implements WsdlAttachment
40  {
41  	private AttachmentConfig config;
42  	private final static Logger log = Logger.getLogger(FileAttachment.class);
43  
44  	public FileAttachment( AttachmentConfig config )
45  	{
46  		this.config = config;
47  		
48  		if( config.getTempFilename() != null )
49  		{
50  			try
51  			{
52  				log.info( "Moving locally cached file [" + config.getTempFilename() + "] to internal cache.." );
53  				File tempFile = new File( config.getTempFilename() );
54  				cacheFileLocally( tempFile);
55  			}
56  			catch (IOException e)
57  			{
58  				if( !config.isSetData() )
59  				{
60  					config.setData( new byte[0] );
61  					config.setSize( 0 );
62  				}
63  				
64  				e.printStackTrace();
65  			}
66  		}
67  		
68  		if( isCached() )
69  		{
70  			if( config.isSetTempFilename())
71  				config.unsetTempFilename();
72  			
73  			if( config.isSetUrl() )
74  				config.unsetUrl();
75  		}
76  	}
77  	
78  	public FileAttachment( File file, boolean cache, AttachmentConfig config ) throws IOException
79  	{
80  		this( config );
81  		
82  		config.setName( file.getName() );
83  		
84  		// cache locally if specified
85  		if( cache )
86  		{
87  			cacheFileLocally( file );
88  		}
89  		else
90  		{
91  			config.setUrl( file.getPath() );
92  		}
93  	}
94  
95  	private void cacheFileLocally(File file) throws FileNotFoundException, IOException
96  	{
97  		// write attachment-data to tempfile
98  		ByteArrayOutputStream data = new ByteArrayOutputStream();
99  		ZipOutputStream out = new ZipOutputStream( data );
100 		out.putNextEntry( new ZipEntry( config.getName() ));
101 		
102 		InputStream in = new FileInputStream( file );
103 		long sz = file.length();
104 		config.setSize( sz );
105 		
106 		Tools.writeAll( out, in );
107 		
108 		in.close();
109 		out.closeEntry();
110 		out.finish();
111 		out.close();
112 		data.close();
113 		
114 		config.setData( data.toByteArray() );
115 	}
116 
117 	public String getContentType()
118 	{
119 		return config.getContentType();
120 	}
121 
122 	public InputStream getInputStream() throws IOException
123 	{
124 		if( isCached() )
125 		{
126 			ZipInputStream zipInputStream = new ZipInputStream( new ByteArrayInputStream( config.getData() ));
127 			zipInputStream.getNextEntry();
128 			return new BufferedInputStream( zipInputStream );
129 		}
130 		else
131 			return new BufferedInputStream( new FileInputStream( config.getUrl() ));
132 	}
133 	
134 	public String getName()
135 	{
136 		return config.getName();
137 	}
138 
139 	public long getSize()
140 	{
141 		if( isCached() ) 
142 			return config.getSize();
143 		else
144 			return new File( config.getUrl() ).length();
145 	}
146 
147 	public void release()
148 	{
149 		if( isCached() )
150 			new File( config.getTempFilename() ).delete();
151 	}
152 
153 	public String getPart()
154 	{
155 		return config.getPart();
156 	}
157 
158 	public void setContentType(String contentType)
159 	{
160 		config.setContentType( contentType );
161 	}
162 
163 	public void setPart(String part)
164 	{
165 		config.setPart( part );
166 	}
167 
168 	public String getUrl()
169 	{
170 		if( isCached() )
171 		{
172 			String name = config.getName();
173 			int ix = name.lastIndexOf( "." );
174 			
175 			try
176 			{
177 				File tempFile = File.createTempFile( "attachment-" + name.substring( 0, ix), name.substring(ix)  );
178 				FileOutputStream out = new FileOutputStream( tempFile );
179 				InputStream in = getInputStream();
180 				
181 				Tools.writeAll( out, in );
182 				
183 				out.close();
184 				in.close();
185 				
186 				return tempFile.getAbsoluteFile().toURL().toString();
187 			}
188 			catch (IOException e)
189 			{
190 				e.printStackTrace();
191 			}
192 		}
193 		else
194 		{
195 			return config.getUrl();
196 		}
197 		
198 		return null;
199 	}
200 
201 	public boolean isCached()
202 	{
203 		return config.isSetData();
204 	}
205 
206 	abstract public AttachmentType getAttachmentType();
207 
208 	public void updateConfig(AttachmentConfig config)
209 	{
210 		this.config = config;
211 	}
212 
213 	public AttachmentConfig getConfig()
214 	{
215 		return config;
216 	}
217 
218 	public void setContentID( String contentID )
219 	{
220 		if( (contentID == null || contentID.length() == 0) && config.isSetContentId() )
221 			config.unsetContentId();
222 		else
223 			config.setContentId( contentID );
224 	}
225 
226 	public String getContentID()
227 	{
228 		return config.getContentId();
229 	}
230 }