1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.ksb.api.bus.support; |
17 | |
|
18 | |
import java.util.List; |
19 | |
import java.util.Map; |
20 | |
|
21 | |
import javax.ws.rs.Path; |
22 | |
|
23 | |
import org.apache.commons.collections.BidiMap; |
24 | |
import org.apache.commons.collections.bidimap.DualHashBidiMap; |
25 | |
import org.kuali.rice.core.api.config.ConfigurationException; |
26 | |
import org.kuali.rice.core.api.exception.RiceRuntimeException; |
27 | |
import org.kuali.rice.ksb.api.KsbApiConstants; |
28 | |
import org.kuali.rice.ksb.api.bus.ServiceConfiguration; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class RestServiceDefinition extends AbstractServiceDefinition { |
39 | |
|
40 | |
private static final long serialVersionUID = 5892163789061959602L; |
41 | |
|
42 | |
private String resourceClass; |
43 | |
transient private List<Object> resources; |
44 | |
private BidiMap resourceToClassNameMap; |
45 | |
transient private List<Object> providers; |
46 | |
transient private Map<Object, Object> extensionMappings; |
47 | |
transient private Map<Object, Object> languageMappings; |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 3 | public RestServiceDefinition() { |
53 | 3 | setBusSecurity(false); |
54 | 3 | } |
55 | |
|
56 | |
@Override |
57 | |
public String getType() { |
58 | 3 | return KsbApiConstants.ServiceTypes.REST; |
59 | |
} |
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
@Override |
68 | |
public void setBusSecurity(Boolean busSecurity) { |
69 | 3 | if (busSecurity == true) { |
70 | 0 | throw new RiceRuntimeException("Rice does not support bus security (digital request/response signing) " + |
71 | |
"for RESTful services"); |
72 | |
} |
73 | 3 | super.setBusSecurity(busSecurity); |
74 | 3 | } |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public void setResourceClass(String resourceClass) { |
82 | 2 | this.resourceClass = resourceClass; |
83 | 2 | } |
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public String getResourceClass() { |
90 | 5 | return this.resourceClass; |
91 | |
} |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
@Override |
99 | |
public void validate() { |
100 | |
|
101 | 1 | List<Object> resources = getResources(); |
102 | |
|
103 | 1 | if (resources != null && !resources.isEmpty()) { |
104 | 1 | resourceToClassNameMap = new DualHashBidiMap(); |
105 | 1 | for (Object resource : resources) { |
106 | |
|
107 | 2 | if (getService() == null) { |
108 | 1 | setService(resource); |
109 | |
} |
110 | |
|
111 | 2 | Class<?> resourceClass = resource.getClass(); |
112 | 2 | if (resourceClass != null) { |
113 | 2 | Class<?>[] interfaces = null; |
114 | |
|
115 | 2 | if (resourceClass.isInterface()) { |
116 | 0 | interfaces = new Class[1]; |
117 | 0 | interfaces[0] = resourceClass; |
118 | |
} else { |
119 | 2 | interfaces = resourceClass.getInterfaces(); |
120 | |
} |
121 | |
|
122 | 2 | if (interfaces != null) { |
123 | 4 | for (Class<?> iface : interfaces) { |
124 | 2 | Path pathAnnotation = (Path)iface.getAnnotation(Path.class); |
125 | 2 | if (pathAnnotation != null) { |
126 | 1 | String pathAnnotationValue = pathAnnotation.value(); |
127 | 1 | String resourceId = pathAnnotationValue == null || pathAnnotationValue.equals("/") ? iface.getSimpleName() : pathAnnotationValue; |
128 | 1 | resourceToClassNameMap.put(resourceId, iface.getName()); |
129 | 1 | } else { |
130 | |
|
131 | 1 | resourceToClassNameMap.put(iface.getSimpleName(), iface.getName()); |
132 | |
} |
133 | |
} |
134 | |
} |
135 | |
} |
136 | 2 | } |
137 | |
|
138 | |
} |
139 | |
|
140 | 1 | super.validate(); |
141 | |
|
142 | |
|
143 | 1 | if (getResourceClass() == null) { |
144 | 1 | Class<?>[] interfaces = getService().getClass().getInterfaces(); |
145 | 1 | if (interfaces != null && interfaces.length > 0) { |
146 | 1 | setResourceClass(interfaces[0].getName()); |
147 | |
} else { |
148 | 0 | throw new ConfigurationException("resource class must be set to export a REST service"); |
149 | |
} |
150 | |
} |
151 | |
|
152 | |
|
153 | |
try { |
154 | 1 | Class.forName(getResourceClass()); |
155 | 0 | } catch (ClassNotFoundException e) { |
156 | 0 | throw new ConfigurationException( |
157 | |
"resource class '" + getResourceClass() + "' could not be found in the classpath"); |
158 | 1 | } |
159 | |
|
160 | 1 | } |
161 | |
|
162 | |
@Override |
163 | |
protected ServiceConfiguration configure() { |
164 | 0 | return RestServiceConfiguration.fromServiceDefinition(this); |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public List<Object> getResources() { |
171 | 1 | return this.resources; |
172 | |
} |
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public void setResources(List<Object> resources) { |
178 | 1 | this.resources = resources; |
179 | 1 | } |
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
@SuppressWarnings("unchecked") |
185 | |
public Map<String, String> getResourceToClassNameMap() { |
186 | 4 | return this.resourceToClassNameMap; |
187 | |
} |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
public boolean hasClass(String className) { |
194 | 0 | if (resourceToClassNameMap == null) return false; |
195 | 0 | return resourceToClassNameMap.containsValue(className); |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
public List<Object> getProviders() { |
202 | 0 | return this.providers; |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public void setProviders(List<Object> providers) { |
209 | 0 | this.providers = providers; |
210 | 0 | } |
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
public Map<Object, Object> getExtensionMappings() { |
216 | 0 | return this.extensionMappings; |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public void setExtensionMappings(Map<Object, Object> extensionMappings) { |
223 | 0 | this.extensionMappings = extensionMappings; |
224 | 0 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public Map<Object, Object> getLanguageMappings() { |
230 | 0 | return this.languageMappings; |
231 | |
} |
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
public void setLanguageMappings(Map<Object, Object> languageMappings) { |
237 | 0 | this.languageMappings = languageMappings; |
238 | 0 | } |
239 | |
|
240 | |
|
241 | |
|
242 | |
} |