All Downloads are FREE. Search and download functionalities are using the official Maven repository.

testutil.WsaBaseSOAPHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2006, 2022 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package testutil;

import java.util.Iterator;
import java.util.Set;

import jakarta.xml.ws.handler.soap.SOAPMessageContext;
import jakarta.xml.ws.handler.soap.SOAPHandler;
import jakarta.xml.ws.handler.MessageContext;
import jakarta.xml.ws.WebServiceException;
import jakarta.xml.soap.SOAPMessage;
import jakarta.xml.soap.SOAPHeader;
import jakarta.xml.soap.SOAPBody;
import jakarta.xml.soap.SOAPException;
import javax.xml.namespace.QName;

import org.w3c.dom.Node;

/**
 * @author Arun Gupta
 */
public abstract class WsaBaseSOAPHandler implements SOAPHandler {

    String name;
//    protected SOAPBody soapBody;

    public WsaBaseSOAPHandler() {
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        boolean outbound = (Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        try {
            String oper = getOperationName(getSOAPBody(context));
            if (outbound) {
                context.put("op.name", oper);
            } else {
                if (getSOAPBody(context) != null && getSOAPBody(context).getFault() != null) {
                    String detailName = getSOAPBody(context).getFault().getDetail().getFirstChild().getLocalName();
                    checkFaultActions((String)context.get("op.name"), detailName, getAction(context));
                } else {
                    checkInboundActions(oper, getAction(context));
                }
            }
        } catch (SOAPException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return handleMessage(context);
    }

    @Override
    public Set getHeaders() {
        return null;
    }

    @Override
    public void close(MessageContext messageContext) {
    }

    protected SOAPBody getSOAPBody(SOAPMessageContext context) throws SOAPException {
        return context.getMessage().getSOAPBody();
    }

    protected String getAction(SOAPMessageContext context) throws SOAPException {
        SOAPMessage message = context.getMessage();
        SOAPHeader header = message.getSOAPHeader();
        Iterator iter = header.getChildElements(getActionQName());
        if (!iter.hasNext())
            throw new WebServiceException("wsa:Action header is missing in the message");

        Node node = (Node)iter.next();
        String action = node.getFirstChild().getNodeValue();
        return action;
    }

    protected String getOperationName(SOAPBody soapBody) throws SOAPException {
        if (soapBody == null)
            return null;

        if (soapBody.getFirstChild() == null)
            return null;

        return soapBody.getFirstChild().getLocalName();
    }

    protected void checkFaultActions(String requestName, String detailName, String action) {
    }

    public QName getActionQName() {
        return W3CAddressingConstants.actionTag;
    }

    protected abstract void checkInboundActions(String oper, String action);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy