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
.io
.IOException
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
16 import java
.util
.UUID
;
17 import java
.util
.regex
.Matcher
;
18 import java
.util
.regex
.Pattern
;
20 import javax
.servlet
.http
.HttpServletRequest
;
21 import javax
.servlet
.http
.HttpServletResponse
;
23 import org
.apache
.log4j
.Logger
;
24 import org
.springframework
.util
.Assert
;
25 import org
.springframework
.web
.bind
.annotation
.RequestMapping
;
26 import org
.springframework
.web
.bind
.annotation
.RequestMethod
;
28 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 * based on org.cateproject.controller.common
35 * @author a.kohlbecker
41 public abstract class BaseController
<T
extends CdmBase
, SERVICE
extends IService
<T
>> {
43 public static final Logger logger
= Logger
.getLogger(BaseController
.class);
45 protected static final Integer DEFAULT_PAGE_SIZE
= 30;
47 protected SERVICE service
;
49 protected Pattern uuidParameterPattern
= null;
51 protected List
<String
> initializationStrategy
= null;
53 public abstract void setService(SERVICE service
);
55 protected void setUuidParameterPattern(String pattern
){
56 uuidParameterPattern
= Pattern
.compile(pattern
);
59 public void setInitializationStrategy(List
<String
> initializationStrategy
) {
60 this.initializationStrategy
= initializationStrategy
;
64 public void initBinder(WebDataBinder binder) {
65 binder.registerCustomEditor(UUID.class, new UUIDPropertyEditor());
66 //TODO do we need this one?: binder.registerCustomEditor(Class.class, new ClassPropertyEditor());
70 @RequestMapping(method
= RequestMethod
.GET
)
71 public T
doGet(HttpServletRequest request
, HttpServletResponse response
) throws IOException
{
72 T obj
= (T
) getCdmBase(request
, response
, initializationStrategy
, CdmBase
.class);
77 protected UUID
readValueUuid(HttpServletRequest request
, String pattern
) {
78 String path
= request
.getServletPath();
82 Pattern suppliedPattern
= Pattern
.compile(pattern
);
83 uuidMatcher
= suppliedPattern
.matcher(path
);
85 uuidMatcher
= uuidParameterPattern
.matcher(path
);
87 if(uuidMatcher
.matches() && uuidMatcher
.groupCount() > 0){
89 UUID uuid
= UUID
.fromString(uuidMatcher
.group(1));
91 } catch (Exception e
) {
92 throw new IllegalArgumentException(HttpStatusMessage
.UUID_INVALID
.toString());
95 throw new IllegalArgumentException(HttpStatusMessage
.UUID_MISSING
.toString());
107 * @throws IOException
109 protected <CDM_BASE
> CDM_BASE
getCdmBase(HttpServletRequest request
, HttpServletResponse response
,
110 List
<String
> initStrategy
, Class
<CDM_BASE
> clazz
) throws IOException
{
113 UUID uuid
= readValueUuid(request
, null);
114 Assert
.notNull(uuid
, HttpStatusMessage
.UUID_MISSING
.toString());
116 if(initStrategy
!= null){
117 obj
= service
.load(uuid
, initStrategy
);
119 obj
= service
.findByUuid(uuid
);
121 Assert
.notNull(obj
, HttpStatusMessage
.UUID_NOT_FOUND
.toString());
123 } catch (IllegalArgumentException iae
) {
124 HttpStatusMessage
.fromString(iae
.getMessage()).send(response
);
130 } catch (Exception e
) {
131 HttpStatusMessage
.UUID_REFERENCES_WRONG_TYPE
.send(response
);
138 @RequestMapping(method = RequestMethod.POST)
139 public T doPost(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
140 validator.validate(object, result);
141 if (result.hasErrors()) {
142 // set http status code depending upon what happened, possibly return
143 // the put object and errors so that they can be rendered into a suitable error response
145 // requires merging detached object ?gilead?
146 service.update(object);
150 @RequestMapping(method = RequestMethod.PUT) // the cdm-server may not allow clients to specify the uuid for resources
151 public T doPut(@PathVariable(value = "uuid") UUID uuid, @ModelAttribute("object") T object, BindingResult result) {
152 validator.validate(object, result);
153 if (result.hasErrors()) {
154 // set http status code depending upon what happened, possibly return
155 // the put object and errors so that they can be rendered into a suitable error response
157 service.save(object);
161 @RequestMapping(method = RequestMethod.DELETE)
162 public void doDelete(@PathVariable(value = "uuid") UUID uuid) {
163 T object = service.find(uuid);
164 // provided the object exists
165 service.delete(uuid);
166 // might return 204 or 200