1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.panels.request.views.content;
14
15 import java.awt.Component;
16 import java.awt.Dimension;
17 import java.awt.event.ActionEvent;
18 import java.beans.PropertyChangeEvent;
19
20 import javax.swing.AbstractAction;
21 import javax.swing.Action;
22 import javax.swing.JButton;
23
24 import org.apache.xmlbeans.SchemaType;
25
26 import com.eviware.soapui.impl.rest.RestRepresentation;
27 import com.eviware.soapui.impl.rest.RestRequestInterface;
28 import com.eviware.soapui.impl.rest.panels.resource.InstanceRestParamsTable;
29 import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
30 import com.eviware.soapui.impl.rest.support.RestUtils;
31 import com.eviware.soapui.impl.support.http.HttpRequestContentView;
32 import com.eviware.soapui.impl.support.panels.AbstractHttpXmlRequestDesktopPanel.HttpRequestMessageEditor;
33 import com.eviware.soapui.impl.wsdl.support.xsd.SampleXmlUtil;
34 import com.eviware.soapui.support.UISupport;
35 import com.eviware.soapui.support.components.JXToolBar;
36 import com.eviware.soapui.support.types.StringList;
37 import com.eviware.soapui.support.types.TupleList;
38
39 public class RestRequestContentView extends HttpRequestContentView
40 {
41 private RestRequestInterface restRequest;
42 private JButton recreateButton;
43 private RestParamsTable paramsTable;
44
45 @SuppressWarnings("unchecked")
46 public RestRequestContentView( HttpRequestMessageEditor restRequestMessageEditor, RestRequestInterface restRequest )
47 {
48 super( restRequestMessageEditor, restRequest );
49 this.restRequest = restRequest;
50 }
51
52 protected RestParamsTable buildParamsTable()
53 {
54 paramsTable = new InstanceRestParamsTable( restRequest.getParams() )
55 {
56 protected void insertAdditionalButtons( JXToolBar toolbar )
57 {
58 toolbar.add( UISupport.createToolbarButton( new UpdateRestParamsAction() ) );
59 }
60 };
61 return paramsTable;
62 }
63
64 public RestParamsTable getParamsTable()
65 {
66 return paramsTable;
67 }
68
69 protected Component buildToolbar()
70 {
71 JXToolBar toolbar = UISupport.createToolbar();
72
73 addMediaTypeCombo( toolbar );
74 toolbar.addSeparator();
75
76 recreateButton = UISupport.createActionButton( new CreateDefaultRepresentationAction(), true );
77 recreateButton.setEnabled( canRecreate() );
78 toolbar.addFixed( recreateButton );
79
80 toolbar.addSeparator();
81
82 addPostQueryCheckBox( toolbar );
83
84 toolbar.setMinimumSize( new Dimension( 50, 20 ) );
85
86 return toolbar;
87 }
88
89 public void propertyChange( PropertyChangeEvent evt )
90 {
91 if( evt.getPropertyName().equals( "mediaType" ) && recreateButton != null )
92 {
93 recreateButton.setEnabled( canRecreate() );
94 }
95 else if( evt.getPropertyName().equals( "restMethod" ))
96 {
97 paramsTable.setParams( restRequest.getParams() );
98 }
99
100 super.propertyChange( evt );
101 }
102
103 protected Object[] getRequestMediaTypes()
104 {
105 StringList result = new StringList( super.getRequestMediaTypes() );
106
107 for( RestRepresentation representation : restRequest.getRepresentations( RestRepresentation.Type.REQUEST, null ) )
108 {
109 if( !result.contains( representation.getMediaType() ) )
110 result.add( representation.getMediaType() );
111 }
112
113 return result.toStringArray();
114 }
115
116 private boolean canRecreate()
117 {
118 for( RestRepresentation representation : restRequest.getRepresentations( RestRepresentation.Type.REQUEST,
119 restRequest.getMediaType() ) )
120 {
121 if( representation.getSchemaType() != null )
122 return true;
123 }
124 return false;
125 }
126
127 private class UpdateRestParamsAction extends AbstractAction
128 {
129 private UpdateRestParamsAction()
130 {
131 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
132 putValue( Action.SHORT_DESCRIPTION, "Updates this Requests params from a specified URL" );
133 }
134
135 public void actionPerformed( ActionEvent e )
136 {
137 String str = UISupport.prompt( "Enter new url below", "Extract Params", "" );
138 if( str == null )
139 return;
140
141 try
142 {
143 restRequest.getParams().resetValues();
144 RestUtils.extractParams( str, restRequest.getParams(), false );
145 paramsTable.refresh();
146 }
147 catch( Exception e1 )
148 {
149 UISupport.showErrorMessage( e1 );
150 }
151 }
152 }
153
154 private class CreateDefaultRepresentationAction extends AbstractAction
155 {
156 private CreateDefaultRepresentationAction()
157 {
158 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/recreate_request.gif" ) );
159 putValue( Action.SHORT_DESCRIPTION, "Recreates a default representation from the schema" );
160 }
161
162 @SuppressWarnings("unchecked")
163 public void actionPerformed( ActionEvent e )
164 {
165 TupleList<RestRepresentation, SchemaType> list = new TupleList<RestRepresentation, SchemaType>()
166 {
167 protected String toStringHandler( Tuple tuple )
168 {
169 return tuple.getValue2().getDocumentElementName().toString();
170 }
171 };
172
173 for( RestRepresentation representation : ( ( RestRequestInterface )restRequest ).getRepresentations(
174 RestRepresentation.Type.REQUEST, restRequest.getMediaType() ) )
175 {
176 SchemaType schemaType = representation.getSchemaType();
177 if( schemaType != null )
178 {
179 list.add( representation, schemaType );
180 }
181 }
182
183 if( list.isEmpty() )
184 {
185 UISupport.showErrorMessage( "Missing recreatable representations for this method" );
186 return;
187 }
188
189 TupleList<RestRepresentation, SchemaType>.Tuple result = ( TupleList.Tuple )UISupport.prompt(
190 "Select element to create", "Create default content", list.toArray() );
191 if( result == null )
192 {
193 return;
194 }
195
196 restRequest.setRequestContent( SampleXmlUtil.createSampleForType( result.getValue2() ) );
197 }
198 }
199 }