Source Code Cross Referenced for PayloadHelper.java in  » ESB » synapse » org » apache » synapse » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » synapse » org.apache.synapse.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.