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 final boolean isXOP;
39  	private final WsdlRequest wsdlRequest;
40  
41  	public WsdlRequestMimeMessageRequestEntity( MimeMessage message, boolean isXOP, WsdlRequest wsdlRequest )
42  	{
43  		this.message = message;
44  		this.isXOP = isXOP;
45  		this.wsdlRequest = wsdlRequest;
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 = wsdlRequest.getOperation().getInterface().getSoapVersion();
68  
69  			if( isXOP )
70  			{
71  				String header = message.getHeader( "Content-Type" )[0];
72  
73  				return AttachmentUtils.buildMTOMContentType( header, wsdlRequest.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() + "\"; " + "start=\""
81  						+ 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 			arg0.write( "\r\n".getBytes() );
102 			( ( MimeMultipart )message.getContent() ).writeTo( arg0 );
103 		}
104 		catch( Exception e )
105 		{
106 			SoapUI.logError( e );
107 		}
108 	}
109 
110 	public static class DummyOutputStream extends OutputStream
111 	{
112 		private int intLength;
113 		private long size = 0;
114 
115 		public DummyOutputStream()
116 		{
117 			ByteArrayOutputStream tempOut = new ByteArrayOutputStream();
118 			tempOut.write( 1 );
119 			intLength = tempOut.toByteArray().length;
120 		}
121 
122 		@Override
123 		public void write( int b ) throws IOException
124 		{
125 			size += intLength;
126 		}
127 
128 		public long getSize()
129 		{
130 			return size;
131 		}
132 	}
133 }