001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 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: SendMessageMarshaller.java 11875 2008-02-04 14:20:31Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.component.http.marshallers;
025:
026: import java.io.UnsupportedEncodingException;
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Set;
030:
031: import javax.activation.DataHandler;
032: import javax.jbi.messaging.MessageExchange;
033: import javax.jbi.messaging.MessagingException;
034: import javax.jbi.messaging.NormalizedMessage;
035: import javax.xml.transform.Source;
036: import javax.xml.transform.dom.DOMSource;
037:
038: import org.apache.axiom.om.OMElement;
039: import org.apache.axiom.om.OMNamespace;
040: import org.apache.axiom.om.OMNode;
041: import org.apache.axiom.soap.SOAPBody;
042: import org.apache.axiom.soap.SOAPEnvelope;
043: import org.apache.axiom.soap.SOAPFactory;
044: import org.apache.axis2.context.MessageContext;
045: import org.apache.commons.codec.binary.Base64;
046: import org.w3c.dom.Element;
047:
048: import com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.ServiceDescriptionHandler;
049: import com.bostechcorp.cbesb.runtime.ccsl.lib.ISoapConsumerMarshaller;
050: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.ByteArraySource;
051: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
052: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
053: import com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.AxiomBuilderFactory;
054: import com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.DOMBuilderFactory;
055: import com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IAxiomBuilder;
056: import com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IDOMBuilder;
057:
058: public class SendMessageMarshaller extends StandardSoapMarshaller
059: implements ISoapConsumerMarshaller {
060:
061: private static final String SENDMESSAGE_NAMESPACE_URL = "http://cbesb.bostechcorp.com/soap/sendmessage/1.0";
062:
063: /**
064: *
065: */
066: public SendMessageMarshaller() {
067:
068: }
069:
070: /* (non-Javadoc)
071: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.AbstractSoapMarshaller#SoapToNormalizedMessage(org.apache.axis2.context.MessageContext, javax.jbi.messaging.NormalizedMessage, javax.jbi.messaging.MessageExchange, com.bostechcorp.cbesb.runtime.ccsl.jbi.messaging.ServiceDescriptionHandler)
072: */
073: public void SoapToNormalizedMessage(MessageContext soapMsgContext,
074: NormalizedMessage message, MessageExchange msgExchange,
075: ServiceDescriptionHandler svcDescHandler, boolean useMTOM,
076: boolean useSwA) throws MessagingException {
077: logger.debug("Converting SendMessage request to DataEnvelope");
078:
079: String messageType = null;
080: NormalizedMessageHandler nmh = new NormalizedMessageHandler(
081: message, svcDescHandler);
082:
083: SOAPBody soapBody = soapMsgContext.getEnvelope().getBody();
084: if (soapBody == null) {
085: throw new MessagingException(
086: "SoapEnvelope does not contain Body.");
087: }
088: OMElement root = soapBody.getFirstElement();
089:
090: if (root != null
091: && SENDMESSAGE_NAMESPACE_URL.equals(root.getNamespace()
092: .getNamespaceURI())
093: && "sendMessage".equals(root.getLocalName())) {
094: for (Iterator children = root.getChildElements(); children
095: .hasNext();) {
096: OMElement child = (OMElement) children.next();
097: if ("messageType".equals(child.getLocalName())) {
098: messageType = child.getText();
099:
100: } else if ("message".equals(child.getLocalName())) {
101: if (messageType == null) {
102: //Missing messageType
103: throw new MessagingException(
104: "Missing required 'messageType' element");
105: }
106: if (messageType.equalsIgnoreCase("STRING")) {
107: StringSource source = new StringSource(child
108: .getText());
109: nmh.addRecord(source);
110:
111: } else if (messageType.equalsIgnoreCase("BASE64")) {
112: try {
113: byte[] binaryBytes = Base64
114: .decodeBase64(child.getText()
115: .getBytes("utf-8"));
116: ByteArraySource bas = new ByteArraySource(
117: binaryBytes);
118: nmh.addRecord(bas);
119: } catch (UnsupportedEncodingException e) {
120: //Will never happen. we always specify utf-8
121: }
122:
123: } else if (messageType.equalsIgnoreCase("XML")) {
124: try {
125: OMElement payload = child.getFirstElement();
126:
127: IDOMBuilder domBuilder = DOMBuilderFactory
128: .newDOMBuilder(useMTOM);
129:
130: Element payloadRoot = domBuilder
131: .getElement(
132: payload,
133: getAttachments(soapMsgContext),
134: useMTOM, useSwA);
135:
136: nmh.addRecord(new DOMSource(payloadRoot));
137:
138: if (useMTOM || useSwA) {
139: HashMap<String, DataHandler> attachments = domBuilder
140: .getOutputAttachments();
141: Set set = attachments.keySet();
142: for (Object object : set) {
143: String id = (String) object;
144: DataHandler dh = attachments
145: .get(id);
146: message.addAttachment(id, dh);
147: }
148: }
149:
150: } catch (Exception e) {
151: throw new MessagingException(e);
152: }
153:
154: } else {
155: //Unsupported MessageType
156: throw new MessagingException(
157: "Unrecognized value in 'messageType': "
158: + messageType);
159: }
160:
161: } else if ("properties".equals(child.getLocalName())) {
162: for (Iterator properties = child.getChildElements(); properties
163: .hasNext();) {
164: OMElement property = (OMElement) properties
165: .next();
166: if ("property".equals(property.getLocalName())) {
167: String name = null;
168: String value = null;
169: OMElement nameElement = property
170: .getFirstElement();
171: if (nameElement != null
172: && "name".equals(nameElement
173: .getLocalName())) {
174: name = nameElement.getText();
175: OMNode sibling = nameElement
176: .getNextOMSibling();
177: if (sibling instanceof OMElement) {
178: OMElement valueElement = (OMElement) sibling;
179: value = valueElement.getText();
180: }
181: }
182: if (name != null && !name.equals("")) {
183: msgExchange.setProperty(name, value);
184: }
185: }
186: }
187: }
188: }
189:
190: nmh.generateMessageContent();
191:
192: } else {
193: throw new MessagingException("SoapBody does not contain {"
194: + SENDMESSAGE_NAMESPACE_URL
195: + "}sendMessage element");
196: }
197: }
198:
199: /* (non-Javadoc)
200: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.AbstractSoapMarshaller#NormalizedMessageToSoap(javax.jbi.messaging.NormalizedMessage, org.apache.axis2.context.MessageContext, javax.jbi.messaging.MessageExchange, org.apache.axiom.soap.SOAPFactory)
201: */
202: public void NormalizedMessageToSoap(NormalizedMessage message,
203: MessageContext soapMsgContext, MessageExchange msgExchange,
204: SOAPFactory soapFactory, boolean useMTOM, boolean useSwA)
205: throws MessagingException {
206: logger
207: .debug("Converting DataEnvelope into SendMessageResponse");
208: try {
209: NormalizedMessageHandler nmh = new NormalizedMessageHandler(
210: message);
211: SOAPEnvelope envelope = soapFactory.createSOAPEnvelope();
212: soapMsgContext.setEnvelope(envelope);
213: SOAPBody body = soapFactory.createSOAPBody(envelope);
214: OMNamespace sendMessageNS = soapFactory.createOMNamespace(
215: SENDMESSAGE_NAMESPACE_URL, "sm");
216: OMElement sendMessageResponse = soapFactory
217: .createOMElement("sendMessageResponse",
218: sendMessageNS);
219: body.addChild(sendMessageResponse);
220: if (nmh.getRecordCount() > 0) {
221: Source record = nmh.getRecordAtIndex(0);
222: if (record instanceof StringSource) {
223: OMElement messageType = soapFactory
224: .createOMElement("messageType",
225: sendMessageNS);
226: messageType.setText("STRING");
227: sendMessageResponse.addChild(messageType);
228: OMElement messageElem = soapFactory
229: .createOMElement("message", sendMessageNS);
230: messageElem.setText(((StringSource) record)
231: .getText());
232: sendMessageResponse.addChild(messageElem);
233:
234: } else if (record instanceof ByteArraySource) {
235: OMElement messageType = soapFactory
236: .createOMElement("messageType",
237: sendMessageNS);
238: messageType.setText("BASE64");
239: sendMessageResponse.addChild(messageType);
240: OMElement messageElem = soapFactory
241: .createOMElement("message", sendMessageNS);
242: byte[] rawBytes = ((ByteArraySource) record)
243: .getBytes();
244: byte[] encodedBytes = Base64.encodeBase64(rawBytes);
245: String encodedString = new String(encodedBytes,
246: "utf-8");
247: messageElem.setText(encodedString);
248: sendMessageResponse.addChild(messageElem);
249:
250: } else {
251: OMElement messageType = soapFactory
252: .createOMElement("messageType",
253: sendMessageNS);
254: messageType.setText("XML");
255: sendMessageResponse.addChild(messageType);
256: OMElement messageElem = soapFactory
257: .createOMElement("message", sendMessageNS);
258:
259: IAxiomBuilder axiomBuilder = AxiomBuilderFactory
260: .newAxiomBuilder(record);
261:
262: OMElement payload = axiomBuilder.getOMTree(record,
263: getAttachments(message), useMTOM, useSwA);
264:
265: messageElem.addChild(payload);
266: sendMessageResponse.addChild(messageElem);
267:
268: if (useSwA) {
269: HashMap<String, DataHandler> attachments = axiomBuilder
270: .getOutputAttachments();
271: Set set = attachments.keySet();
272: for (Object object : set) {
273: String id = (String) object;
274: DataHandler dh = attachments.get(id);
275: soapMsgContext.addAttachment(id, dh);
276: }
277: }
278:
279: }
280: } else {
281: //SendMessage requires 1 record
282: throw new MessagingException(
283: "No records found in DataEnvelope");
284: }
285:
286: // now add the properties
287: Set<String> properties = message.getPropertyNames();
288: if (properties.size() > 0) {
289: OMElement propertiesElem = soapFactory.createOMElement(
290: "properties", sendMessageNS);
291: sendMessageResponse.addChild(propertiesElem);
292: for (String propertyName : properties) {
293: Object value = message.getProperty(propertyName);
294: OMElement property = soapFactory.createOMElement(
295: "property", sendMessageNS);
296: OMElement name = soapFactory.createOMElement(
297: "name", sendMessageNS);
298: name.setText(propertyName);
299: property.addChild(name);
300: OMElement valueElem = soapFactory.createOMElement(
301: "value", sendMessageNS);
302: valueElem.setText(value.toString());
303: property.addChild(valueElem);
304: propertiesElem.addChild(property);
305: }
306: }
307:
308: } catch (Exception e) {
309: throw new MessagingException(
310: "Exception converting NormalizedMesage into SOAP Envelope",
311: e);
312: }
313: }
314:
315: }
|