minimum base web services for CDM Portal v2.0
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / controller / BaseController.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.remote.controller;
12
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;
19
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
22
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;
27
28 import eu.etaxonomy.cdm.api.service.IService;
29 import eu.etaxonomy.cdm.model.common.CdmBase;
30
31 //$Id$
32 /**
33 * based on org.cateproject.controller.common
34 * @author b.clark
35 * @author a.kohlbecker
36 *
37 * @param <T>
38 * @param <SERVICE>
39 */
40
41 public abstract class BaseController<T extends CdmBase, SERVICE extends IService<T>> {
42
43 public static final Logger logger = Logger.getLogger(BaseController.class);
44
45 protected static final Integer DEFAULT_PAGE_SIZE = 30;
46
47 protected SERVICE service;
48
49 protected Pattern uuidParameterPattern = null;
50
51 protected List<String> initializationStrategy = null;
52
53 public abstract void setService(SERVICE service);
54
55 protected void setUuidParameterPattern(String pattern){
56 uuidParameterPattern = Pattern.compile(pattern);
57 }
58
59 public void setInitializationStrategy(List<String> initializationStrategy) {
60 this.initializationStrategy = initializationStrategy;
61 }
62
63 /**@InitBinder
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());
67 }
68 */
69
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);
73 return obj;
74 }
75
76
77 protected UUID readValueUuid(HttpServletRequest request, String pattern) {
78 String path = request.getServletPath();
79 if(path != null) {
80 Matcher uuidMatcher;
81 if(pattern != null){
82 Pattern suppliedPattern = Pattern.compile(pattern);
83 uuidMatcher = suppliedPattern.matcher(path);
84 } else {
85 uuidMatcher = uuidParameterPattern.matcher(path);
86 }
87 if(uuidMatcher.matches() && uuidMatcher.groupCount() > 0){
88 try {
89 UUID uuid = UUID.fromString(uuidMatcher.group(1));
90 return uuid;
91 } catch (Exception e) {
92 throw new IllegalArgumentException(HttpStatusMessage.UUID_INVALID.toString());
93 }
94 } else {
95 throw new IllegalArgumentException(HttpStatusMessage.UUID_MISSING.toString());
96 }
97 }
98 return null;
99 }
100
101
102 /**
103 * @param request
104 * @param response
105 * @param obj
106 * @return
107 * @throws IOException
108 */
109 protected <CDM_BASE> CDM_BASE getCdmBase(HttpServletRequest request, HttpServletResponse response,
110 List<String> initStrategy, Class<CDM_BASE> clazz) throws IOException {
111 T obj = null;
112 try {
113 UUID uuid = readValueUuid(request, null);
114 Assert.notNull(uuid, HttpStatusMessage.UUID_MISSING.toString());
115
116 if(initStrategy != null){
117 obj = service.load(uuid, initStrategy);
118 } else {
119 obj = service.findByUuid(uuid);
120 }
121 Assert.notNull(obj, HttpStatusMessage.UUID_NOT_FOUND.toString());
122
123 } catch (IllegalArgumentException iae) {
124 HttpStatusMessage.fromString(iae.getMessage()).send(response);
125 }
126 CDM_BASE t;
127 try {
128 t = (CDM_BASE)obj;
129 return t;
130 } catch (Exception e) {
131 HttpStatusMessage.UUID_REFERENCES_WRONG_TYPE.send(response);
132 return null;
133 }
134 }
135
136 /* TODO implement
137 *
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
144 } else {
145 // requires merging detached object ?gilead?
146 service.update(object);
147 }
148 }
149
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
156 } else {
157 service.save(object);
158 }
159 }
160
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
167 }
168 }
169 */
170
171
172 }