1
|
package eu.dnetlib.utils;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.io.StringWriter;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.Iterator;
|
7
|
|
8
|
import javax.xml.XMLConstants;
|
9
|
import javax.xml.namespace.NamespaceContext;
|
10
|
import javax.xml.namespace.QName;
|
11
|
import javax.xml.parsers.DocumentBuilderFactory;
|
12
|
import javax.xml.parsers.ParserConfigurationException;
|
13
|
import javax.xml.transform.stream.StreamResult;
|
14
|
import javax.xml.transform.stream.StreamSource;
|
15
|
import javax.xml.ws.Endpoint;
|
16
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
17
|
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;
|
18
|
import javax.xml.xpath.XPath;
|
19
|
import javax.xml.xpath.XPathConstants;
|
20
|
import javax.xml.xpath.XPathExpression;
|
21
|
import javax.xml.xpath.XPathExpressionException;
|
22
|
import javax.xml.xpath.XPathFactory;
|
23
|
|
24
|
import org.apache.log4j.Logger;
|
25
|
import org.w3c.dom.Document;
|
26
|
import org.w3c.dom.Element;
|
27
|
import org.w3c.dom.Node;
|
28
|
import org.xml.sax.InputSource;
|
29
|
|
30
|
import eu.dnetlib.domain.EPR;
|
31
|
import eu.dnetlib.soap.EndpointReferenceBuilder;
|
32
|
|
33
|
public class EPRUtils {
|
34
|
private static final String REFERENCE_PARAMETER_NS = "http://www.driver.org";
|
35
|
|
36
|
private static XPathExpression ADDRESS_EXPR = null;
|
37
|
private static XPathExpression REFERENCE_PARAMETER_EXPR = null;
|
38
|
|
39
|
static {
|
40
|
XPathFactory factory = XPathFactory.newInstance();
|
41
|
XPath xpath = factory.newXPath();
|
42
|
xpath.setNamespaceContext(new RSClientNSContext());
|
43
|
|
44
|
try {
|
45
|
REFERENCE_PARAMETER_EXPR = xpath.compile("//wsa:ReferenceParameters");
|
46
|
ADDRESS_EXPR = xpath.compile("//wsa:Address");
|
47
|
|
48
|
} catch (XPathExpressionException e) {
|
49
|
throw new RuntimeException("Failed to build xpath expression.", e);
|
50
|
}
|
51
|
}
|
52
|
|
53
|
private static Logger logger = Logger.getLogger(EPRUtils.class);
|
54
|
|
55
|
public static EPR createEPR(W3CEndpointReference w3cEpr) {
|
56
|
|
57
|
try {
|
58
|
StringWriter writer = new StringWriter();
|
59
|
w3cEpr.writeTo(new StreamResult(writer));
|
60
|
EPR epr = new EPR();
|
61
|
Node node = null;
|
62
|
String address = null;
|
63
|
|
64
|
synchronized (EPRUtils.class) {
|
65
|
node = (Node) REFERENCE_PARAMETER_EXPR.evaluate(new InputSource(new StringReader(writer.toString())), XPathConstants.NODE);
|
66
|
address = (String) ADDRESS_EXPR.evaluate(new InputSource(new StringReader(writer.toString())));
|
67
|
}
|
68
|
|
69
|
if (node != null && node.getChildNodes() != null) {
|
70
|
for (int i = 0; i < node.getChildNodes().getLength(); i++) {
|
71
|
Node child = node.getChildNodes().item(i);
|
72
|
|
73
|
if (child.getNamespaceURI() != null &&
|
74
|
child.getNamespaceURI().equals(REFERENCE_PARAMETER_NS)) {
|
75
|
String name = child.getLocalName();
|
76
|
String value = child.getTextContent();
|
77
|
|
78
|
if (logger.isDebugEnabled())
|
79
|
logger.debug("Found parameter " + name + ": " + value);
|
80
|
|
81
|
epr.setParameter(name, value);
|
82
|
}
|
83
|
}
|
84
|
}
|
85
|
|
86
|
epr.setAddress(address);
|
87
|
epr.setEpr(writer.toString());
|
88
|
|
89
|
return epr;
|
90
|
} catch (XPathExpressionException e) {
|
91
|
logger.error("Error parsing epr", e);
|
92
|
e.printStackTrace();
|
93
|
}
|
94
|
|
95
|
return null;
|
96
|
}
|
97
|
|
98
|
public static W3CEndpointReference createW3CEPR(EPR epr) {
|
99
|
W3CEndpointReference w3cEpr = null;
|
100
|
|
101
|
if (epr.getEpr() != null) {
|
102
|
StringReader reader = new StringReader(epr.getEpr());
|
103
|
|
104
|
w3cEpr = new W3CEndpointReference(new StreamSource(reader));
|
105
|
} else {
|
106
|
StringWriter writer = new StringWriter();
|
107
|
|
108
|
w3cEpr = buildW3CEpr(epr);
|
109
|
w3cEpr.writeTo(new StreamResult(writer));
|
110
|
|
111
|
epr.setEpr(writer.toString());
|
112
|
}
|
113
|
|
114
|
return w3cEpr;
|
115
|
}
|
116
|
|
117
|
public static W3CEndpointReference createW3CEPR(EndpointReferenceBuilder<Endpoint> builder, Endpoint endpoint) {
|
118
|
W3CEndpointReference w3cepr = builder.getEndpointReference(endpoint);
|
119
|
|
120
|
return w3cepr;
|
121
|
}
|
122
|
|
123
|
public static EPR createEPR(EndpointReferenceBuilder<Endpoint> builder,
|
124
|
Endpoint endpoint) {
|
125
|
EPR epr = null;
|
126
|
|
127
|
if (logger.isDebugEnabled()) {
|
128
|
logger.debug("Creating epr for " + endpoint.getImplementor().getClass());
|
129
|
}
|
130
|
|
131
|
epr = createEPR(createW3CEPR(builder, endpoint));
|
132
|
|
133
|
if (logger.isDebugEnabled()) {
|
134
|
logger.debug("Created epr: " + epr);
|
135
|
}
|
136
|
|
137
|
return epr;
|
138
|
}
|
139
|
|
140
|
public static String eprToXml(final EPR epr) {
|
141
|
/************************************************************************************************
|
142
|
* WARNING: Avoid using cached XML string because it can not be parsed again safely. If the *
|
143
|
* W3C EPR generated by parsing the cached XML string is serialized again back to XML, the *
|
144
|
* builder keeps adding namespaces (basically the same namespace URI with just different *
|
145
|
* namespace prefix). As a consequence *
|
146
|
* *
|
147
|
* EPRUtils.eprToXml(epr).equals(EPRUtils.eprToXml(EPRUtils.xmlToEpr(EPRUtils.eprToXml(epr)))) *
|
148
|
* *
|
149
|
* would always be false for any given EPR epr. *
|
150
|
* **********************************************************************************************/
|
151
|
epr.setEpr(null);
|
152
|
|
153
|
final StringWriter xml = new StringWriter();
|
154
|
createW3CEPR(epr).writeTo(new StreamResult(xml));
|
155
|
return xml.toString();
|
156
|
}
|
157
|
|
158
|
public static EPR xmlToEpr(final String xml) {
|
159
|
return createEPR((W3CEndpointReference) W3CEndpointReference.readFrom(new StreamSource(new StringReader(xml))));
|
160
|
}
|
161
|
|
162
|
private static W3CEndpointReference buildW3CEpr(EPR epr) {
|
163
|
try {
|
164
|
W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
|
165
|
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
|
166
|
|
167
|
builder.address(epr.getAddress());
|
168
|
builder.serviceName(new QName(REFERENCE_PARAMETER_NS, "serviceName"));
|
169
|
|
170
|
for (String name:epr.getParameterNames()) {
|
171
|
Element element = doc.createElementNS(REFERENCE_PARAMETER_NS, name);
|
172
|
element.setTextContent(epr.getParameter(name));
|
173
|
|
174
|
builder.referenceParameter(element);
|
175
|
}
|
176
|
|
177
|
return builder.build();
|
178
|
} catch (ParserConfigurationException e) {
|
179
|
e.printStackTrace();
|
180
|
}
|
181
|
|
182
|
return null;
|
183
|
}
|
184
|
}
|
185
|
|
186
|
class RSClientNSContext implements NamespaceContext {
|
187
|
|
188
|
HashMap<String, String> prefixes = null;
|
189
|
|
190
|
public String getNamespaceURI(String prefix) {
|
191
|
if (prefixes == null) {
|
192
|
prefixes = new HashMap<String, String>();
|
193
|
|
194
|
prefixes.put("wsa", "http://www.w3.org/2005/08/addressing");
|
195
|
prefixes.put("driver", "http://www.driver.org");
|
196
|
prefixes.put("wsaw", "http://www.w3.org/2006/05/addressing/wsdl");
|
197
|
prefixes.put("wsdli", "http://www.w3.org/2005/08/wsdl-instance");
|
198
|
}
|
199
|
|
200
|
if (prefix == null) {
|
201
|
throw new NullPointerException("Null prefix");
|
202
|
} else {
|
203
|
prefix = prefixes.get(prefix);
|
204
|
|
205
|
if (prefix == null) {
|
206
|
return XMLConstants.DEFAULT_NS_PREFIX;
|
207
|
} else {
|
208
|
return prefix;
|
209
|
}
|
210
|
}
|
211
|
}
|
212
|
|
213
|
public String getPrefix(String namespaceURI) {
|
214
|
throw new UnsupportedOperationException();
|
215
|
}
|
216
|
|
217
|
public Iterator<String> getPrefixes(String namespaceURI) {
|
218
|
throw new UnsupportedOperationException();
|
219
|
}
|
220
|
}
|