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 org.apache.commons.httpclient.Header;
16  import org.apache.commons.httpclient.HeaderElement;
17  import org.apache.commons.httpclient.NameValuePair;
18  
19  import com.eviware.soapui.SoapUI;
20  import com.eviware.soapui.impl.support.AbstractHttpOperation;
21  import com.eviware.soapui.impl.support.AbstractHttpRequest;
22  import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
23  import com.eviware.soapui.impl.wsdl.WsdlRequest;
24  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpResponse;
25  import com.eviware.soapui.impl.wsdl.submit.transports.http.ExtendedHttpMethod;
26  import com.eviware.soapui.model.iface.Attachment;
27  import com.eviware.soapui.model.propertyexpansion.PropertyExpansionContext;
28  import com.eviware.soapui.settings.HttpSettings;
29  
30  /***
31   * WsdlMockResponse for a MimeResponse
32   * 
33   * @author ole.matzura
34   */
35  
36  public class MimeMessageResponse extends BaseHttpResponse
37  {
38  	private long timeTaken;
39  	private long responseContentLength;
40  	private String requestContent;
41  	private MultipartMessageSupport mmSupport;
42  	private PostResponseDataSource postResponseDataSource;
43  
44  	public MimeMessageResponse( AbstractHttpRequestInterface<?> httpRequest, ExtendedHttpMethod httpMethod,
45  			String requestContent, PropertyExpansionContext context )
46  	{
47  		super( httpMethod, httpRequest );
48  
49  		if( getRequestContent() == null || !getRequestContent().equals( requestContent ))
50  			this.requestContent = requestContent;
51  
52  		try
53  		{
54  			postResponseDataSource = new PostResponseDataSource( httpMethod );
55  			responseContentLength = postResponseDataSource.getDataSize();
56  
57  			Header h = httpMethod.getResponseHeader( "Content-Type" );
58  			HeaderElement[] elements = h.getElements();
59  
60  			String rootPartId = null;
61  
62  			for( HeaderElement element : elements )
63  			{
64  				String name = element.getName().toUpperCase();
65  				if( name.startsWith( "MULTIPART/" ) )
66  				{
67  					NameValuePair parameter = element.getParameterByName( "start" );
68  					if( parameter != null )
69  						rootPartId = parameter.getValue();
70  				}
71  			}
72  
73  			mmSupport = new MultipartMessageSupport( postResponseDataSource, rootPartId,
74  					( AbstractHttpOperation )httpRequest.getOperation(), false, httpRequest.isPrettyPrint() );
75  
76  			if( httpRequest.getSettings().getBoolean( HttpSettings.INCLUDE_RESPONSE_IN_TIME_TAKEN ) )
77  				this.timeTaken += httpMethod.getResponseReadTime();
78  		}
79  		catch( Exception e )
80  		{
81  			SoapUI.logError( e );
82  		}
83  	}
84  
85  	protected MultipartMessageSupport getMmSupport()
86  	{
87  		return mmSupport;
88  	}
89  
90  	public long getContentLength()
91  	{
92  		return responseContentLength;
93  	}
94  
95  	public String getRequestContent()
96  	{
97  		return requestContent == null ? super.getRequestContent() : requestContent;
98  	}
99  
100 	public void setResponseContent( String responseContent )
101 	{
102 		String oldContent = getContentAsString();
103 		mmSupport.setResponseContent( responseContent );
104 
105 		( ( AbstractHttpRequest<?> )getRequest() ).notifyPropertyChanged( WsdlRequest.RESPONSE_CONTENT_PROPERTY,
106 				oldContent, responseContent );
107 	}
108 
109 	public Attachment[] getAttachments()
110 	{
111 		return mmSupport.getAttachments();
112 	}
113 
114 	public Attachment[] getAttachmentsForPart( String partName )
115 	{
116 		return mmSupport.getAttachmentsForPart( partName );
117 	}
118 
119 	public String getContentAsString()
120 	{
121 		return mmSupport.getContentAsString();
122 	}
123 
124 	// public byte[] getRawRequestData()
125 	// {
126 	// return requestData;
127 	// }
128 	//
129 	// public byte[] getRawResponseData()
130 	// {
131 	// return postResponseDataSource.getData();
132 	// }
133 }