Example usage for org.apache.commons.httpclient.auth AuthScheme getRealm

List of usage examples for org.apache.commons.httpclient.auth AuthScheme getRealm

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.auth AuthScheme getRealm.

Prototype

public abstract String getRealm();

Source Link

Usage

From source file:com.gargoylesoftware.htmlunit.DefaultCredentialsProvider.java

/**
 * @param scheme the request scheme for which Credentials are asked
 * @param scope the configured authorization scope
 * @return <code>true</code> if the scope's realm matches the one of the scheme
 *///from  w  ww  .j a v  a2s .  c om
protected boolean matchRealm(final AuthScope scope, final AuthScheme scheme) {
    return scope.getRealm() == AuthScope.ANY_REALM || scope.getRealm().equals(scheme.getRealm());
}

From source file:com.gargoylesoftware.htmlunit.DefaultCredentialsProvider.java

/**
 * Builds a key with the specified data.
 * @param scheme the scheme//w  w w .  j a va  2 s  .  c o  m
 * @param host the server name
 * @param port the server port
 * @param proxy is proxy
 * @return the new key
 */
protected Object buildKey(final AuthScheme scheme, final String host, final int port, final boolean proxy) {
    return scheme.getSchemeName() + " " + scheme.getRealm() + " " + host + ":" + port + " " + proxy;
}

From source file:com.alibaba.antx.config.resource.http.HttpSession.java

public HttpSession(ResourceDriver driver) {
    super(driver);

    client = new HttpClient();

    client.getParams().setAuthenticationPreemptive(true);
    client.getParams().setParameter(CredentialsProvider.PROVIDER, new CredentialsProvider() {
        public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy)
                throws CredentialsNotAvailableException {
            URI uri = ResourceContext.get().getCurrentURI();
            String username = ResourceContext.get().getCurrentUsername();
            Set visitedURIs = ResourceContext.get().getVisitedURIs();
            ResourceKey key = new ResourceKey(new ResourceURI(uri));
            String message;//w w w.j a  v a2 s  .  co m

            message = "\n";
            message += "Authentication required.\n";
            message += "realm: " + scheme.getRealm() + "\n";
            message += "  uri: " + uri + "\n";

            UsernamePassword up = getResourceManager().getAuthenticationHandler().authenticate(message, uri,
                    username, visitedURIs.contains(key));

            visitedURIs.add(key);

            return new UsernamePasswordCredentials(up.getUsername(), up.getPassword());
        }
    });
}

From source file:com.globalsight.everest.webapp.applet.util.AuthenticationPrompter.java

/**
 * Returns the appropriate credentials for authentication. This method
 * may be called many times, but the username,password entered the first
 * time is re-used so the user is not prompted multiple times if the connection
 * isn't kept alive./* w w w. j  av  a  2s.  c om*/
 * 
 * @param authscheme
 * @param host
 * @param port
 * @param proxy
 * @return Credentials
 * @exception CredentialsNotAvailableException
 */
public Credentials getCredentials(final AuthScheme authscheme, final String host, int port, boolean proxy)
        throws CredentialsNotAvailableException {
    if (authscheme == null) {
        System.out.println("----No Proxy Authentication Required.");
        return null;
    }
    try {
        if (authscheme instanceof NTLMScheme) {
            System.out.println("----" + host + ":" + port + " requires Windows authentication");
            if (m_askForAuth)
                askForAuthentication();
            return new NTCredentials(m_username, m_password, host, m_domain);
        } else if (authscheme instanceof RFC2617Scheme) {
            System.out.println("----" + host + ":" + port + " requires authentication with the realm '"
                    + authscheme.getRealm() + "'");
            if (m_askForAuth)
                askForAuthentication();
            return new UsernamePasswordCredentials(m_username, m_password);
        } else {
            throw new CredentialsNotAvailableException(
                    "Unsupported authentication scheme: " + authscheme.getSchemeName());
        }
    } catch (Exception e) {
        throw new CredentialsNotAvailableException(e.getMessage(), e);
    }
}

From source file:com.eviware.soapui.impl.wsdl.submit.filters.WsdlRequestCredentialsProvider.java

