Package br.net.woodstock.rockframework.web.ws

Source Code of br.net.woodstock.rockframework.web.ws.WSSecurityUsernameTokenHandler

/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.web.ws;

import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
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 br.net.woodstock.rockframework.security.digest.DigestType;
import br.net.woodstock.rockframework.security.digest.impl.AsStringDigester;
import br.net.woodstock.rockframework.security.digest.impl.Base64Digester;
import br.net.woodstock.rockframework.security.digest.impl.BasicDigester;
import br.net.woodstock.rockframework.web.config.WebLog;

public class WSSecurityUsernameTokenHandler implements SOAPHandler<SOAPMessageContext> {

  private static final AsStringDigester  DIGESTER  = new AsStringDigester(new Base64Digester(new BasicDigester(DigestType.SHA1)));

  private String              username;

  private String              password;

  private boolean              digest;

  public WSSecurityUsernameTokenHandler(final String username, final String password, final boolean digest) {
    super();
    this.username = username;
    this.password = password;
    this.digest = digest;
  }

  @Override
  public boolean handleMessage(final SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outboundProperty.booleanValue()) {
      try {
        SOAPMessage message = smc.getMessage();
        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        SOAPHeader header = envelope.getHeader();

        if (header == null) {
          header = envelope.addHeader();
        }

        SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");

        SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
        usernameToken.addAttribute(new QName("wsu"), "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");

        SOAPElement username = usernameToken.addChildElement("Username", "wsse");
        username.addTextNode(this.username);

        SOAPElement password = usernameToken.addChildElement("Password", "wsse");

        if (this.digest) {
          password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
          password.addTextNode(WSSecurityUsernameTokenHandler.DIGESTER.digestAsString(this.password));
        } else {
          password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText");
          password.addTextNode(this.password);
        }
      } catch (SOAPException e) {
        WebLog.getInstance().getLogger().warn(e.getMessage(), e);
      }
    }

    return outboundProperty.booleanValue();

  }

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

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

  @Override
  public void close(final MessageContext context) {
    //
  }

}
TOP

Related Classes of br.net.woodstock.rockframework.web.ws.WSSecurityUsernameTokenHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.