Example usage for javax.management AttributeNotFoundException toString

List of usage examples for javax.management AttributeNotFoundException toString

Introduction

In this page you can find the example usage for javax.management AttributeNotFoundException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:uk.ac.diamond.cas.shibboleth.authentication.handler.ShibbolethAuthenticationHandler.java

@Override
protected final Principal authenticateUsernamePasswordInternal(final String username, final String password)
        throws GeneralSecurityException, PreventedException {

    logger.debug("Attempting to authenticate {} at {}", username, IdP);

    try {/*from   ww  w .  j  a  va2s  . co m*/
        // Initialise the library
        DefaultBootstrap.bootstrap();
        final BasicParserPool parserPool = new BasicParserPool();
        parserPool.setNamespaceAware(true);

        // Set proxy
        HttpHost proxy = null;
        logger.debug("Setting proxy");
        if ((this.proxyHost != null) && (!this.proxyHost.isEmpty())) {
            if (this.proxyPort == 0) {
                proxy = new HttpHost(this.proxyHost, 8080);
            } else {
                proxy = new HttpHost(this.proxyHost, this.proxyPort);
            }
        }
        logger.debug("Set proxy successfully");

        // Instantiate a copy of the client, try to authentication, catch any errors that occur
        ShibbolethECPAuthClient ecpClient = new ShibbolethECPAuthClient(proxy, this.IdP, this.SP,
                disableCertCheck);
        Response response = ecpClient.authenticate(username, password);
        logger.debug("Successfully authenticated {}", username);

        // if the attribute is empty, we simply authenticate and return the username as principal
        if ((this.attribute == null) || (this.attribute.isEmpty())) {
            return new SimplePrincipal(username);
        }

        // get the first assertion in the response. Any exceptions here are a problem
        List<Attribute> attributes = response.getAssertions().get(0)
                // get the first (and should be only) attribute statement
                .getAttributeStatements().get(0)
                // get all attributes
                .getAttributes();

        // if there are no attributes, we can't do a lookup.
        if (attributes.isEmpty()) {
            throw new AttributeNotFoundException("The Shibboleth Identity Provider at " + this.IdP
                    + " returned a SAML assertion with no attributes");
        }

        // trawl the attributes to check if we can find ours
        String lookupAttributeValue = null;
        boolean idFound = false;
        for (Attribute attribute : attributes) {
            if ((attribute.getName().equals(this.attribute))
                    || (attribute.getFriendlyName().equals(this.attribute))) {
                idFound = true;
                XMLObject attributeValue = attribute.getAttributeValues().get(0);
                if (attributeValue instanceof XSString) {
                    lookupAttributeValue = ((XSString) attributeValue).getValue();
                } else if (attributeValue instanceof XSAny) {
                    lookupAttributeValue = ((XSAny) attributeValue).getTextContent();
                }
                logger.debug("Attribute: " + this.attribute + ", value: " + lookupAttributeValue);
                break;
            } // if getName()...
        } // for attribute...

        // Attribute was not found in the SAML statement
        if (!idFound) {
            throw new AttributeNotFoundException("The attribute " + this.attribute
                    + " was not returned by the Shibboleth Identity Provider.");
        }

        logger.info("Authentication was successful. Credential {} mapped to {}", username,
                lookupAttributeValue);
        return new SimplePrincipal(lookupAttributeValue);

    } catch (final AttributeNotFoundException e) {
        logger.debug("AttributeNotFoundException raised: {}", e.toString());
        throw new FailedLoginException(e.toString());
    } catch (final AuthenticationException e) {
        logger.debug("AuthenticationException raised: {}", e.toString());
        throw new FailedLoginException(e.toString());
    } catch (final IOException e) {
        logger.debug("IOException raised: {}", e.toString());
        throw new PreventedException(e);
    } catch (final Exception e) {
        logger.debug("Exception raised: {}", e.toString());
        throw new PreventedException(e);
    }
}