public Credentials getCredentials(final AuthScheme authscheme, final String host, int port, boolean proxy)
        throws CredentialsNotAvailableException {
    if (checkedCredentials)
        throw new CredentialsNotAvailableException("Missing valid credentials");

    if (authscheme == null) {
        return null;
    }/*from w w  w. ja v  a  2  s . c om*/
    try {
        String password = wsdlRequest.getPassword();
        if (password == null)
            password = "";

        if (authscheme instanceof NTLMScheme) {
            logger.info(host + ":" + port + " requires Windows authentication");
            return new NTCredentials(wsdlRequest.getUsername(), password, host, wsdlRequest.getDomain());
        } else if (authscheme instanceof RFC2617Scheme) {
            logger.info(host + ":" + port + " requires authentication with the realm '" + authscheme.getRealm()
                    + "'");
            return new UsernamePasswordCredentials(wsdlRequest.getUsername(), password);
        } else {
            throw new CredentialsNotAvailableException(
                    "Unsupported authentication scheme: " + authscheme.getSchemeName());
        }
    } catch (IOException e) {
        throw new CredentialsNotAvailableException(e.getMessage(), e);
    } finally {
        checkedCredentials = true;
    }
}

From source file:com.sun.jersey.client.apache.config.DefaultCredentialsProvider.java

