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.List;
 
 import  javax.security.jacc.PolicyContext;
 import  javax.security.jacc.PolicyContextException;
 import  javax.servlet.http.Cookie;
 import  javax.servlet.http.HttpServletRequest;
 

Handler that looks for a binary data that exists in the HTTP payload either as a header or a cookie based on configuration.

Configuration:

System Properties:

  • binary.http.header: http header name. Can be a comma separated set of names
  • binary.http.cookie: http cookie name
  • 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
  • binary.http.cleanToken: true or false dependending on whether the binary token has to be cleaned
Setters:

Please see the see also section.

Author(s):
Anil.Saldhana@redhat.com
Since:
Apr 5, 2011
See also:
setHttpHeaderName(String)
setHttpCookieName(String)
setEncodingType(String)
setValueType(String)
setValueTypeNamespace(String)
setValueTypePrefix(String)
setCleanToken(boolean)
 
 public class BinaryTokenHandler extends AbstractPicketLinkTrustHandler {
    
The HTTP header name that this token looks for. Either this or the httpCookieName should be set.
 
     private String httpHeaderName = SecurityActions.getSystemProperty("binary.http.header"null);

    
The HTTP cookie name that this token looks for. Either this or the httpHeaderName should be set.
 
     private String httpCookieName = SecurityActions.getSystemProperty("binary.http.cookie"null);

    
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);

    
Some binary tokens need to be cleaned. This handler just cleans upto the first blank space and discards before that.
    private boolean cleanToken = Boolean.parseBoolean(SecurityActions.getSystemProperty("binary.http.cleanToken""false"));
    private SOAPFactory factory = null;

    

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;
    }

    

Set the Http Header Name

Alternatively, set the system property: "binary.http.header"

Parameters:
http
    public void setHttpHeaderName(String http) {
         = http;
    }

    

Set the Http Cookie Name

Alternatively, set the system property: ""binary.http.cookie"

Parameters:
http
    public void setHttpCookieName(String http) {
         = http;
    }

    

Should we not clean the extracted binary token.

Alternatively, set the system property: "binary.http.cleanToken"

Parameters:
clean
    public void setCleanToken(boolean clean) {
        this. = clean;
    }
    @Override
    protected boolean handleOutbound(MessageContext msgContext) {
        .trace("Handling Outbound Message");
        if ( == null &&  == null)
            throw .injectedValueMissing("Either httpHeaderName or httpCookieName should be set");
        HttpServletRequest servletRequest = getHttpRequest(msgContext);
        if (servletRequest == null)
            throw .nullValueError("Http request");
        String token = getTokenValue(servletRequest);
        if (token == null)
            throw .nullValueError("Null Token");
        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(envelopenew 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 (.isTraceEnabled()) {
            .trace("SOAP Message=" + SOAPUtil.soapMessageAsString(sm));            
        }
        return true;
    }

    
Get the HttpServletRequest from the MessageContext

Parameters:
msgContext
Returns:
    private HttpServletRequest getHttpRequest(MessageContext msgContext) {
        HttpServletRequest request = (HttpServletRequest) msgContext.get(.);
        if (request == null) {
            try {
                request = (HttpServletRequest) PolicyContext.getContext("javax.servlet.http.HttpServletRequest");
            } catch (PolicyContextException e) {
                throw new RuntimeException(e);
            }
        }
        return request;
    }

    
Given the HttpServletRequest, look for the http header or the cookie depending on the configuration

Parameters:
http
Returns:
    private String getTokenValue(HttpServletRequest http) {
        if ( != null && !.isEmpty()) {
            // Sometime the http header name can be a comma separated list of names
            if (.contains(",")) {
                List<Stringheaders = getHeaderNames();
                StringBuilder builder = new StringBuilder();
                for (String header : headers) {
                    String value = getTokenValue(httpheader);
                    if (value != null)
                        builder.append(value);
                }
                String headerValue = builder.toString();
                .trace("Header value has been identified " + headerValue);
                
                return clean(headerValue);
            } else {
                String header = http.getHeader();
                if (header != null) {
                    .trace("Header value has been identified " + header);
                    return clean(header);
                }
            }
        }
        if ( != null && !.isEmpty()) {
            Cookie[] cookies = http.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals()) {
                        .trace("Cookie value has been identified " + cookie.getValue());
                        return clean(cookie.getValue());
                    }
                }
            }
        }
        return null;
    }
    private String getTokenValue(HttpServletRequest httpString header) {
        String headerValue = http.getHeader(header);
        if (headerValue != null && !headerValue.isEmpty()) {
            return clean(headerValue);
        }
        return null;
    }

    
Given a binary token, create a SOAPElement

Parameters:
token
Returns:
Throws:
SOAPException
    private SOAPElement create(String tokenthrows SOAPException {
        if ( == null)
             = SOAPFactory.newInstance();
        if ( != null) {
        }
                .);
        binarySecurityToken.addTextNode(token);
        if ( != null && !.isEmpty()) {
            binarySecurityToken.setAttribute(.);
        }
        if ( != null) {
            binarySecurityToken.setAttribute(.);
        }
        security.addChildElement(binarySecurityToken);
        return security;
    }

    
Some 3rd party systems send in the binary token in the format Discardable<space>ValidToken

Parameters:
value
Returns:
    private String clean(String value) {
        int i = -1;
        if () {
            value = value.trim();
            while ((i = value.indexOf(' ')) != -1) {
                value = value.substring(i + 1);
            }
        }
        return value;
    }
    private List<StringgetHeaderNames(String str) {
        List<Stringlist = new ArrayList<String>();
        if (StringUtil.isNotNull(str)) {
            list.addAll(StringUtil.tokenize(str));
        }
        return list;
    }
New to GrepCode? Check out our FAQ X