1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.rest.actions.project;
14
15 import com.eviware.soapui.SoapUI;
16 import com.eviware.soapui.impl.rest.RestService;
17 import com.eviware.soapui.impl.rest.RestServiceFactory;
18 import com.eviware.soapui.impl.rest.actions.service.NewRestResourceAction;
19 import com.eviware.soapui.impl.wsdl.WsdlProject;
20 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
21 import com.eviware.soapui.model.ModelItem;
22 import com.eviware.soapui.support.MessageSupport;
23 import com.eviware.soapui.support.UISupport;
24 import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
25 import com.eviware.x.form.XFormDialog;
26 import com.eviware.x.form.XFormField;
27 import com.eviware.x.form.XFormFieldListener;
28 import com.eviware.x.form.support.ADialogBuilder;
29 import com.eviware.x.form.support.AField;
30 import com.eviware.x.form.support.AField.AFieldType;
31 import com.eviware.x.form.support.AForm;
32 import com.eviware.x.form.validators.RequiredValidator;
33 import org.apache.log4j.Logger;
34
35 import java.net.MalformedURLException;
36 import java.net.URL;
37
38 /***
39 * Actions for importing an existing soapUI project file into the current workspace
40 *
41 * @author Ole.Matzura
42 */
43
44 public class NewRestServiceAction extends AbstractSoapUIAction<WsdlProject>
45 {
46 public static final String SOAPUI_ACTION_ID = "NewRestServiceAction";
47 public static final MessageSupport messages = MessageSupport.getMessages( NewRestServiceAction.class );
48 private XFormDialog dialog;
49 private final static Logger log = Logger.getLogger( NewRestServiceAction.class );
50
51 public NewRestServiceAction()
52 {
53 super( messages.get( "title" ), messages.get( "description" ) );
54 }
55
56 public void perform( WsdlProject project, Object param )
57 {
58 if( dialog == null )
59 {
60 dialog = ADialogBuilder.buildDialog( Form.class );
61 dialog.getFormField( Form.SERVICENAME ).addFormFieldValidator( new RequiredValidator( "Service Name is required" ) );
62 dialog.getFormField( Form.SERVICEENDPOINT ).addFormFieldListener( new XFormFieldListener()
63 {
64
65 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
66 {
67 boolean enable = false;
68
69 try
70 {
71 URL url = new URL( newValue );
72 enable = url.getPath().length() > 0 &&
73 !( url.getPath().length() == 1 && url.getPath().charAt( 0 ) == '/' );
74 }
75 catch( MalformedURLException e )
76 {
77 }
78
79 dialog.getFormField( Form.EXTRACTPARAMS ).setEnabled( enable );
80 }
81 } );
82
83 dialog.getFormField( Form.EXTRACTPARAMS ).setEnabled( false );
84 dialog.getFormField( Form.EXTRACTPARAMS ).addFormFieldListener( new XFormFieldListener()
85 {
86 public void valueChanged( XFormField sourceField, String newValue, String oldValue )
87 {
88 dialog.getFormField( Form.CREATERESOURCE ).setEnabled( !dialog.getBooleanValue( Form.EXTRACTPARAMS ) );
89 }
90 } );
91
92 }
93 else
94 {
95 dialog.setValue( Form.SERVICEENDPOINT, "" );
96 dialog.setBooleanValue( Form.EXTRACTPARAMS, false );
97 }
98
99 if( param instanceof ModelItem )
100 dialog.setValue( Form.SERVICENAME, ( (ModelItem) param ).getName() );
101 else
102 dialog.setValue( Form.SERVICENAME, project.getName() );
103
104 if( dialog.show() )
105 {
106 RestService restService = (RestService) project.addNewInterface( dialog.getValue( Form.SERVICENAME ), RestServiceFactory.REST_TYPE );
107 UISupport.select( restService );
108 URL url = null;
109
110 try
111 {
112 url = new URL( dialog.getValue( Form.SERVICEENDPOINT ) );
113 String endpoint = url.getProtocol() + "://" + url.getHost();
114 if( url.getPort() > 0 )
115 endpoint += ":" + url.getPort();
116
117 restService.addEndpoint( endpoint );
118 restService.setBasePath( url.getPath() );
119 }
120 catch( Exception e )
121 {
122 }
123
124 if( dialog.getFormField( Form.EXTRACTPARAMS ).isEnabled() && dialog.getBooleanValue( Form.EXTRACTPARAMS ) )
125 {
126 restService.setBasePath( "" );
127 SoapUI.getActionRegistry().getAction( NewRestResourceAction.SOAPUI_ACTION_ID ).perform( restService, url );
128 }
129
130 if( dialog.getFormField( Form.CREATERESOURCE ).isEnabled() && dialog.getBooleanValue( Form.CREATERESOURCE ) )
131 {
132 SoapUI.getActionRegistry().getAction( NewRestResourceAction.SOAPUI_ACTION_ID ).perform( restService, null );
133 }
134 }
135 }
136
137 @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
138 public interface Form
139 {
140 @AField( description = "Form.ServiceName.Description", type = AFieldType.STRING )
141 public final static String SERVICENAME = messages.get( "Form.ServiceName.Label" );
142
143 @AField( description = "Form.ServiceUrl.Description", type = AFieldType.STRING )
144 public final static String SERVICEENDPOINT = messages.get( "Form.ServiceUrl.Label" );
145
146 @AField( description = "Form.ExtractParams.Description", type = AFieldType.BOOLEAN )
147 public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
148
149 @AField( description = "Form.CreateResource.Description", type = AFieldType.BOOLEAN )
150 public final static String CREATERESOURCE = messages.get( "Form.CreateResource.Label" );
151
152 }
153 }