001: package org.apache.synapse.util;
002:
003: import java.util.Iterator;
004:
005: import javax.activation.DataHandler;
006:
007: import javax.xml.namespace.QName;
008: import javax.xml.stream.XMLStreamReader;
009:
010: import org.apache.axiom.om.OMAbstractFactory;
011: import org.apache.axiom.om.OMElement;
012: import org.apache.axiom.om.OMFactory;
013: import org.apache.axiom.om.OMNode;
014: import org.apache.axiom.om.OMText;
015: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
016: import org.apache.axiom.soap.SOAP11Version;
017: import org.apache.axiom.soap.SOAPBody;
018: import org.apache.axiom.soap.SOAPEnvelope;
019: import org.apache.axiom.soap.SOAPVersion;
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.apache.synapse.MessageContext;
023: import org.apache.synapse.SynapseException;
024: import org.apache.synapse.util.SimpleMap;
025:
026: public class PayloadHelper {
027:
028: // this has to match org.apache.axis2.base.transport.BaseConstants
029: // at some future point we will merge this into Axiom as a common parent
030: public final static String AXIOMPAYLOADNS = "http://ws.apache.org/commons/ns/payload";
031:
032: public final static QName BINARYELT = new QName(AXIOMPAYLOADNS,
033: "binary", "ax");
034:
035: public final static QName TEXTELT = new QName(AXIOMPAYLOADNS,
036: "text", "ax");
037:
038: public final static QName MAPELT = new QName(AXIOMPAYLOADNS, "map",
039: "ax");
040:
041: public final static int XMLPAYLOADTYPE = 0, BINARYPAYLOADTYPE = 1,
042: TEXTPAYLOADTYPE = 2, MAPPAYLOADTYPE = 3;
043:
044: public static final Log log = LogFactory
045: .getLog(PayloadHelper.class);
046:
047: // gets a indication of the payload type. Default is XML
048: // You cannot set the payload type. Instead, it is set automatically when
049: // the payload is set
050: public static int getPayloadType(SOAPEnvelope envelope) {
051: OMElement el = getXMLPayload(envelope);
052: if (el.getQName().equals(BINARYELT))
053: return BINARYPAYLOADTYPE;
054: else if (el.getQName().equals(TEXTELT))
055: return TEXTPAYLOADTYPE;
056: else if (el.getQName().equals(MAPELT))
057: return MAPPAYLOADTYPE;
058: else
059: return XMLPAYLOADTYPE; // default XML
060: }
061:
062: public static int getPayloadType(MessageContext mc) {
063: if (mc.getEnvelope() == null)
064: return 0;
065: return getPayloadType(mc.getEnvelope());
066: }
067:
068: // XML Payload is carried as the first (and only) child of the body
069: public static OMElement getXMLPayload(SOAPEnvelope envelope) {
070: SOAPBody body = envelope.getBody();
071: if (body == null) {
072: log.error("No body found");
073: return null;
074: }
075: OMElement bodyEl = body.getFirstElement();
076: if (bodyEl == null) {
077: log.error("No body child found");
078: return null;
079: }
080: return bodyEl;
081: }
082:
083: public static void setXMLPayload(SOAPEnvelope envelope,
084: OMElement element) {
085: SOAPBody body = envelope.getBody();
086: if (body == null) {
087:
088: SOAPVersion version = envelope.getVersion();
089: if (version.getEnvelopeURI().equals(
090: SOAP11Version.SOAP_ENVELOPE_NAMESPACE_URI)) {
091: body = OMAbstractFactory.getSOAP11Factory()
092: .createSOAPBody();
093: } else {
094: body = OMAbstractFactory.getSOAP12Factory()
095: .createSOAPBody();
096: }
097: if (envelope.getHeader() != null) {
098: envelope.getHeader().insertSiblingAfter(body);
099: } else {
100: envelope.addChild(body);
101: }
102: } else {
103: for (Iterator it = body.getChildren(); it.hasNext();) {
104: OMNode node = (OMNode) it.next();
105: node.discard();
106: }
107: }
108: body.addChild(element);
109: }
110:
111: public static void setXMLPayload(MessageContext mc,
112: OMElement element) {
113: if (mc.getEnvelope() == null) {
114: try {
115: mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
116: .createSOAPEnvelope());
117: } catch (Exception e) {
118: throw new SynapseException(e);
119: }
120: }
121: setXMLPayload(mc.getEnvelope(), element);
122: }
123:
124: // Binary Payload is carried in a wrapper element with QName BINARYELT
125: public static DataHandler getBinaryPayload(SOAPEnvelope envelope) {
126: OMElement el = getXMLPayload(envelope);
127: if (el == null)
128: return null;
129: if (!el.getQName().equals(BINARYELT)) {
130: log.error("Wrong QName" + el.getQName());
131: return null;
132: }
133: OMNode textNode = el.getFirstOMChild();
134: if (textNode.getType() != OMNode.TEXT_NODE) {
135: log.error("Text Node not found");
136: return null;
137: }
138: OMText text = (OMText) textNode;
139: DataHandler dh = null;
140: try {
141: dh = (DataHandler) text.getDataHandler();
142: } catch (ClassCastException ce) {
143: log.error("cannot get DataHandler" + ce.getMessage());
144: return null;
145: }
146: return dh;
147:
148: }
149:
150: public static DataHandler getBinaryPayload(MessageContext mc) {
151: if (mc.getEnvelope() == null) {
152: log.error("null envelope");
153: return null;
154: }
155: return getBinaryPayload(mc.getEnvelope());
156: }
157:
158: public static void setBinaryPayload(SOAPEnvelope envelope,
159: DataHandler dh) {
160: OMFactory fac = envelope.getOMFactory();
161: OMElement binaryElt = envelope.getOMFactory().createOMElement(
162: BINARYELT);
163: OMText text = fac.createOMText(dh, true);
164: binaryElt.addChild(text);
165: setXMLPayload(envelope, binaryElt);
166: }
167:
168: public static void setBinaryPayload(MessageContext mc,
169: DataHandler dh) {
170: if (mc.getEnvelope() == null) {
171: try {
172: mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
173: .createSOAPEnvelope());
174: } catch (Exception e) {
175: throw new SynapseException(e);
176: }
177: }
178: setBinaryPayload(mc.getEnvelope(), dh);
179:
180: }
181:
182: // Text payload is carried in a wrapper element with QName TEXTELT
183: public static String getTextPayload(SOAPEnvelope envelope) {
184: OMElement el = getXMLPayload(envelope);
185: if (el == null)
186: return null;
187: if (!el.getQName().equals(TEXTELT)) {
188: log.error("Wrong QName" + el.getQName());
189: return null;
190: }
191: OMNode textNode = el.getFirstOMChild();
192: if (textNode.getType() != OMNode.TEXT_NODE) {
193: log.error("Text Node not found");
194: return null;
195: }
196: OMText text = (OMText) textNode;
197: return text.getText();
198: }
199:
200: public static String getTextPayload(MessageContext mc) {
201: if (mc.getEnvelope() == null) {
202: log.error("null envelope");
203: return null;
204: }
205: return getTextPayload(mc.getEnvelope());
206: }
207:
208: public static void setTextPayload(SOAPEnvelope envelope, String text) {
209: OMFactory fac = envelope.getOMFactory();
210: OMElement textElt = envelope.getOMFactory().createOMElement(
211: TEXTELT);
212: OMText textNode = fac.createOMText(text);
213: textElt.addChild(textNode);
214: setXMLPayload(envelope, textElt);
215: }
216:
217: public static void setTextPayload(MessageContext mc, String text) {
218: if (mc.getEnvelope() == null) {
219: try {
220: mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
221: .createSOAPEnvelope());
222: } catch (Exception e) {
223: throw new SynapseException(e);
224: }
225: }
226: setTextPayload(mc.getEnvelope(), text);
227: }
228:
229: // Map payload must be a Map of String->int, boolean, float, double, char,
230: // short, byte, byte[], long, String
231: public static SimpleMap getMapPayload(SOAPEnvelope envelope) {
232: OMElement el = getXMLPayload(envelope);
233: if (el == null)
234: return null;
235: if (!el.getQName().equals(MAPELT)) {
236: log.error("Wrong QName" + el.getQName());
237: return null;
238: }
239: SimpleMap map = new SimpleMapImpl(el);
240: return map;
241: }
242:
243: public static SimpleMap getMapPayload(MessageContext mc) {
244: if (mc.getEnvelope() == null) {
245: log.error("null envelope");
246: return null;
247: }
248: return getMapPayload(mc.getEnvelope());
249: }
250:
251: public static void setMapPayload(SOAPEnvelope envelope,
252: SimpleMap map) {
253:
254: if (map instanceof SimpleMapImpl) {
255: SimpleMapImpl impl = (SimpleMapImpl) map;
256: OMElement mapElt = impl.getOMElement(envelope
257: .getOMFactory());
258: if (mapElt == null) {
259: log.debug("null map element returned");
260: return;
261: }
262: setXMLPayload(envelope, mapElt);
263: } else {
264: throw new SynapseException(
265: "cannot handle any other instance of SimpleMap at this point TODO");
266: }
267: }
268:
269: public static void setMapPayload(MessageContext mc, SimpleMap map) {
270: if (mc.getEnvelope() == null) {
271: try {
272: mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
273: .createSOAPEnvelope());
274: } catch (Exception e) {
275: throw new SynapseException(e);
276: }
277: }
278: setMapPayload(mc.getEnvelope(), map);
279: }
280:
281: public static XMLStreamReader getStAXPayload(SOAPEnvelope envelope) {
282:
283: OMElement el = getXMLPayload(envelope);
284: if (el == null) {
285: return null;
286: }
287: return el.getXMLStreamReader();
288: }
289:
290: public static XMLStreamReader getStAXPayload(MessageContext mc) {
291: if (mc.getEnvelope() == null) {
292: log.error("null envelope");
293: return null;
294: }
295: return getStAXPayload(mc.getEnvelope());
296: }
297:
298: public static void setStAXPayload(SOAPEnvelope envelope,
299: XMLStreamReader streamReader) {
300: StAXOMBuilder builder = new StAXOMBuilder(envelope
301: .getOMFactory(), streamReader);
302: OMElement el = builder.getDocumentElement();
303: setXMLPayload(envelope, el);
304: }
305:
306: public static void setStAXPayload(MessageContext mc,
307: XMLStreamReader streamReader) {
308: if (mc.getEnvelope() == null) {
309: try {
310: mc.setEnvelope(OMAbstractFactory.getSOAP12Factory()
311: .createSOAPEnvelope());
312: } catch (Exception e) {
313: throw new SynapseException(e);
314: }
315: setStAXPayload(mc.getEnvelope(), streamReader);
316: }
317:
318: }
319:
320: }
|