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.filters;
14  
15  import org.apache.commons.httpclient.Header;
16  
17  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
18  import com.eviware.soapui.impl.support.AbstractHttpRequestInterface;
19  import com.eviware.soapui.impl.support.http.HttpRequestInterface;
20  import com.eviware.soapui.impl.wsdl.WsdlRequest;
21  import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport;
22  import com.eviware.soapui.impl.wsdl.submit.transports.http.ExtendedHttpMethod;
23  import com.eviware.soapui.impl.wsdl.submit.transports.http.SinglePartHttpResponse;
24  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.MimeMessageResponse;
25  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.WsdlMimeMessageResponse;
26  import com.eviware.soapui.impl.wsdl.submit.transports.http.support.attachments.WsdlSinglePartHttpResponse;
27  import com.eviware.soapui.model.iface.Response;
28  import com.eviware.soapui.model.iface.SubmitContext;
29  
30  public class HttpPackagingResponseFilter extends AbstractRequestFilter
31  {
32  	@Override
33  	public void afterAbstractHttpResponse( SubmitContext context, AbstractHttpRequestInterface<?> request )
34  	{
35  		ExtendedHttpMethod httpMethod = ( ExtendedHttpMethod )context.getProperty( BaseHttpRequestTransport.HTTP_METHOD );
36  		String requestContent = ( String )context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT );
37  
38  		// check content-type for multiplart
39  		Header responseContentTypeHeader = httpMethod.getResponseHeader( "Content-Type" );
40  		Response response = null;
41  		if( request instanceof WsdlRequest )
42  			response = wsdlRequest( context, ( WsdlRequest )request, httpMethod, responseContentTypeHeader, requestContent );
43  		else if( request instanceof HttpRequestInterface<?> )
44  			response = httpRequest( context, ( HttpRequestInterface<?> )request, httpMethod, responseContentTypeHeader,
45  					requestContent );
46  
47  		context.setProperty( BaseHttpRequestTransport.RESPONSE, response );
48  	}
49  
50  	private Response wsdlRequest( SubmitContext context, WsdlRequest request, ExtendedHttpMethod httpMethod,
51  			Header responseContentTypeHeader, String requestContent )
52  	{
53  		if( context.hasProperty( "PreWssProcessedDocument" ) )
54  			requestContent = String.valueOf( context.getProperty( "PreWssProcessedDocument" ) );
55  
56  		XmlBeansSettingsImpl settings = request.getSettings();
57  		if( !settings.getBoolean( WsdlRequest.INLINE_RESPONSE_ATTACHMENTS ) && responseContentTypeHeader != null
58  				&& responseContentTypeHeader.getValue().toUpperCase().startsWith( "MULTIPART" ) )
59  		{
60  			return new WsdlMimeMessageResponse( request, httpMethod, requestContent, context );
61  		}
62  		else
63  		{
64  			return new WsdlSinglePartHttpResponse( request, httpMethod, requestContent, context );
65  		}
66  	}
67  
68  	private Response httpRequest( SubmitContext context, HttpRequestInterface<?> request, ExtendedHttpMethod httpMethod,
69  			Header responseContentTypeHeader, String requestContent )
70  	{
71  		if( responseContentTypeHeader != null
72  				&& responseContentTypeHeader.getValue().toUpperCase().startsWith( "MULTIPART" ) )
73  		{
74  			return new MimeMessageResponse( request, httpMethod, requestContent, context );
75  		}
76  		else
77  		{
78  			return new SinglePartHttpResponse( request, httpMethod, requestContent, context );
79  		}
80  	}
81  }