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.IOException;
16  import java.io.OutputStream;
17  
18  import javax.mail.MessagingException;
19  import javax.mail.internet.MimeMessage;
20  import javax.mail.internet.MimeMultipart;
21  
22  import org.apache.commons.httpclient.methods.RequestEntity;
23  
24  import com.eviware.soapui.SoapUI;
25  import com.eviware.soapui.impl.support.http.HttpRequestInterface;
26  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.WsdlRequestMimeMessageRequestEntity.DummyOutputStream;
27  
28  /***
29   * MimeMessage request class
30   * 
31   * @author ole.matzura
32   */
33  
34  public class HttpRequestMimeMessageRequestEntity implements RequestEntity
35  {
36  	private final MimeMessage message;
37  	private final HttpRequestInterface<?> restRequest;
38  
39  	public HttpRequestMimeMessageRequestEntity( MimeMessage message, HttpRequestInterface<?> restRequest )
40  	{
41  		this.message = message;
42  		this.restRequest = restRequest;
43  	}
44  
45  	public long getContentLength()
46  	{
47  		try
48  		{
49  			DummyOutputStream out = new DummyOutputStream();
50  			writeRequest( out );
51  			return out.getSize();
52  		}
53  		catch( Exception e )
54  		{
55  			SoapUI.logError( e );
56  			return -1;
57  		}
58  	}
59  
60  	public String getContentType()
61  	{
62  		try
63  		{
64  			String header = message.getHeader( "Content-Type" )[0];
65  			int ix = header.indexOf( "boundary" );
66  
67  			return restRequest.getMediaType() + "; " + header.substring( ix );
68  		}
69  		catch( MessagingException e )
70  		{
71  			SoapUI.logError( e );
72  		}
73  
74  		return restRequest.getMediaType();
75  	}
76  
77  	public boolean isRepeatable()
78  	{
79  		return true;
80  	}
81  
82  	public void writeRequest( OutputStream arg0 ) throws IOException
83  	{
84  		try
85  		{
86  			arg0.write( "\r\n".getBytes() );
87  			( ( MimeMultipart )message.getContent() ).writeTo( arg0 );
88  		}
89  		catch( Exception e )
90  		{
91  			SoapUI.logError( e );
92  		}
93  	}
94  }