1 |
a7f32579
|
m.doering
|
package eu.etaxonomy.cdm.remote.service;
|
2 |
|
|
|
3 |
6960ca08
|
m.doering
|
import java.io.IOException;
|
4 |
a7f32579
|
m.doering
|
import java.util.*;
|
5 |
|
|
|
6 |
|
|
import javax.servlet.http.HttpServletRequest;
|
7 |
|
|
import javax.servlet.http.HttpServletResponse;
|
8 |
|
|
|
9 |
09bb4286
|
m.doering
|
import org.apache.commons.logging.Log;
|
10 |
|
|
import org.apache.commons.logging.LogFactory;
|
11 |
b5d18cd8
|
m.doering
|
import org.springframework.beans.factory.annotation.Autowired;
|
12 |
|
|
import org.springframework.stereotype.Component;
|
13 |
a7f32579
|
m.doering
|
import org.springframework.web.servlet.ModelAndView;
|
14 |
|
|
import org.springframework.web.servlet.mvc.AbstractController;
|
15 |
|
|
|
16 |
3d84e778
|
m.doering
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
17 |
63105da4
|
m.doering
|
import eu.etaxonomy.cdm.remote.dto.NameTO;
|
18 |
6e1e4b0f
|
m.doering
|
import eu.etaxonomy.cdm.remote.dto.ResultSetPageSTO;
|
19 |
3d84e778
|
m.doering
|
import eu.etaxonomy.cdm.remote.dto.TreeNode;
|
20 |
09bb4286
|
m.doering
|
import eu.etaxonomy.cdm.remote.view.XmlView;
|
21 |
6e1e4b0f
|
m.doering
|
import eu.etaxonomy.cdm.remote.service.Utils;
|
22 |
63105da4
|
m.doering
|
|
23 |
a7f32579
|
m.doering
|
|
24 |
|
|
/**
|
25 |
|
|
* Controller to generate the Home Page basics to be rendered by a view.
|
26 |
|
|
* It extends the convenience class AbstractController that encapsulates most
|
27 |
|
|
* of the drudgery involved in handling HTTP requests.
|
28 |
|
|
*/
|
29 |
99c3b13b
|
m.doering
|
public class RestController extends AbstractController
|
30 |
a7f32579
|
m.doering
|
{
|
31 |
ed6b517f
|
m.doering
|
Log log = LogFactory.getLog(RestController.class);
|
32 |
09bb4286
|
m.doering
|
|
33 |
b5d18cd8
|
m.doering
|
@Autowired
|
34 |
02c32709
|
m.doering
|
private ICdmService service;
|
35 |
6e1e4b0f
|
m.doering
|
|
36 |
|
|
/*
|
37 |
|
|
* return page not found http error (400?) for unknown or incorrect UUIDs
|
38 |
|
|
* (non-Javadoc)
|
39 |
|
|
* @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
40 |
|
|
*/
|
41 |
|
|
protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp) throws Exception
|
42 |
a7f32579
|
m.doering
|
{
|
43 |
bb379ef9
|
m.doering
|
try{
|
44 |
|
|
ModelAndView mv = new ModelAndView();
|
45 |
|
|
String action = getNonNullPara("action",req);
|
46 |
3d84e778
|
m.doering
|
String op = getNonNullPara("operation",req);
|
47 |
|
|
String dto = getNonNullPara("dto",req);
|
48 |
|
|
String uuid = getNonNullPara("uuid",req);
|
49 |
|
|
String sec = getStringPara("sec",req);
|
50 |
ed6b517f
|
m.doering
|
|
51 |
|
|
log.info(String.format("Request received: act=%s op=%s dto=%s uuid=%s sec=%s", action, op, dto, uuid, sec));
|
52 |
|
|
|
53 |
3d84e778
|
m.doering
|
if(action==null){
|
54 |
|
|
// get Object by UUID
|
55 |
|
|
if(dto.equalsIgnoreCase("name")){
|
56 |
|
|
NameTO n = service.getName( getUuid(uuid));
|
57 |
|
|
mv.addObject(n);
|
58 |
|
|
}else if(dto.equalsIgnoreCase("taxon")){
|
59 |
|
|
NameTO n = service.getName( getUuid(uuid));
|
60 |
|
|
mv.addObject(n);
|
61 |
|
|
}else if(dto.equalsIgnoreCase("whatis")){
|
62 |
|
|
//TODO: somehow the whatis url path is not delegated to this controller ?!#!??
|
63 |
|
|
NameTO n = service.getName( getUuid(uuid));
|
64 |
|
|
mv.addObject(n);
|
65 |
|
|
}
|
66 |
|
|
}else if(action.equalsIgnoreCase("find")){
|
67 |
bb379ef9
|
m.doering
|
//
|
68 |
|
|
// retrieve meaningful parameters
|
69 |
|
|
String q = getStringPara("q",req);
|
70 |
|
|
if (q==null){
|
71 |
|
|
q="";
|
72 |
|
|
};
|
73 |
|
|
Set<UUID> higherTaxa = new HashSet<UUID>();
|
74 |
|
|
// TODO: take higher taxa UUIDs from "higherTaxa"
|
75 |
|
|
Boolean matchAnywhere = getBoolPara("matchAnywhere",req);
|
76 |
|
|
if (matchAnywhere==null){
|
77 |
|
|
matchAnywhere=false;
|
78 |
|
|
};
|
79 |
|
|
Boolean onlyAccepted = getBoolPara("onlyAccepted",req);
|
80 |
|
|
if (onlyAccepted==null){
|
81 |
|
|
onlyAccepted=false;
|
82 |
|
|
};
|
83 |
|
|
Integer page = getIntPara("page",req);
|
84 |
|
|
if (page==null){
|
85 |
|
|
page=1;
|
86 |
|
|
};
|
87 |
|
|
Integer pagesize = getIntPara("pagesize",req);
|
88 |
|
|
if (pagesize==null){
|
89 |
|
|
pagesize=25;
|
90 |
|
|
};
|
91 |
|
|
//
|
92 |
|
|
// search for taxa
|
93 |
3d84e778
|
m.doering
|
Object obj = service.findTaxa(q, getUuid(sec), higherTaxa, matchAnywhere, onlyAccepted, page, pagesize);
|
94 |
bb379ef9
|
m.doering
|
mv.addObject(obj);
|
95 |
3d84e778
|
m.doering
|
}else if(action.equalsIgnoreCase("taxonomy")){
|
96 |
|
|
Object obj = null;
|
97 |
|
|
if(op!=null && op.equalsIgnoreCase("children")){
|
98 |
|
|
obj = service.getChildrenTaxa(getUuid(uuid));
|
99 |
|
|
}
|
100 |
|
|
else if(op!=null && op.equalsIgnoreCase("parent")){
|
101 |
|
|
obj = service.getParentTaxa(getUuid(uuid));
|
102 |
64b85721
|
m.doering
|
}
|
103 |
3d84e778
|
m.doering
|
mv.addObject(obj);
|
104 |
|
|
}else if(action.equalsIgnoreCase("insert")){
|
105 |
|
|
// insert test data.
|
106 |
|
|
//
|
107 |
|
|
// TODO: THIS OPERATION IS FOR TESTING ONLY AND SHOULD BE REMOVED !!!
|
108 |
|
|
//
|
109 |
|
|
Taxon t = TestDataGenerator.getTestTaxon();
|
110 |
ed6b517f
|
m.doering
|
service.saveTaxon(t);
|
111 |
3d84e778
|
m.doering
|
mv.addObject("status", "Test data inserted");
|
112 |
6e1e4b0f
|
m.doering
|
}
|
113 |
bb379ef9
|
m.doering
|
// set xml or json view
|
114 |
|
|
mv.setViewName(getLogicalView(req));
|
115 |
|
|
return mv;
|
116 |
|
|
}catch(CdmObjectNonExisting e){
|
117 |
|
|
sendNonExistingUuidError(resp, e);
|
118 |
|
|
return null;
|
119 |
6e1e4b0f
|
m.doering
|
}
|
120 |
a7f32579
|
m.doering
|
}
|
121 |
09bb4286
|
m.doering
|
|
122 |
ed6b517f
|
m.doering
|
/**
|
123 |
|
|
* return a proper UUID for a string resembling a UUID
|
124 |
|
|
* If the uuid string is not a valid UUID, a CdmObjectNonExisting exception is thrown
|
125 |
|
|
* @param uuid
|
126 |
|
|
* @return
|
127 |
|
|
* @throws CdmObjectNonExisting
|
128 |
|
|
*/
|
129 |
3d84e778
|
m.doering
|
private UUID getUuid(String uuid) throws CdmObjectNonExisting{
|
130 |
8c72c691
|
m.doering
|
UUID u=null;
|
131 |
|
|
try{
|
132 |
|
|
u = UUID.fromString(uuid);
|
133 |
|
|
}catch(IllegalArgumentException e){
|
134 |
bb379ef9
|
m.doering
|
throw new CdmObjectNonExisting(uuid);
|
135 |
8c72c691
|
m.doering
|
}
|
136 |
|
|
return u;
|
137 |
6960ca08
|
m.doering
|
}
|
138 |
bb379ef9
|
m.doering
|
private void sendNonExistingUuidError(HttpServletResponse resp, CdmObjectNonExisting e) throws IOException{
|
139 |
|
|
resp.sendError(404, e.getMessage() );
|
140 |
6960ca08
|
m.doering
|
}
|
141 |
12a21cf5
|
m.doering
|
/**
|
142 |
|
|
* return the value for the given parameter name as a string.
|
143 |
|
|
* in case the parameters doesnt exist return an empty string "", not null.
|
144 |
|
|
* @param parameterName
|
145 |
|
|
* @param req
|
146 |
|
|
* @return
|
147 |
|
|
*/
|
148 |
|
|
private String getStringPara(String parameterName, HttpServletRequest req){
|
149 |
|
|
// first try URL parameters set by org.springframework.web.servlet.handler.SimpleUrlHandlerMapping controller mapping
|
150 |
|
|
Object map = req.getAttribute("ParameterizedUrlHandlerMapping.path-parameters");
|
151 |
|
|
String result = null;
|
152 |
|
|
if (map!=null){
|
153 |
|
|
// first look into url parameters
|
154 |
|
|
Map<String,String> urlParas = (Map) map;
|
155 |
|
|
result = urlParas.get(parameterName);
|
156 |
|
|
}
|
157 |
|
|
if (result == null){
|
158 |
|
|
// alternatively try querystring parameters
|
159 |
|
|
result = req.getParameter(parameterName);
|
160 |
|
|
}
|
161 |
|
|
return result;
|
162 |
|
|
}
|
163 |
64b85721
|
m.doering
|
private String getNonNullPara(String parameterName, HttpServletRequest req){
|
164 |
|
|
String val = getStringPara(parameterName, req);
|
165 |
|
|
if (val==null){
|
166 |
|
|
return "";
|
167 |
|
|
}
|
168 |
|
|
return val;
|
169 |
|
|
}
|
170 |
12a21cf5
|
m.doering
|
private Integer getIntPara(String parameterName, HttpServletRequest req){
|
171 |
|
|
// first try URL parameters set by org.springframework.web.servlet.handler.SimpleUrlHandlerMapping controller mapping
|
172 |
|
|
Integer result;
|
173 |
|
|
String tmp = getStringPara(parameterName, req);
|
174 |
|
|
try{
|
175 |
|
|
result = Integer.valueOf(tmp);
|
176 |
|
|
}catch (Exception e){
|
177 |
|
|
result = null;
|
178 |
|
|
}
|
179 |
|
|
return result;
|
180 |
|
|
}
|
181 |
|
|
private Boolean getBoolPara(String parameterName, HttpServletRequest req){
|
182 |
|
|
// first try URL parameters set by org.springframework.web.servlet.handler.SimpleUrlHandlerMapping controller mapping
|
183 |
|
|
String tmp = getStringPara(parameterName, req);
|
184 |
|
|
return Utils.isTrue(tmp);
|
185 |
|
|
}
|
186 |
6e1e4b0f
|
m.doering
|
/**
|
187 |
|
|
* Read http request parameter "Accept" and decide whether to use JSON or XML for the response.
|
188 |
|
|
* Defaults to XML in case no matching header can be identified.
|
189 |
|
|
* @param request
|
190 |
|
|
* @return
|
191 |
|
|
*/
|
192 |
09bb4286
|
m.doering
|
private String getLogicalView(HttpServletRequest request){
|
193 |
6548c2c7
|
m.doering
|
String ctype = request.getHeader("Accept");
|
194 |
6e1e4b0f
|
m.doering
|
String[] ctypes = ctype.split("[,;]");
|
195 |
|
|
for (String ct : ctypes){
|
196 |
|
|
if (ct.endsWith("json")){
|
197 |
|
|
return "jsonView";
|
198 |
|
|
}else if (ct.endsWith("xml")){
|
199 |
|
|
return "xmlView";
|
200 |
|
|
}
|
201 |
|
|
}
|
202 |
|
|
// default to XML
|
203 |
09bb4286
|
m.doering
|
return "xmlView";
|
204 |
|
|
}
|
205 |
6e1e4b0f
|
m.doering
|
|
206 |
|
|
|
207 |
a7f32579
|
m.doering
|
}
|