1
|
package eu.dnetlib.functionality.modular.ui.is;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.net.HttpURLConnection;
|
6
|
import java.net.URL;
|
7
|
import java.util.ArrayList;
|
8
|
import java.util.Collection;
|
9
|
import java.util.Date;
|
10
|
import java.util.List;
|
11
|
import java.util.Map;
|
12
|
|
13
|
import javax.annotation.Resource;
|
14
|
import javax.servlet.ServletOutputStream;
|
15
|
import javax.servlet.ServletResponse;
|
16
|
|
17
|
import org.apache.commons.io.IOUtils;
|
18
|
import org.apache.commons.logging.Log;
|
19
|
import org.apache.commons.logging.LogFactory;
|
20
|
import org.springframework.stereotype.Controller;
|
21
|
import org.springframework.web.bind.annotation.RequestMapping;
|
22
|
import org.springframework.web.bind.annotation.RequestParam;
|
23
|
import org.springframework.web.bind.annotation.ResponseBody;
|
24
|
|
25
|
import com.google.common.base.Splitter;
|
26
|
import com.google.common.collect.Iterables;
|
27
|
import com.google.common.collect.Maps;
|
28
|
|
29
|
import eu.dnetlib.enabling.is.rmi.InformationService;
|
30
|
import eu.dnetlib.enabling.is.rmi.objects.BlackboardMessage;
|
31
|
import eu.dnetlib.enabling.is.rmi.objects.DnetDataStructure;
|
32
|
import eu.dnetlib.enabling.is.rmi.objects.DnetResourceType;
|
33
|
import eu.dnetlib.enabling.is.rmi.objects.DnetService;
|
34
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
35
|
import eu.dnetlib.functionality.modular.ui.AbstractAjaxController;
|
36
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
37
|
|
38
|
@Controller
|
39
|
@RequestMapping("/ui/ajax_is")
|
40
|
public class IsAjaxController extends AbstractAjaxController {
|
41
|
|
42
|
@Resource
|
43
|
private UniqueServiceLocator serviceLocator;
|
44
|
|
45
|
private static final Log log = LogFactory.getLog(IsAjaxController.class);
|
46
|
|
47
|
@RequestMapping("query.do")
|
48
|
public @ResponseBody
|
49
|
List<String> query(@RequestParam(value = "query", required = true) final String query) throws Exception {
|
50
|
log.debug("Executing sql query: " + query);
|
51
|
return serviceLocator.getService(InformationService.class).searchSql(query);
|
52
|
}
|
53
|
|
54
|
@RequestMapping("listResourceTypes.do")
|
55
|
public @ResponseBody
|
56
|
List<DnetResourceType> listResourceTypes() throws Exception {
|
57
|
return serviceLocator.getService(InformationService.class).listResourceTypes();
|
58
|
}
|
59
|
|
60
|
@RequestMapping("listDatastructures.do")
|
61
|
public @ResponseBody
|
62
|
List<DnetDataStructure> listDatastructures(@RequestParam(value = "type", required = true) final String type) throws Exception {
|
63
|
return serviceLocator.getService(InformationService.class).listDatastructures(type);
|
64
|
}
|
65
|
|
66
|
@RequestMapping("getDs.do")
|
67
|
public @ResponseBody
|
68
|
DnetDataStructure getDs(@RequestParam(value = "id", required = true) final String id) throws Exception {
|
69
|
return serviceLocator.getService(InformationService.class).getDsById(id);
|
70
|
}
|
71
|
|
72
|
@RequestMapping("registerDs.do")
|
73
|
public @ResponseBody
|
74
|
String registerDs(@RequestParam(value = "code", required = true) final String code,
|
75
|
@RequestParam(value = "name", required = true) final String name,
|
76
|
@RequestParam(value = "description", required = false) final String description,
|
77
|
@RequestParam(value = "type", required = true) final String type,
|
78
|
@RequestParam(value = "serviceId", required = false) final String serviceId,
|
79
|
@RequestParam(value = "content", required = true) final String content) throws Exception {
|
80
|
final DnetDataStructure ds = new DnetDataStructure();
|
81
|
ds.setCode(code);
|
82
|
ds.setName(name);
|
83
|
ds.setDescription(description);
|
84
|
ds.setType(type);
|
85
|
ds.setServiceId(serviceId);
|
86
|
ds.setContent(content);
|
87
|
return serviceLocator.getService(InformationService.class).registerDs(ds);
|
88
|
}
|
89
|
|
90
|
@RequestMapping("updateDs.do")
|
91
|
public @ResponseBody
|
92
|
String updateDs(
|
93
|
@RequestParam(value = "id", required = true) final String id,
|
94
|
@RequestParam(value = "name", required = false) final String name,
|
95
|
@RequestParam(value = "description", required = false) final String description,
|
96
|
@RequestParam(value = "content", required = false) final String content) throws Exception {
|
97
|
|
98
|
final InformationService is = serviceLocator.getService(InformationService.class);
|
99
|
|
100
|
final DnetDataStructure ds = is.getDsById(id);
|
101
|
if (name != null) {
|
102
|
ds.setName(name);
|
103
|
}
|
104
|
if (description != null) {
|
105
|
ds.setDescription(description);
|
106
|
}
|
107
|
if (content != null) {
|
108
|
ds.setContent(content);
|
109
|
}
|
110
|
ds.setDate(new Date());
|
111
|
|
112
|
return is.registerDs(ds);
|
113
|
|
114
|
}
|
115
|
|
116
|
@RequestMapping("deleteDs.do")
|
117
|
public @ResponseBody
|
118
|
boolean deleteDs(@RequestParam(value = "id", required = true) final String id) throws Exception {
|
119
|
return serviceLocator.getService(InformationService.class).deleteDsById(id);
|
120
|
}
|
121
|
|
122
|
@RequestMapping("import.do")
|
123
|
public @ResponseBody
|
124
|
Map<String, Integer> importProfiles(
|
125
|
@RequestParam(value = "path", required = true) final String path,
|
126
|
@RequestParam(value = "types", required = true) final boolean types,
|
127
|
@RequestParam(value = "resources", required = true) final boolean resources) throws Exception {
|
128
|
|
129
|
log.info("importing types/resources from " + path);
|
130
|
final Map<String, Integer> res = Maps.newHashMap();
|
131
|
if (types) {
|
132
|
// res.putAll(profileImporter.importSchemas(path + "/**/*.xsd"));
|
133
|
}
|
134
|
if (resources) {
|
135
|
// res.putAll(profileImporter.importProfiles(path + "/**/*.xml"));
|
136
|
}
|
137
|
return res;
|
138
|
}
|
139
|
|
140
|
@RequestMapping("listBlackboards.do")
|
141
|
public @ResponseBody
|
142
|
List<BlackboardMessage> listBlackboards() throws Exception {
|
143
|
return serviceLocator.getService(InformationService.class).listBlackboardMessages();
|
144
|
}
|
145
|
|
146
|
@RequestMapping("listServices.do")
|
147
|
public @ResponseBody
|
148
|
Collection<ServiceGrouper> listServices() throws Exception {
|
149
|
final Map<String, ServiceGrouper> map = Maps.newHashMap();
|
150
|
for (DnetService s : serviceLocator.getService(InformationService.class).listServices()) {
|
151
|
for (String address : s.getProtocols().values()) {
|
152
|
final URL url = new URL(address);
|
153
|
final String host = url.getHost();
|
154
|
final int port = url.getPort();
|
155
|
final String context = Iterables.getFirst(Splitter.on("/").omitEmptyStrings().split(url.getPath()), "");
|
156
|
final String tmpKey = host + "@@@" + port + "@@@" + context;
|
157
|
if (!map.containsKey(tmpKey)) {
|
158
|
map.put(tmpKey, new ServiceGrouper(host, port, context, new ArrayList<DnetService>()));
|
159
|
}
|
160
|
map.get(tmpKey).getServices().add(s);
|
161
|
}
|
162
|
}
|
163
|
return map.values();
|
164
|
}
|
165
|
|
166
|
@RequestMapping("getService.do")
|
167
|
public @ResponseBody
|
168
|
DnetService getService(@RequestParam(value = "id", required = true) final String id) throws Exception {
|
169
|
return serviceLocator.getService(InformationService.class).getService(id);
|
170
|
}
|
171
|
|
172
|
@RequestMapping("ping.do")
|
173
|
public @ResponseBody
|
174
|
long pingUrl(@RequestParam(value = "url", required = true) final String url, @RequestParam(value = "timeout", required = true) final int timeout)
|
175
|
throws IOException {
|
176
|
|
177
|
final long start = DateUtils.now();
|
178
|
|
179
|
final HttpURLConnection urlConn = (HttpURLConnection) new URL(url).openConnection();
|
180
|
urlConn.setConnectTimeout(timeout);
|
181
|
urlConn.setReadTimeout(timeout);
|
182
|
|
183
|
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
184
|
return DateUtils.now() - start;
|
185
|
} else {
|
186
|
throw new IllegalArgumentException("Invalid Url");
|
187
|
}
|
188
|
}
|
189
|
|
190
|
public void sendXML(final ServletResponse response, final String xml) throws IOException {
|
191
|
response.setContentType("text/xml");
|
192
|
|
193
|
final ServletOutputStream out = response.getOutputStream();
|
194
|
IOUtils.copy(new StringReader(xml), out);
|
195
|
|
196
|
out.flush();
|
197
|
out.close();
|
198
|
}
|
199
|
|
200
|
}
|