KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > engine > edifact > EdifactProcessor


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: EdifactProcessor.java
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.engine.edifact;
23
24 import java.util.Map JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27
28 import javax.jbi.component.ComponentContext;
29 import javax.jbi.messaging.DeliveryChannel;
30 import javax.jbi.messaging.ExchangeStatus;
31 import javax.jbi.messaging.Fault;
32 import javax.jbi.messaging.InOut;
33 import javax.jbi.messaging.MessagingException;
34 import javax.jbi.messaging.NormalizedMessage;
35 import javax.jbi.servicedesc.ServiceEndpoint;
36 import javax.xml.namespace.QName JavaDoc;
37 import javax.xml.transform.Source JavaDoc;
38
39 import org.ebmwebsourcing.b2bgw.edifact.edifact_beans.InterchangeEDI;
40 import org.objectweb.petals.component.common.listener.AbstractInternalMEProcessor;
41 import org.objectweb.petals.component.common.util.SourceHelper;
42 import org.objectweb.petals.component.common.util.StringHelper;
43
44 /**
45  *
46  * @version $Rev: 250 $Date: {date}
47  * @since Petals 1.0
48  * @author Rocio BARRAZA - eBMWebsourcing
49  *
50  */

51 public class EdifactProcessor extends AbstractInternalMEProcessor {
52
53     private ComponentContext ctx;
54
55     private Map JavaDoc<QName JavaDoc, QName JavaDoc> endpointXslt;
56
57     public EdifactProcessor(DeliveryChannel channel, Logger JavaDoc logger,
58             ComponentContext context, Map JavaDoc<QName JavaDoc, QName JavaDoc> EndpointXslt) {
59         super(channel, logger);
60
61         this.ctx = context;
62         this.endpointXslt = EndpointXslt;
63     }
64
65     protected String JavaDoc callXsltTransformation(Source JavaDoc content, QName JavaDoc endpoint)
66         throws Exception JavaDoc {
67
68         String JavaDoc xml = null;
69         String JavaDoc ediMessage = null;
70
71         QName JavaDoc service = findXsltEndpointForEndpoint(endpoint);
72         if (service != null) {
73             ServiceEndpoint[] xsltEndpoint = this.ctx
74                     .getEndpointsForService(service);
75
76             if (xsltEndpoint.length > 0) {
77                 NormalizedMessage nm = null;
78                 try {
79
80                     /*
81                      * Call xslt service to be xml transformation The service
82                      * Unit that activate endpoint with Invoice xslt sheet must
83                      * be
84                      */

85                     InOut inOut = channel.createExchangeFactory()
86                             .createInOutExchange();
87                     nm = inOut.createMessage();
88
89                     nm.setContent(content);
90                     inOut.setMessage(nm, "IN");
91                     inOut.setService(service);
92                     inOut.setEndpoint(xsltEndpoint[0]);
93                     channel.sendSync(inOut);
94
95                     /*
96                      * Get transformation or exception
97                      */

98
99                     ExchangeStatus status = inOut.getStatus();
100
101                     String JavaDoc fault = null;
102
103                     if (ExchangeStatus.ACTIVE.equals(status)) {
104                         NormalizedMessage nm2 = inOut.getMessage("OUT");
105                         if (nm2 != null) {
106                             xml = SourceHelper.createString(nm2.getContent());
107                             if (xml != null) {
108                                 ediMessage = xmlToEdifact(xml);
109                                 if (ediMessage == null) {
110                                     fault = "Edifact messsage is empty";
111                                 }
112                             }
113                         } else {
114                             Fault sourceFault = inOut.getFault();
115                             if (sourceFault != null) {
116                                 fault = SourceHelper.createString(sourceFault
117                                         .getContent());
118                             }
119                         }
120                     } else {
121                         if (ExchangeStatus.DONE.equals(status)) {
122                             throw new Exception JavaDoc(
123                                     "The exchange has been end too soon");
124                         } else {
125                             if (ExchangeStatus.ERROR.equals(status)) {
126                                 Exception JavaDoc exception = inOut.getError();
127                                 if (exception != null) {
128                                     throw new Exception JavaDoc(exception);
129                                 }
130                             }
131
132                         }
133                     }
134
135                     inOut.setStatus(ExchangeStatus.DONE);
136                     channel.send(inOut);
137
138                     if (fault != null) {
139                         throw new Exception JavaDoc("Error in xslt transformation : "
140                                 + fault);
141                     }
142
143                 } catch (MessagingException e) {
144                     throw new Exception JavaDoc(e);
145                 }
146             }
147         } else {
148             throw new Exception JavaDoc(
149                     "This endpoint doesn't have any xslt endpoint match");
150         }
151         return "<message><![CDATA[" + ediMessage + "]]></message>";
152     }
153
154     protected QName JavaDoc findXsltEndpointForEndpoint(QName JavaDoc endpoint) {
155
156         return this.endpointXslt.get(endpoint);
157     }
158
159     @Override JavaDoc
160     protected void sendInMessage(QName JavaDoc service, QName JavaDoc operation,
161             NormalizedMessage in) throws Exception JavaDoc {
162         String JavaDoc content = SourceHelper.createString(in.getContent());
163
164         if (StringHelper.isEmpty(content)) {
165             l.log(Level.WARNING, "edifactToXML operation : message is empty");
166             // this will be automaticly added to a Fault if the message
167
throw new Exception JavaDoc("message is empty");
168         } else {
169             throw new Exception JavaDoc("Only InOut message is supported");
170         }
171     }
172
173     @Override JavaDoc
174     protected boolean sendInOutMessage(QName JavaDoc service, QName JavaDoc operation,
175             NormalizedMessage in, NormalizedMessage out, boolean optionalOut)
176         throws Exception JavaDoc {
177         Source JavaDoc content = in.getContent();
178
179         if (content == null) {
180             l.log(Level.WARNING,
181                     "edifactToString operation : any string to be transformed");
182             // this will be automaticly added to a Fault if the message
183
throw new Exception JavaDoc(
184                     "edifactToString operation : any string to be transformed");
185         } else {
186             String JavaDoc response = null;
187             if (!service.getLocalPart().equals("EdifactService")) {
188                 try {
189                     response = callXsltTransformation(content, service);
190                 } catch (Exception JavaDoc e) {
191                     throw new Exception JavaDoc(e);
192                 }
193             } else {
194                 if (operation.getLocalPart().equals("edifactToXml")) {
195                     response = edifactToXml(content);
196                 } else if (operation.getLocalPart().equals("xmlToEdifact")) {
197                     String JavaDoc edi = SourceHelper.createString(content);
198                     response = xmlToEdifact(edi);
199                     response = "<message><![CDATA[" + response + "]]></message>";
200                 } else {
201                     throw new Exception JavaDoc(
202                             "Only 'edifactToXml' and 'xmlToEdifact' operations are supported with 'EdifactService'");
203                 }
204             }
205             if (response != null) {
206                 out.setContent(SourceHelper.createSource(response));
207             } else {
208                 out.setContent(SourceHelper.createSource("<message/>"));
209             }
210             return true;
211
212         }
213
214     }
215
216     private String JavaDoc edifactToXml(Source JavaDoc content) throws Exception JavaDoc {
217
218         String JavaDoc edi = null;
219         InterchangeEDI interchange = null;
220         try {
221             edi = SourceHelper.createString(content);
222             if (edi.indexOf("<message><![CDATA[") > -1) {
223                 edi = edi.substring(18, edi.indexOf("]]></"));
224                 edi = edi.replace("!", " ");
225                 edi = edi.replace("#", " ");
226                 interchange = InterchangeEDI.loadFromEDIFACTString(edi);
227             } else {
228                 throw new Exception JavaDoc(
229                         "You must provide the edifact message between two 'message' xml tags and a CDATA section (Example : <message><![CDATA[edi]]></message>");
230             }
231         } catch (NullPointerException JavaDoc e) {
232             throw new Exception JavaDoc("You did not provide a valid EDIFACT message");
233         } catch (Exception JavaDoc e) {
234             throw new Exception JavaDoc(e.getMessage());
235         } catch (Error JavaDoc e) {
236             throw new Exception JavaDoc(e.getMessage());
237         }
238
239         try {
240             interchange.validateAndEnrich();
241             return interchange.toXmlString();
242         } catch (Exception JavaDoc e) {
243             throw new Exception JavaDoc(e.getMessage());
244         }
245     }
246
247     protected String JavaDoc xmlToEdifact(String JavaDoc xml) throws Exception JavaDoc {
248
249         String JavaDoc edi = null;
250         InterchangeEDI interchange = null;
251         try {
252             xml = xml.replace(">", ">\n");
253             interchange = InterchangeEDI.loadFromXmlString(xml);
254
255             l.log(Level.INFO, interchange.toString());
256
257             try {
258                 if (interchange != null) {
259                     interchange.buildAndValidate();
260                     edi = interchange.toEDIFACTString();
261                 }
262
263             } catch (Exception JavaDoc e) {
264                 throw new Exception JavaDoc("ERROR: Edifact message is not validate"
265                         + e.getMessage());
266             }
267         } catch (Exception JavaDoc e) {
268             throw new Exception JavaDoc("ERROR: Create Edifact message"
269                     + e.getMessage());
270         }
271
272         return edi;
273     }
274
275 }
276
Popular Tags