| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| RESTServiceDefinition |
|
| 3.0588235294117645;3.059 |
| 1 | /* | |
| 2 | * Copyright 2006-2011 The Kuali Foundation | |
| 3 | * | |
| 4 | * Licensed under the Educational Community License, Version 2.0 (the "License"); | |
| 5 | * you may not use this file except in compliance with the License. | |
| 6 | * You may obtain a copy of the License at | |
| 7 | * | |
| 8 | * http://www.opensource.org/licenses/ecl2.php | |
| 9 | * | |
| 10 | * Unless required by applicable law or agreed to in writing, software | |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | * See the License for the specific language governing permissions and | |
| 14 | * limitations under the License. | |
| 15 | */ | |
| 16 | ||
| 17 | package org.kuali.rice.ksb.messaging; | |
| 18 | ||
| 19 | import org.apache.commons.collections.BidiMap; | |
| 20 | import org.apache.commons.collections.bidimap.DualHashBidiMap; | |
| 21 | import org.kuali.rice.core.api.config.ConfigurationException; | |
| 22 | import org.kuali.rice.core.api.exception.RiceRuntimeException; | |
| 23 | ||
| 24 | import javax.ws.rs.Path; | |
| 25 | import java.util.List; | |
| 26 | import java.util.Map; | |
| 27 | ||
| 28 | /** | |
| 29 | * Service definition for RESTful services. A JAX-WS service has a resource class, which is the class or | |
| 30 | * interface marked by the JAX-WS annotations (e.g. @Path, @GET, etc). This may or may not be the implementation | |
| 31 | * class. | |
| 32 | * @author Kuali Rice Team (rice.collab@kuali.org) | |
| 33 | */ | |
| 34 | public class RESTServiceDefinition extends ServiceDefinition { | |
| 35 | ||
| 36 | private static final long serialVersionUID = 5892163789061959602L; | |
| 37 | ||
| 38 | private String resourceClass; | |
| 39 | transient private List<Object> resources; | |
| 40 | private BidiMap resourceToClassNameMap; | |
| 41 | transient private List<Object> providers; | |
| 42 | transient private Map<Object, Object> extensionMappings; | |
| 43 | transient private Map<Object, Object> languageMappings; | |
| 44 | ||
| 45 | /** | |
| 46 | * Default constructor. Sets bus security to FALSE. | |
| 47 | */ | |
| 48 | public RESTServiceDefinition() { | |
| 49 | 0 | super(Boolean.FALSE); |
| 50 | 0 | } |
| 51 | ||
| 52 | /** | |
| 53 | * To ensure transparency that RESTful services are not digitally signed, throw an exception | |
| 54 | * if someone tries to enable bus security. | |
| 55 | * | |
| 56 | * @see org.kuali.rice.ksb.messaging.ServiceDefinition#setBusSecurity(java.lang.Boolean) | |
| 57 | */ | |
| 58 | @Override | |
| 59 | public void setBusSecurity(Boolean busSecurity) { | |
| 60 | 0 | if (busSecurity == true) { |
| 61 | 0 | throw new RiceRuntimeException("Rice does not support bus security (digital request/response signing) " + |
| 62 | "for RESTful services"); | |
| 63 | } | |
| 64 | 0 | super.setBusSecurity(busSecurity); |
| 65 | 0 | } |
| 66 | ||
| 67 | /** | |
| 68 | * Set the resourceClass, the class or interface marked by the JAX-WS annotations | |
| 69 | * which specify the RESTful URL interface. | |
| 70 | * @param resourceClass the resourceClass to set | |
| 71 | */ | |
| 72 | public void setResourceClass(String resourceClass) { | |
| 73 | 0 | this.resourceClass = resourceClass; |
| 74 | 0 | } |
| 75 | ||
| 76 | /** | |
| 77 | * @see #setResourceClass(String) | |
| 78 | * @return the resourceClass | |
| 79 | */ | |
| 80 | public String getResourceClass() { | |
| 81 | 0 | return this.resourceClass; |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * sets the service implementation | |
| 86 | * | |
| 87 | * @see org.kuali.rice.ksb.messaging.ServiceDefinition#setService(java.lang.Object) | |
| 88 | */ | |
| 89 | @Override | |
| 90 | public void setService(Object service) { | |
| 91 | 0 | super.setService(service); |
| 92 | 0 | } |
| 93 | ||
| 94 | /** | |
| 95 | * does some simple validation of this RESTServiceDefinition | |
| 96 | * | |
| 97 | * @see org.kuali.rice.ksb.messaging.ServiceDefinition#validate() | |
| 98 | */ | |
| 99 | @Override | |
| 100 | public void validate() { | |
| 101 | ||
| 102 | 0 | List<Object> resources = getResources(); |
| 103 | ||
| 104 | 0 | if (resources != null && !resources.isEmpty()) { |
| 105 | 0 | resourceToClassNameMap = new DualHashBidiMap(); |
| 106 | 0 | for (Object resource : resources) { |
| 107 | // If there is no service set then we have to assume that it's the first resource | |
| 108 | 0 | if (getService() == null) { |
| 109 | 0 | setService(resource); |
| 110 | } | |
| 111 | ||
| 112 | 0 | Class resourceClass = resource.getClass(); |
| 113 | 0 | if (resourceClass != null) { |
| 114 | 0 | Class[] interfaces = null; |
| 115 | ||
| 116 | 0 | if (resourceClass.isInterface()) { |
| 117 | 0 | interfaces = new Class[1]; |
| 118 | 0 | interfaces[0] = resourceClass; |
| 119 | } else { | |
| 120 | 0 | interfaces = resourceClass.getInterfaces(); |
| 121 | } | |
| 122 | ||
| 123 | 0 | if (interfaces != null) { |
| 124 | 0 | for (Class iface : interfaces) { |
| 125 | 0 | Path pathAnnotation = (Path)iface.getAnnotation(Path.class); |
| 126 | 0 | if (pathAnnotation != null) { |
| 127 | 0 | String pathAnnotationValue = pathAnnotation.value(); |
| 128 | 0 | String resourceId = pathAnnotationValue == null || pathAnnotationValue.equals("/") ? iface.getSimpleName() : pathAnnotationValue; |
| 129 | 0 | resourceToClassNameMap.put(resourceId, iface.getName()); |
| 130 | 0 | } else { |
| 131 | // If no path annotation exists, use the simple class name | |
| 132 | 0 | resourceToClassNameMap.put(iface.getSimpleName(), iface.getName()); |
| 133 | } | |
| 134 | } | |
| 135 | } | |
| 136 | } | |
| 137 | 0 | } |
| 138 | ||
| 139 | } | |
| 140 | ||
| 141 | 0 | super.validate(); |
| 142 | ||
| 143 | // if interface is null, set it to the service class | |
| 144 | 0 | if (getResourceClass() == null) { |
| 145 | 0 | Class[] interfaces = getService().getClass().getInterfaces(); |
| 146 | 0 | if (interfaces != null && interfaces.length > 0) { |
| 147 | 0 | setResourceClass(interfaces[0].getName()); |
| 148 | } else { | |
| 149 | 0 | throw new ConfigurationException("resource class must be set to export a REST service"); |
| 150 | } | |
| 151 | } | |
| 152 | ||
| 153 | // Validate that the JAX-WS annotated class / interface is available to the classloader. | |
| 154 | try { | |
| 155 | 0 | Class.forName(getResourceClass()); |
| 156 | 0 | } catch (ClassNotFoundException e) { |
| 157 | 0 | throw new ConfigurationException( |
| 158 | "resource class '" + getResourceClass() + "' could not be found in the classpath"); | |
| 159 | 0 | } |
| 160 | ||
| 161 | 0 | if (getBusSecurity() == null) { |
| 162 | 0 | setBusSecurity(false); |
| 163 | } | |
| 164 | 0 | } |
| 165 | ||
| 166 | /** | |
| 167 | * @return true if the given {@link RESTServiceDefinition} has the same resource class as this one. | |
| 168 | * @see org.kuali.rice.ksb.messaging.ServiceDefinition#isSame(org.kuali.rice.ksb.messaging.ServiceDefinition) | |
| 169 | */ | |
| 170 | @Override | |
| 171 | public boolean isSame(final ServiceDefinition serviceDefinition) { | |
| 172 | 0 | boolean same = super.isSame(serviceDefinition) |
| 173 | && serviceDefinition instanceof RESTServiceDefinition; | |
| 174 | 0 | if (!same) { |
| 175 | 0 | return same; |
| 176 | } | |
| 177 | ||
| 178 | 0 | RESTServiceDefinition otherServiceDefinition = (RESTServiceDefinition) serviceDefinition; |
| 179 | ||
| 180 | // To be the same, they have to have the same resource class name | |
| 181 | 0 | if (!otherServiceDefinition.getResourceClass().equals(this.getResourceClass())) |
| 182 | 0 | return false; |
| 183 | ||
| 184 | // If neither has multiple resources, then they are the same | |
| 185 | 0 | if (otherServiceDefinition.getResourceToClassNameMap() == null && getResourceToClassNameMap() == null) |
| 186 | 0 | return true; |
| 187 | ||
| 188 | // If one of them has multiple resources and the other doesn't, then they're not the same | |
| 189 | 0 | if ((otherServiceDefinition.getResourceToClassNameMap() == null && |
| 190 | getResourceToClassNameMap() != null) | |
| 191 | || | |
| 192 | (otherServiceDefinition.getResourceToClassNameMap() != null && | |
| 193 | getResourceToClassNameMap() == null)) | |
| 194 | 0 | return false; |
| 195 | ||
| 196 | 0 | return otherServiceDefinition.getResourceToClassNameMap().equals(getResourceToClassNameMap()); |
| 197 | } | |
| 198 | ||
| 199 | /** | |
| 200 | * @return the resources | |
| 201 | */ | |
| 202 | public List<Object> getResources() { | |
| 203 | 0 | return this.resources; |
| 204 | } | |
| 205 | ||
| 206 | /** | |
| 207 | * @param resources the resources to set | |
| 208 | */ | |
| 209 | public void setResources(List<Object> resources) { | |
| 210 | 0 | this.resources = resources; |
| 211 | 0 | } |
| 212 | ||
| 213 | /** | |
| 214 | * @return the resourceToClassNameMap | |
| 215 | */ | |
| 216 | @SuppressWarnings("unchecked") | |
| 217 | public Map<String, String> getResourceToClassNameMap() { | |
| 218 | 0 | return this.resourceToClassNameMap; |
| 219 | } | |
| 220 | ||
| 221 | /** | |
| 222 | * @param className | |
| 223 | * @return true if this service contains a resource for the given class name | |
| 224 | */ | |
| 225 | public boolean hasClass(String className) { | |
| 226 | 0 | if (resourceToClassNameMap == null) return false; |
| 227 | 0 | return resourceToClassNameMap.containsValue(className); |
| 228 | } | |
| 229 | ||
| 230 | /** | |
| 231 | * @return the providers | |
| 232 | */ | |
| 233 | public List<Object> getProviders() { | |
| 234 | 0 | return this.providers; |
| 235 | } | |
| 236 | ||
| 237 | /** | |
| 238 | * @param providers the providers to set | |
| 239 | */ | |
| 240 | public void setProviders(List<Object> providers) { | |
| 241 | 0 | this.providers = providers; |
| 242 | 0 | } |
| 243 | ||
| 244 | /** | |
| 245 | * @return the extensionMappings | |
| 246 | */ | |
| 247 | public Map<Object, Object> getExtensionMappings() { | |
| 248 | 0 | return this.extensionMappings; |
| 249 | } | |
| 250 | ||
| 251 | /** | |
| 252 | * @param extensionMappings the extensionMappings to set | |
| 253 | */ | |
| 254 | public void setExtensionMappings(Map<Object, Object> extensionMappings) { | |
| 255 | 0 | this.extensionMappings = extensionMappings; |
| 256 | 0 | } |
| 257 | ||
| 258 | /** | |
| 259 | * @return the languageMappings | |
| 260 | */ | |
| 261 | public Map<Object, Object> getLanguageMappings() { | |
| 262 | 0 | return this.languageMappings; |
| 263 | } | |
| 264 | ||
| 265 | /** | |
| 266 | * @param languageMappings the languageMappings to set | |
| 267 | */ | |
| 268 | public void setLanguageMappings(Map<Object, Object> languageMappings) { | |
| 269 | 0 | this.languageMappings = languageMappings; |
| 270 | 0 | } |
| 271 | ||
| 272 | ||
| 273 | ||
| 274 | } |