View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 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 java.io.IOException;
16  import java.io.OutputStream;
17  
18  import javax.mail.MessagingException;
19  import javax.mail.internet.MimeMessage;
20  import javax.mail.internet.MimeMultipart;
21  
22  import org.apache.commons.httpclient.methods.RequestEntity;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.impl.wsdl.mock.WsdlMockResponse;
26  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.WsdlRequestMimeMessageRequestEntity.DummyOutputStream;
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 final boolean isXOP;
39  	private final WsdlMockResponse mockResponse;
40  
41  	public MimeMessageMockResponseEntity( MimeMessage message, boolean isXOP, WsdlMockResponse response )
42  	{
43  		this.message = message;
44  		this.isXOP = isXOP;
45  		this.mockResponse = response;
46  	}
47  
48  	public long getContentLength()
49  	{
50  		try
51  		{
52  			DummyOutputStream out = new DummyOutputStream();
53  			writeRequest( out );
54  			return out.getSize();
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() + "\"; start=\""
80  						+ 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 			arg0.write( "\r\n".getBytes() );
101 			( ( MimeMultipart )message.getContent() ).writeTo( arg0 );
102 		}
103 		catch( Exception e )
104 		{
105 			SoapUI.logError( e );
106 		}
107 	}
108 }