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