View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 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  				response.setResponseContent( xml );
61  			}
62  			finally
63  			{
64  				settingResponse = false;
65  			}
66  		}
67  	}
68  
69  	public void propertyChange( PropertyChangeEvent evt )
70  	{
71  		if( settingResponse )
72  			return;
73  
74  		if( evt.getPropertyName().equals( WsdlRequest.RESPONSE_PROPERTY ) )
75  		{
76  			Response oldResponse = ( Response ) evt.getOldValue();
77  			Response newResponse = ( Response ) evt.getNewValue();
78  
79  			fireXmlChanged( oldResponse == null ? null : oldResponse.getContentAsString(), newResponse == null ? null
80  						: newResponse.getContentAsString() );
81  		}
82  
83  		if( evt.getPropertyName().equals( WsdlRequest.RESPONSE_CONTENT_PROPERTY ) )
84  		{
85  			String oldResponse = ( String ) evt.getOldValue();
86  			String newResponse = ( String ) evt.getNewValue();
87  
88  			fireXmlChanged( oldResponse, newResponse );
89  		}
90  	}
91  
92  	public SchemaTypeSystem getTypeSystem()
93  	{
94  		WsdlInterface iface = ( WsdlInterface ) request.getOperation().getInterface();
95  		WsdlContext wsdlContext = iface.getWsdlContext();
96  		try
97  		{
98  			return wsdlContext.getSchemaTypeSystem();
99  		}
100 		catch( Exception e1 )
101 		{
102 			SoapUI.logError( e1 );
103 			return XmlBeans.getBuiltinTypeSystem();
104 		}
105 	}
106 
107 	public void release()
108 	{
109 		request.removePropertyChangeListener( this );
110 	}
111 }