1
2
3
4
5
6
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
48
49
50
51 public class Jbi4EjbDenormalizer {
52
53
54 private static final Logger LOG
55 = LoggerFactory.getLogger(Jbi4EjbDenormalizer.class);
56 private static final Messages MESSAGES
57 = Messages.getMessages(Jbi4EjbDenormalizer.class);
58
59
60 private WrapperParser wrapperParser = null;
61
62
63 private Transformer mTrans = null;
64
65
66
67
68
69
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114 public Jbi4EjbMessage denormalize(NormalizedMessage normalizedMessage,
115 Jbi4EjbEndpoint endpoint,
116 QName operation) throws Jbi4EjbException {
117
118 try {
119
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
125
126
127
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
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
155
156 wrapperParser.parse(normalizedDoc, endpoint.getDefinition());
157
158
159
160 Source source = normalizedMessage.getContent();
161
162 if (source instanceof DOMSource) {
163
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
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
185 return new Jbi4EjbMessage(new DOMSource(nodes.item(0)), true);
186 } else {
187
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