public Credentials getCredentials(AuthScheme scheme, String host, int port, boolean proxy)
        throws CredentialsNotAvailableException {
    if (scheme == null) {
        return null;
    }//  ww w. jav  a2s.c o  m

    try {
        JTextField userField = new JTextField();
        JPasswordField passwordField = new JPasswordField();
        int response;

        if (scheme instanceof NTLMScheme) {
            JTextField domainField = new JTextField();
            Object[] msg = { host + ":" + port + " requires Windows authentication", "Domain", domainField,
                    "User Name", userField, "Password", passwordField };
            response = JOptionPane.showConfirmDialog(null, msg, "Authenticate", JOptionPane.OK_CANCEL_OPTION);

            if ((response == JOptionPane.CANCEL_OPTION) || (response == JOptionPane.CLOSED_OPTION)) {
                throw new CredentialsNotAvailableException("User cancled windows authentication.");
            }

            return new NTCredentials(userField.getText(), new String(passwordField.getPassword()), host,
                    domainField.getText());

        } else if (scheme instanceof RFC2617Scheme) {
            Object[] msg = {
                    host + ":" + port + " requires authentication with the realm '" + scheme.getRealm() + "'",
                    "User Name", userField, "Password", passwordField };

            response = JOptionPane.showConfirmDialog(null, msg, "Authenticate", JOptionPane.OK_CANCEL_OPTION);

            if ((response == JOptionPane.CANCEL_OPTION) || (response == JOptionPane.CLOSED_OPTION)) {
                throw new CredentialsNotAvailableException("User cancled windows authentication.");
            }

            return new UsernamePasswordCredentials(userField.getText(),
                    new String(passwordField.getPassword()));

        } else {

            throw new CredentialsNotAvailableException(
                    "Unsupported authentication scheme: " + scheme.getSchemeName());

        }
    } catch (IOException ioe) {

        throw new CredentialsNotAvailableException(ioe.getMessage(), ioe);

    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * Server is looking for basic/digest auth credentials (RFC2617). If we have
 * any, put them into the CrawlURI and have it come around again. Presence
 * of the credential serves as flag to frontier to requeue promptly. If we
 * already tried this domain and still got a 401, then our credentials are
 * bad. Remove them and let this curi die.
 *
 * @param method Method that got a 401./*from  ww  w .  j  a v  a2 s. c  om*/
 * @param curi CrawlURI that got a 401.
 */
protected void handle401(final HttpMethod method, final CrawlURI curi) {
    AuthScheme authscheme = getAuthScheme(method, curi);
    if (authscheme == null) {
        return;
    }
    String realm = authscheme.getRealm();

    // Look to see if this curi had rfc2617 avatars loaded.  If so, are
    // any of them for this realm?  If so, then the credential failed
    // if we got a 401 and it should be let die a natural 401 death.
    Set curiRfc2617Credentials = getCredentials(getSettingsHandler(), curi, Rfc2617Credential.class);
    Rfc2617Credential extant = Rfc2617Credential.getByRealm(curiRfc2617Credentials, realm, curi);
    if (extant != null) {
        // Then, already tried this credential.  Remove ANY rfc2617
        // credential since presence of a rfc2617 credential serves
        // as flag to frontier to requeue this curi and let the curi
        // die a natural death.
        extant.detachAll(curi);
        logger.warning("Auth failed (401) though supplied realm " + realm + " to " + curi.toString());
    } else {
        // Look see if we have a credential that corresponds to this
        // realm in credential store.  Filter by type and credential
        // domain.  If not, let this curi die. Else, add it to the
        // curi and let it come around again. Add in the AuthScheme
        // we got too.  Its needed when we go to run the Auth on
        // second time around.
        CredentialStore cs = CredentialStore.getCredentialStore(getSettingsHandler());
        if (cs == null) {
            logger.severe("No credential store for " + curi);
        } else {
            CrawlServer server = getController().getServerCache().getServerFor(curi);
            Set storeRfc2617Credentials = cs.subset(curi, Rfc2617Credential.class, server.getName());
            if (storeRfc2617Credentials == null || storeRfc2617Credentials.size() <= 0) {
                logger.info("No rfc2617 credentials for " + curi);
            } else {
                Rfc2617Credential found = Rfc2617Credential.getByRealm(storeRfc2617Credentials, realm, curi);
                if (found == null) {
                    logger.info("No rfc2617 credentials for realm " + realm + " in " + curi);
                } else {
                    found.attach(curi, authscheme.getRealm());
                    logger.info("Found credential for realm " + realm + " in store for " + curi.toString());
                }
            }
        }
    }
}

From source file:com.cyberway.issue.crawler.fetcher.FetchHTTP.java

/**
 * @param method Method that got a 401./*from  w  w  w. j a  v  a  2 s  .c  o  m*/
 * @param curi CrawlURI that got a 401.
 * @return Returns first wholesome authscheme found else null.
 */
protected AuthScheme getAuthScheme(final HttpMethod method, final CrawlURI curi) {
    Header[] headers = method.getResponseHeaders("WWW-Authenticate");
    if (headers == null || headers.length <= 0) {
        logger.info("We got a 401 but no WWW-Authenticate challenge: " + curi.toString());
        return null;
    }

    Map authschemes = null;
    try {
        authschemes = AuthChallengeParser.parseChallenges(headers);
    } catch (MalformedChallengeException e) {
        logger.info("Failed challenge parse: " + e.getMessage());
    }
    if (authschemes == null || authschemes.size() <= 0) {
        logger.info("We got a 401 and WWW-Authenticate challenge" + " but failed parse of the header "
                + curi.toString());
        return null;
    }

    AuthScheme result = null;
    // Use the first auth found.
    for (Iterator i = authschemes.keySet().iterator(); result == null && i.hasNext();) {
        String key = (String) i.next();
        String challenge = (String) authschemes.get(key);
        if (key == null || key.length() <= 0 || challenge == null || challenge.length() <= 0) {
            logger.warning("Empty scheme: " + curi.toString() + ": " + headers);
        }
        AuthScheme authscheme = null;
        if (key.equals("basic")) {
            authscheme = new BasicScheme();
        } else if (key.equals("digest")) {
            authscheme = new DigestScheme();
        } else {
            logger.info("Unsupported scheme: " + key);
            continue;
        }

        try {
            authscheme.processChallenge(challenge);
        } catch (MalformedChallengeException e) {
            logger.info(e.getMessage() + " " + curi + " " + headers);
            continue;
        }
        if (authscheme.isConnectionBased()) {
            logger.info("Connection based " + authscheme);
            continue;
        }

        if (authscheme.getRealm() == null || authscheme.getRealm().length() <= 0) {
            logger.info("Empty realm " + authscheme + " for " + curi);
            continue;
        }
        result = authscheme;
    }

    return result;
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * Server is looking for basic/digest auth credentials (RFC2617). If we have
 * any, put them into the CrawlURI and have it come around again. Presence
 * of the credential serves as flag to frontier to requeue promptly. If we
 * already tried this domain and still got a 401, then our credentials are
 * bad. Remove them and let this curi die.
 *
 * @param method Method that got a 401./*from  w w  w.j  a  v  a 2s .co m*/
 * @param curi CrawlURI that got a 401.
 */
protected void handle401(final HttpMethod method, final CrawlURI curi) {
    AuthScheme authscheme = getAuthScheme(method, curi);
    if (authscheme == null) {
        return;
    }
    String realm = authscheme.getRealm();

    // Look to see if this curi had rfc2617 avatars loaded.  If so, are
    // any of them for this realm?  If so, then the credential failed
    // if we got a 401 and it should be let die a natural 401 death.
    Set curiRfc2617Credentials = getCredentials(getSettingsHandler(), curi, Rfc2617Credential.class);
    Rfc2617Credential extant = Rfc2617Credential.getByRealm(curiRfc2617Credentials, realm, curi);
    if (extant != null) {
        // Then, already tried this credential.  Remove ANY rfc2617
        // credential since presence of a rfc2617 credential serves
        // as flag to frontier to requeue this curi and let the curi
        // die a natural death.
        extant.detachAll(curi);
        logger.warn("Auth failed (401) though supplied realm " + realm + " to " + curi.toString());
    } else {
        // Look see if we have a credential that corresponds to this
        // realm in credential store.  Filter by type and credential
        // domain.  If not, let this curi die. Else, add it to the
        // curi and let it come around again. Add in the AuthScheme
        // we got too.  Its needed when we go to run the Auth on
        // second time around.
        CredentialStore cs = CredentialStore.getCredentialStore(getSettingsHandler());
        if (cs == null) {
            logger.error("No credential store for " + curi);
        } else {
            CrawlServer server = getController().getServerCache().getServerFor(curi);
            Set storeRfc2617Credentials = cs.subset(curi, Rfc2617Credential.class, server.getName());
            if (storeRfc2617Credentials == null || storeRfc2617Credentials.size() <= 0) {
                logger.info("No rfc2617 credentials for " + curi);
            } else {
                Rfc2617Credential found = Rfc2617Credential.getByRealm(storeRfc2617Credentials, realm, curi);
                if (found == null) {
                    logger.info("No rfc2617 credentials for realm " + realm + " in " + curi);
                } else {
                    found.attach(curi, authscheme.getRealm());
                    logger.info("Found credential for realm " + realm + " in store for " + curi.toString());
                }
            }
        }
    }
}

From source file:org.archive.crawler.fetcher.OptimizeFetchHTTP.java

/**
 * @param method Method that got a 401./* ww  w.ja va  2 s  . c  o  m*/
 * @param curi CrawlURI that got a 401.
 * @return Returns first wholesome authscheme found else null.
 */
protected AuthScheme getAuthScheme(final HttpMethod method, final CrawlURI curi) {
    Header[] headers = method.getResponseHeaders("WWW-Authenticate");
    if (headers == null || headers.length <= 0) {
        logger.info("We got a 401 but no WWW-Authenticate challenge: " + curi.toString());
        return null;
    }

    Map authschemes = null;
    try {
        authschemes = AuthChallengeParser.parseChallenges(headers);
    } catch (MalformedChallengeException e) {
        logger.info("Failed challenge parse: " + e.getMessage());
    }
    if (authschemes == null || authschemes.size() <= 0) {
        logger.info("We got a 401 and WWW-Authenticate challenge" + " but failed parse of the header "
                + curi.toString());
        return null;
    }

    AuthScheme result = null;
    // Use the first auth found.
    for (Iterator i = authschemes.keySet().iterator(); result == null && i.hasNext();) {
        String key = (String) i.next();
        String challenge = (String) authschemes.get(key);
        if (key == null || key.length() <= 0 || challenge == null || challenge.length() <= 0) {
            logger.warn("Empty scheme: " + curi.toString() + ": " + headers);
        }
        AuthScheme authscheme = null;
        if (key.equals("basic")) {
            authscheme = new BasicScheme();
        } else if (key.equals("digest")) {
            authscheme = new DigestScheme();
        } else {
            logger.info("Unsupported scheme: " + key);
            continue;
        }

        try {
            authscheme.processChallenge(challenge);
        } catch (MalformedChallengeException e) {
            logger.info(e.getMessage() + " " + curi + " " + headers);
            continue;
        }
        if (authscheme.isConnectionBased()) {
            logger.info("Connection based " + authscheme);
            continue;
        }

        if (authscheme.getRealm() == null || authscheme.getRealm().length() <= 0) {
            logger.info("Empty realm " + authscheme + " for " + curi);
            continue;
        }
        result = authscheme;
    }

    return result;
}