View Javadoc

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