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 protected void addToToolbar( JXToolBar toolbar )
54 {
55 enableRecordingCheckBox = new JCheckBox( "Auto-Create" );
56 enableRecordingCheckBox.setToolTipText( "Automatically create Representations from received Responses" );
57 enableRecordingCheckBox.setOpaque( false );
58 UISupport.setFixedSize( enableRecordingCheckBox, 150, 20 );
59 toolbar.addFixed( enableRecordingCheckBox );
60 XmlBeansSettingsImpl settings = getMethod().getSettings();
61 if( settings.isSet( RECORD_RESPONSE_REPRESENTATIONS ) )
62 {
63 enableRecordingCheckBox.setSelected( settings.getBoolean( RECORD_RESPONSE_REPRESENTATIONS ) );
64 }
65 else
66 {
67 enableRecordingCheckBox.setSelected( getMethod().getResource() == null
68 || getMethod().getResource().getService().isGenerated() );
69 }
70
71 enableRecordingCheckBox.addItemListener( new ItemListener()
72 {
73 public void itemStateChanged( ItemEvent e )
74 {
75 getMethod().getSettings()
76 .setBoolean( RECORD_RESPONSE_REPRESENTATIONS, enableRecordingCheckBox.isSelected() );
77 }
78 } );
79 }
80
81 public boolean beforeSubmit( Submit submit, SubmitContext context )
82 {
83 return true;
84 }
85
86 public void afterSubmit( Submit submit, SubmitContext context )
87 {
88 HttpResponse response = request.getResponse();
89 if( response != null && enableRecordingCheckBox.isSelected() )
90 {
91 if( HttpUtils.isErrorStatus( response.getStatusCode() ) )
92 {
93 extractRepresentation( response, RestRepresentation.Type.FAULT );
94 }
95 else
96 {
97 extractRepresentation( response, RestRepresentation.Type.RESPONSE );
98 }
99 }
100 }
101
102 @SuppressWarnings( "unchecked" )
103 protected void extractRepresentation( HttpResponse response, RestRepresentation.Type type )
104 {
105 RestRepresentation[] representations = getMethod().getRepresentations( type, null );
106 int c = 0;
107 for( ; c < representations.length; c++ )
108 {
109 if( representations[c].getMediaType() != null
110 && representations[c].getMediaType().equals( response.getContentType() ) )
111 {
112 List status = representations[c].getStatus();
113 if( status == null || !status.contains( response.getStatusCode() ) )
114 {
115 status = status == null ? new ArrayList<Integer>() : new ArrayList<Integer>( status );
116 status.add( response.getStatusCode() );
117 representations[c].setStatus( status );
118 }
119 break;
120 }
121 }
122
123 if( c == representations.length )
124 {
125 RestRepresentation representation = getMethod().addNewRepresentation( type );
126 representation.setMediaType( response.getContentType() );
127 representation.setStatus( Arrays.asList( response.getStatusCode() ) );
128
129 String xmlContent = response.getContentAsXml();
130
131 if( !xmlContent.equals( "<xml/>" ) )
132 {
133
134
135 try
136 {
137 XmlCursor cursor = XmlObject.Factory.parse( xmlContent ).newCursor();
138 cursor.toFirstChild();
139 representation.setElement( cursor.getName() );
140 }
141 catch( Exception e )
142 {
143
144 }
145 }
146 }
147 }
148
149 public void release()
150 {
151 super.release();
152 request.removeSubmitListener( this );
153 }
154 }