View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   package it.imolinfo.jbi4corba.utils;
9   
10  import it.imolinfo.jbi4corba.Logger;
11  import it.imolinfo.jbi4corba.LoggerFactory;
12  import it.imolinfo.jbi4corba.jbi.Messages;
13  import it.imolinfo.jbi4corba.jbi.component.runtime.RuntimeHelper;
14  
15  import java.io.IOException;
16  import java.io.StringReader;
17  
18  import javax.jbi.servicedesc.ServiceEndpoint;
19  import javax.xml.namespace.QName;
20  import javax.xml.parsers.DocumentBuilder;
21  import javax.xml.parsers.DocumentBuilderFactory;
22  import javax.xml.parsers.ParserConfigurationException;
23  
24  import org.w3c.dom.Document;
25  import org.w3c.dom.NamedNodeMap;
26  import org.w3c.dom.Node;
27  import org.w3c.dom.NodeList;
28  import org.xml.sax.InputSource;
29  import org.xml.sax.SAXException;
30  
31  /**
32   * An utility  Class for the management of the EndpointReference
33   * 
34   * @author <a href="mailto:lacquaviva@imolinfo.it">Luca Acquaviva</a>
35   */
36  public class HelperEPRUtils {
37      
38    /**
39     * Logger.
40     */
41    private static final Logger LOG
42            = LoggerFactory.getLogger(HelperEPRUtils.class);
43    
44  	/**
45  	 * The responsible to translate localized messages.
46  	 */
47  	private static final Messages MESSAGES = Messages
48  			.getMessages(HelperEPRUtils.class);  
49    /**
50     * WS_ADDRESSING Namespaces 
51     **/
52    private static final String WS_ADDRESSING_NS = "http://schemas.xmlsoap.org/ws/2004/08/addressing";
53    private static final String WS_ADDRESSING_NS2 ="http://www.w3.org/2005/08/addressing";  
54      
55       /**
56        * Extract the IOR from a DocumentFragment
57        * @param String DocumentFragment the EPR document faragment
58        */
59      public static String readAddressFromEPR(final String DocumentFragment) {
60          
61           
62           Document document=null;
63           String address="";
64  	 DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
65  	 try {
66  		DocumentBuilder bd = fact.newDocumentBuilder();
67                  
68                  InputSource source = new InputSource(new StringReader(DocumentFragment)); 
69                  document=bd.parse(source);
70           
71                  
72  	} catch (SAXException ex) {
73              LOG.debug("Error "+ex );    
74          } catch (ParserConfigurationException ex) {
75               LOG.debug("Error "+ex );    
76          } catch (IOException ex){
77               LOG.debug("Error "+ex );    
78          }  
79           
80  	
81  	NodeList ndl = document.getElementsByTagName( "Address" );
82          address=ndl.item(0).getTextContent(); 
83          //Remove the jbi4Corba: from the address and return only IOR:0000...
84          if(address.length()>10){
85              address=address.substring(10);
86          }
87          return address;
88          
89                  
90      }
91      
92       
93      
94     /**
95      * Extract the IOR from a DocumentFragment    * 
96      * @param Node the parentnode of the DocumentFragment
97      * @return String
98      * */
99     public static final String getDynamicIORFromEPR(final Node parentNode) throws Exception {
100        String IOR = null;
101        //URL url = null;
102        NodeList children = parentNode.getChildNodes();
103    	for(int ii=0; ii< children.getLength(); ii++) {
104            Node child = children.item(ii);
105            if ("Address".equalsIgnoreCase(child.getLocalName())  &&
106                WS_ADDRESSING_NS.equals(child.getNamespaceURI())) {
107                IOR = child.getTextContent().trim(); 	
108                break;
109            } 
110            if ("Address".equalsIgnoreCase(child.getLocalName())  &&
111                    WS_ADDRESSING_NS2.equals(child.getNamespaceURI())) {
112                    IOR = child.getTextContent().trim(); 	
113                    break;
114                } 
115        }
116        
117         if(IOR.startsWith("jbi4corba:")){
118              IOR=IOR.substring(10);
119         }else{
120             IOR=null;
121             throw new Exception("CRB0000250 jbi4Corba BC can't resolve this EPR");
122         }    
123        //Return the ior without jbi4corba:
124        return IOR;
125        
126    }
127    
128    /**
129     * Extract EndpointInfo as Endpointname and InterfaceName from document fragment
130     * This Method Support both WS-ADDRESSING Specifics Format
131     * @param Node the ChildNode of the DocumentFragment
132     **/
133    public static final ServiceEndpoint getEndpointInfo(final Node parentNode) throws Exception {
134        ServiceEndpoint endpoint = null;
135    	
136    	NodeList children = parentNode.getChildNodes();
137    	for(int ii=0; ii< children.getLength(); ii++) {
138            Node child = children.item(ii);
139           
140            //WS_Addressing 08_2004
141            if ("ServiceName".equalsIgnoreCase(child.getLocalName())  &&
142                WS_ADDRESSING_NS.equals(child.getNamespaceURI())) {
143            	
144                NamedNodeMap attributes = child.getAttributes();
145                Node portNode = attributes.getNamedItem("PortName");
146                String content = child.getTextContent().trim();
147                int index = content.indexOf(":"); 			// locate the namespace prefix
148                if (index <= 0) {
149                   LOG.debug("Invalid Content"); 
150                }
151                String prefix = content.substring(0, index);
152                String serviceLocalName = content.substring(index + 1);
153                String namespaceURI = (child.lookupNamespaceURI(prefix) == null)? parentNode.lookupNamespaceURI(prefix) :
154                                      child.lookupNamespaceURI(prefix);
155                if (namespaceURI == null) {
156             	   LOG.debug("Invalid Content"); 
157                }
158                String endpointName = portNode != null? portNode.getNodeValue() : null;
159                LOG.debug("Endpoint Name:" + endpointName);
160                LOG.debug("Serivce Name:" + namespaceURI+" "+serviceLocalName);
161                //Find the Endpoint on the bus
162                endpoint = RuntimeHelper.getComponentContext().getEndpoint( new QName(namespaceURI, serviceLocalName),endpointName);
163               
164            }
165            
166            LOG.debug("=="+child.getLocalName());
167            LOG.debug("=="+child.getNamespaceURI());
168           
169            // WS_Addressing 08_2005    	   
170            if ("Metadata".equalsIgnoreCase(child.getLocalName())  &&
171         		   WS_ADDRESSING_NS2.equals(child.getNamespaceURI())) {
172         	   		
173             	   NodeList metadata = child.getChildNodes();
174             	   for(int i=0; i< metadata.getLength(); i++) {
175             		   Node mchild = metadata.item(i);
176             		   if ("ServiceName".equalsIgnoreCase(mchild.getLocalName())) {
177                        		
178                            NamedNodeMap attributes = mchild.getAttributes();
179                            Node portNode = attributes.getNamedItem("EndpointName");
180                        		
181                            String content = mchild.getTextContent().trim();
182                            int index = content.indexOf(":"); 			// locate the namespace prefix
183                            if (index <= 0) {
184                            
185                         	   String msg = MESSAGES.getString("CRB001001_Invalid_content");
186                         	   LOG.error(msg);
187                            
188                            }
189                            String prefix = content.substring(0, index);
190                            String serviceLocalName = content.substring(index + 1);
191                            String namespaceURI = (mchild.lookupNamespaceURI(prefix) == null)? parentNode.lookupNamespaceURI(prefix) :
192                                                  mchild.lookupNamespaceURI(prefix);
193                            if (namespaceURI == null) {
194                         	   String msg = MESSAGES.getString("CRB001001_Invalid_content");
195                         	   LOG.error(msg);
196                            }
197                            
198                            String endpointName = portNode != null? portNode.getNodeValue() : null;
199                            LOG.debug("Endpoint Name"+endpointName);
200                            LOG.debug("Service Name"+namespaceURI+" "+serviceLocalName);
201                            //Find the Endpoint on the bus
202                            endpoint =RuntimeHelper.getComponentContext().getEndpoint( new QName(namespaceURI, serviceLocalName),endpointName);
203                           
204                        }
205             		   
206             		   		   
207             	  } 		   
208            }	
209         	   
210            
211        }
212       
213        return endpoint;
214    }
215    
216 
217 }