001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.soapui.impl.wsdl.support;
014:
015: import java.io.BufferedInputStream;
016: import java.io.ByteArrayInputStream;
017: import java.io.ByteArrayOutputStream;
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.FileNotFoundException;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.util.zip.ZipEntry;
025: import java.util.zip.ZipInputStream;
026: import java.util.zip.ZipOutputStream;
027:
028: import org.apache.commons.codec.binary.Base64;
029: import org.apache.commons.codec.binary.Hex;
030: import org.apache.log4j.Logger;
031:
032: import com.eviware.soapui.SoapUI;
033: import com.eviware.soapui.config.AttachmentConfig;
034: import com.eviware.soapui.support.Tools;
035:
036: /**
037: * Attachments cached locally for each request
038: *
039: * @author Ole.Matzura
040: */
041:
042: public abstract class FileAttachment implements WsdlAttachment {
043: private AttachmentConfig config;
044: private final static Logger log = Logger
045: .getLogger(FileAttachment.class);
046:
047: public FileAttachment(AttachmentConfig config) {
048: this .config = config;
049:
050: if (config.getTempFilename() != null) {
051: try {
052: log.info("Moving locally cached file ["
053: + config.getTempFilename()
054: + "] to internal cache..");
055: File tempFile = new File(config.getTempFilename());
056: cacheFileLocally(tempFile);
057: } catch (IOException e) {
058: if (!config.isSetData()) {
059: config.setData(new byte[0]);
060: config.setSize(0);
061: }
062:
063: SoapUI.logError(e);
064: }
065: }
066:
067: if (isCached()) {
068: if (config.isSetTempFilename())
069: config.unsetTempFilename();
070:
071: if (config.isSetUrl())
072: config.unsetUrl();
073: }
074: }
075:
076: public FileAttachment(File file, boolean cache,
077: AttachmentConfig config) throws IOException {
078: this (config);
079:
080: config.setName(file.getName());
081:
082: // cache locally if specified
083: if (cache) {
084: cacheFileLocally(file);
085: } else {
086: config.setUrl(file.getPath());
087: }
088: }
089:
090: private void cacheFileLocally(File file)
091: throws FileNotFoundException, IOException {
092: // write attachment-data to tempfile
093: ByteArrayOutputStream data = new ByteArrayOutputStream();
094: ZipOutputStream out = new ZipOutputStream(data);
095: out.putNextEntry(new ZipEntry(config.getName()));
096:
097: InputStream in = new FileInputStream(file);
098: long sz = file.length();
099: config.setSize(sz);
100:
101: Tools.writeAll(out, in);
102:
103: in.close();
104: out.closeEntry();
105: out.finish();
106: out.close();
107: data.close();
108:
109: config.setData(data.toByteArray());
110: }
111:
112: public String getContentType() {
113: return config.getContentType();
114: }
115:
116: public InputStream getInputStream() throws IOException {
117: BufferedInputStream inputStream = null;
118:
119: if (isCached()) {
120: ZipInputStream zipInputStream = new ZipInputStream(
121: new ByteArrayInputStream(config.getData()));
122: zipInputStream.getNextEntry();
123: inputStream = new BufferedInputStream(zipInputStream);
124: } else {
125: inputStream = new BufferedInputStream(new FileInputStream(
126: config.getUrl()));
127: }
128:
129: AttachmentEncoding encoding = getEncoding();
130: if (encoding == AttachmentEncoding.BASE64) {
131: ByteArrayOutputStream data = Tools.readAll(inputStream,
132: Tools.READ_ALL);
133: return new ByteArrayInputStream(Base64.encodeBase64(data
134: .toByteArray()));
135: } else if (encoding == AttachmentEncoding.HEX) {
136: ByteArrayOutputStream data = Tools.readAll(inputStream,
137: Tools.READ_ALL);
138: return new ByteArrayInputStream(new String(Hex
139: .encodeHex(data.toByteArray())).getBytes());
140: }
141:
142: return inputStream;
143: }
144:
145: public String getName() {
146: return config.getName();
147: }
148:
149: public long getSize() {
150: if (isCached())
151: return config.getSize();
152: else
153: return new File(config.getUrl()).length();
154: }
155:
156: public void release() {
157: if (isCached())
158: new File(config.getTempFilename()).delete();
159: }
160:
161: public String getPart() {
162: return config.getPart();
163: }
164:
165: public void setContentType(String contentType) {
166: config.setContentType(contentType);
167: }
168:
169: public void setPart(String part) {
170: config.setPart(part);
171: }
172:
173: public String getUrl() {
174: if (isCached()) {
175: String name = config.getName();
176: int ix = name.lastIndexOf(".");
177:
178: try {
179: File tempFile = File.createTempFile("attachment-"
180: + name.substring(0, ix), name.substring(ix));
181: FileOutputStream out = new FileOutputStream(tempFile);
182: InputStream in = getInputStream();
183:
184: Tools.writeAll(out, in);
185:
186: out.close();
187: in.close();
188:
189: return tempFile.getAbsoluteFile().toURL().toString();
190: } catch (IOException e) {
191: SoapUI.logError(e);
192: }
193: } else {
194: return config.getUrl();
195: }
196:
197: return null;
198: }
199:
200: public boolean isCached() {
201: return config.isSetData();
202: }
203:
204: abstract public AttachmentType getAttachmentType();
205:
206: public void updateConfig(AttachmentConfig config) {
207: this .config = config;
208: }
209:
210: public AttachmentConfig getConfig() {
211: return config;
212: }
213:
214: public void setContentID(String contentID) {
215: if ((contentID == null || contentID.length() == 0)
216: && config.isSetContentId())
217: config.unsetContentId();
218: else
219: config.setContentId(contentID);
220: }
221:
222: public String getContentID() {
223: return config.getContentId();
224: }
225: }
|