1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.remote.service;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.List;
|
13
|
import java.util.Map;
|
14
|
|
15
|
import javax.servlet.http.HttpServletRequest;
|
16
|
import javax.servlet.http.HttpServletResponse;
|
17
|
|
18
|
import org.apache.commons.logging.Log;
|
19
|
import org.apache.commons.logging.LogFactory;
|
20
|
import org.springframework.beans.factory.annotation.Autowired;
|
21
|
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
|
22
|
import org.springframework.stereotype.Controller;
|
23
|
import org.springframework.web.servlet.ModelAndView;
|
24
|
import org.springframework.web.servlet.mvc.AbstractController;
|
25
|
|
26
|
import eu.etaxonomy.cdm.database.DataSourceReloader;
|
27
|
|
28
|
|
29
|
@Controller("managementController")
|
30
|
public class ManagementController extends AbstractController
|
31
|
{
|
32
|
Log log = LogFactory.getLog(ManagementController.class);
|
33
|
|
34
|
@Autowired
|
35
|
private DataSourceReloader datasourceLoader;
|
36
|
|
37
|
private static final int DEFAULT_PAGE_SIZE = 25;
|
38
|
|
39
|
/*
|
40
|
* return page not found http error (404) for unknown or incorrect UUIDs
|
41
|
* (non-Javadoc)
|
42
|
* @see org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
43
|
*/
|
44
|
protected ModelAndView handleRequestInternal(HttpServletRequest req, HttpServletResponse resp) throws Exception
|
45
|
{
|
46
|
|
47
|
ModelAndView mv = new ModelAndView();
|
48
|
String resource = getNonNullPara("resource",req);
|
49
|
String op = getNonNullPara("operation",req);
|
50
|
String q = getNonNullPara("q",req);
|
51
|
|
52
|
log.info(String.format("Request received for manager: resource=%s op=%s ", resource, op));
|
53
|
|
54
|
if(resource.equalsIgnoreCase("dataSources")){
|
55
|
// get Object by UUID
|
56
|
if(op.equalsIgnoreCase("reload")){
|
57
|
mv.addObject("title", "CDM Community Server - Manager");
|
58
|
Map<String,SimpleDriverDataSource> dataSources = datasourceLoader.reload();
|
59
|
String bodyHtml = "<div><h4>Available Data Sources</h4><dl>";
|
60
|
bodyHtml += "<p><i>The following data sources have been loaded:</i></p><table><th>BasePath</th><th>DataSource URI</th>";
|
61
|
for(String key : dataSources.keySet()) {
|
62
|
SimpleDriverDataSource ds = dataSources.get(key);
|
63
|
bodyHtml += "<tr><td>"+key+"</td><td>"+ds.getUrl()+"</td></tr>";
|
64
|
}
|
65
|
bodyHtml += "</table><form name=\"input\" action=\"\" method=\"get\"><input type=\"submit\" value=\"Update\"></td></form></div>";
|
66
|
mv.addObject("body", bodyHtml);
|
67
|
}
|
68
|
}
|
69
|
else{
|
70
|
// nothing matches
|
71
|
mv.addObject("title", "CDM Community Server - Manager");
|
72
|
mv.addObject("body", "<h4>Error</h4><p>Controller does not know this operation.</p>");
|
73
|
}
|
74
|
// set xml or json view
|
75
|
mv.setViewName("htmlView");
|
76
|
|
77
|
|
78
|
return mv;
|
79
|
|
80
|
}
|
81
|
|
82
|
|
83
|
/**
|
84
|
* return the value for the given parameter name as a string.
|
85
|
* in case the parameters doesnt exist return an empty string "", not null.
|
86
|
* @param parameterName
|
87
|
* @param req
|
88
|
* @return
|
89
|
*/
|
90
|
private String getStringPara(String parameterName, HttpServletRequest req){
|
91
|
// first try URL parameters set by org.springframework.web.servlet.handler.SimpleUrlHandlerMapping controller mapping
|
92
|
Object map = req.getAttribute("ParameterizedUrlHandlerMapping.path-parameters");
|
93
|
String result = null;
|
94
|
if (map!=null){
|
95
|
// first look into url parameters
|
96
|
Map<String,String> urlParas = (Map<String, String>) map;
|
97
|
result = urlParas.get(parameterName);
|
98
|
}
|
99
|
if (result == null){
|
100
|
// alternatively try querystring parameters
|
101
|
result = req.getParameter(parameterName);
|
102
|
}
|
103
|
return result;
|
104
|
}
|
105
|
private List<String> getListPara(String parameterName, HttpServletRequest req){
|
106
|
ArrayList<String> list = new ArrayList<String>();
|
107
|
String[] map = req.getParameterValues(parameterName);
|
108
|
if(map != null){
|
109
|
for(String param : map){
|
110
|
list.add(param);
|
111
|
}
|
112
|
}
|
113
|
return list;
|
114
|
}
|
115
|
private String getNonNullPara(String parameterName, HttpServletRequest req){
|
116
|
String val = getStringPara(parameterName, req);
|
117
|
if (val==null){
|
118
|
return "";
|
119
|
}
|
120
|
return val;
|
121
|
}
|
122
|
|
123
|
private Integer getIntPara(String parameterName, HttpServletRequest req){
|
124
|
return getIntPara(parameterName, null, req);
|
125
|
}
|
126
|
|
127
|
private Integer getIntPara(String parameterName, Integer defaultValue, HttpServletRequest req){
|
128
|
// first try URL parameters set by org.springframework.web.servlet.handler.SimpleUrlHandlerMapping controller mapping
|
129
|
Integer result;
|
130
|
String tmp = getStringPara(parameterName, req);
|
131
|
try{
|
132
|
result = Integer.valueOf(tmp);
|
133
|
}catch (Exception e){
|
134
|
result = defaultValue;
|
135
|
}
|
136
|
return result;
|
137
|
}
|
138
|
|
139
|
private Boolean getBoolPara(String parameterName, HttpServletRequest req){
|
140
|
// first try URL parameters set by org.springframework.web.servlet.handler.SimpleUrlHandlerMapping controller mapping
|
141
|
String tmp = getStringPara(parameterName, req);
|
142
|
return Utils.isTrue(tmp);
|
143
|
}
|
144
|
|
145
|
|
146
|
|
147
|
}
|
148
|
|