View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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   
9   
10  package it.imolinfo.jbi4ejb.processor;
11  
12  import it.imolinfo.jbi4ejb.Logger;
13  import it.imolinfo.jbi4ejb.LoggerFactory;
14  import it.imolinfo.jbi4ejb.exception.Jbi4EjbException;
15  import it.imolinfo.jbi4ejb.jbi.Messages;
16  import it.imolinfo.jbi4ejb.jbi.endpoint.Jbi4EjbEndpoint;
17  import it.imolinfo.jbi4ejb.jbi.xfire.EjbChannel;
18  
19  import java.util.Iterator;
20  
21  import javax.jbi.messaging.NormalizedMessage;
22  import javax.wsdl.Message;
23  import javax.wsdl.Operation;
24  import javax.wsdl.Port;
25  import javax.wsdl.PortType;
26  import javax.wsdl.Service;
27  import javax.xml.namespace.QName;
28  import javax.xml.transform.Source;
29  import javax.xml.transform.Transformer;
30  import javax.xml.transform.TransformerConfigurationException;
31  import javax.xml.transform.TransformerException;
32  import javax.xml.transform.TransformerFactory;
33  import javax.xml.transform.TransformerFactoryConfigurationError;
34  import javax.xml.transform.dom.DOMResult;
35  import javax.xml.transform.dom.DOMSource;
36  
37  import org.w3c.dom.Document;
38  import org.w3c.dom.Element;
39  import org.w3c.dom.Node;
40  import org.w3c.dom.NodeList;
41  
42  import com.sun.jbi.nms.wsdl11wrapper.HelperFactory;
43  import com.sun.jbi.nms.wsdl11wrapper.WrapperParser;
44  import com.sun.jbi.nms.wsdl11wrapper.WrapperProcessingException;
45  
46  /**
47   *  Message denormalizer class. Extract from the NRM message the message part.
48   * 
49   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
50   */
51  public class Jbi4EjbDenormalizer {
52  
53      /** The Constant LOG. */
54      private static final Logger LOG
55          = LoggerFactory.getLogger(Jbi4EjbDenormalizer.class);
56      private static final Messages MESSAGES 
57      = Messages.getMessages(Jbi4EjbDenormalizer.class);
58  
59      /** The wrapper parser. */
60      private WrapperParser wrapperParser = null;
61      
62      /** The m trans. */
63      private Transformer mTrans = null;         
64      
65      /**
66       * Instantiates a new denormalizer.
67       *             
68       * @throws Jbi4EjbException
69       *                       if some problem occurs in denormalizing 
70       */
71      public Jbi4EjbDenormalizer() throws Jbi4EjbException {
72          
73          try {
74              wrapperParser = HelperFactory.createParser();
75          } catch (WrapperProcessingException ex) {
76          	String msg=MESSAGES.getString("EJB000601_Failed_to_create_WrapperParser");
77              LOG.error(msg,ex);
78              throw new Jbi4EjbException(msg,ex);   
79          }
80          
81          try {        
82              TransformerFactory factory = TransformerFactory.newInstance();
83              mTrans = factory.newTransformer();            
84          } catch (TransformerFactoryConfigurationError ex) {
85          	String msg=MESSAGES.getString("EJB000602_Jbi4Ejb_processor", new Object[]{ex.getMessage()});
86              LOG.error(msg,ex);
87              throw new Jbi4EjbException(msg,ex);
88          } catch (TransformerConfigurationException e) {
89          	String msg=MESSAGES.getString("EJB000602_Jbi4Ejb_processor", new Object[]{e.getMessage()});
90              LOG.error(msg,e);
91              throw new Jbi4EjbException(msg,e);
92          }                        
93    
94      }
95      
96      
97      
98      /**denormalize
99       * Denormalize the message to the source. This implementation takes the
100      * first (shuold be th only) part
101      * 
102      * @param normalizedMessage
103      *            the message to denormalize
104      * @param endpoint
105      *            the endpoint invoked
106      * @param operation
107      *            the opration invoked
108      * 
109      * @return the <code>Jbi4EjbMessage containing the source.
110      * 
111      * @throws Jbi4EjbException
112      *             if some problem occurs in message denormalizing
113      */
114     public Jbi4EjbMessage denormalize(NormalizedMessage normalizedMessage, 
115             Jbi4EjbEndpoint endpoint,
116             QName operation) throws Jbi4EjbException {
117 
118         try {
119             // Gets the WSDL data
120             final Service service  = endpoint.getDefinition().getService(endpoint.getServiceName());
121             final Port port = service.getPort(QName.valueOf(endpoint.getEndpointName()).getLocalPart());
122             final PortType portType = port.getBinding().getPortType();
123 
124             // Grab the operation that matches the operationName.  There actually may
125             // be more than one operation with the same name (but different input/output)
126             // names.  We need to fix this so that we uniquely identify which operation we're
127             // going after
128             final Iterator it = portType.getOperations().iterator();
129             Message wsdlMessage = null;
130             while (it.hasNext()) {
131                 final Operation op = (Operation)it.next();
132                 if (op.getName().equals(operation.toString()) ||
133                         op.getName().equals(operation.getLocalPart())) {
134                     wsdlMessage = op.getInput().getMessage();
135                 }
136             }
137 
138             // Convert the normalizedMessage into a DOM object
139             final DOMResult result = new DOMResult();
140             final Source src = normalizedMessage.getContent();
141             if (src != null) {
142                 final TransformerFactory fact = TransformerFactory.newInstance();
143                 final Transformer transformer = fact.newTransformer();
144                 transformer.transform( src, result );
145             }
146             Node node = result.getNode();
147             Document normalizedDoc = null;
148             if (node instanceof Document) {
149                 normalizedDoc = (Document) node;
150             } else {
151                 normalizedDoc = ((Element) node).getOwnerDocument();
152             }
153 
154             // Use the WrapperParser to help in parsing out the Parts
155 
156             wrapperParser.parse(normalizedDoc, endpoint.getDefinition());
157                         
158             // use helper class to parse wrapped msg
159 
160             Source source = normalizedMessage.getContent();
161 
162             if (source instanceof DOMSource) {
163                 // saves a transformation
164                 node = ((DOMSource) source).getNode();
165             } else {
166                 DOMResult domResult = new DOMResult();
167                 mTrans.transform(source, domResult);
168                 node = domResult.getNode();
169             }
170             if (node instanceof Document) {
171                 wrapperParser.parse((Document) node, wsdlMessage);
172             } else {
173                 wrapperParser.parse(node.getOwnerDocument(), wsdlMessage);
174             }
175 
176             if (wrapperParser.getNoOfParts() != 0) {
177             // Take ALWAYS the first part
178                 String[] partNames = wrapperParser.getPartNames(); 
179                 NodeList nodes = wrapperParser.getPartNodes(partNames[0]);
180 
181                 if (nodes == null || nodes.getLength() == 0) {
182                     throw new Jbi4EjbException("Unable to find valid part during denormalization");
183                 }
184                 // return the first node
185                 return new Jbi4EjbMessage(new DOMSource(nodes.item(0)), true);
186             } else {
187                 // Support if no parts are found
188                 return new Jbi4EjbMessage(new DOMSource((Document) node), false); 
189             }           
190 
191         } catch (TransformerException ex) {
192         	String msg=MESSAGES.getString("EJB000602_Jbi4Ejb_processor", new Object[]{ex.getMessage()});
193             LOG.error(msg,ex);
194             throw new Jbi4EjbException(msg,ex);
195         }
196         catch (WrapperProcessingException ex) {
197         	String msg=MESSAGES.getString("EJB000602_Jbi4Ejb_processor", new Object[]{ex.getMessage()});
198             LOG.error(msg,ex);
199             throw new Jbi4EjbException(msg,ex);
200         }
201     }
202 
203 
204 }
205