Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2008, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * 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, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.picketlink.trust.jbossws.handler;
 
 import java.util.Map;
 
 

Handler that looks for a binary token data that exists in jaasOptionsMap supplied in constructor.

Configuration:

System Properties:

  • map.token.key: key which will be used to fetch binary token from the jaasOptionsMap. Default value is ClientID
  • map.token.validation.class.key: validation class for binary token inside handleInbound method
  • binary.http.encodingType: attribute value of the EncodingType attribute
  • binary.http.valueType: attribute value of the ValueType attribute
  • binary.http.valueType.namespace: namespace for the ValueType attribute
  • binary.http.valueType.prefix: namespace for the ValueType attribute
Setters:

Please see the see also section.

 
 
     public static final String SYS_PROP_TOKEN_KEY = "map.token.key";
     public static final String DEFAULT_TOKEN_KEY = "ClientID";
 
     private boolean trace = .isTraceEnabled();
    
    
The JAAS shared options map key name for binary token to be stored in.
 
     public final String tokenOptionKey = SecurityActions.getSystemProperty(
             "ClientID");

    
Key in the JAAS options map to find class name to validate token in inbound message handle method.
 
     public final String validationTokenClassKey = SecurityActions
             .getSystemProperty("map.token.validation.class.key",
                     "tokenValidationClass");

    
Attribute value for the EncodingType attribute
 
     private String encodingType = SecurityActions
             .getSystemProperty(
                     "binary.http.encodingType",
                     "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary");

    
Attribute value for the ValueType attribute
    private String valueType = SecurityActions.getSystemProperty(
            "binary.http.valueType"null);

    
Namespace for the ValueType. Can be null. If null, then a separate namespace is not added.
    private String valueTypeNamespace = SecurityActions.getSystemProperty(
            "binary.http.valueType.namespace"null);

    
Prefix for the ValueType. Can be null.
    private String valueTypePrefix = SecurityActions.getSystemProperty(
            "binary.http.valueType.prefix"null);
    private SOAPFactory factory = null;

    
Shared options from calling login module (@see

JBWSTokenIssuingLoginModule:
.
    private Map<String, ?> jaasLoginModuleOptions = null;
    public MapBasedTokenHandler(Map<String, ?> jaasOptionsMap) {
         = jaasOptionsMap;
    }

    

Set the EncodingType value.

Alternatively, set the system property "binary.http.encodingType"

Parameters:
binaryEncodingType
    public void setEncodingType(String binaryEncodingType) {
        this. = binaryEncodingType;
    }

    

Set the Value type

Alternatively, set the system property "binary.http.valueType"

Parameters:
binaryValueType
    public void setValueType(String binaryValueType) {
        this. = binaryValueType;
    }

    

Set the ValueType Namespace

Alternatively, set the system property "binary.http.valueType.namespace"

Parameters:
binaryValueNamespace
    public void setValueTypeNamespace(String binaryValueNamespace) {
        this. = binaryValueNamespace;
    }

    

Set the Value Type Prefix

Alternatively, set the system property "binary.http.valueType.prefix"

Parameters:
binaryValuePrefix
    public void setValueTypePrefix(String binaryValuePrefix) {
        this. = binaryValuePrefix;
    }
    @Override
    protected boolean handleInbound(MessageContext msgContext) {
        if () {
            .trace("Handling Inbound Message");
        }
        String tokenValidation = (String
                .get();
        if (tokenValidation == null) {
            return true;
        }
        BinaryTokenValidation validation = null;
        try {
            ClassLoader cl = SecurityActions.getClassLoader(getClass());
            validation = (BinaryTokenValidationcl.loadClass(tokenValidation)
                    .newInstance();
        } catch (Exception e) {
            throw new RuntimeException(.
                    + "Class not loaded:" + tokenValidatione);
        }
        String token = getToken(msgContext);
        if () {
            .trace("Validating token=" + token);
        }
        return validation.validateBinaryToken(tokenmsgContext);
    }
    @Override
    protected boolean handleOutbound(MessageContext msgContext) {
        if () {
            .trace("Handling Outbound Message");
        }
        String token = (String.get();
        if (token == null)
            throw new RuntimeException(.
                    + 
                    + " has to be set by calling LoginMoule in option map.");
        SOAPElement security = null;
        try {
            security = create(token);
        } catch (SOAPException e) {
            .jbossWSUnableToCreateBinaryToken(e);
        }
        if (security == null) {
            return true;
        }
        SOAPMessage sm = ((SOAPMessageContextmsgContext).getMessage();
        SOAPEnvelope envelope;
        try {
            envelope = sm.getSOAPPart().getEnvelope();
            SOAPHeader header = (SOAPHeader) Util.findElement(envelope,
                    new QName(envelope.getNamespaceURI(), "Header"));
            if (header == null) {
                header = (SOAPHeaderenvelope.getOwnerDocument()
                        .createElementNS(envelope.getNamespaceURI(),
                                envelope.getPrefix() + ":Header");
                envelope.insertBefore(headerenvelope.getFirstChild());
            }
            header.addChildElement(security);
        } catch (SOAPException e) {
            .jbossWSUnableToCreateBinaryToken(e);
        }
        if () {
            .trace("SOAP Message=" + SOAPUtil.soapMessageAsString(sm));
        }
        return true;
    }

    
Given a binary token, create a javax.xml.soap.SOAPElement

Parameters:
token
Returns:
Throws:
javax.xml.soap.SOAPException
    private SOAPElement create(String tokenthrows SOAPException {
        if ( == null)
             = SOAPFactory.newInstance();
                ..);
        if ( != null) {
            security.addNamespaceDeclaration(,
                    );
        }
        SOAPElement binarySecurityToken = .createElement(
                .);
        binarySecurityToken.addTextNode(token);
        if ( != null && !.isEmpty()) {
            binarySecurityToken.setAttribute(.,
                    );
        }
        if ( != null) {
            binarySecurityToken.setAttribute(.,
                    );
        }
        security.addChildElement(binarySecurityToken);
        return security;
    }
    private String getToken(MessageContext msgContext) {
        SOAPMessage sm = ((SOAPMessageContextmsgContext).getMessage();
        SOAPEnvelope envelope;
        try {
            envelope = sm.getSOAPPart().getEnvelope();
            SOAPHeader header = (SOAPHeader) Util.findElement(envelope,
                    new QName(envelope.getNamespaceURI(), "Header"));
            if (header == null) {
                header = (SOAPHeaderenvelope.getOwnerDocument()
                        .createElementNS(envelope.getNamespaceURI(),
                                envelope.getPrefix() + ":Header");
            }
            return Util.findElementByWsuId(header"BinarySecurityToken")
                    .getTextContent();
        } catch (SOAPException e) {
            .jbossWSUnableToCreateBinaryToken(e);
            return null;
        }
    }
New to GrepCode? Check out our FAQ X