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