View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2008 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
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              public void valueChanged( XFormField sourceField, String newValue, String oldValue )
65              {
66                 boolean enable = false;
67  
68                 try
69                 {
70                    URL url = new URL( newValue );
71                    enable = url.getPath().length() > 0 &&
72                            !( url.getPath().length() == 1 && url.getPath().charAt( 0 ) == '/' );
73                 }
74                 catch( MalformedURLException e )
75                 {
76                 }
77  
78                 dialog.getFormField( Form.EXTRACTPARAMS ).setEnabled( enable );
79              }
80           } );
81  
82           dialog.getFormField( Form.EXTRACTPARAMS ).setEnabled( false );
83           dialog.getFormField( Form.EXTRACTPARAMS ).addFormFieldListener( new XFormFieldListener()
84           {
85              public void valueChanged( XFormField sourceField, String newValue, String oldValue )
86              {
87                 dialog.getFormField( Form.CREATERESOURCE ).setEnabled( !dialog.getBooleanValue( Form.EXTRACTPARAMS ) );
88              }
89           } );
90        }
91        else
92        {
93           dialog.setValue( Form.SERVICEENDPOINT, "" );
94           dialog.setBooleanValue( Form.EXTRACTPARAMS, false );
95        }
96  
97        if( param instanceof ModelItem )
98           dialog.setValue( Form.SERVICENAME, ( (ModelItem) param ).getName() );
99        else
100          dialog.setValue( Form.SERVICENAME, project.getName() );
101 
102       if( dialog.show() )
103       {
104          RestService restService = (RestService) project.addNewInterface( dialog.getValue( Form.SERVICENAME ), RestServiceFactory.REST_TYPE );
105          UISupport.select( restService );
106          URL url = null;
107 
108          try
109          {
110             url = new URL( dialog.getValue( Form.SERVICEENDPOINT ) );
111             String endpoint = url.getProtocol() + "://" + url.getHost();
112             if( url.getPort() > 0 )
113                endpoint += ":" + url.getPort();
114 
115             restService.addEndpoint( endpoint );
116             restService.setBasePath( url.getPath() );
117          }
118          catch( Exception e )
119          {
120          }
121 
122          if( dialog.getFormField( Form.EXTRACTPARAMS ).isEnabled() && dialog.getBooleanValue( Form.EXTRACTPARAMS ) )
123          {
124             restService.setBasePath( "" );
125             SoapUI.getActionRegistry().getAction( NewRestResourceAction.SOAPUI_ACTION_ID ).perform( restService, url );
126          }
127 
128          if( dialog.getFormField( Form.CREATERESOURCE ).isEnabled() && dialog.getBooleanValue( Form.CREATERESOURCE ) )
129          {
130             SoapUI.getActionRegistry().getAction( NewRestResourceAction.SOAPUI_ACTION_ID ).perform( restService, null );
131          }
132       }
133    }
134 
135    @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
136    public interface Form
137    {
138       @AField( description = "Form.ServiceName.Description", type = AFieldType.STRING )
139       public final static String SERVICENAME = messages.get( "Form.ServiceName.Label" );
140 
141       @AField( description = "Form.ServiceUrl.Description", type = AFieldType.STRING )
142       public final static String SERVICEENDPOINT = messages.get( "Form.ServiceUrl.Label" );
143 
144       @AField( description = "Form.ExtractParams.Description", type = AFieldType.BOOLEAN )
145       public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
146 
147       @AField( description = "Form.CreateResource.Description", type = AFieldType.BOOLEAN )
148       public final static String CREATERESOURCE = messages.get( "Form.CreateResource.Label" );
149 
150    }
151 }