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