1 22 package org.objectweb.petals.engine.edifact; 23 24 import java.util.Map ; 25 import java.util.logging.Level ; 26 import java.util.logging.Logger ; 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 ; 37 import javax.xml.transform.Source ; 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 51 public class EdifactProcessor extends AbstractInternalMEProcessor { 52 53 private ComponentContext ctx; 54 55 private Map <QName , QName > endpointXslt; 56 57 public EdifactProcessor(DeliveryChannel channel, Logger logger, 58 ComponentContext context, Map <QName , QName > EndpointXslt) { 59 super(channel, logger); 60 61 this.ctx = context; 62 this.endpointXslt = EndpointXslt; 63 } 64 65 protected String callXsltTransformation(Source content, QName endpoint) 66 throws Exception { 67 68 String xml = null; 69 String ediMessage = null; 70 71 QName 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 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 98 99 ExchangeStatus status = inOut.getStatus(); 100 101 String 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 ( 123 "The exchange has been end too soon"); 124 } else { 125 if (ExchangeStatus.ERROR.equals(status)) { 126 Exception exception = inOut.getError(); 127 if (exception != null) { 128 throw new Exception (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 ("Error in xslt transformation : " 140 + fault); 141 } 142 143 } catch (MessagingException e) { 144 throw new Exception (e); 145 } 146 } 147 } else { 148 throw new Exception ( 149 "This endpoint doesn't have any xslt endpoint match"); 150 } 151 return "<message><![CDATA[" + ediMessage + "]]></message>"; 152 } 153 154 protected QName findXsltEndpointForEndpoint(QName endpoint) { 155 156 return this.endpointXslt.get(endpoint); 157 } 158 159 @Override 160 protected void sendInMessage(QName service, QName operation, 161 NormalizedMessage in) throws Exception { 162 String content = SourceHelper.createString(in.getContent()); 163 164 if (StringHelper.isEmpty(content)) { 165 l.log(Level.WARNING, "edifactToXML operation : message is empty"); 166 throw new Exception ("message is empty"); 168 } else { 169 throw new Exception ("Only InOut message is supported"); 170 } 171 } 172 173 @Override 174 protected boolean sendInOutMessage(QName service, QName operation, 175 NormalizedMessage in, NormalizedMessage out, boolean optionalOut) 176 throws Exception { 177 Source content = in.getContent(); 178 179 if (content == null) { 180 l.log(Level.WARNING, 181 "edifactToString operation : any string to be transformed"); 182 throw new Exception ( 184 "edifactToString operation : any string to be transformed"); 185 } else { 186 String response = null; 187 if (!service.getLocalPart().equals("EdifactService")) { 188 try { 189 response = callXsltTransformation(content, service); 190 } catch (Exception e) { 191 throw new Exception (e); 192 } 193 } else { 194 if (operation.getLocalPart().equals("edifactToXml")) { 195 response = edifactToXml(content); 196 } else if (operation.getLocalPart().equals("xmlToEdifact")) { 197 String edi = SourceHelper.createString(content); 198 response = xmlToEdifact(edi); 199 response = "<message><![CDATA[" + response + "]]></message>"; 200 } else { 201 throw new Exception ( 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 edifactToXml(Source content) throws Exception { 217 218 String 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 ( 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 e) { 232 throw new Exception ("You did not provide a valid EDIFACT message"); 233 } catch (Exception e) { 234 throw new Exception (e.getMessage()); 235 } catch (Error e) { 236 throw new Exception (e.getMessage()); 237 } 238 239 try { 240 interchange.validateAndEnrich(); 241 return interchange.toXmlString(); 242 } catch (Exception e) { 243 throw new Exception (e.getMessage()); 244 } 245 } 246 247 protected String xmlToEdifact(String xml) throws Exception { 248 249 String 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 e) { 264 throw new Exception ("ERROR: Edifact message is not validate" 265 + e.getMessage()); 266 } 267 } catch (Exception e) { 268 throw new Exception ("ERROR: Create Edifact message" 269 + e.getMessage()); 270 } 271 272 return edi; 273 } 274 275 } 276 | Popular Tags |