Source Code Cross Referenced for HeaderMediator.java in  » ESB » synapse » org » apache » synapse » mediators » transform » 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.mediators.transform 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one
003:         *  or more contributor license agreements.  See the NOTICE file
004:         *  distributed with this work for additional information
005:         *  regarding copyright ownership.  The ASF licenses this file
006:         *  to you under the Apache License, Version 2.0 (the
007:         *  "License"); you may not use this file except in compliance
008:         *  with the License.  You may obtain a copy of the License at
009:         *
010:         *   http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         *  Unless required by applicable law or agreed to in writing,
013:         *  software distributed under the License is distributed on an
014:         *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         *  KIND, either express or implied.  See the License for the
016:         *  specific language governing permissions and limitations
017:         *  under the License.
018:         */
019:
020:        package org.apache.synapse.mediators.transform;
021:
022:        import org.apache.axiom.om.OMElement;
023:        import org.apache.axiom.soap.SOAPEnvelope;
024:        import org.apache.axiom.soap.SOAPFactory;
025:        import org.apache.axiom.soap.SOAPHeader;
026:        import org.apache.axiom.soap.SOAPHeaderBlock;
027:        import org.apache.axis2.addressing.EndpointReference;
028:        import org.apache.axis2.addressing.RelatesTo;
029:        import org.apache.synapse.MessageContext;
030:        import org.apache.synapse.SynapseConstants;
031:        import org.apache.synapse.mediators.AbstractMediator;
032:        import org.apache.synapse.util.xpath.SynapseXPath;
033:
034:        import javax.xml.namespace.QName;
035:        import java.util.Iterator;
036:        import java.util.List;
037:
038:        /**
039:         * The header mediator is able to set a given value as a SOAP header, or remove a given
040:         * header from the current message instance. This supports the headers currently
041:         * supported by the HeaderType class. If an expression is supplied, its runtime value
042:         * is evaluated using the current message. Unless the action is set to remove, the
043:         * default behavior of this mediator is to set a header value.
044:         */
045:        public class HeaderMediator extends AbstractMediator {
046:
047:            public static final int ACTION_SET = 0;
048:            public static final int ACTION_REMOVE = 1;
049:
050:            /** The qName of the header @see HeaderType */
051:            private QName qName = null;
052:            /** The literal value to be set as the header (if one was specified) */
053:            private String value = null;
054:            /** Set the header (ACTION_SET) or remove it (ACTION_REMOVE). Defaults to ACTION_SET */
055:            private int action = ACTION_SET;
056:            /** An expression which should be evaluated, and the result set as the header value */
057:            private SynapseXPath expression = null;
058:
059:            /**
060:             * Sets/Removes a SOAP header on the current message
061:             *
062:             * @param synCtx the current message which is altered as necessary
063:             * @return true always
064:             */
065:            public boolean mediate(MessageContext synCtx) {
066:
067:                boolean traceOn = isTraceOn(synCtx);
068:                boolean traceOrDebugOn = isTraceOrDebugOn(traceOn);
069:
070:                if (traceOrDebugOn) {
071:                    traceOrDebug(traceOn, "Start : Header mediator");
072:
073:                    if (traceOn && trace.isTraceEnabled()) {
074:                        trace.trace("Message : " + synCtx.getEnvelope());
075:                    }
076:                }
077:
078:                if (action == ACTION_SET) {
079:
080:                    String value = (getExpression() == null ? getValue()
081:                            : expression.getStringValue(synCtx));
082:
083:                    if (traceOrDebugOn) {
084:                        traceOrDebug(traceOn, "Set SOAP header : " + qName
085:                                + " to : " + value);
086:                    }
087:
088:                    if (qName.getNamespaceURI() == null
089:                            || "".equals(qName.getNamespaceURI())) {
090:
091:                        // is this a "well known" Synapse header?
092:                        if (SynapseConstants.HEADER_TO.equals(qName
093:                                .getLocalPart())) {
094:                            synCtx.setTo(new EndpointReference(value));
095:                        } else if (SynapseConstants.HEADER_FROM.equals(qName
096:                                .getLocalPart())) {
097:                            synCtx.setFrom(new EndpointReference(value));
098:                        } else if (SynapseConstants.HEADER_ACTION.equals(qName
099:                                .getLocalPart())) {
100:                            synCtx.setWSAAction(value);
101:                        } else if (SynapseConstants.HEADER_FAULT.equals(qName
102:                                .getLocalPart())) {
103:                            synCtx.setFaultTo(new EndpointReference(value));
104:                        } else if (SynapseConstants.HEADER_REPLY_TO
105:                                .equals(qName.getLocalPart())) {
106:                            synCtx.setReplyTo(new EndpointReference(value));
107:                        } else if (SynapseConstants.HEADER_RELATES_TO
108:                                .equals(qName.getLocalPart())) {
109:                            synCtx
110:                                    .setRelatesTo(new RelatesTo[] { new RelatesTo(
111:                                            value) });
112:                        } else {
113:                            addCustomHeader(synCtx, value);
114:                        }
115:                    } else {
116:                        addCustomHeader(synCtx, value);
117:                    }
118:
119:                } else {
120:
121:                    if (traceOrDebugOn) {
122:                        traceOrDebug(traceOn, "Removing SOAP Header : " + qName);
123:                    }
124:
125:                    if (qName.getNamespaceURI() == null
126:                            || "".equals(qName.getNamespaceURI())) {
127:
128:                        // is this a "well known" Synapse header?
129:                        if (SynapseConstants.HEADER_TO.equals(qName
130:                                .getLocalPart())) {
131:                            synCtx.setTo(null);
132:                        } else if (SynapseConstants.HEADER_FROM.equals(qName
133:                                .getLocalPart())) {
134:                            synCtx.setFrom(null);
135:                        } else if (SynapseConstants.HEADER_ACTION.equals(qName
136:                                .getLocalPart())) {
137:                            synCtx.setWSAAction(null);
138:                        } else if (SynapseConstants.HEADER_FAULT.equals(qName
139:                                .getLocalPart())) {
140:                            synCtx.setFaultTo(null);
141:                        } else if (SynapseConstants.HEADER_REPLY_TO
142:                                .equals(qName.getLocalPart())) {
143:                            synCtx.setReplyTo(null);
144:                        } else if (SynapseConstants.HEADER_RELATES_TO
145:                                .equals(qName.getLocalPart())) {
146:                            synCtx.setRelatesTo(null);
147:                        } else {
148:                            SOAPEnvelope envelope = synCtx.getEnvelope();
149:                            if (envelope != null) {
150:                                SOAPHeader header = envelope.getHeader();
151:                                if (header != null) {
152:                                    removeFromHeaderList(header
153:                                            .getHeaderBlocksWithNSURI(""));
154:                                }
155:                            }
156:                        }
157:
158:                    } else {
159:                        SOAPEnvelope envelope = synCtx.getEnvelope();
160:                        if (envelope != null) {
161:                            SOAPHeader header = envelope.getHeader();
162:                            if (header != null) {
163:                                removeFromHeaderList(header
164:                                        .getHeaderBlocksWithNSURI(qName
165:                                                .getNamespaceURI()));
166:                            }
167:                        }
168:                    }
169:                }
170:
171:                if (traceOrDebugOn) {
172:                    traceOrDebug(traceOn, "End : Header mediator");
173:                }
174:                return true;
175:            }
176:
177:            private void addCustomHeader(MessageContext synCtx, String value) {
178:                SOAPEnvelope env = synCtx.getEnvelope();
179:                if (env == null) {
180:                    return;
181:                }
182:                SOAPFactory fac = (SOAPFactory) env.getOMFactory();
183:                SOAPHeader header = env.getHeader();
184:                if (header == null) {
185:                    header = fac.createSOAPHeader(env);
186:                }
187:                SOAPHeaderBlock hb = header.addHeaderBlock(
188:                        qName.getLocalPart(), fac.createOMNamespace(qName
189:                                .getNamespaceURI(), qName.getPrefix()));
190:                hb.setText(value);
191:            }
192:
193:            private void removeFromHeaderList(List headersList) {
194:                if (headersList == null || headersList.isEmpty()) {
195:                    return;
196:                }
197:                for (Iterator iter = headersList.iterator(); iter.hasNext();) {
198:                    Object o = iter.next();
199:                    if (o instanceof  SOAPHeaderBlock) {
200:                        SOAPHeaderBlock header = (SOAPHeaderBlock) o;
201:                        if (header.getLocalName().equals(qName.getLocalPart())) {
202:                            header.detach();
203:                        }
204:                    } else if (o instanceof  OMElement) {
205:                        OMElement omElem = (OMElement) o;
206:                        if (omElem.getLocalName().equals(qName.getLocalPart())) {
207:                            omElem.detach();
208:                        }
209:                    }
210:                }
211:            }
212:
213:            public int getAction() {
214:                return action;
215:            }
216:
217:            public void setAction(int action) {
218:                this .action = action;
219:            }
220:
221:            public QName getQName() {
222:                return qName;
223:            }
224:
225:            public void setQName(QName qName) {
226:                this .qName = qName;
227:            }
228:
229:            public String getValue() {
230:                return value;
231:            }
232:
233:            public void setValue(String value) {
234:                this .value = value;
235:            }
236:
237:            public SynapseXPath getExpression() {
238:                return expression;
239:            }
240:
241:            public void setExpression(SynapseXPath expression) {
242:                this.expression = expression;
243:            }
244:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.