com.hiperium.integration.access.control.SoapSessionHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.hiperium.integration.access.control.SoapSessionHandler.java

Source

/**
 * Product  : Hiperium Project
 * Architect: Andres Solorzano.
 * Created  : 08-05-2009 - 23:30:00
 * 
 * The contents of this file are copyrighted by Andres Solorzano 
 * and it is protected by the license: "GPL V3." You can find a copy of this 
 * license at: http://www.hiperium.com/about/licence.html
 * 
 * Copyright 2014 Andres Solorzano. All rights reserved.
 * 
 */
package com.hiperium.integration.access.control;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFault;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;
import javax.xml.ws.soap.SOAPFaultException;

import org.apache.commons.lang.StringUtils;

import com.hiperium.bo.delegate.SecurityBusinessDelegate;
import com.hiperium.commons.CommonsUtil;
import com.hiperium.commons.EnumHiperiumTier;
import com.hiperium.commons.EnumI18N;
import com.hiperium.commons.HiperiumTier;
import com.hiperium.commons.log.HiperiumLogger;
import com.hiperium.integration.common.utils.Resources;

/**
 * @author Andres Solorzano
 *
 */
public class SoapSessionHandler implements SOAPHandler<SOAPMessageContext> {

    /** The LOGGER property for logger messages. */
    private static final HiperiumLogger LOGGER = HiperiumLogger.getLogger(SoapSessionHandler.class);

    /** The property securityBusinessDelegate. */
    @Inject
    @HiperiumTier(EnumHiperiumTier.INTEGRATION)
    private SecurityBusinessDelegate securityBusinessDelegate;

    /* (non-Javadoc)
     * @see javax.xml.ws.handler.Handler#handleMessage(javax.xml.ws.handler.MessageContext)
     */
    @SuppressWarnings("unchecked")
    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        LOGGER.debug("handleMessage - BEGIN");
        // Only message arriving from the client. Not processing responses.
        Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
        if (!outbound) {
            Map<String, List<String>> map = (Map<String, List<String>>) context
                    .get(MessageContext.HTTP_REQUEST_HEADERS);
            List<String> sessionHeader = this.getHTTPHeader(map, CommonsUtil.SESSIONID);
            if (sessionHeader == null) {
                SOAPMessage msg = context.getMessage();
                this.generateFault(msg, Resources.getResourceBundle(EnumI18N.SECURITY, Locale.getDefault())
                        .getString("ilegalAccessResource"));
            }
            // Get the sessionId from the entire HTTP Message
            StringBuffer sessionIdBuffer = new StringBuffer();
            for (String session : sessionHeader) {
                sessionIdBuffer.append(session);
            }
            // Validate that the session ID is valid 
            if (StringUtils.isNotBlank(sessionIdBuffer.toString()) && !this.securityBusinessDelegate
                    .getSessionManagerBO().findIfHomeLoggedIn(sessionIdBuffer.toString())) {
                SOAPMessage msg = context.getMessage();
                this.generateFault(msg, Resources.getResourceBundle(EnumI18N.SECURITY, Locale.getDefault())
                        .getString("ilegalAccessResource"));
            }
        }
        LOGGER.debug("handleMessage - END");
        return true; //continue other handler chain
    }

    /* (non-Javadoc)
     * @see javax.xml.ws.handler.Handler#close(javax.xml.ws.handler.MessageContext)
     */
    @Override
    public void close(MessageContext arg0) {
        // Nothing to do.
    }

    /* (non-Javadoc)
     * @see javax.xml.ws.handler.Handler#handleFault(javax.xml.ws.handler.MessageContext)
     */
    @Override
    public boolean handleFault(SOAPMessageContext arg0) {
        return false;
    }

    /* (non-Javadoc)
     * @see javax.xml.ws.handler.soap.SOAPHandler#getHeaders()
     */
    @Override
    public Set<QName> getHeaders() {
        return null;
    }

    /**
     * 
     * @param headers
     * @param header
     * @return
     */
    private List<String> getHTTPHeader(@NotNull Map<String, List<String>> headers, @NotNull String header) {
        for (Map.Entry<String, List<String>> entry : headers.entrySet()) {
            String name = entry.getKey();
            if (name.equalsIgnoreCase(header))
                return entry.getValue();
        }
        return null;
    }

    /**
     * 
     * @param msg
     * @param reason
     */
    private void generateFault(SOAPMessage msg, String reason) {
        try {
            SOAPBody body = msg.getSOAPBody();
            SOAPFault fault = body.addFault();
            fault.setFaultString(reason);
            throw new SOAPFaultException(fault);
        } catch (SOAPException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
}