View Javadoc

1   /////////////////////////////////////////////////////////////////////
2   //
3   // © University of Southampton IT Innovation Centre, 2008
4   //
5   // Copyright in this software belongs to the IT Innovation Centre of
6   // 2 Venture Road, Chilworth Science Park, Southampton SO16 7NP, UK.
7   //
8   // This software may not be used, sold, licensed, transferred, copied
9   // or reproduced in whole or in part in any manner or form or in or
10  // on any media by any person other than in accordance with the terms
11  // of the Licence Agreement supplied with the software, or otherwise
12  // without the prior written consent of the copyright owners.
13  //
14  // This software is distributed WITHOUT ANY WARRANTY, without even the
15  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16  // PURPOSE, except where stated in the Licence Agreement supplied with
17  // the software.
18  //
19  //      Created By :            Nikolaos Matskanis
20  //      Created Date :          08/01/2008
21  //      Created for Project :   NextGRID
22  //
23  /////////////////////////////////////////////////////////////////////////
24  //
25  //      Dependencies : none
26  //
27  /////////////////////////////////////////////////////////////////////////
28  //
29  //      Last commit info:       $Author: Nodens2k $
30  //                              $Date: 2008-01-24 19:56:06 +0100 (jue, 24 ene 2008) $
31  //                              $Revision: 158 $
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;//Base64.encode(sr.getBytes());
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  		//logger.debug("Serialised to: " + writer);
83  		return writer.toString();
84  	} 
85          
86          public static  EndpointReferenceType decodeAndDeserialise(String sEPR) {
87                  String info=sEPR;//new String(Base64.decode(sEPR));
88                  info=info.replaceAll("&lt;","<");
89                  info=info.replaceAll("&gt;",">");
90                  info=info.replaceAll("&amp;","&");
91                  info=info.replaceAll("&apos;","'");
92                  info=info.replaceAll("&quot;","\"");
93                          
94                  return deserialise( info);
95          }
96  
97          public static  String encode(String sEPR) {
98                  String info=sEPR;//new String(Base64.decode(sEPR));
99                  info=info.replaceAll("<","&lt;");
100                 info=info.replaceAll(">","&gt;");
101                 info=info.replaceAll("&","&amp;");
102                 info=info.replaceAll("'","&apos;");
103                 info=info.replaceAll("\"","&quot;");
104                 return info;
105         }
106 
107         public static  EndpointReferenceType deserialise(String sEPR) {
108 		if (sEPR == null)
109 			return null;
110 		//logger.info("Deserialising: " + sEPR);
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 }