1
|
package eu.dnetlib.domain.enabling;
|
2
|
|
3
|
import java.io.ByteArrayInputStream;
|
4
|
import java.util.Date;
|
5
|
|
6
|
import javax.xml.parsers.DocumentBuilder;
|
7
|
import javax.xml.parsers.DocumentBuilderFactory;
|
8
|
import javax.xml.parsers.ParserConfigurationException;
|
9
|
import javax.xml.xpath.XPath;
|
10
|
import javax.xml.xpath.XPathConstants;
|
11
|
import javax.xml.xpath.XPathExpression;
|
12
|
import javax.xml.xpath.XPathFactory;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.w3c.dom.Document;
|
16
|
import org.w3c.dom.Node;
|
17
|
|
18
|
import eu.dnetlib.domain.ActionType;
|
19
|
import eu.dnetlib.domain.ResourceType;
|
20
|
|
21
|
public class Notification {
|
22
|
private static Logger logger = Logger.getLogger(Notification.class);
|
23
|
|
24
|
private String subscriptionId = null;
|
25
|
private String message = null;
|
26
|
private String topic = null;
|
27
|
private String isId = null;
|
28
|
private Date date = new Date();
|
29
|
|
30
|
private ActionType actionType = null;
|
31
|
private ResourceType resourceType = null;
|
32
|
private String resource = null;
|
33
|
private String resourceId = null;
|
34
|
|
35
|
public Notification(String subscriptionId, String message, String topic,
|
36
|
String isId) throws Exception {
|
37
|
super();
|
38
|
|
39
|
this.subscriptionId = subscriptionId;
|
40
|
this.message = message;
|
41
|
this.topic = topic;
|
42
|
this.isId = isId;
|
43
|
|
44
|
this.resource = message;
|
45
|
this.resourceId = this.getField(message, "RESOURCE_IDENTIFIER");
|
46
|
this.resourceType = ResourceType.valueOf(ResourceType.class, this.getField(message, "RESOURCE_TYPE").toUpperCase());
|
47
|
this.actionType = ActionType.valueOf(topic.split("\\.")[0]);
|
48
|
}
|
49
|
|
50
|
public Notification(String subscriptionId, String resource,
|
51
|
String resourceId, ResourceType resourceType, ActionType actionType) {
|
52
|
this.resource = resource;
|
53
|
this.resourceId = resourceId;
|
54
|
this.resourceType = resourceType;
|
55
|
this.actionType = actionType;
|
56
|
|
57
|
this.subscriptionId = subscriptionId;
|
58
|
this.message = resource;
|
59
|
this.topic = actionType.getValue() + "." + resourceType.getValue();
|
60
|
}
|
61
|
|
62
|
public String getSubscriptionId() {
|
63
|
return subscriptionId;
|
64
|
}
|
65
|
|
66
|
public String getMessage() {
|
67
|
return message;
|
68
|
}
|
69
|
|
70
|
public String getTopic() {
|
71
|
return topic;
|
72
|
}
|
73
|
|
74
|
public String getIsId() {
|
75
|
return isId;
|
76
|
}
|
77
|
|
78
|
public Date getDate() {
|
79
|
return date;
|
80
|
}
|
81
|
|
82
|
public ActionType getActionType() {
|
83
|
return actionType;
|
84
|
}
|
85
|
|
86
|
public ResourceType getResourceType() {
|
87
|
return resourceType;
|
88
|
}
|
89
|
|
90
|
public String getResource() {
|
91
|
return resource;
|
92
|
}
|
93
|
|
94
|
public String getResourceId() {
|
95
|
return resourceId;
|
96
|
}
|
97
|
|
98
|
private String getField(String xml, String fieldName) throws Exception {
|
99
|
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
|
100
|
DocumentBuilderFactory domFactory = DocumentBuilderFactory
|
101
|
.newInstance();
|
102
|
|
103
|
domFactory.setNamespaceAware(true); // never forget this!
|
104
|
|
105
|
try {
|
106
|
DocumentBuilder builder = domFactory.newDocumentBuilder();
|
107
|
Document doc = builder.parse(bis);
|
108
|
XPathFactory factory = XPathFactory.newInstance();
|
109
|
XPath xpath = factory.newXPath();
|
110
|
|
111
|
XPathExpression expression = xpath.compile("//*[local-name()='"
|
112
|
+ fieldName + "']");
|
113
|
|
114
|
Node node = (Node) expression.evaluate(doc, XPathConstants.NODE);
|
115
|
String value = node.getAttributes().getNamedItem("value").getTextContent();
|
116
|
|
117
|
logger.debug("Field : " + fieldName + ":" + value);
|
118
|
|
119
|
return value;
|
120
|
} catch (ParserConfigurationException e) {
|
121
|
throw new Exception("XML Parser configuration Error", e);
|
122
|
}
|
123
|
}
|
124
|
}
|