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 public RestRequestContentView( HttpRequestMessageEditor restRequestMessageEditor, RestRequestInterface restRequest )
46 {
47 super( restRequestMessageEditor, restRequest );
48 this.restRequest = restRequest;
49 }
50
51 protected RestParamsTable buildParamsTable()
52 {
53 paramsTable = new InstanceRestParamsTable( restRequest.getParams() )
54 {
55 protected void insertAdditionalButtons( JXToolBar toolbar )
56 {
57 toolbar.add( UISupport.createToolbarButton( new UpdateRestParamsAction() ) );
58 }
59 };
60 return paramsTable;
61 }
62
63 public RestParamsTable getParamsTable()
64 {
65 return paramsTable;
66 }
67
68 protected Component buildToolbar()
69 {
70 JXToolBar toolbar = UISupport.createToolbar();
71
72 addMediaTypeCombo( toolbar );
73 toolbar.addSeparator();
74
75 recreateButton = UISupport.createActionButton( new CreateDefaultRepresentationAction(), true );
76 recreateButton.setEnabled( canRecreate() );
77 toolbar.addFixed( recreateButton );
78
79 toolbar.addSeparator();
80
81 addPostQueryCheckBox( toolbar );
82
83 toolbar.setMinimumSize( new Dimension( 50, 20 ) );
84
85 return toolbar;
86 }
87
88 public void propertyChange( PropertyChangeEvent evt )
89 {
90 if( evt.getPropertyName().equals( "mediaType" ) && recreateButton != null )
91 {
92 recreateButton.setEnabled( canRecreate() );
93 }
94 super.propertyChange( evt );
95 }
96
97 protected Object[] getRequestMediaTypes()
98 {
99 StringList result = new StringList( super.getRequestMediaTypes() );
100
101 for( RestRepresentation representation : restRequest.getRepresentations( RestRepresentation.Type.REQUEST, null ) )
102 {
103 if( !result.contains( representation.getMediaType() ) )
104 result.add( representation.getMediaType() );
105 }
106
107 return result.toStringArray();
108 }
109
110 private boolean canRecreate()
111 {
112 for( RestRepresentation representation : restRequest.getRepresentations( RestRepresentation.Type.REQUEST,
113 restRequest.getMediaType() ) )
114 {
115 if( representation.getSchemaType() != null )
116 return true;
117 }
118 return false;
119 }
120
121 private class UpdateRestParamsAction extends AbstractAction
122 {
123 private UpdateRestParamsAction()
124 {
125 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/add_property.gif" ) );
126 putValue( Action.SHORT_DESCRIPTION, "Updates this Requests params from a specified URL" );
127 }
128
129 public void actionPerformed( ActionEvent e )
130 {
131 String str = UISupport.prompt( "Enter new url below", "Extract Params", "" );
132 if( str == null )
133 return;
134
135 try
136 {
137 restRequest.getParams().resetValues();
138 RestUtils.extractParams( str, restRequest.getParams(), false );
139 paramsTable.refresh();
140 }
141 catch( Exception e1 )
142 {
143 UISupport.showErrorMessage( e1 );
144 }
145 }
146 }
147
148 private class CreateDefaultRepresentationAction extends AbstractAction
149 {
150 private CreateDefaultRepresentationAction()
151 {
152 putValue( Action.SMALL_ICON, UISupport.createImageIcon( "/recreate_request.gif" ) );
153 putValue( Action.SHORT_DESCRIPTION, "Recreates a default representation from the schema" );
154 }
155
156 public void actionPerformed( ActionEvent e )
157 {
158 TupleList<RestRepresentation, SchemaType> list = new TupleList<RestRepresentation, SchemaType>()
159 {
160 protected String toStringHandler( Tuple tuple )
161 {
162 return tuple.getValue2().getDocumentElementName().toString();
163 }
164 };
165
166 for( RestRepresentation representation : ( ( RestRequestInterface )restRequest ).getRepresentations(
167 RestRepresentation.Type.REQUEST, restRequest.getMediaType() ) )
168 {
169 SchemaType schemaType = representation.getSchemaType();
170 if( schemaType != null )
171 {
172 list.add( representation, schemaType );
173 }
174 }
175
176 if( list.isEmpty() )
177 {
178 UISupport.showErrorMessage( "Missing recreatable representations for this method" );
179 return;
180 }
181
182 TupleList<RestRepresentation, SchemaType>.Tuple result = ( TupleList.Tuple )UISupport.prompt(
183 "Select element to create", "Create default content", list.toArray() );
184 if( result == null )
185 {
186 return;
187 }
188
189 restRequest.setRequestContent( SampleXmlUtil.createSampleForType( result.getValue2() ) );
190 }
191 }
192 }