Source Code Cross Referenced for FileAttachment.java in  » Web-Services » soapui-1.7.5 » com » eviware » soapui » impl » wsdl » support » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Services » soapui 1.7.5 » com.eviware.soapui.impl.wsdl.support 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.