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.resource;
14  
15  import com.eviware.soapui.impl.rest.RestMethod;
16  import com.eviware.soapui.impl.rest.RestRequestInterface;
17  import com.eviware.soapui.impl.rest.RestResource;
18  import com.eviware.soapui.impl.rest.actions.support.NewRestResourceActionBase;
19  import com.eviware.soapui.support.MessageSupport;
20  import com.eviware.soapui.support.UISupport;
21  import com.eviware.x.form.XFormDialog;
22  
23  /***
24   * Actions for importing an existing soapUI project file into the current
25   * workspace
26   * 
27   * @author Ole.Matzura
28   */
29  
30  public class NewRestChildResourceAction extends NewRestResourceActionBase<RestResource>
31  {
32  	public static final String SOAPUI_ACTION_ID = "NewRestChildResourceAction";
33  	public static final MessageSupport messages = MessageSupport.getMessages( NewRestChildResourceAction.class );
34  
35  	public NewRestChildResourceAction()
36  	{
37  		super( messages.get( "title" ), messages.get( "description" ) );
38  	}
39  
40  	protected RestResource createRestResource( RestResource parentResource, String path, XFormDialog dialog )
41  	{
42  		RestResource possibleParent = null;
43  		String p = parentResource.getFullPath() + path;
44  
45  		for( RestResource resource : parentResource.getAllChildResources() )
46  		{
47  			if( p.startsWith( resource.getFullPath() ) )
48  			{
49  				int c = 0;
50  				for( ; c < resource.getChildResourceCount(); c++ )
51  				{
52  					if( p.startsWith( resource.getChildResourceAt( c ).getFullPath() ) )
53  						break;
54  				}
55  
56  				// found subresource?
57  				if( c != resource.getChildResourceCount() )
58  					continue;
59  
60  				possibleParent = resource;
61  				break;
62  			}
63  		}
64  
65  		RestResource resource;
66  
67  		if( possibleParent != null
68  				&& UISupport.confirm( "Create resource as child to [" + possibleParent.getName() + "]",
69  						"New Child Resource" ) )
70  		{
71  			// adjust path
72  			path = path.substring( p.length() - possibleParent.getFullPath().length() - 1 );
73  			resource = possibleParent.addNewChildResource( dialog.getValue( Form.RESOURCENAME ), path );
74  		}
75  		else
76  		{
77  			resource = parentResource.addNewChildResource( dialog.getValue( Form.RESOURCENAME ), path );
78  		}
79  
80  		return resource;
81  	}
82  
83  	@Override
84  	protected RestMethod createRestMethod( RestResource resource, XFormDialog dialog )
85  	{
86  		RestMethod method = resource.addNewMethod( dialog.getValue( Form.RESOURCENAME ) );
87  		method.setMethod( RestRequestInterface.RequestMethod.GET );
88  		return method;
89  	}
90  }