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