Example usage for org.apache.commons.httpclient URIException getReasonCode

List of usage examples for org.apache.commons.httpclient URIException getReasonCode

Introduction

In this page you can find the example usage for org.apache.commons.httpclient URIException getReasonCode.

Prototype

public int getReasonCode() 

Source Link

Document

Get the reason code.

Usage

From source file:de.juwimm.cms.http.HttpClientWrapper.java

/**
 * tries first with no auth toherwise first with non-ntlm proxy if still
 * http-error-code 407 with ntlm-proxy/*from w  ww .  j  a  va  2 s . c  o  m*/
 * 
 * @param testURL
 * @param userName
 *                for testURL authentication NOT for proxy
 * @param password
 *                for testURL authentication NOT for proxy
 * @throws HttpException
 */
private void testAndConfigureConnectionTryInvoke(URL testURL, String userName, String password)
        throws HttpException {
    HttpMethodBase method = null;
    try {
        /*
         * At first Im trying here with default settings, so no proxy auth,
         * proxy if given and or proxy auth with username and password auth.
         */
        method = invoke(testURL, userName, password);
    } catch (URIException exe) {
        if (exe.getReasonCode() == 407) {
            log.info("Proxy needs authorization to be configured");
            if (this.getHttpProxyUser() != null) {
                try {
                    /*
                     * Now I want to try the proxy with NTLM
                     * authentification. I can not figure out if I can use
                     * NTLM, so we're trying it.
                     */
                    this.setUseNTproxy(true);
                    method = invoke(testURL, null, null);
                } catch (URIException exe2) {
                    if (exe2.getReasonCode() == 407) {
                        /*
                         * Something went wrong - in general username /
                         * password pair does not match - in this case this
                         * could also be a wrong NT-DOMAIN.
                         */
                        this.setUseNTproxy(false);
                        throw exe2;
                    }
                    throw new HttpException(HttpMessages.getString("HttpClientWrapper.testConnectionFailed",
                            testURL.getHost(), exe2.getMessage()));
                }
            } else {
                log.info("...but first you have to enter one");
                this.setUseNTproxy(false);
                throw exe;
            }
        } else {
            throw new HttpException(HttpMessages.getString("HttpClientWrapper.testConnectionFailed",
                    testURL.getHost(), exe.getMessage()));
        }
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:de.juwimm.cms.http.HttpClientWrapper.java

/**
 * /*from w w w  .  j a v  a 2 s  .  c  om*/
 * @param testUrlString
 *                destination
 * @param userName
 *                for authentication at testUrlString NOT for proxy
 * @param password
 *                for authentication at testUrlString NOT for proxy
 * @throws HttpException
 */
public void testAndConfigureConnection(String testUrlString, String userName, String password)
        throws HttpException {
    URL testURL = null;
    try {
        testURL = new URL(testUrlString);
    } catch (MalformedURLException exe1) {
        throw new HttpException(
                HttpMessages.getString("HttpClientWrapper.testConnectionFailed", testUrlString, "\n"));
    }

    DlgUsernamePassword dlg = new DlgUsernamePassword();
    if ((getHttpProxyUser() == null || "".equalsIgnoreCase(getHttpProxyUser()))
            && getHttpProxyPassword() == null || "".equalsIgnoreCase(getHttpProxyPassword())) {
        dlg.getTxtUsername().setText(System.getProperty("user.name"));
    } else {
        dlg.getTxtUsername().setText(getHttpProxyUser());
    }
    dlg.getTxtPassword().setText(getHttpProxyPassword());
    // dlg.getTxtNTDomain().setText(httpProxyNTDomain);

    // try no auth, base auth, ntlm auth with user giving username and
    // password until successful
    while (true) {
        try {

            testAndConfigureConnectionTryInvoke(testURL, userName, password);
            // save password only if connect successful
            saveProperties(dlg.getCboSave().isSelected());
            break;
        } catch (URIException exe) {
            // http-Error-Code: 407 = Proxy Authentication Required
            if (exe.getReasonCode() == 407) {
                // ask user for user and password
                dlg.setVisible(true);
                if (dlg.isCanceled()) {
                    throw new HttpException(
                            HttpMessages.getString("HttpClientWrapper.noProxyWhereNeededExeption"));
                }
                setHttpProxyUser(dlg.getTxtUsername().getText());
                setHttpProxyPassword(String.copyValueOf(dlg.getTxtPassword().getPassword()));
                // httpProxyNTDomain = dlg.getTxtNTDomain().getText();
            } else {
                throw new HttpException(HttpMessages.getString("HttpClientWrapper.testConnectionFailed",
                        testURL.getHost(), exe.getMessage()));
            }
        }
    }
    log.debug("finished test");
}

From source file:com.cyberway.issue.crawler.framework.CrawlController.java

/**
 * Log a URIException from deep inside other components to the crawl's
 * shared log. //from   ww  w.ja v  a 2  s .c o  m
 * 
 * @param e URIException encountered
 * @param u CrawlURI where problem occurred
 * @param l String which could not be interpreted as URI without exception
 */
public void logUriError(URIException e, UURI u, CharSequence l) {
    if (e.getReasonCode() == UURIFactory.IGNORED_SCHEME) {
        // don't log those that are intentionally ignored
        return;
    }
    Object[] array = { u, l };
    uriErrors.log(Level.INFO, e.getMessage(), array);
}