1
|
package eu.dnetlib.enabling.inspector;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import javax.annotation.Resource;
|
8
|
import javax.xml.transform.dom.DOMResult;
|
9
|
import javax.xml.xpath.XPathExpressionException;
|
10
|
import javax.xml.xpath.XPathFactory;
|
11
|
|
12
|
import eu.dnetlib.enabling.is.sn.NotificationInvocationLogger;
|
13
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscription;
|
14
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscriptionRegistry;
|
15
|
import eu.dnetlib.miscutils.coupling.StaticCondition;
|
16
|
import org.apache.commons.logging.Log;
|
17
|
import org.apache.commons.logging.LogFactory;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.stereotype.Controller;
|
20
|
import org.springframework.ui.Model;
|
21
|
import org.springframework.web.bind.annotation.RequestMapping;
|
22
|
import org.springframework.web.bind.annotation.RequestParam;
|
23
|
|
24
|
/**
|
25
|
* Low-level basic interface for managing SN subscriptions.
|
26
|
*
|
27
|
* @author marko
|
28
|
*
|
29
|
*/
|
30
|
@Controller
|
31
|
public class SubscriptionController extends AbstractInspectorController {
|
32
|
/**
|
33
|
* logger.
|
34
|
*/
|
35
|
private static final Log log = LogFactory.getLog(SubscriptionController.class); // NOPMD by marko on 11/24/08 5:02 PM
|
36
|
|
37
|
/**
|
38
|
* is sn subscription registries.
|
39
|
*/
|
40
|
@Resource(name = "issResourceStateNotificationRegistries")
|
41
|
private transient List<ResourceStateSubscriptionRegistry> registries;
|
42
|
|
43
|
/**
|
44
|
* allows to control the global enable of issn.
|
45
|
*/
|
46
|
@Resource(name = "issnInhibitionCondition")
|
47
|
private transient StaticCondition inhibitionCondition;
|
48
|
|
49
|
/**
|
50
|
* issn invocation logger.
|
51
|
*/
|
52
|
@Autowired
|
53
|
private transient NotificationInvocationLogger invocationLogger;
|
54
|
|
55
|
/**
|
56
|
* show a list of subscriptions.
|
57
|
*
|
58
|
* @param model
|
59
|
* view parameters
|
60
|
* @return view
|
61
|
* @throws XPathExpressionException
|
62
|
* could happen
|
63
|
*/
|
64
|
@RequestMapping("/inspector/sn.do")
|
65
|
String listSubscriptions(final Model model) throws XPathExpressionException {
|
66
|
|
67
|
log.debug("registries: " + registries);
|
68
|
|
69
|
final List<Map<String, String>> subscriptions = new ArrayList<Map<String, String>>();
|
70
|
for (ResourceStateSubscriptionRegistry registry : registries) {
|
71
|
for (ResourceStateSubscription sub : registry.getSubscriptionDao().listSubscriptions()) {
|
72
|
final Map<String, String> attrs = new HashMap<String, String>(); // NOPMD
|
73
|
attrs.put("prefix", sub.getPrefix());
|
74
|
attrs.put("type", sub.getType());
|
75
|
attrs.put("resourceId", sub.getResourceId());
|
76
|
attrs.put("xpath", sub.getXpath());
|
77
|
attrs.put("id", sub.getSubscriptionId());
|
78
|
|
79
|
final DOMResult result = new DOMResult(); // NOPMD
|
80
|
sub.getSubscriberAsEpr().writeTo(result);
|
81
|
attrs.put("address", XPathFactory.newInstance().newXPath().evaluate("//*[local-name() = 'Address']", result.getNode()));
|
82
|
subscriptions.add(attrs);
|
83
|
}
|
84
|
}
|
85
|
|
86
|
model.addAttribute("subscriptions", subscriptions);
|
87
|
model.addAttribute("enabled", !inhibitionCondition.isTrue());
|
88
|
return "inspector/subscriptions";
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
* show a list of subscriptions.
|
93
|
*
|
94
|
* @param model
|
95
|
* view parameters
|
96
|
@param address
|
97
|
* the address prefix used as filter
|
98
|
* @return view
|
99
|
* @throws XPathExpressionException
|
100
|
* could happen
|
101
|
*/
|
102
|
@RequestMapping("/inspector/snByAddress.do")
|
103
|
String listSubscriptionsByAddress(final Model model, @RequestParam(value="address", required=false) final String address) throws XPathExpressionException {
|
104
|
|
105
|
log.debug("registries: " + registries);
|
106
|
|
107
|
final List<Map<String, String>> subscriptions = new ArrayList<Map<String, String>>();
|
108
|
log.debug("Address is "+address);
|
109
|
|
110
|
model.addAttribute("address", address);
|
111
|
|
112
|
for (ResourceStateSubscriptionRegistry registry : registries) {
|
113
|
for (ResourceStateSubscription sub : registry.getSubscriptionDao().listSubscriptions()) {
|
114
|
final DOMResult result = new DOMResult(); // NOPMD
|
115
|
sub.getSubscriberAsEpr().writeTo(result);
|
116
|
final String addr = XPathFactory.newInstance().newXPath().evaluate("//*[local-name() = 'Address']", result.getNode());
|
117
|
|
118
|
if ( address==null || (addr != null && addr.startsWith(address)) ) {
|
119
|
final Map<String, String> attrs = new HashMap<String, String>(); // NOPMD
|
120
|
attrs.put("prefix", sub.getPrefix());
|
121
|
attrs.put("type", sub.getType());
|
122
|
attrs.put("resourceId", sub.getResourceId());
|
123
|
attrs.put("xpath", sub.getXpath());
|
124
|
attrs.put("id", sub.getSubscriptionId());
|
125
|
attrs.put("address", addr);
|
126
|
subscriptions.add(attrs);
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
|
131
|
model.addAttribute("subscriptions", subscriptions);
|
132
|
model.addAttribute("enabled", !inhibitionCondition.isTrue());
|
133
|
return "inspector/subscriptionsByAddress";
|
134
|
}
|
135
|
/**
|
136
|
* toggle global notification inhibit flag.
|
137
|
*
|
138
|
* @return view name (redirect)
|
139
|
*/
|
140
|
@RequestMapping("/inspector/toggleNotifications.do")
|
141
|
String deleteSubscription() {
|
142
|
inhibitionCondition.setCondition(! inhibitionCondition.isTrue());
|
143
|
return "redirect:sn.do";
|
144
|
}
|
145
|
|
146
|
/**
|
147
|
* delete a issn subscription.
|
148
|
*
|
149
|
* @param subscriptionId
|
150
|
* subscription identifier web parameter
|
151
|
* @return view name (redirect)
|
152
|
*/
|
153
|
@RequestMapping("/inspector/deleteSubscription.do")
|
154
|
String deleteSubscription(@RequestParam("id") final String subscriptionId) {
|
155
|
for (ResourceStateSubscriptionRegistry registry : registries) {
|
156
|
if (registry.getSubscriptionDao().removeSubscription(subscriptionId))
|
157
|
break;
|
158
|
}
|
159
|
return "redirect:sn.do";
|
160
|
}
|
161
|
|
162
|
/**
|
163
|
* delete issn subscriptions by address prefix.
|
164
|
* @param model
|
165
|
* view parameters
|
166
|
* @param address
|
167
|
* address prefix
|
168
|
* @return view name (redirect)
|
169
|
* @throws XPathExpressionException
|
170
|
*/
|
171
|
@RequestMapping("/inspector/deleteSubscriptionsByAddress.do")
|
172
|
String deleteSubscriptionsByAddress(final Model model, @RequestParam("address") final String address) throws XPathExpressionException {
|
173
|
|
174
|
final List<String> deleted = new ArrayList<String>();
|
175
|
|
176
|
if (address != null && address.length() > "http://".length()) {
|
177
|
for (ResourceStateSubscriptionRegistry registry : registries) {
|
178
|
for (ResourceStateSubscription sub : registry.getSubscriptionDao().listSubscriptions()) {
|
179
|
final DOMResult result = new DOMResult(); // NOPMD
|
180
|
sub.getSubscriberAsEpr().writeTo(result);
|
181
|
final String addr = XPathFactory.newInstance().newXPath().evaluate("//*[local-name() = 'Address']", result.getNode());
|
182
|
|
183
|
if (addr != null && addr.startsWith(address)) {
|
184
|
String id = sub.getSubscriptionId();
|
185
|
registry.getSubscriptionDao().removeSubscription(id);
|
186
|
deleted.add(id + " ("+ addr +")");
|
187
|
}
|
188
|
}
|
189
|
}
|
190
|
}
|
191
|
|
192
|
model.addAttribute("deleted", deleted);
|
193
|
|
194
|
return "inspector/deleteSubscriptionsByAddress";
|
195
|
}
|
196
|
/**
|
197
|
* Show log of notifications.
|
198
|
*
|
199
|
* @param model mvc model
|
200
|
*/
|
201
|
@RequestMapping("/inspector/notificationLog.do")
|
202
|
void notificationLog(final Model model) {
|
203
|
model.addAttribute("log", invocationLogger.getEntries());
|
204
|
}
|
205
|
}
|