1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 package uk.ac.soton.itinnovation.utils;
37
38 import org.apache.axis.MessageContext;
39 import org.apache.axis.encoding.SerializationContext;
40 import org.apache.axis.encoding.TypeMappingRegistryImpl;
41 import org.apache.axis.message.addressing.Constants;
42 import org.apache.axis.message.addressing.EndpointReference;
43 import org.apache.axis.message.addressing.EndpointReferenceType;
44 import org.apache.axis.message.addressing.util.AddressingUtils;
45 import org.w3c.dom.Document;
46 import org.w3c.dom.Element;
47 import org.w3c.dom.Node;
48 import org.w3c.dom.NodeList;
49 import org.xml.sax.InputSource;
50 import org.xml.sax.SAXException;
51
52 import java.io.IOException;
53 import java.io.StringReader;
54 import java.io.StringWriter;
55 import java.util.ArrayList;
56 import java.util.List;
57
58 import javax.xml.namespace.QName;
59 import javax.xml.parsers.DocumentBuilder;
60 import javax.xml.parsers.DocumentBuilderFactory;
61
62 import org.apache.axis.encoding.Base64;
63
64 public class EPRUtils{
65 public static String encodeAndSerialise(EndpointReferenceType epr) {
66 String sr =serialise(epr);
67 return sr;
68 }
69 public static String serialise(EndpointReferenceType epr) {
70 StringWriter writer = new StringWriter();
71 try {
72 MessageContext msgContext = new MessageContext(null);
73 msgContext.setTypeMappingRegistry(new TypeMappingRegistryImpl());
74 msgContext.setEncodingStyle(null);
75 SerializationContext context = new SerializationContext(writer, msgContext);
76 context.setPretty(false);
77 context.setSendDecl(false);
78 context.serialize(new QName(null, "epr"), null, epr, null, false, null);
79 } catch (IOException ex) {
80 throw new RuntimeException(ex);
81 }
82
83 return writer.toString();
84 }
85
86 public static EndpointReferenceType decodeAndDeserialise(String sEPR) {
87 String info=sEPR;
88 info=info.replaceAll("<","<");
89 info=info.replaceAll(">",">");
90 info=info.replaceAll("&","&");
91 info=info.replaceAll("'","'");
92 info=info.replaceAll(""","\"");
93
94 return deserialise( info);
95 }
96
97 public static String encode(String sEPR) {
98 String info=sEPR;
99 info=info.replaceAll("<","<");
100 info=info.replaceAll(">",">");
101 info=info.replaceAll("&","&");
102 info=info.replaceAll("'","'");
103 info=info.replaceAll("\"",""");
104 return info;
105 }
106
107 public static EndpointReferenceType deserialise(String sEPR) {
108 if (sEPR == null)
109 return null;
110
111 Document doc;
112 try {
113 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
114 factory.setNamespaceAware(true);
115 factory.setAttribute("http://apache.org/xml/features/dom/defer-node-expansion",
116 new Boolean(false));
117 DocumentBuilder docBuilder = factory.newDocumentBuilder();
118 doc = docBuilder.parse(new InputSource(new StringReader(sEPR)));
119 Element root = doc.getDocumentElement();
120 fixNamespaces(root);
121 return new EndpointReference(root);
122 } catch (SAXException e) {
123 throw new RuntimeException("XML is not correct: " + sEPR, e);
124 } catch (Exception e) {
125 throw new RuntimeException(e);
126 }
127 }
128
129
130 private static void fixNamespaces(Element element) {
131 Document doc = element.getOwnerDocument();
132 NodeList children = element.getChildNodes();
133 for (int i=0;i<children.getLength();i++) {
134 Node child = (Node)children.item(i);
135 if (child instanceof Element) {
136 if (!AddressingUtils.isAddressingNamespaceURI(child.getNamespaceURI())) {
137 Element correct = doc.createElementNS(Constants.NS_URI_ADDRESSING_DEFAULT,
138 "wsa:" + child.getLocalName());
139 element.insertBefore(correct, child);
140 moveChildren((Element) child, correct);
141 element.removeChild(child);
142 }
143 }
144 }
145 }
146
147 private static void moveChildren(Element from, Element to) {
148 NodeList children = from.getChildNodes();
149 while (children.getLength() > 0) {
150 Node child = children.item(0);
151 from.removeChild(child);
152 to.appendChild(child);
153 }
154 }
155 }