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: }
|