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