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 public RestServiceDefinition() {
53 setBusSecurity(false);
54 }
55
56 @Override
57 public String getType() {
58 return KsbApiConstants.ServiceTypes.REST;
59 }
60
61
62
63
64
65
66
67 @Override
68 public void setBusSecurity(Boolean busSecurity) {
69 if (busSecurity == true) {
70 throw new RiceRuntimeException("Rice does not support bus security (digital request/response signing) " +
71 "for RESTful services");
72 }
73 super.setBusSecurity(busSecurity);
74 }
75
76
77
78
79
80
81 public void setResourceClass(String resourceClass) {
82 this.resourceClass = resourceClass;
83 }
84
85
86
87
88
89 public String getResourceClass() {
90 return this.resourceClass;
91 }
92
93
94
95
96
97
98 @Override
99 public void validate() {
100
101 List<Object> resources = getResources();
102
103 if (resources != null && !resources.isEmpty()) {
104 resourceToClassNameMap = new DualHashBidiMap();
105 for (Object resource : resources) {
106
107 if (getService() == null) {
108 setService(resource);
109 }
110
111 Class<?> resourceClass = resource.getClass();
112 if (resourceClass != null) {
113 Class<?>[] interfaces = null;
114
115 if (resourceClass.isInterface()) {
116 interfaces = new Class[1];
117 interfaces[0] = resourceClass;
118 } else {
119 interfaces = resourceClass.getInterfaces();
120 }
121
122 if (interfaces != null) {
123 for (Class<?> iface : interfaces) {
124 Path pathAnnotation = (Path)iface.getAnnotation(Path.class);
125 if (pathAnnotation != null) {
126 String pathAnnotationValue = pathAnnotation.value();
127 String resourceId = pathAnnotationValue == null || pathAnnotationValue.equals("/") ? iface.getSimpleName() : pathAnnotationValue;
128 resourceToClassNameMap.put(resourceId, iface.getName());
129 } else {
130
131 resourceToClassNameMap.put(iface.getSimpleName(), iface.getName());
132 }
133 }
134 }
135 }
136 }
137
138 }
139
140 super.validate();
141
142
143 if (getResourceClass() == null) {
144 Class<?>[] interfaces = getService().getClass().getInterfaces();
145 if (interfaces != null && interfaces.length > 0) {
146 setResourceClass(interfaces[0].getName());
147 } else {
148 throw new ConfigurationException("resource class must be set to export a REST service");
149 }
150 }
151
152
153 try {
154 Class.forName(getResourceClass());
155 } catch (ClassNotFoundException e) {
156 throw new ConfigurationException(
157 "resource class '" + getResourceClass() + "' could not be found in the classpath");
158 }
159
160 }
161
162 @Override
163 protected ServiceConfiguration configure() {
164 return RestServiceConfiguration.fromServiceDefinition(this);
165 }
166
167
168
169
170 public List<Object> getResources() {
171 return this.resources;
172 }
173
174
175
176
177 public void setResources(List<Object> resources) {
178 this.resources = resources;
179 }
180
181
182
183
184 @SuppressWarnings("unchecked")
185 public Map<String, String> getResourceToClassNameMap() {
186 return this.resourceToClassNameMap;
187 }
188
189
190
191
192
193 public boolean hasClass(String className) {
194 if (resourceToClassNameMap == null) return false;
195 return resourceToClassNameMap.containsValue(className);
196 }
197
198
199
200
201 public List<Object> getProviders() {
202 return this.providers;
203 }
204
205
206
207
208 public void setProviders(List<Object> providers) {
209 this.providers = providers;
210 }
211
212
213
214
215 public Map<Object, Object> getExtensionMappings() {
216 return this.extensionMappings;
217 }
218
219
220
221
222 public void setExtensionMappings(Map<Object, Object> extensionMappings) {
223 this.extensionMappings = extensionMappings;
224 }
225
226
227
228
229 public Map<Object, Object> getLanguageMappings() {
230 return this.languageMappings;
231 }
232
233
234
235
236 public void setLanguageMappings(Map<Object, Object> languageMappings) {
237 this.languageMappings = languageMappings;
238 }
239
240
241
242 }