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;
14  
15  import com.eviware.soapui.config.RestResourceConfig;
16  import com.eviware.soapui.config.RestServiceConfig;
17  import com.eviware.soapui.impl.support.AbstractInterface;
18  import com.eviware.soapui.impl.support.DefinitionContext;
19  import com.eviware.soapui.impl.wadl.WadlDefinitionContext;
20  import com.eviware.soapui.impl.wsdl.WsdlProject;
21  import com.eviware.soapui.model.iface.Operation;
22  import com.eviware.soapui.support.StringUtils;
23  
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  /***
30   * WSDL implementation of Interface, maps to a WSDL Binding
31   *
32   * @author Ole.Matzura
33   */
34  
35  public class RestService extends AbstractInterface<RestServiceConfig> implements RestResourceContainer
36  {
37     private List<RestResource> resources = new ArrayList<RestResource>();
38     private WadlDefinitionContext wadlContext;
39  
40     public RestService( WsdlProject project, RestServiceConfig serviceConfig )
41     {
42        super( serviceConfig, project, "/rest_service.gif" );
43  
44        List<RestResourceConfig> resourceConfigs = serviceConfig.getResourceList();
45        for( int i = 0; i < resourceConfigs.size(); i++ )
46        {
47           resources.add( new RestResource( this, resourceConfigs.get( i ) ) );
48        }
49     }
50  
51     public String getInterfaceType()
52     {
53        return RestServiceFactory.REST_TYPE;
54     }
55  
56     public RestResource getOperationAt( int index )
57     {
58        return resources.get( index );
59     }
60  
61     public RestResource getOperationByName( String name )
62     {
63        return (RestResource) getWsdlModelItemByName( resources, name );
64     }
65  
66     public int getOperationCount()
67     {
68        return resources.size();
69     }
70  
71     public List<Operation> getOperationList()
72     {
73        return new ArrayList<Operation>( resources );
74     }
75  
76     public String getBasePath()
77     {
78        return getConfig().isSetBasePath() ? getConfig().getBasePath() : "";
79     }
80  
81     public void setBasePath( String basePath )
82     {
83        String old = getBasePath();
84        getConfig().setBasePath( basePath );
85  
86        notifyPropertyChanged( "basePath", old, basePath );
87     }
88  
89     public boolean isGenerated()
90     {
91        return StringUtils.isNullOrEmpty( getConfig().getDefinitionUrl() );
92     }
93  
94     public String getWadlUrl()
95     {
96        return isGenerated() ? generateWadlUrl() : getConfig().getDefinitionUrl();
97     }
98  
99     public String generateWadlUrl()
100    {
101       return getName() + ".wadl";
102    }
103 
104    public void setWadlUrl( String wadlUrl )
105    {
106       String old = getWadlUrl();
107       getConfig().setDefinitionUrl( wadlUrl );
108 
109       notifyPropertyChanged( "wadlUrl", old, wadlUrl );
110    }
111 
112    public String getTechnicalId()
113    {
114       return getConfig().getBasePath();
115    }
116 
117    public RestResource addNewResource( String name, String path )
118    {
119       RestResourceConfig resourceConfig = getConfig().addNewResource();
120       resourceConfig.setName( name );
121       resourceConfig.setPath( path );
122 
123       RestResource resource = new RestResource( this, resourceConfig );
124       resources.add( resource );
125 
126       fireOperationAdded( resource );
127       return resource;
128    }
129 
130    public RestResource cloneResource( RestResource resource, String name )
131    {
132       RestResourceConfig resourceConfig = (RestResourceConfig) getConfig().addNewResource().set( resource.getConfig() );
133       resourceConfig.setName( name );
134 
135       RestResource newResource = new RestResource( this, resourceConfig );
136       resources.add( newResource );
137 
138       fireOperationAdded( newResource );
139       return newResource;
140    }
141 
142    public void deleteResource( RestResource resource )
143    {
144       int ix = resources.indexOf( resource );
145       if( !resources.remove( resource ) )
146          return;
147 
148       fireOperationRemoved( resource );
149 
150       getConfig().removeResource( ix );
151       resource.release();
152    }
153 
154    public List<RestResource> getAllResources()
155    {
156       List<RestResource> result = new ArrayList<RestResource>();
157       for( RestResource resource : resources )
158       {
159          addResourcesToResult( resource, result );
160       }
161 
162       return result;
163    }
164 
165     public Map<String, RestResource> getResources()
166    {
167       Map<String,RestResource> result = new HashMap<String,RestResource>();
168 
169       for( RestResource resource : getAllResources() )
170       {
171          result.put( resource.getFullPath( false ), resource );
172       }
173 
174       return result;
175    }
176 
177    private void addResourcesToResult( RestResource resource, List<RestResource> result )
178    {
179       result.add( resource );
180 
181       for( RestResource res : resource.getChildResourceList() )
182       {
183          addResourcesToResult( res, result );
184       }
185    }
186 
187    public RestResource getResourceByPath( String resourcePath )
188    {
189       for( RestResource resource : getAllResources() )
190       {
191          if( resource.getPath().equals( resourcePath ) )
192             return resource;
193       }
194 
195       return null;
196    }
197 
198    @Override
199    public DefinitionContext getDefinitionContext()
200    {
201       return getWadlContext();
202    }
203 
204    public WadlDefinitionContext getWadlContext()
205    {
206       if( wadlContext == null )
207          wadlContext = new WadlDefinitionContext( getWadlUrl(), this );
208 
209       return wadlContext;
210    }
211 
212    @Override
213    public String getDefinition()
214    {
215       return getWadlUrl();
216    }
217 
218    public String getType()
219    {
220       return RestServiceFactory.REST_TYPE;
221    }
222 
223    public boolean isDefinitionShareble()
224    {
225       return !isGenerated();
226    }
227 
228    public void beforeSave()
229    {
230       if( isGenerated() && wadlContext != null )
231       {
232          try
233          {
234             wadlContext.getDefinitionCache().clear();
235          }
236          catch( Exception e )
237          {
238             e.printStackTrace();  
239          }
240       }
241 
242    }
243 }