1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 package org.apache.synapse.mediators.transform; 21 22 import org.apache.axiom.om.OMElement; 23 import org.apache.axiom.soap.SOAPEnvelope; 24 import org.apache.axiom.soap.SOAPFactory; 25 import org.apache.axiom.soap.SOAPHeader; 26 import org.apache.axiom.soap.SOAPHeaderBlock; 27 import org.apache.axis2.addressing.EndpointReference; 28 import org.apache.axis2.addressing.RelatesTo; 29 import org.apache.synapse.MessageContext; 30 import org.apache.synapse.SynapseConstants; 31 import org.apache.synapse.mediators.AbstractMediator; 32 import org.apache.synapse.util.xpath.SynapseXPath; 33 34 import javax.xml.namespace.QName; 35 import java.util.Iterator; 36 import java.util.List; 37 38 /** 39 * The header mediator is able to set a given value as a SOAP header, or remove a given 40 * header from the current message instance. This supports the headers currently 41 * supported by the HeaderType class. If an expression is supplied, its runtime value 42 * is evaluated using the current message. Unless the action is set to remove, the 43 * default behavior of this mediator is to set a header value. 44 */ 45 public class HeaderMediator extends AbstractMediator { 46 47 public static final int ACTION_SET = 0; 48 public static final int ACTION_REMOVE = 1; 49 50 /** The qName of the header @see HeaderType */ 51 private QName qName = null; 52 /** The literal value to be set as the header (if one was specified) */ 53 private String value = null; 54 /** Set the header (ACTION_SET) or remove it (ACTION_REMOVE). Defaults to ACTION_SET */ 55 private int action = ACTION_SET; 56 /** An expression which should be evaluated, and the result set as the header value */ 57 private SynapseXPath expression = null; 58 59 /** 60 * Sets/Removes a SOAP header on the current message 61 * 62 * @param synCtx the current message which is altered as necessary 63 * @return true always 64 */ 65 public boolean mediate(MessageContext synCtx) { 66 67 boolean traceOn = isTraceOn(synCtx); 68 boolean traceOrDebugOn = isTraceOrDebugOn(traceOn); 69 70 if (traceOrDebugOn) { 71 traceOrDebug(traceOn, "Start : Header mediator"); 72 73 if (traceOn && trace.isTraceEnabled()) { 74 trace.trace("Message : " + synCtx.getEnvelope()); 75 } 76 } 77 78 if (action == ACTION_SET) { 79 80 String value = (getExpression() == null ? getValue() : 81 expression.stringValueOf(synCtx)); 82 83 if (traceOrDebugOn) { 84 traceOrDebug(traceOn, "Set SOAP header : " + qName + " to : " + value); 85 } 86 87 if (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI())) { 88 89 // is this a "well known" Synapse header? 90 if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) { 91 synCtx.setTo(new EndpointReference(value)); 92 } else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) { 93 synCtx.setFrom(new EndpointReference(value)); 94 } else if (SynapseConstants.HEADER_ACTION.equals(qName.getLocalPart())) { 95 synCtx.setWSAAction(value); 96 } else if (SynapseConstants.HEADER_FAULT.equals(qName.getLocalPart())) { 97 synCtx.setFaultTo(new EndpointReference(value)); 98 } else if (SynapseConstants.HEADER_REPLY_TO.equals(qName.getLocalPart())) { 99 synCtx.setReplyTo(new EndpointReference(value)); 100 } else if (SynapseConstants.HEADER_RELATES_TO.equals(qName.getLocalPart())) { 101 synCtx.setRelatesTo(new RelatesTo[] { new RelatesTo(value) }); 102 } else { 103 addCustomHeader(synCtx, value); 104 } 105 } else { 106 addCustomHeader(synCtx, value); 107 } 108 109 } else { 110 111 if (traceOrDebugOn) { 112 traceOrDebug(traceOn, "Removing SOAP Header : " + qName); 113 } 114 115 if (qName.getNamespaceURI() == null || "".equals(qName.getNamespaceURI())) { 116 117 // is this a "well known" Synapse header? 118 if (SynapseConstants.HEADER_TO.equals(qName.getLocalPart())) { 119 synCtx.setTo(null); 120 } else if (SynapseConstants.HEADER_FROM.equals(qName.getLocalPart())) { 121 synCtx.setFrom(null); 122 } else if (SynapseConstants.HEADER_ACTION.equals(qName.getLocalPart())) { 123 synCtx.setWSAAction(null); 124 } else if (SynapseConstants.HEADER_FAULT.equals(qName.getLocalPart())) { 125 synCtx.setFaultTo(null); 126 } else if (SynapseConstants.HEADER_REPLY_TO.equals(qName.getLocalPart())) { 127 synCtx.setReplyTo(null); 128 } else if (SynapseConstants.HEADER_RELATES_TO.equals(qName.getLocalPart())) { 129 synCtx.setRelatesTo(null); 130 } else { 131 SOAPEnvelope envelope = synCtx.getEnvelope(); 132 if (envelope != null) { 133 SOAPHeader header = envelope.getHeader(); 134 if (header != null) { 135 removeFromHeaderList(header. 136 getHeaderBlocksWithNSURI("")); 137 } 138 } 139 } 140 141 } else { 142 SOAPEnvelope envelope = synCtx.getEnvelope(); 143 if (envelope != null) { 144 SOAPHeader header = envelope.getHeader(); 145 if (header != null) { 146 removeFromHeaderList(header. 147 getHeaderBlocksWithNSURI(qName.getNamespaceURI())); 148 } 149 } 150 } 151 } 152 153 if (traceOrDebugOn) { 154 traceOrDebug(traceOn, "End : Header mediator"); 155 } 156 return true; 157 } 158 159 private void addCustomHeader(MessageContext synCtx, String value) { 160 SOAPEnvelope env = synCtx.getEnvelope(); 161 if (env == null) { 162 return; 163 } 164 SOAPFactory fac = (SOAPFactory) env.getOMFactory(); 165 SOAPHeader header = env.getHeader(); 166 if (header == null) { 167 header = fac.createSOAPHeader(env); 168 } 169 SOAPHeaderBlock hb = header.addHeaderBlock(qName.getLocalPart(), 170 fac.createOMNamespace(qName.getNamespaceURI(), qName.getPrefix())); 171 hb.setText(value); 172 } 173 174 private void removeFromHeaderList(List headersList) { 175 if (headersList == null || headersList.isEmpty()) { 176 return; 177 } 178 for ( Iterator iter = headersList.iterator();iter.hasNext();) { 179 Object o = iter.next(); 180 if (o instanceof SOAPHeaderBlock) { 181 SOAPHeaderBlock header = (SOAPHeaderBlock) o; 182 if (header.getLocalName().equals(qName.getLocalPart())) { 183 header.detach(); 184 } 185 } else if (o instanceof OMElement) { 186 OMElement omElem = (OMElement) o; 187 if (omElem.getLocalName().equals(qName.getLocalPart())) { 188 omElem.detach(); 189 } 190 } 191 } 192 } 193 194 public int getAction() { 195 return action; 196 } 197 198 public void setAction(int action) { 199 this.action = action; 200 } 201 202 public QName getQName() { 203 return qName; 204 } 205 206 public void setQName(QName qName) { 207 this.qName = qName; 208 } 209 210 public String getValue() { 211 return value; 212 } 213 214 public void setValue(String value) { 215 this.value = value; 216 } 217 218 public SynapseXPath getExpression() { 219 return expression; 220 } 221 222 public void setExpression(SynapseXPath expression) { 223 this.expression = expression; 224 } 225 }