3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
11 package eu
.etaxonomy
.cdm
.remote
.controller
;
13 import java
.beans
.PropertyDescriptor
;
14 import java
.io
.IOException
;
15 import java
.lang
.reflect
.InvocationTargetException
;
16 import java
.lang
.reflect
.Method
;
17 import java
.util
.ArrayList
;
18 import java
.util
.Arrays
;
19 import java
.util
.Collection
;
20 import java
.util
.List
;
21 import java
.util
.UUID
;
23 import javax
.servlet
.http
.HttpServletRequest
;
24 import javax
.servlet
.http
.HttpServletResponse
;
26 import org
.apache
.commons
.beanutils
.PropertyUtils
;
27 import org
.apache
.commons
.io
.FilenameUtils
;
28 import org
.apache
.commons
.lang
.StringUtils
;
29 import org
.hibernate
.mapping
.Map
;
30 import org
.springframework
.web
.bind
.WebDataBinder
;
31 import org
.springframework
.web
.bind
.annotation
.InitBinder
;
32 import org
.springframework
.web
.bind
.annotation
.PathVariable
;
33 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
34 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
35 import org
.springframework
.web
.bind
.annotation
.RequestParam
;
36 import org
.springframework
.web
.servlet
.ModelAndView
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
39 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
40 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
41 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
42 import eu
.etaxonomy
.cdm
.remote
.editor
.UUIDPropertyEditor
;
45 * based on org.cateproject.controller.common
47 * @author a.kohlbecker
53 public abstract class BaseController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> extends AbstractController
{
55 protected SERVICE service
;
57 protected Class
<T
> baseClass
;
59 public abstract void setService(SERVICE service
);
62 public void initBinder(WebDataBinder binder
) {
63 binder
.registerCustomEditor(UUID
.class, new UUIDPropertyEditor());
66 //TODO implement bulk version of this method
67 @RequestMapping(method
= RequestMethod
.GET
)
68 public T
doGet(@PathVariable("uuid") UUID uuid
,
69 HttpServletRequest request
,
70 HttpServletResponse response
) throws IOException
{
71 logger
.info("doGet() " + request
.getServletPath());
72 T obj
= (T
) getCdmBaseInstance(uuid
, response
, initializationStrategy
);
83 * TODO implement bulk version of this method
85 @RequestMapping(value
= "*", method
= RequestMethod
.GET
)
86 public ModelAndView
doGetMethod(
87 @PathVariable("uuid") UUID uuid
,
88 // doPage request parametes
89 @RequestParam(value
= "pageNumber", required
= false) Integer pageNumber
,
90 @RequestParam(value
= "pageSize", required
= false) Integer pageSize
,
91 // doList request parametes
92 @RequestParam(value
= "start", required
= false) Integer start
,
93 @RequestParam(value
= "limit", required
= false) Integer limit
,
94 HttpServletRequest request
,
95 HttpServletResponse response
) throws IOException
{
97 ModelAndView modelAndView
= new ModelAndView();
99 String servletPath
= request
.getServletPath();
100 String baseName
= FilenameUtils
.getBaseName(servletPath
);
102 logger
.info("doGetMethod()[doGet" + StringUtils
.capitalize(baseName
) + "] " + request
.getServletPath());
105 // T instance = getCdmBaseInstance(uuid, response, (List<String>)null);
107 //Class<?> propertyClass = propertyClass(instance, baseName);
109 Object objectFromProperty
= doGetProperty(uuid
, baseName
, response
);// invokeProperty(instance, baseName, response);
113 if(objectFromProperty
!= null){
115 if( Collection
.class.isAssignableFrom(objectFromProperty
.getClass())){
116 // Map types cannot be returend as list or in a pager!
118 Collection c
= (Collection
)objectFromProperty
;
121 limit
= (limit
== null ? DEFAULT_PAGE_SIZE
: limit
);
122 Collection sub_c
= subCollection(c
, start
, limit
);
123 modelAndView
.addObject(sub_c
);
126 pageSize
= (pageSize
== null ? DEFAULT_PAGE_SIZE
: pageSize
);
127 pageNumber
= (pageNumber
== null ?
0 : pageNumber
);
128 start
= pageNumber
* pageSize
;
129 List sub_c
= subCollection(c
, start
, pageSize
);
130 Pager p
= new DefaultPagerImpl(pageNumber
, c
.size(), pageSize
, sub_c
);
131 modelAndView
.addObject(p
);
135 modelAndView
.addObject(objectFromProperty
);
140 if(modelAndView
.isEmpty()){
148 public Object
doGetProperty(UUID uuid
, String property
, HttpServletResponse response
) throws IOException
{
149 T instance
= getCdmBaseInstance(uuid
, response
, (List
<String
>) null);
151 Object objectFromProperty
= invokeProperty(instance
, property
, response
);
153 return objectFromProperty
;
156 private Class
<?
> propertyClass(T instance
, String baseName
) {
157 PropertyDescriptor propertyDescriptor
= null;
160 propertyDescriptor
= PropertyUtils
.getPropertyDescriptor(instance
, baseName
);
161 if(propertyDescriptor
!= null){
162 c
= propertyDescriptor
.getClass();
164 } catch (IllegalAccessException e
) {
165 // TODO Auto-generated catch block
167 } catch (InvocationTargetException e
) {
168 // TODO Auto-generated catch block
170 } catch (NoSuchMethodException e
) {
171 // TODO Auto-generated catch block
182 * @param pathProperties
184 * @throws IOException
186 @SuppressWarnings("unchecked")
187 protected final <SUB_T
extends T
> SUB_T
getCdmBaseInstance(Class
<SUB_T
> clazz
, UUID uuid
, HttpServletResponse response
, List
<String
> pathProperties
)
190 CdmBase cdmBaseObject
= getCdmBaseInstance(uuid
, response
, pathProperties
);
191 if(!clazz
.isAssignableFrom(cdmBaseObject
.getClass())){
192 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
194 return (SUB_T
) cdmBaseObject
;
202 * @param pathProperty
204 * @throws IOException
206 @SuppressWarnings("unchecked")
207 protected final <SUB_T
extends T
> SUB_T
getCdmBaseInstance(Class
<SUB_T
> clazz
, UUID uuid
, HttpServletResponse response
, String pathProperty
)
210 CdmBase cdmBaseObject
= getCdmBaseInstance(uuid
, response
, pathProperty
);
211 if(!clazz
.isAssignableFrom(cdmBaseObject
.getClass())){
212 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
214 return (SUB_T
) cdmBaseObject
;
220 * @param pathProperty
222 * @throws IOException
224 protected final T
getCdmBaseInstance(UUID uuid
, HttpServletResponse response
, String pathProperty
)
226 return getCdmBaseInstance(baseClass
, uuid
, response
, Arrays
227 .asList(new String
[] { pathProperty
}));
234 * @param pathProperties
236 * @throws IOException
238 protected final T
getCdmBaseInstance(UUID uuid
, HttpServletResponse response
, List
<String
> pathProperties
)
240 return getCdmBaseInstance(baseClass
, service
, uuid
, response
, pathProperties
);
249 * @param pathProperties
251 * @throws IOException
253 protected final <CDM_BASE
extends CdmBase
> CDM_BASE
getCdmBaseInstance(Class
<CDM_BASE
> clazz
, IService
<CDM_BASE
> service
, UUID uuid
, HttpServletResponse response
, List
<String
> pathProperties
)
256 CDM_BASE cdmBaseObject
= service
.load(uuid
, pathProperties
);
257 if (cdmBaseObject
== null) {
258 HttpStatusMessage
.UUID_NOT_FOUND
.send(response
);
260 return cdmBaseObject
;
268 * @throws IOException
270 private final Object
invokeProperty(T instance
,
271 String baseName
, HttpServletResponse response
) throws IOException
{
273 Object result
= null;
275 PropertyDescriptor propertyDescriptor
= PropertyUtils
.getPropertyDescriptor(instance
, baseName
);
276 Method method
= propertyDescriptor
.getReadMethod();
278 Class
<?
> returnType
= method
.getReturnType();
280 if(CdmBase
.class.isAssignableFrom(returnType
)
281 || Collection
.class.isAssignableFrom(returnType
)
282 || Map
.class.isAssignableFrom(returnType
)){
283 result
= method
.invoke(instance
, (Object
[])null);
285 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
287 } catch (SecurityException e
) {
288 logger
.error("SecurityException: ", e
);
289 HttpStatusMessage
.INTERNAL_ERROR
.send(response
);
290 } catch (NoSuchMethodException e
) {
291 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
292 } catch (IllegalArgumentException e
) {
293 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
294 } catch (IllegalAccessException e
) {
295 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
296 } catch (InvocationTargetException e
) {
297 HttpStatusMessage
.PROPERTY_NOT_FOUND
.send(response
);
302 private <E
> List
<E
> subCollection(Collection
<?
extends E
> c
, Integer start
, Integer length
){
303 List
<E
> sub_c
= new ArrayList
<E
>(length
);
304 if(c
.size() > length
){
305 E
[] a
= (E
[]) c
.toArray();
306 for(int i
= start
; i
< start
+ length
; i
++){
319 private Validator validator;
321 private javax.validation.Validator javaxValidator;
323 @RequestMapping(method = RequestMethod.PUT, headers="content-type=multipart/form-data")
324 public T doPutForm(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
325 object.setUuid(uuid);
326 validator.validate(object, result);
327 if (result.hasErrors()) {
329 // set http status code depending upon what happened, possibly return
330 // the put object and errors so that they can be rendered into a suitable error response
332 // requires merging detached object ?gilead?
333 service.save(object);
339 @RequestMapping(method = RequestMethod.PUT, headers="content-type=text/json")
340 public T doPutJSON(@PathVariable(value = "uuid") UUID uuid, @RequestBody String jsonMessage) {
341 JSONObject jsonObject = JSONObject.fromObject(jsonMessage);
342 T object = (T)JSONObject.toBean(jsonObject, this.getClass());
345 Set<ConstraintViolation<T>> constraintViolations = javaxValidator.validate(object);
346 if (!constraintViolations.isEmpty()) {
348 // set http status code depending upon what happened, possibly return
349 // the put object and errors so that they can be rendered into a suitable error response
351 // requires merging detached object ?gilead?
352 service.save(object);
358 @RequestMapping(method = RequestMethod.PUT) // the cdm-server may not allow clients to specify the uuid for resources
359 public T doPut(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
360 validator.validate(object, result);
361 if (result.hasErrors()) {
362 // set http status code depending upon what happened, possibly return
363 // the put object and errors so that they can be rendered into a suitable error response
365 service.save(object);
369 @RequestMapping(method = RequestMethod.DELETE)
370 public void doDelete(@PathVariable(value = "uuid") UUID uuid) {
371 T object = service.find(uuid);
372 // provided the object exists
373 service.delete(uuid);
374 // might return 204 or 200