View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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.submit.transports.http.support.attachments;
14  
15  import com.eviware.soapui.SoapUI;
16  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
17  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
18  import org.apache.commons.httpclient.methods.RequestEntity;
19  
20  import javax.mail.MessagingException;
21  import javax.mail.internet.MimeMessage;
22  import javax.mail.internet.MimeMultipart;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  import java.io.OutputStream;
26  
27  /***
28   * MimeMessage response for a WsdlMockResponse
29   * 
30   * @author ole.matzura
31   */
32  
33  public class MimeMessageMockResponseEntity implements RequestEntity
34  {
35  	private final MimeMessage message;
36  	private byte[] buffer;
37  	private final boolean isXOP;
38  	private final WsdlMockResponse mockResponse;
39  
40  	public MimeMessageMockResponseEntity(MimeMessage message, boolean isXOP, WsdlMockResponse response )
41  	{
42  		this.message = message;
43  		this.isXOP = isXOP;
44  		this.mockResponse = response;
45  	}
46  
47  	public long getContentLength()
48  	{
49  		try
50  		{
51  			ByteArrayOutputStream out = new ByteArrayOutputStream();
52  			writeRequest( out );
53  			buffer = out.toByteArray();
54  			return buffer.length;
55  		}
56  		catch (Exception e)
57  		{
58  			SoapUI.logError( e );
59  			return -1;
60  		}
61  	}
62  
63  	public String getContentType()
64  	{
65  		try
66  		{
67  			SoapVersion soapVersion = mockResponse.getSoapVersion();
68  			
69  			if( isXOP )
70  			{
71  				String header = message.getHeader( "Content-Type" )[0];
72  				
73  				return AttachmentUtils.buildMTOMContentType(header, null, soapVersion);
74  			}
75  			else
76  			{
77  				String header = message.getHeader( "Content-Type" )[0];
78  				int ix = header.indexOf( "boundary" );
79  				return "multipart/related; type=\"" + soapVersion.getContentType() + 
80  					"\"; start=\"" + AttachmentUtils.ROOTPART_SOAPUI_ORG + "\"; "  + header.substring( ix );
81  			}
82  		}
83  		catch (MessagingException e)
84  		{
85  			SoapUI.logError( e );
86  		}
87  		
88  		return null;
89  	}
90  
91  	public boolean isRepeatable()
92  	{
93  		return true;
94  	}
95  
96  	public void writeRequest(OutputStream arg0) throws IOException
97  	{
98  		try
99  		{
100 			if( buffer == null )
101 			{
102 				arg0.write( "\r\n".getBytes() );
103 				((MimeMultipart)message.getContent()).writeTo( arg0 );
104 			}
105 			else
106 				arg0.write( buffer );
107 		}
108 		catch (Exception e)
109 		{
110 			SoapUI.logError( e );
111 		}
112 	}
113 }