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.support;
14  
15  import com.eviware.soapui.config.RestParametersConfig;
16  import com.eviware.soapui.impl.rest.RestRequest;
17  import com.eviware.soapui.impl.rest.RestResource;
18  import com.eviware.soapui.impl.rest.panels.resource.RestParamsTable;
19  import com.eviware.soapui.impl.rest.support.RestUtils;
20  import com.eviware.soapui.impl.rest.support.XmlBeansRestParamsTestPropertyHolder;
21  import com.eviware.soapui.impl.support.AbstractHttpRequest.RequestMethod;
22  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
23  import com.eviware.soapui.model.ModelItem;
24  import com.eviware.soapui.support.MessageSupport;
25  import com.eviware.soapui.support.StringUtils;
26  import com.eviware.soapui.support.UISupport;
27  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
28  import com.eviware.x.form.XFormDialog;
29  import com.eviware.x.form.support.ADialogBuilder;
30  import com.eviware.x.form.support.AField;
31  import com.eviware.x.form.support.AField.AFieldType;
32  import com.eviware.x.form.support.AForm;
33  import com.eviware.x.form.validators.RequiredValidator;
34  
35  import javax.swing.*;
36  import java.awt.event.ActionEvent;
37  import java.net.MalformedURLException;
38  import java.net.URL;
39  
40  /***
41   * Actions for importing an existing soapUI project file into the current workspace
42   *
43   * @author Ole.Matzura
44   */
45  
46  public abstract class NewRestResourceActionBase<T extends ModelItem> extends AbstractSoapUIAction<T>
47  {
48     private XFormDialog dialog;
49     private XmlBeansRestParamsTestPropertyHolder params;
50     private RestParamsTable paramsTable;
51     public static final MessageSupport messages = MessageSupport.getMessages( NewRestResourceActionBase.class );
52  
53     public NewRestResourceActionBase( String title, String description )
54     {
55        super( title, description );
56     }
57  
58     public void perform( T service, Object param )
59     {
60        if( dialog == null )
61        {
62           dialog = ADialogBuilder.buildDialog( Form.class );
63           dialog.getFormField( Form.RESOURCENAME ).addFormFieldValidator( new RequiredValidator() );
64           dialog.getFormField( Form.EXTRACTPARAMS ).setProperty( "action", new ExtractParamsAction() );
65           dialog.setBooleanValue( Form.CREATEREQUEST, true );
66        }
67        else
68        {
69           dialog.setValue( Form.RESOURCENAME, "" );
70           dialog.setValue( Form.RESOURCEPATH, "" );
71        }
72  
73        params = new XmlBeansRestParamsTestPropertyHolder( service,
74                RestParametersConfig.Factory.newInstance() );
75  
76        if( param instanceof URL )
77        {
78           String path = RestUtils.extractParams( param.toString(), params, false );
79           dialog.setValue( Form.RESOURCEPATH, path );
80  
81           setNameFromPath( path );
82  
83           if( paramsTable != null )
84              paramsTable.refresh();
85        }
86  
87        paramsTable = new RestParamsTable( params, false );
88        dialog.getFormField( Form.PARAMSTABLE ).setProperty( "component", paramsTable );
89  
90        if( dialog.show() )
91        {
92           String path = dialog.getValue( Form.RESOURCEPATH );
93  
94           try
95           {
96              URL url = new URL( path );
97              path = url.getPath();
98           }
99           catch( MalformedURLException e )
100          {
101          }
102 
103          RestResource resource = createRestResource( service, path, dialog );
104 
105          resource.getParams().addParameters( params );
106 
107          UISupport.select( resource );
108 
109          if( dialog.getBooleanValue( Form.CREATEREQUEST ) )
110          {
111             createRequest( resource );
112          }
113       }
114    }
115 
116    protected abstract RestResource createRestResource( T service, String path, XFormDialog dialog );
117 
118    private void setNameFromPath( String path )
119    {
120       String[] items = path.split( "/" );
121 
122       if( items.length > 0 )
123       {
124          dialog.setValue( Form.RESOURCENAME, items[items.length - 1] );
125       }
126    }
127 
128    protected void createRequest( RestResource resource )
129    {
130       RestRequest request = resource.addNewRequest( dialog.getValue( Form.RESOURCENAME ) );
131       request.setMethod( RequestMethod.GET );
132       UISupport.showDesktopPanel( request );
133    }
134 
135    private class ExtractParamsAction extends AbstractAction
136    {
137       public ExtractParamsAction()
138       {
139          super( "Extract Params" );
140       }
141 
142       public void actionPerformed( ActionEvent e )
143       {
144          try
145          {
146             String path = RestUtils.extractParams( dialog.getValue( Form.RESOURCEPATH ), params, false );
147             dialog.setValue( Form.RESOURCEPATH, path );
148 
149             if( StringUtils.isNullOrEmpty( dialog.getValue( Form.RESOURCENAME ) ) )
150                setNameFromPath( path );
151 
152             paramsTable.refresh();
153          }
154          catch( Exception e1 )
155          {
156             UISupport.showInfoMessage( "No parameters to extract!" );
157          }
158       }
159    }
160 
161    @AForm( name = "Form.Title", description = "Form.Description", helpUrl = HelpUrls.NEWRESTSERVICE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
162    public interface Form
163    {
164       @AField( description = "Form.ServiceName.Description", type = AFieldType.STRING )
165       public final static String RESOURCENAME = messages.get( "Form.ResourceName.Label" );
166 
167       @AField( description = "Form.ServiceUrl.Description", type = AFieldType.STRING )
168       public final static String RESOURCEPATH = messages.get( "Form.ResourcePath.Label" );
169 
170       @AField( description = "Form.ExtractParams.Description", type = AFieldType.ACTION )
171       public final static String EXTRACTPARAMS = messages.get( "Form.ExtractParams.Label" );
172 
173       @AField( description = "Form.ParamsTable.Description", type = AFieldType.COMPONENT )
174       public final static String PARAMSTABLE = messages.get( "Form.ParamsTable.Label" );
175 
176       @AField( description = "Form.CreateRequest.Description", type = AFieldType.BOOLEAN )
177       public final static String CREATEREQUEST = messages.get( "Form.CreateRequest.Label" );
178    }
179 }