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