test.unit.be.e_contract.dssp.client.WSSecuritySOAPHandler.java Source code

Java tutorial

Introduction

Here is the source code for test.unit.be.e_contract.dssp.client.WSSecuritySOAPHandler.java

Source

/*
 * Digital Signature Service Protocol Project.
 * Copyright (C) 2013-2014 e-Contract.be BVBA.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package test.unit.be.e_contract.dssp.client;

import java.util.Collections;
import java.util.List;
import java.util.Set;

import javax.security.auth.callback.CallbackHandler;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.ProtocolException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ws.security.WSSConfig;
import org.apache.ws.security.WSSecurityEngine;
import org.apache.ws.security.WSSecurityEngineResult;
import org.apache.ws.security.WSSecurityException;

public class WSSecuritySOAPHandler implements SOAPHandler<SOAPMessageContext> {

    private static final Log LOG = LogFactory.getLog(WSSecuritySOAPHandler.class);

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (false == outboundProperty.booleanValue()) {
            try {
                handleInboundMessage(context);
            } catch (Exception e) {
                throw new ProtocolException(e);
            }
        }
        return true;
    }

    private void handleInboundMessage(SOAPMessageContext context) throws WSSecurityException, SOAPException {
        SOAPMessage soapMessage = context.getMessage();
        SOAPPart soapPart = soapMessage.getSOAPPart();

        WSSecurityEngine secEngine = new WSSecurityEngine();
        WSSConfig wssConfig = new WSSConfig();
        secEngine.setWssConfig(wssConfig);
        CallbackHandler callbackHandler = new CallbackTestHandler();
        List<WSSecurityEngineResult> results = secEngine.processSecurityHeader(soapPart, null, callbackHandler,
                null);
        if (null == results) {
            LOG.debug("no WS-Security results");
            return;
        }
        for (WSSecurityEngineResult result : results) {
            LOG.debug("result key set: " + result.keySet());
        }
    }

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

    @Override
    public void close(MessageContext context) {
    }

    @Override
    public Set<QName> getHeaders() {
        return Collections.singleton(new QName(
                "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd", "Security"));
    }
}