1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package com.eviware.soapui.impl.rest.panels.request.inspectors.representations;
26
27 import com.eviware.soapui.impl.rest.RestRepresentation;
28 import com.eviware.soapui.impl.rest.RestRequest;
29 import com.eviware.soapui.impl.settings.XmlBeansSettingsImpl;
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.StringUtils;
35 import com.eviware.soapui.support.UISupport;
36 import com.eviware.soapui.support.components.JXToolBar;
37
38 import javax.swing.*;
39 import java.awt.event.ItemEvent;
40 import java.awt.event.ItemListener;
41
42 public class RestRequestRepresentationsInspector extends AbstractRestRepresentationsInspector implements SubmitListener
43 {
44 private JCheckBox enableRecordingCheckBox;
45 public static final String RECORD_REQUEST_REPRESENTATIONS = "RecordRequestRepresentations";
46
47 protected RestRequestRepresentationsInspector( RestRequest request )
48 {
49 super( request, "Representations", "Request Representations", new RestRepresentation.Type[]
50 {RestRepresentation.Type.REQUEST} );
51
52 request.addSubmitListener( this );
53 }
54
55 protected JXToolBar buildToolbar()
56 {
57 JXToolBar toolbar = super.buildToolbar();
58
59 toolbar.addSeparator();
60
61 enableRecordingCheckBox = new JCheckBox( "Auto-Create" );
62 enableRecordingCheckBox.setToolTipText( "Automatically create Representations from sent Requests" );
63 enableRecordingCheckBox.setOpaque( false );
64 UISupport.setFixedSize( enableRecordingCheckBox, 100, 20 );
65 toolbar.addFixed( enableRecordingCheckBox );
66 XmlBeansSettingsImpl settings = getRequest().getSettings();
67 if( settings.isSet( RECORD_REQUEST_REPRESENTATIONS ) )
68 {
69 enableRecordingCheckBox.setSelected( settings.getBoolean( RECORD_REQUEST_REPRESENTATIONS ) );
70 }
71 else
72 {
73 enableRecordingCheckBox.setSelected( getRequest().getResource().getService().isGenerated() );
74 }
75
76 enableRecordingCheckBox.addItemListener( new ItemListener()
77 {
78 public void itemStateChanged( ItemEvent e )
79 {
80 getRequest().getSettings().setBoolean( RECORD_REQUEST_REPRESENTATIONS, enableRecordingCheckBox.isSelected() );
81 }
82 } );
83
84 return toolbar;
85 }
86
87 public boolean beforeSubmit( Submit submit, SubmitContext context )
88 {
89 return true;
90 }
91
92 public void afterSubmit( Submit submit, SubmitContext context )
93 {
94 HttpResponse response = (HttpResponse) submit.getResponse();
95 if( response != null && enableRecordingCheckBox.isSelected() )
96 {
97 extractRepresentation( response );
98 }
99 }
100
101 @SuppressWarnings( "unchecked" )
102 protected void extractRepresentation( HttpResponse response )
103 {
104 String responseContentType = response.getRequestHeaders().get( "Content-Type" );
105 if( StringUtils.isNullOrEmpty( responseContentType ))
106 return;
107
108 RestRepresentation[] representations = getRequest().getRepresentations( RestRepresentation.Type.REQUEST, null );
109 int c = 0;
110
111 for( ; c < representations.length; c++ )
112 {
113 String repMediaType = representations[c].getMediaType();
114
115 if( responseContentType.equals( repMediaType ))
116 {
117 break;
118 }
119 }
120
121 if( c == representations.length )
122 {
123 RestRepresentation representation = getRequest().addNewRepresentation( RestRepresentation.Type.REQUEST );
124 representation.setMediaType( responseContentType );
125 }
126 }
127
128 public void release()
129 {
130 super.release();
131 getRequest().removeSubmitListener( this );
132 }
133 }