1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.panels.request.inspectors.representations;
14
15 import java.awt.event.ItemEvent;
16 import java.awt.event.ItemListener;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.List;
20
21 import javax.swing.JCheckBox;
22
23 import org.apache.xmlbeans.XmlCursor;
24 import org.apache.xmlbeans.XmlObject;
25
26 import com.eviware.soapui.impl.rest.RestRepresentation;
27 import com.eviware.soapui.impl.rest.RestRequestInterface;
28 import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
29 import com.eviware.soapui.impl.support.HttpUtils;
30 import com.eviware.soapui.impl.wsdl.submit.transports.http.HttpResponse;
31 import com.eviware.soapui.model.iface.Submit;
32 import com.eviware.soapui.model.iface.SubmitContext;
33 import com.eviware.soapui.model.iface.SubmitListener;
34 import com.eviware.soapui.support.UISupport;
35 import com.eviware.soapui.support.components.JXToolBar;
36
37 public class RestResponseRepresentationsInspector extends AbstractRestRepresentationsInspector implements
38 SubmitListener
39 {
40 private JCheckBox enableRecordingCheckBox;
41 public static final String RECORD_RESPONSE_REPRESENTATIONS = "RecordResponseRepresentations";
42 private RestRequestInterface request;
43
44 protected RestResponseRepresentationsInspector( RestRequestInterface request )
45 {
46 super( request.getRestMethod(), "Representations", "Response Representations", new RestRepresentation.Type[] {
47 RestRepresentation.Type.RESPONSE, RestRepresentation.Type.FAULT } );
48
49 request.addSubmitListener( this );
50 this.request = request;
51 }
52
53 @Override
54 protected void addToToolbar( JXToolBar toolbar )
55 {
56 enableRecordingCheckBox = new JCheckBox( "Auto-Create" );
57 enableRecordingCheckBox.setToolTipText( "Automatically create Representations from received Responses" );
58 enableRecordingCheckBox.setOpaque( false );
59 UISupport.setFixedSize( enableRecordingCheckBox, 150, 20 );
60 toolbar.addFixed( enableRecordingCheckBox );
61 XmlBeansSettingsImpl settings = getMethod().getSettings();
62 if( settings.isSet( RECORD_RESPONSE_REPRESENTATIONS ) )
63 {
64 enableRecordingCheckBox.setSelected( settings.getBoolean( RECORD_RESPONSE_REPRESENTATIONS ) );
65 }
66 else
67 {
68 enableRecordingCheckBox.setSelected( getMethod().getResource() == null
69 || getMethod().getResource().getService().isGenerated() );
70 }
71
72 enableRecordingCheckBox.addItemListener( new ItemListener()
73 {
74 public void itemStateChanged( ItemEvent e )
75 {
76 getMethod().getSettings()
77 .setBoolean( RECORD_RESPONSE_REPRESENTATIONS, enableRecordingCheckBox.isSelected() );
78 }
79 } );
80 }
81
82 @Override
83 public boolean beforeSubmit( Submit submit, SubmitContext context )
84 {
85 return true;
86 }
87
88 public void afterSubmit( Submit submit, SubmitContext context )
89 {
90 HttpResponse response = ( HttpResponse )submit.getResponse();
91 if( response != null && enableRecordingCheckBox.isSelected() )
92 {
93 if( HttpUtils.isErrorStatus( response.getStatusCode() ) )
94 {
95 extractRepresentation( response, RestRepresentation.Type.FAULT );
96 }
97 else
98 {
99 extractRepresentation( response, RestRepresentation.Type.RESPONSE );
100 }
101 }
102 }
103
104 @SuppressWarnings( "unchecked" )
105 protected void extractRepresentation( HttpResponse response, RestRepresentation.Type type )
106 {
107 RestRepresentation[] representations = getMethod().getRepresentations( type, null );
108 int c = 0;
109 for( ; c < representations.length; c++ )
110 {
111 if( representations[c].getMediaType() != null
112 && representations[c].getMediaType().equals( response.getContentType() ) )
113 {
114 List status = representations[c].getStatus();
115 if( status == null || !status.contains( response.getStatusCode() ) )
116 {
117 status = status == null ? new ArrayList<Integer>() : new ArrayList<Integer>( status );
118 status.add( response.getStatusCode() );
119 representations[c].setStatus( status );
120 }
121 break;
122 }
123 }
124
125 if( c == representations.length )
126 {
127 RestRepresentation representation = getMethod().addNewRepresentation( type );
128 representation.setMediaType( response.getContentType() );
129 representation.setStatus( Arrays.asList( response.getStatusCode() ) );
130
131 String xmlContent = response.getContentAsXml();
132
133 if( !xmlContent.equals( "<xml/>" ) )
134 {
135
136
137 try
138 {
139 XmlCursor cursor = XmlObject.Factory.parse( xmlContent ).newCursor();
140 cursor.toFirstChild();
141 representation.setElement( cursor.getName() );
142 }
143 catch( Exception e )
144 {
145
146 }
147 }
148 }
149 }
150
151 @Override
152 public void release()
153 {
154 super.release();
155 request.removeSubmitListener( this );
156 }
157 }