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.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.WsdlRequest;
27  import com.eviware.soapui.impl.wsdl.support.soap.SoapVersion;
28  
29  /***
30   * MimeMessage request class
31   * 
32   * @author ole.matzura
33   */
34  
35  public class WsdlRequestMimeMessageRequestEntity implements RequestEntity
36  {
37  	private final MimeMessage message;
38  	private byte[] buffer;
39  	private final boolean isXOP;
40  	private final WsdlRequest wsdlRequest;
41  
42  	public WsdlRequestMimeMessageRequestEntity( MimeMessage message, boolean isXOP, WsdlRequest wsdlRequest )
43  	{
44  		this.message = message;
45  		this.isXOP = isXOP;
46  		this.wsdlRequest = wsdlRequest;
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 = wsdlRequest.getOperation().getInterface().getSoapVersion();
70  
71  			if( isXOP )
72  			{
73  				String header = message.getHeader( "Content-Type" )[0];
74  
75  				return AttachmentUtils.buildMTOMContentType( header, wsdlRequest.getAction(), soapVersion );
76  			}
77  			else
78  			{
79  				String header = message.getHeader( "Content-Type" )[0];
80  				int ix = header.indexOf( "boundary" );
81  
82  				return "multipart/related; type=\"" + soapVersion.getContentType() + "\"; " + "start=\""
83  						+ AttachmentUtils.ROOTPART_SOAPUI_ORG + "\"; " + header.substring( ix );
84  			}
85  		}
86  		catch( MessagingException e )
87  		{
88  			SoapUI.logError( e );
89  		}
90  
91  		return null;
92  	}
93  
94  	public boolean isRepeatable()
95  	{
96  		return true;
97  	}
98  
99  	public void writeRequest( OutputStream arg0 ) throws IOException
100 	{
101 		try
102 		{
103 			if( buffer == null )
104 			{
105 				arg0.write( "\r\n".getBytes() );
106 				( ( MimeMultipart )message.getContent() ).writeTo( arg0 );
107 			}
108 			else
109 				arg0.write( buffer );
110 		}
111 		catch( Exception e )
112 		{
113 			SoapUI.logError( e );
114 		}
115 	}
116 }