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