View Javadoc

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