View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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.submit.transports.http;
14  
15  import java.io.BufferedOutputStream;
16  import java.io.File;
17  import java.io.FileOutputStream;
18  import java.io.IOException;
19  import java.io.InputStream;
20  import java.io.OutputStream;
21  import java.net.MalformedURLException;
22  
23  import javax.mail.BodyPart;
24  import javax.mail.MessagingException;
25  
26  import com.eviware.soapui.model.iface.Attachment;
27  import com.eviware.soapui.support.Tools;
28  
29  public class BodyPartAttachment implements Attachment
30  {
31  	private final BodyPart bodyPart;
32  	private File tempFile;
33  
34  	public BodyPartAttachment(BodyPart bodyPart)
35  	{
36  		this.bodyPart = bodyPart;
37  	}
38  
39  	public String getContentType()
40  	{
41  		try
42  		{
43  			return bodyPart.getContentType();
44  		}
45  		catch (MessagingException e)
46  		{
47  			e.printStackTrace();
48  			return null;
49  		}
50  	}
51  
52  	public InputStream getInputStream() throws IOException
53  	{
54  		try
55  		{
56  			return bodyPart.getInputStream();
57  		}
58  		catch (MessagingException e)
59  		{
60  			e.printStackTrace();
61  			return null;
62  		}
63  	}
64  
65  	public String getName()
66  	{
67  		try
68  		{
69  			String[] header = bodyPart.getHeader( "Content-Id" );
70  			if( header == null || header.length == 0 )
71  				return "<missing name>";
72  			
73  			if( header[0].startsWith( "<" ) && header[0].endsWith( ">" ))
74  				header[0] = header[0].substring( 1, header[0].length()-1 );
75  			
76  			return header[0];
77  		}
78  		catch (MessagingException e)
79  		{
80  			e.printStackTrace();
81  			return null;
82  		}
83  	}
84  
85  	public String getPart()
86  	{
87  		return getName();
88  	}
89  
90  	public long getSize()
91  	{
92  		try
93  		{
94  			return bodyPart.getSize();
95  		}
96  		catch (MessagingException e)
97  		{
98  			e.printStackTrace();
99  			return -1;
100 		}
101 	}
102 
103 	public String getUrl()
104 	{
105 		if( tempFile == null )
106 		{
107 			String contentType = getContentType();
108 			int ix = contentType.lastIndexOf( '/' );
109 			int iy = -1;
110 			if (ix != -1)
111 			iy = contentType.indexOf(';', ix);
112 	
113 			try
114 			{
115 				tempFile = File.createTempFile( "response-attachment", (ix == -1 ? ".dat" : "." + (iy == -1 ?
116 					contentType.substring(ix+1) : contentType.substring(ix+1, iy))));
117 
118 				OutputStream out = new BufferedOutputStream( new FileOutputStream( tempFile ));
119 				InputStream inputStream = getInputStream();
120 				out.write( Tools.readAll( inputStream, 0 ).toByteArray() );
121 				out.flush();
122 				out.close();
123 	
124 				inputStream.reset();
125 			}
126 			catch (Exception e)
127 			{
128 				e.printStackTrace();
129 			}
130 		}
131 			
132 		try
133 		{
134 			return tempFile.toURL().toString();
135 		}
136 		catch (MalformedURLException e)
137 		{
138 			e.printStackTrace();
139 			return null;
140 		}
141 	}
142 
143 	public void setContentType(String contentType)
144 	{
145 	}
146 
147 	public void setPart(String part)
148 	{
149 	}
150 
151 	public boolean isCached()
152 	{
153 		return true;
154 	}
155 
156 	public AttachmentType getAttachmentType()
157 	{
158 		return AttachmentType.UNKNOWN;
159 	}
160 
161 	public void release()
162 	{
163 	}
164 
165 	public String getContentID()
166 	{
167 		try
168 		{
169 			String[] header = bodyPart.getHeader( "Content-ID" );
170 			if( header != null && header.length > 0 )
171 				return header[0];
172 		}
173 		catch( MessagingException e )
174 		{
175 			e.printStackTrace();
176 		}
177 		
178 		return null;
179 	}
180 }