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.support.components;
14  
15  import java.beans.PropertyChangeEvent;
16  import java.beans.PropertyChangeListener;
17  
18  import org.apache.xmlbeans.SchemaTypeSystem;
19  import org.apache.xmlbeans.XmlBeans;
20  
21  import com.eviware.soapui.SoapUI;
22  import com.eviware.soapui.impl.wsdl.WsdlInterface;
23  import com.eviware.soapui.impl.wsdl.WsdlRequest;
24  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
25  import com.eviware.soapui.impl.wsdl.support.wsdl.WsdlContext;
26  import com.eviware.soapui.model.iface.Response;
27  import com.eviware.soapui.support.editor.xml.support.AbstractXmlDocument;
28  
29  /***
30   * XmlDocument for the response to a WsdlRequest
31   * 
32   * @author ole.matzura
33   */
34  
35  public class ResponseXmlDocument extends AbstractXmlDocument implements PropertyChangeListener
36  {
37  	private final WsdlRequest request;
38  	private boolean settingResponse;
39  
40  	public ResponseXmlDocument( WsdlRequest request )
41  	{
42  		this.request = request;
43  		request.addPropertyChangeListener( this );
44  	}
45  
46  	public String getXml()
47  	{
48  		Response response = request.getResponse();
49  		return response == null ? null : response.getContentAsString();
50  	}
51  
52  	public void setXml( String xml )
53  	{
54  		HttpResponse response = ( HttpResponse )request.getResponse();
55  		if( response != null )
56  		{
57  			try
58  			{
59  				settingResponse = true;
60  				String oldXml = response.getContentAsString();
61  				response.setResponseContent( xml );
62  				fireXmlChanged( oldXml, xml );
63  			}
64  			finally
65  			{
66  				settingResponse = false;
67  			}
68  		}
69  	}
70  
71  	public void propertyChange( PropertyChangeEvent evt )
72  	{
73  		if( settingResponse )
74  			return;
75  
76  		if( evt.getPropertyName().equals( WsdlRequest.RESPONSE_PROPERTY ) )
77  		{
78  			Response oldResponse = ( Response )evt.getOldValue();
79  			Response newResponse = ( Response )evt.getNewValue();
80  
81  			fireXmlChanged( oldResponse == null ? null : oldResponse.getContentAsString(), newResponse == null ? null
82  					: newResponse.getContentAsString() );
83  		}
84  
85  		if( evt.getPropertyName().equals( WsdlRequest.RESPONSE_CONTENT_PROPERTY ) )
86  		{
87  			String oldResponse = ( String )evt.getOldValue();
88  			String newResponse = ( String )evt.getNewValue();
89  
90  			fireXmlChanged( oldResponse, newResponse );
91  		}
92  	}
93  
94  	public SchemaTypeSystem getTypeSystem()
95  	{
96  		WsdlInterface iface = ( WsdlInterface )request.getOperation().getInterface();
97  		WsdlContext wsdlContext = iface.getWsdlContext();
98  		try
99  		{
100 			return wsdlContext.getSchemaTypeSystem();
101 		}
102 		catch( Exception e1 )
103 		{
104 			SoapUI.logError( e1 );
105 			return XmlBeans.getBuiltinTypeSystem();
106 		}
107 	}
108 
109 	public void release()
110 	{
111 		request.removePropertyChangeListener( this );
112 	}
113 }