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  /*
14   *  soapUI, copyright (C) 2004-2010 eviware.com 
15   *
16   *  soapUI is free software; you can redistribute it and/or modify it under the 
17   *  terms of version 2.1 of the GNU Lesser General Public License as published by 
18   *  the Free Software Foundation.
19   *
20   *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
21   *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
22   *  See the GNU Lesser General Public License for more details at gnu.org.
23   */
24  
25  package com.eviware.soapui.impl.rest.panels.request.inspectors.representations;
26  
27  import java.awt.event.ItemEvent;
28  import java.awt.event.ItemListener;
29  
30  import javax.swing.JCheckBox;
31  
32  import org.apache.xmlbeans.XmlCursor;
33  import org.apache.xmlbeans.XmlObject;
34  
35  import com.eviware.soapui.impl.rest.RestRepresentation;
36  import com.eviware.soapui.impl.rest.RestRequest;
37  import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
38  import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
39  import com.eviware.soapui.model.iface.Submit;
40  import com.eviware.soapui.model.iface.SubmitContext;
41  import com.eviware.soapui.model.iface.SubmitListener;
42  import com.eviware.soapui.support.StringUtils;
43  import com.eviware.soapui.support.UISupport;
44  import com.eviware.soapui.support.components.JXToolBar;
45  
46  public class RestRequestRepresentationsInspector extends AbstractRestRepresentationsInspector implements SubmitListener
47  {
48  	private JCheckBox enableRecordingCheckBox;
49  	public static final String RECORD_REQUEST_REPRESENTATIONS = "RecordRequestRepresentations";
50  	private RestRequest request;
51  
52  	protected RestRequestRepresentationsInspector( RestRequest request )
53  	{
54  		super( request.getRestMethod(), "Representations", "Request Representations",
55  				new RestRepresentation.Type[] { RestRepresentation.Type.REQUEST } );
56  
57  		request.addSubmitListener( this );
58  		this.request = request;
59  	}
60  
61  	protected void addToToolbar( JXToolBar toolbar )
62  	{
63  		enableRecordingCheckBox = new JCheckBox( "Auto-Create" );
64  		enableRecordingCheckBox.setToolTipText( "Automatically create Representations from sent Requests" );
65  		enableRecordingCheckBox.setOpaque( false );
66  		UISupport.setFixedSize( enableRecordingCheckBox, 100, 20 );
67  		toolbar.addFixed( enableRecordingCheckBox );
68  		XmlBeansSettingsImpl settings = request.getSettings();
69  		if( settings.isSet( RECORD_REQUEST_REPRESENTATIONS ) )
70  		{
71  			enableRecordingCheckBox.setSelected( settings.getBoolean( RECORD_REQUEST_REPRESENTATIONS ) );
72  		}
73  		else
74  		{
75  			enableRecordingCheckBox.setSelected( getMethod().getResource() == null
76  					|| getMethod().getResource().getService().isGenerated() );
77  		}
78  
79  		enableRecordingCheckBox.addItemListener( new ItemListener()
80  		{
81  			public void itemStateChanged( ItemEvent e )
82  			{
83  				request.getSettings().setBoolean( RECORD_REQUEST_REPRESENTATIONS, enableRecordingCheckBox.isSelected() );
84  			}
85  		} );
86  	}
87  
88  	public boolean beforeSubmit( Submit submit, SubmitContext context )
89  	{
90  		return true;
91  	}
92  
93  	public void afterSubmit( Submit submit, SubmitContext context )
94  	{
95  		HttpResponse response = ( HttpResponse )submit.getResponse();
96  		if( response != null && enableRecordingCheckBox.isSelected() )
97  		{
98  			extractRepresentation( response );
99  		}
100 	}
101 
102 	@SuppressWarnings( "unchecked" )
103 	protected void extractRepresentation( HttpResponse response )
104 	{
105 		String responseContentType = response.getRequestHeaders().get( "Content-Type" );
106 		if( StringUtils.isNullOrEmpty( responseContentType ) )
107 			return;
108 		responseContentType = responseContentType.split( ";" )[0].trim();
109 
110 		RestRepresentation[] representations = getMethod().getRepresentations( RestRepresentation.Type.REQUEST, null );
111 		int c = 0;
112 
113 		for( ; c < representations.length; c++ )
114 		{
115 			String repMediaType = representations[c].getMediaType();
116 
117 			if( responseContentType.equals( repMediaType ) )
118 			{
119 				break;
120 			}
121 		}
122 
123 		if( c == representations.length )
124 		{
125 			RestRepresentation representation = getMethod().addNewRepresentation( RestRepresentation.Type.REQUEST );
126 			representation.setMediaType( responseContentType );
127 
128 			String xmlContent = response.getContentAsXml();
129 
130 			if( !xmlContent.equals( "<xml/>" ) )
131 			{
132 				try
133 				{
134 					XmlCursor cursor = XmlObject.Factory.parse( xmlContent ).newCursor();
135 					cursor.toFirstChild();
136 					representation.setElement( cursor.getName() );
137 				}
138 				catch( Exception e )
139 				{
140 				}
141 			}
142 		}
143 	}
144 
145 	public void release()
146 	{
147 		super.release();
148 		request.removeSubmitListener( this );
149 	}
150 }