Search in sources :

Example 1 with HeaderMediator

use of org.apache.synapse.mediators.transform.HeaderMediator in project wso2-synapse by wso2.

the class HeaderMediatorFactory method createSpecificMediator.

public Mediator createSpecificMediator(OMElement elem, Properties properties) {
    HeaderMediator headerMediator = new HeaderMediator();
    OMAttribute name = elem.getAttribute(ATT_NAME);
    OMAttribute value = elem.getAttribute(ATT_VALUE);
    OMAttribute exprn = elem.getAttribute(ATT_EXPRN);
    OMAttribute action = elem.getAttribute(ATT_ACTION);
    OMAttribute scope = elem.getAttribute(ATT_SCOPE);
    if (name == null || name.getAttributeValue() == null) {
        if (elem.getChildElements() == null || !elem.getChildElements().hasNext()) {
            String msg = "A valid name attribute is required for the header mediator";
            log.error(msg);
            throw new SynapseException(msg);
        }
    } else {
        if (scope == null || scope.getAttributeValue().equals(XMLConfigConstants.SCOPE_DEFAULT)) {
            String nameAtt = name.getAttributeValue();
            int colonPos = nameAtt.indexOf(":");
            if (colonPos != -1) {
                // has a NS prefix.. find it and the NS it maps into
                String prefix = nameAtt.substring(0, colonPos);
                String namespaceURI = OMElementUtils.getNameSpaceWithPrefix(prefix, elem);
                if (namespaceURI == null) {
                    handleException("Invalid namespace prefix '" + prefix + "' in name attribute");
                } else {
                    headerMediator.setQName(new QName(namespaceURI, nameAtt.substring(colonPos + 1), prefix));
                }
            } else {
                // no prefix
                if (SynapseConstants.HEADER_TO.equals(nameAtt) || SynapseConstants.HEADER_FROM.equals(nameAtt) || SynapseConstants.HEADER_ACTION.equals(nameAtt) || SynapseConstants.HEADER_FAULT.equals(nameAtt) || SynapseConstants.HEADER_REPLY_TO.equals(nameAtt) || SynapseConstants.HEADER_RELATES_TO.equals(nameAtt)) {
                    headerMediator.setQName(new QName(nameAtt));
                } else {
                    handleException("Invalid SOAP header: " + nameAtt + " specified at the " + "header mediator. All SOAP headers must be namespace qualified.");
                }
            }
        } else {
            headerMediator.setQName(new QName(name.getAttributeValue()));
        }
    }
    if (scope != null) {
        String valueStr = scope.getAttributeValue();
        if (!XMLConfigConstants.SCOPE_TRANSPORT.equals(valueStr) && !XMLConfigConstants.SCOPE_DEFAULT.equals(valueStr)) {
            String msg = "Only '" + XMLConfigConstants.SCOPE_TRANSPORT + "' or '" + XMLConfigConstants.SCOPE_DEFAULT + "' values are allowed for attribute scope for a header mediator" + ", Unsupported scope " + valueStr;
            log.error(msg);
            throw new SynapseException(msg);
        }
        headerMediator.setScope(valueStr);
    }
    // after successfully creating the mediator
    // set its common attributes such as tracing etc
    processAuditStatus(headerMediator, elem);
    // header mediator will act as a header remove mediator
    if (action != null && "remove".equals(action.getAttributeValue())) {
        headerMediator.setAction(HeaderMediator.ACTION_REMOVE);
    }
    if (headerMediator.getAction() == HeaderMediator.ACTION_SET && value == null && exprn == null && !headerMediator.isImplicit()) {
        handleException("A 'value' or 'expression' attribute is required for a [set] " + "header mediator");
    }
    if (value != null && value.getAttributeValue() != null) {
        headerMediator.setValue(value.getAttributeValue());
    } else if (exprn != null && exprn.getAttributeValue() != null) {
        try {
            headerMediator.setExpression(SynapseXPathFactory.getSynapseXPath(elem, ATT_EXPRN));
        } catch (JaxenException je) {
            handleException("Invalid XPath expression : " + exprn.getAttributeValue());
        }
    } else if (headerMediator.isImplicit()) {
        // we have an implicit, non standard header
        Iterator i = elem.getChildElements();
        if (i == null) {
            handleException("A non standard header with both value and expression are null must " + "contain an embedded XML definition.");
        }
        for (; i.hasNext(); ) {
            headerMediator.addEmbeddedXml((OMElement) i.next());
        }
    }
    return headerMediator;
}
Also used : HeaderMediator(org.apache.synapse.mediators.transform.HeaderMediator) SynapseException(org.apache.synapse.SynapseException) QName(javax.xml.namespace.QName) JaxenException(org.jaxen.JaxenException) Iterator(java.util.Iterator) OMAttribute(org.apache.axiom.om.OMAttribute)

Example 2 with HeaderMediator

use of org.apache.synapse.mediators.transform.HeaderMediator in project wso2-synapse by wso2.

the class HeaderMediatorSerializer method serializeSpecificMediator.

public OMElement serializeSpecificMediator(Mediator m) {
    if (!(m instanceof HeaderMediator)) {
        handleException("Unsupported mediator passed in for serialization : " + m.getType());
    }
    HeaderMediator mediator = (HeaderMediator) m;
    OMElement header = fac.createOMElement("header", synNS);
    saveTracingState(header, mediator);
    QName qName = mediator.getQName();
    if (qName != null) {
        if (qName.getNamespaceURI() != null) {
            header.addAttribute(fac.createOMAttribute("name", nullNS, (qName.getPrefix() != null && !"".equals(qName.getPrefix()) ? qName.getPrefix() + ":" : "") + qName.getLocalPart()));
            header.declareNamespace(qName.getNamespaceURI(), qName.getPrefix());
        } else {
            header.addAttribute(fac.createOMAttribute("name", nullNS, qName.getLocalPart()));
        }
    }
    if (mediator.getScope() != null) {
        // if we have already built a mediator with scope, scope should be valid, now save it
        header.addAttribute(fac.createOMAttribute("scope", nullNS, mediator.getScope()));
    }
    if (mediator.getAction() == HeaderMediator.ACTION_REMOVE) {
        header.addAttribute(fac.createOMAttribute("action", nullNS, "remove"));
    } else {
        if (mediator.getValue() != null) {
            header.addAttribute(fac.createOMAttribute("value", nullNS, mediator.getValue()));
        } else if (mediator.getExpression() != null) {
            SynapseXPathSerializer.serializeXPath(mediator.getExpression(), header, "expression");
        } else if (!mediator.isImplicit()) {
            handleException("Value or expression required for a set header mediator");
        }
    }
    if (mediator.hasEmbeddedXml()) {
        for (OMElement e : mediator.getEmbeddedXml()) {
            header.addChild(e);
        }
    }
    return header;
}
Also used : HeaderMediator(org.apache.synapse.mediators.transform.HeaderMediator) QName(javax.xml.namespace.QName) OMElement(org.apache.axiom.om.OMElement)

Aggregations

QName (javax.xml.namespace.QName)2 HeaderMediator (org.apache.synapse.mediators.transform.HeaderMediator)2 Iterator (java.util.Iterator)1 OMAttribute (org.apache.axiom.om.OMAttribute)1 OMElement (org.apache.axiom.om.OMElement)1 SynapseException (org.apache.synapse.SynapseException)1 JaxenException (org.jaxen.JaxenException)1