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