001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: PrarserProviderProcessor.java 2256 2006-10-21 11:14:06Z elu $
023: */
024: package com.bostechcorp.cbesb.runtime.component.parser.processors;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.InputStream;
029: import java.io.UnsupportedEncodingException;
030:
031: import javax.jbi.JBIException;
032: import javax.jbi.messaging.InOnly;
033: import javax.jbi.messaging.MessageExchange;
034: import javax.jbi.messaging.NormalizedMessage;
035: import javax.xml.namespace.QName;
036: import javax.xml.transform.Source;
037: import javax.xml.transform.Transformer;
038: import javax.xml.transform.TransformerFactory;
039: import javax.xml.transform.dom.DOMSource;
040: import javax.xml.transform.stream.StreamResult;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044: import org.w3c.dom.Document;
045:
046: import com.bostechcorp.cbesb.common.constant.MetadataConstants;
047: import com.bostechcorp.cbesb.common.runtime.CbesbException;
048: import com.bostechcorp.cbesb.common.runtime.DataContentException;
049: import com.bostechcorp.cbesb.common.util.Dom;
050: import com.bostechcorp.cbesb.common.util.EsbPathHelper;
051: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.CbProviderProcessor;
052: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.ByteArraySource;
053: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
054: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
055: import com.bostechcorp.cbesb.runtime.component.parser.ParserEndpoint;
056: import com.bostechcorp.cbesb.runtime.parser.util.ParserUtil;
057:
058: /**
059: * The class for parser provider processsor for provider endpoint in Parser
060: * component. The Parser component only can be used as provider.
061: *
062: */
063: public class ParserProviderProcessor extends CbProviderProcessor {
064:
065: protected final transient Log logger = LogFactory
066: .getLog(getClass());
067:
068: private ParserEndpoint endpoint;
069:
070: public ParserProviderProcessor(ParserEndpoint ep) {
071: super (ep);
072: this .endpoint = ep;
073: }
074:
075: /**
076: *
077: */
078: public void processInMessage(QName service, QName operation,
079: NormalizedMessage in, MessageExchange exchange)
080: throws JBIException {
081:
082: String exchangeType;
083: if (exchange instanceof InOnly) {
084: exchangeType = "InOnly";
085: } else {
086: exchangeType = "RobustInOnly";
087: }
088: throw new JBIException(
089: "Parser Component only support In-Out message exchanges. Received "
090: + exchangeType + " message exchange.");
091: }
092:
093: /**
094: *
095: */
096: public boolean processInOutMessage(QName service, QName operation,
097: NormalizedMessage in, NormalizedMessage out,
098: boolean optionalOut, MessageExchange exchange)
099: throws JBIException, CbesbException {
100:
101: String msgDefStr;
102:
103: NormalizedMessageHandler nmh = new NormalizedMessageHandler(in);
104:
105: // Create an instance of NormalizedMessageHandler to wrap the NormalizedMessage
106: NormalizedMessageHandler nmhOut = new NormalizedMessageHandler(
107: out, getProviderSvcDescHandlerInstance());
108:
109: String parserType = endpoint.getParserType();
110: if (exchange
111: .getProperty(MetadataConstants.PARSER_CONFIG_PARSERTYPE) != null) {
112: parserType = exchange.getProperty(
113: MetadataConstants.PARSER_CONFIG_PARSERTYPE)
114: .toString();
115: }
116: msgDefStr = endpoint.getMsgDef();
117: if (exchange
118: .getProperty(MetadataConstants.PARSER_CONFIG_MSGDEF) != null) {
119: msgDefStr = exchange.getProperty(
120: MetadataConstants.PARSER_CONFIG_MSGDEF).toString();
121: }
122: for (int i = 0; i < nmh.getRecordCount(); i++) {
123: Source src = nmh.getRecordAtIndex(i);
124:
125: Source result = null;
126: InputStream is = getInputStreamFromSource(src);
127:
128: int index = msgDefStr.lastIndexOf(':');
129: String msgName = msgDefStr.substring(index + 1);
130: String formatSpec = msgDefStr.substring(0, index);
131: String saName = EsbPathHelper.getProjectName(msgDefStr);
132:
133: ParserUtil parser = new ParserUtil(saName);
134: if (parserType.equals("x12")) {
135: if (exchange
136: .getProperty(MetadataConstants.PARSER_X12_SEGMENTTERMINATOR) != null) {
137: String value = exchange
138: .getProperty(
139: MetadataConstants.PARSER_X12_SEGMENTTERMINATOR)
140: .toString();
141: if (value.length() > 0)
142: parser.setX12SegmentTerminator(value.charAt(0));
143:
144: }
145: if (exchange
146: .getProperty(MetadataConstants.PARSER_X12_FIELDSEPARATOR) != null) {
147: String value = exchange
148: .getProperty(
149: MetadataConstants.PARSER_X12_FIELDSEPARATOR)
150: .toString();
151: if (value.length() > 0)
152: parser.setX12FieldSeparator(value.charAt(0));
153:
154: }
155: if (exchange
156: .getProperty(MetadataConstants.PARSER_X12_COMPOSITESEPARATOR) != null) {
157: String value = exchange
158: .getProperty(
159: MetadataConstants.PARSER_X12_COMPOSITESEPARATOR)
160: .toString();
161: if (value.length() > 0)
162: parser
163: .setX12CompositeSeparator(value
164: .charAt(0));
165: }
166:
167: }
168: Document domDoc = parser.parse(is, parserType, formatSpec,
169: msgName);
170:
171: result = new DOMSource(domDoc);
172: nmhOut.addRecord(result);
173:
174: }
175:
176: out = nmhOut.generateMessageContent();
177: return true;
178:
179: }
180:
181: private InputStream getInputStreamFromSource(Source src)
182: throws CbesbException {
183: InputStream is = null;
184: if (src instanceof StringSource) {
185: is = ((StringSource) src).getInputStream();
186: } else if (src instanceof ByteArraySource) {
187: is = ((ByteArraySource) src).getInputStream();
188: } else {
189: try {
190: ByteArrayOutputStream baos = new ByteArrayOutputStream();
191: StreamResult result = new StreamResult(baos);
192: TransformerFactory tf = TransformerFactory
193: .newInstance();
194: Transformer t = tf.newTransformer();
195: t.transform(src, result);
196: ByteArrayInputStream bais = new ByteArrayInputStream(
197: baos.toByteArray());
198: is = bais;
199: } catch (Exception e) {
200: DataContentException dce = new DataContentException(
201: "Error getting InputStream from Source - "
202: + e.getMessage(), e);
203: throw dce;
204: }
205: }
206:
207: return is;
208: }
209: }
|