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.*;
|
8
|
import javax.annotation.Resource;
|
9
|
import javax.servlet.ServletOutputStream;
|
10
|
import javax.servlet.ServletResponse;
|
11
|
import javax.xml.transform.dom.DOMResult;
|
12
|
import javax.xml.xpath.XPathFactory;
|
13
|
|
14
|
import com.google.common.base.Splitter;
|
15
|
import com.google.common.collect.Iterables;
|
16
|
import com.google.common.collect.Lists;
|
17
|
import com.google.common.collect.Maps;
|
18
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
|
19
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
|
20
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
21
|
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
|
22
|
import eu.dnetlib.enabling.is.sn.SubscriptionRegistry;
|
23
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscription;
|
24
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
25
|
import eu.dnetlib.functionality.modular.ui.AbstractAjaxController;
|
26
|
import eu.dnetlib.functionality.modular.ui.is.bulk.ProfileImporter;
|
27
|
import eu.dnetlib.functionality.modular.ui.is.objects.*;
|
28
|
import eu.dnetlib.functionality.modular.ui.is.objects.ServiceDesc.ServiceStatus;
|
29
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
30
|
import org.apache.commons.io.IOUtils;
|
31
|
import org.apache.commons.lang.StringUtils;
|
32
|
import org.apache.commons.lang.math.NumberUtils;
|
33
|
import org.apache.commons.logging.Log;
|
34
|
import org.apache.commons.logging.LogFactory;
|
35
|
import org.dom4j.Document;
|
36
|
import org.dom4j.io.SAXReader;
|
37
|
import org.springframework.stereotype.Controller;
|
38
|
import org.springframework.web.bind.annotation.RequestMapping;
|
39
|
import org.springframework.web.bind.annotation.RequestParam;
|
40
|
import org.springframework.web.bind.annotation.ResponseBody;
|
41
|
|
42
|
@Controller
|
43
|
public class InformationServiceInternalController extends AbstractAjaxController {
|
44
|
|
45
|
@Resource
|
46
|
private UniqueServiceLocator serviceLocator;
|
47
|
|
48
|
@Resource(name = "modularUiProfileImporter")
|
49
|
private ProfileImporter profileImporter;
|
50
|
|
51
|
/**
|
52
|
* is sn subscription registries.
|
53
|
*/
|
54
|
@Resource(name = "issResourceStateNotificationRegistry")
|
55
|
private transient SubscriptionRegistry registry;
|
56
|
|
57
|
private static final Log log = LogFactory.getLog(InformationServiceInternalController.class);
|
58
|
|
59
|
@RequestMapping("/ui/is/xquery.do")
|
60
|
public @ResponseBody
|
61
|
List<String> query(@RequestParam(value = "query", required = true) final String query) throws Exception {
|
62
|
log.debug("Executing xquery: " + query);
|
63
|
return serviceLocator.getService(ISLookUpService.class).quickSearchProfile(query);
|
64
|
}
|
65
|
|
66
|
@RequestMapping("/ui/is/listSchemas.do")
|
67
|
public @ResponseBody
|
68
|
List<String> listSchemas() throws Exception {
|
69
|
return serviceLocator.getService(ISLookUpService.class).listResourceTypes();
|
70
|
}
|
71
|
|
72
|
@RequestMapping("/ui/is/getSchema.do")
|
73
|
public @ResponseBody
|
74
|
String getSchema(@RequestParam(value = "name", required = true) final String name) throws Exception {
|
75
|
return serviceLocator.getService(ISLookUpService.class).getResourceTypeSchema(name);
|
76
|
}
|
77
|
|
78
|
@RequestMapping("/ui/is/listCollections.do")
|
79
|
public @ResponseBody
|
80
|
List<CollectionDesc> listCollections() throws Exception {
|
81
|
final String xquery = "for $kind in xmldb:get-child-collections('/db/DRIVER') " +
|
82
|
"for $type in xmldb:get-child-collections(concat('/db/DRIVER/', $kind)) " +
|
83
|
"return concat ($kind, ' @@@ ', $type, ' @@@ ', count(xmldb:get-child-resources(concat('/db/DRIVER/', $kind, '/', $type))))";
|
84
|
|
85
|
final Map<String, CollectionDesc> map = Maps.newHashMap();
|
86
|
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery)) {
|
87
|
final String[] arr = s.split("@@@");
|
88
|
final String kind = arr[0].trim();
|
89
|
final String type = arr[1].trim();
|
90
|
final int size = NumberUtils.toInt(arr[2].trim(), 0);
|
91
|
if (!map.containsKey(kind)) {
|
92
|
map.put(kind, new CollectionDesc(kind));
|
93
|
}
|
94
|
map.get(kind).addType(type, size);
|
95
|
}
|
96
|
|
97
|
final List<CollectionDesc> res = Lists.newArrayList(map.values());
|
98
|
for (CollectionDesc d : res) {
|
99
|
Collections.sort(d.getTypes());
|
100
|
}
|
101
|
Collections.sort(res);
|
102
|
|
103
|
return res;
|
104
|
}
|
105
|
|
106
|
@RequestMapping("/ui/is/listProfiles.do")
|
107
|
public @ResponseBody
|
108
|
List<String> listProfiles(@RequestParam(value = "kind", required = true) final String kind,
|
109
|
@RequestParam(value = "type", required = true) final String type) throws Exception {
|
110
|
final String collName = "/db/DRIVER/" + kind + "/" + type;
|
111
|
final String xquery = "distinct-values(for $x in collection('" + collName + "') return $x//RESOURCE_IDENTIFIER/@value/string())";
|
112
|
final List<String> res = serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xquery);
|
113
|
|
114
|
Collections.sort(res);
|
115
|
|
116
|
return res;
|
117
|
}
|
118
|
|
119
|
@RequestMapping("/ui/is/getProfile.do")
|
120
|
public @ResponseBody
|
121
|
String getProfiles(@RequestParam(value = "id", required = true) final String id) throws Exception {
|
122
|
return serviceLocator.getService(ISLookUpService.class).getResourceProfile(id);
|
123
|
}
|
124
|
|
125
|
@RequestMapping("/ui/is/registerProfile.do")
|
126
|
public @ResponseBody
|
127
|
String registerProfile(@RequestParam(value = "profile", required = true) final String profile) throws Exception {
|
128
|
return serviceLocator.getService(ISRegistryService.class).registerProfile(profile);
|
129
|
}
|
130
|
|
131
|
@RequestMapping("/ui/is/updateProfile.do")
|
132
|
public @ResponseBody
|
133
|
String updateProfile(@RequestParam(value = "profile", required = true) final String profile) throws Exception {
|
134
|
final SAXReader reader = new SAXReader();
|
135
|
final Document doc = reader.read(new StringReader(profile));
|
136
|
|
137
|
final String id = doc.valueOf("//RESOURCE_IDENTIFIER/@value");
|
138
|
final String type = doc.valueOf("//RESOURCE_TYPE/@value");
|
139
|
|
140
|
if (StringUtils.isEmpty(id)) {
|
141
|
throw new Exception("RESOURCE_IDENTIFIER is empty");
|
142
|
} else if (StringUtils.isEmpty(type)) {
|
143
|
throw new Exception("RESOURCE_TYPE is empty");
|
144
|
} else if (serviceLocator.getService(ISRegistryService.class).updateProfile(id, profile, type)) {
|
145
|
return id;
|
146
|
} else {
|
147
|
throw new Exception("Profile not updated");
|
148
|
}
|
149
|
}
|
150
|
|
151
|
@RequestMapping("/ui/is/deleteProfile.do")
|
152
|
public @ResponseBody
|
153
|
boolean deleteProfile(@RequestParam(value = "id", required = true) final String id) throws Exception {
|
154
|
return serviceLocator.getService(ISRegistryService.class).deleteProfile(id);
|
155
|
}
|
156
|
|
157
|
@RequestMapping("/ui/is/import.do")
|
158
|
public @ResponseBody
|
159
|
Map<String, Integer> importProfiles(
|
160
|
@RequestParam(value = "path", required = true) final String path,
|
161
|
@RequestParam(value = "profiles", required = true) final boolean profiles,
|
162
|
@RequestParam(value = "schemas", required = true) final boolean schemas) throws Exception {
|
163
|
|
164
|
log.info("importing profiles/schemas from " + path);
|
165
|
|
166
|
final Map<String, Integer> res = Maps.newHashMap();
|
167
|
if (schemas) {
|
168
|
res.putAll(profileImporter.importSchemas(path + "/**/*.xsd"));
|
169
|
}
|
170
|
if (profiles) {
|
171
|
res.putAll(profileImporter.importProfiles(path + "/**/*.xml"));
|
172
|
}
|
173
|
|
174
|
return res;
|
175
|
}
|
176
|
|
177
|
@RequestMapping("/ui/is/listBlackboards.do")
|
178
|
public @ResponseBody
|
179
|
List<BlackboardMessage> listBlackboards() throws Exception {
|
180
|
final List<BlackboardMessage> list = Lists.newArrayList();
|
181
|
|
182
|
final SAXReader reader = new SAXReader();
|
183
|
|
184
|
for (String xml : serviceLocator
|
185
|
.getService(ISLookUpService.class)
|
186
|
.quickSearchProfile(
|
187
|
"for $x in collection('/db/DRIVER/ServiceResources')//MESSAGE return <message>{$x/../../..//RESOURCE_TYPE}{$x/../../..//RESOURCE_IDENTIFIER}{$x}</message>")) {
|
188
|
final BlackboardMessage info = new BlackboardMessage();
|
189
|
final Document doc = reader.read(new StringReader(xml));
|
190
|
info.setProfId(doc.valueOf(".//RESOURCE_IDENTIFIER/@value"));
|
191
|
info.setMessageId(doc.valueOf(".//@id"));
|
192
|
info.setResourceType(doc.valueOf(".//RESOURCE_TYPE/@value"));
|
193
|
info.setAction(doc.valueOf(".//ACTION"));
|
194
|
info.setDate(doc.valueOf(".//@date"));
|
195
|
info.setActionStatus(doc.valueOf(".//ACTION_STATUS"));
|
196
|
info.setError(doc.valueOf(".//PARAMETER[@name='error']/@value"));
|
197
|
list.add(info);
|
198
|
}
|
199
|
return list;
|
200
|
}
|
201
|
|
202
|
@RequestMapping("/ui/is/getMetaWfIdForFamily.do")
|
203
|
public @ResponseBody
|
204
|
Map<String, String> getMetaWfId(@RequestParam(value = "family", required = true) final String family) throws ISLookUpException {
|
205
|
final String xq = "for $x in collection('/db/DRIVER/WorkflowDSResources/WorkflowDSResourceType') " +
|
206
|
"where $x//WORKFLOW_FAMILY='" + family + "' " +
|
207
|
"return concat($x//RESOURCE_IDENTIFIER/@value, ' @@@ ', $x//WORKFLOW_NAME/@menuSection)";
|
208
|
|
209
|
|
210
|
|
211
|
final Map<String, String> map = Maps.newHashMap();
|
212
|
try {
|
213
|
final String[] arr = serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(xq).split("@@@");
|
214
|
map.put("id", arr[0].trim());
|
215
|
map.put("section", arr[1].trim());
|
216
|
} catch (ISLookUpDocumentNotFoundException e) {
|
217
|
map.put("id", "");
|
218
|
map.put("section", "");
|
219
|
}
|
220
|
map.put("family", family);
|
221
|
|
222
|
return map;
|
223
|
}
|
224
|
|
225
|
@RequestMapping("/ui/is/listServices.do")
|
226
|
public @ResponseBody
|
227
|
Collection<ServiceGrouperDesc> listServices() throws Exception {
|
228
|
final String xq = IOUtils.toString(getClass().getResourceAsStream("/eu/dnetlib/functionality/modular/xquery/listServices.xquery"));
|
229
|
|
230
|
final List<ServiceDesc> list = Lists.newArrayList();
|
231
|
|
232
|
final SAXReader reader = new SAXReader();
|
233
|
for (String s : serviceLocator.getService(ISLookUpService.class).quickSearchProfile(xq)) {
|
234
|
final Document doc = reader.read(new StringReader(s));
|
235
|
final String id = doc.valueOf("/service/id").trim();
|
236
|
final String name = doc.valueOf("/service/name").trim();
|
237
|
final String wsdl = doc.valueOf("/service/wsdl").trim();
|
238
|
list.add(new ServiceDesc(id, name, wsdl));
|
239
|
}
|
240
|
|
241
|
final XPathFactory xpathFactory = XPathFactory.newInstance();
|
242
|
|
243
|
for (ResourceStateSubscription sub : registry.listSubscriptions()) {
|
244
|
boolean notFound = true;
|
245
|
final DOMResult result = new DOMResult(); // NOPMD
|
246
|
sub.getSubscriberAsEpr().writeTo(result);
|
247
|
final String wsdl = xpathFactory.newXPath().evaluate("//*[local-name() = 'Address']", result.getNode()) + "?wsdl";
|
248
|
|
249
|
for (ServiceDesc s : list) {
|
250
|
if (s.getWsdl().equalsIgnoreCase(wsdl)) {
|
251
|
s.getSubscriptions().add(new SubscriptionDesc(sub));
|
252
|
notFound = false;
|
253
|
}
|
254
|
}
|
255
|
if (notFound) {
|
256
|
final ServiceDesc desc = new ServiceDesc("", "", wsdl);
|
257
|
desc.getSubscriptions().add(new SubscriptionDesc(sub));
|
258
|
desc.setStatus(ServiceStatus.MISSING);
|
259
|
list.add(desc);
|
260
|
}
|
261
|
}
|
262
|
|
263
|
final Map<String, ServiceGrouperDesc> map = Maps.newHashMap();
|
264
|
for (ServiceDesc s : list) {
|
265
|
final URL url = new URL(s.getWsdl());
|
266
|
final String host = url.getHost();
|
267
|
final int port = url.getPort();
|
268
|
final String context = Iterables.getFirst(Splitter.on("/").omitEmptyStrings().split(url.getPath()), "");
|
269
|
final String tmpKey = host + "@@@" + port + "@@@" + context;
|
270
|
if (!map.containsKey(tmpKey)) {
|
271
|
map.put(tmpKey, new ServiceGrouperDesc(host, port, context, new ArrayList<ServiceDesc>()));
|
272
|
}
|
273
|
map.get(tmpKey).getServices().add(s);
|
274
|
}
|
275
|
|
276
|
return map.values();
|
277
|
}
|
278
|
|
279
|
@RequestMapping("/ui/is/ping.do")
|
280
|
public @ResponseBody
|
281
|
long pingUrl(@RequestParam(value = "url", required = true) final String url, @RequestParam(value = "timeout", required = true) final int timeout)
|
282
|
throws IOException {
|
283
|
|
284
|
final long start = DateUtils.now();
|
285
|
|
286
|
final HttpURLConnection urlConn = (HttpURLConnection) new URL(url).openConnection();
|
287
|
urlConn.setConnectTimeout(timeout);
|
288
|
urlConn.setReadTimeout(timeout);
|
289
|
|
290
|
if (urlConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
|
291
|
return DateUtils.now() - start;
|
292
|
} else {
|
293
|
throw new IllegalArgumentException("Invalid Url");
|
294
|
}
|
295
|
}
|
296
|
|
297
|
public void sendXML(final ServletResponse response, final String xml) throws IOException {
|
298
|
response.setContentType("text/xml");
|
299
|
|
300
|
final ServletOutputStream out = response.getOutputStream();
|
301
|
IOUtils.copy(new StringReader(xml), out);
|
302
|
|
303
|
out.flush();
|
304
|
out.close();
|
305
|
}
|
306
|
|
307
|
}
|