Example usage for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED

List of usage examples for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED.

Prototype

int SC_UNAUTHORIZED

To view the source code for org.apache.commons.httpclient HttpStatus SC_UNAUTHORIZED.

Click Source Link

Document

<tt>401 Unauthorized</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:com.cloud.network.bigswitch.BigSwitchVnsApi.java

protected void executeMethod(HttpMethodBase method) throws BigSwitchVnsApiException {
    try {/*  w  w w.  j a  v a2s  . c om*/
        _client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            method.releaseConnection();
            // login and try again
            login();
            _client.executeMethod(method);
        }
    } catch (HttpException e) {
        s_logger.error("HttpException caught while trying to connect to the BigSwitch Controller", e);
        method.releaseConnection();
        throw new BigSwitchVnsApiException("API call to BigSwitch Controller Failed", e);
    } catch (IOException e) {
        s_logger.error("IOException caught while trying to connect to the BigSwitch Controller", e);
        method.releaseConnection();
        throw new BigSwitchVnsApiException("API call to BigSwitch Controller Failed", e);
    }
}

From source file:com.zimbra.qa.unittest.TestCookieReuse.java

/**
 * Verify that we canNOT RE-use the cookie for REST session after logging out of plain HTML client
 * @throws URISyntaxException/*from  ww  w .  jav  a 2s.co m*/
 * @throws InterruptedException
 */
@Test
public void testWebLogOut() throws ServiceException, IOException, URISyntaxException, InterruptedException {
    //establish legitimate connection
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraForceClearCookies, "FALSE");
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    URI uri = mbox.getRestURI("Inbox?fmt=rss");
    HttpClient alice = mbox.getHttpClient(uri);

    //create evesdropper's connection
    HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    Cookie[] cookies = alice.getState().getCookies();
    HttpState state = new HttpState();
    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie = cookies[i];
        state.addCookie(new Cookie(uri.getHost(), cookie.getName(), cookie.getValue(), "/", null, false));
    }
    eve.setState(state);
    Account a = TestUtil.getAccount(USER_NAME);
    a.setForceClearCookies(false);
    URI logoutUri = new URI(String.format("%s://%s%s/?loginOp=logout", uri.getScheme(), uri.getHost(),
            (uri.getPort() > 80 ? (":" + uri.getPort()) : "")));
    GetMethod logoutMethod = new GetMethod(logoutUri.toString());
    int statusCode = alice.executeMethod(logoutMethod);
    Assert.assertEquals("Log out request should succeed. Getting status code " + statusCode, HttpStatus.SC_OK,
            statusCode);
    GetMethod get = new GetMethod(uri.toString());
    statusCode = HttpClientUtil.executeMethod(eve, get);
    Assert.assertEquals("This request should not succeed. Getting status code " + statusCode,
            HttpStatus.SC_UNAUTHORIZED, statusCode);
}

From source file:com.mirth.connect.connectors.ws.WebServiceMessageDispatcher.java

/**
 * Returns the URL for the passed in String. If the URL requires
 * authentication, then the WSDL is saved as a temp file and the URL for
 * that file is returned.//from  www .java2s .c  o m
 * 
 * @param wsdlUrl
 * @param username
 * @param password
 * @return
 * @throws Exception
 */
private URL getWsdlUrl(String wsdlUrl, String username, String password) throws Exception {
    URI uri = new URI(wsdlUrl);

    // If the URL points to file, just return it
    if (!uri.getScheme().equalsIgnoreCase("file")) {
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(wsdlUrl);

        int status = client.executeMethod(method);
        if ((status == HttpStatus.SC_UNAUTHORIZED) && (username != null) && (password != null)) {
            client.getState().setCredentials(AuthScope.ANY,
                    new UsernamePasswordCredentials(username, password));
            status = client.executeMethod(method);

            if (status == HttpStatus.SC_OK) {
                String wsdl = method.getResponseBodyAsString();
                File tempFile = File.createTempFile("WebServiceSender", ".wsdl");
                tempFile.deleteOnExit();

                FileUtils.writeStringToFile(tempFile, wsdl);

                return tempFile.toURI().toURL();
            }
        }
    }

    return uri.toURL();
}

From source file:com.idega.slide.business.FileSystemCopyServiceBean.java

private void handleException(Exception ex) {
    if (ex instanceof HttpException) {
        if (((HttpException) ex).getReasonCode() == HttpStatus.SC_METHOD_NOT_ALLOWED) {
            System.out.println("Warning: Not WebDAV-enabled?");
        } else if (((HttpException) ex).getReasonCode() == HttpStatus.SC_UNAUTHORIZED) {
            System.out.println("Warning: Unauthorized");
        } else {//ww w.  j av a2  s.  c  om
            System.out.println("Warning: " + ex.getMessage());
        }
    } else if (ex instanceof IOException) {
        System.out.println("Error: " + ex.getMessage());
    } else {
        System.out.println("Fatal Error: " + ex.getMessage());
        ex.printStackTrace(System.out);
        System.out.println("Please, email to slide-user@jakarta.apache.org");

    }
}

From source file:com.zimbra.qa.unittest.TestCalDav.java

@Test
public void testBadBasicAuth() throws Exception {
    Account dav1 = users[1].create();/*from w w  w  .  ja va 2s.  co m*/
    String calFolderUrl = getFolderUrl(dav1, "Calendar").replaceAll("@", "%40");
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(calFolderUrl);
    addBasicAuthHeaderForUser(method, dav1, "badPassword");
    HttpMethodExecutor.execute(client, method, HttpStatus.SC_UNAUTHORIZED);
}

From source file:davmail.exchange.ExchangeSession.java

/**
 * Test authentication mode : form based or basic.
 *
 * @param url        exchange base URL/*from   w w w  .j a v  a2  s  . com*/
 * @param httpClient httpClient instance
 * @return true if basic authentication detected
 */
protected boolean isBasicAuthentication(HttpClient httpClient, String url) {
    return DavGatewayHttpClientFacade.getHttpStatus(httpClient, url) == HttpStatus.SC_UNAUTHORIZED;
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Execute method with httpClient, do not follow 30x redirects.
 *
 * @param httpClient Http client instance
 * @param method     Http method//from w  w w  .  j a v  a  2s  .co m
 * @return status
 * @throws IOException on error
 */
public static int executeNoRedirect(HttpClient httpClient, HttpMethod method) throws IOException {
    int status;
    try {
        status = httpClient.executeMethod(method);
        // check NTLM
        if ((status == HttpStatus.SC_UNAUTHORIZED || status == HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED)
                && acceptsNTLMOnly(method) && !hasNTLM(httpClient)) {
            LOGGER.debug("Received " + status + " unauthorized at " + method.getURI() + ", retrying with NTLM");
            resetMethod(method);
            addNTLM(httpClient);
            status = httpClient.executeMethod(method);
        }
    } finally {
        method.releaseConnection();
    }
    // caller will need to release connection
    return status;
}

From source file:com.cloud.network.bigswitch.BigSwitchBcfApi.java

protected void executeMethod(final HttpMethodBase method) throws BigSwitchBcfApiException {
    try {//from   w  w  w .jav a 2s.  com
        _client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            method.releaseConnection();
        }
    } catch (HttpException e) {
        S_LOGGER.error("HttpException caught while trying to connect to the BigSwitch Controller", e);
        method.releaseConnection();
        throw new BigSwitchBcfApiException("API call to BigSwitch Controller Failed", e);
    } catch (IOException e) {
        S_LOGGER.error("IOException caught while trying to connect to the BigSwitch Controller", e);
        method.releaseConnection();
        throw new BigSwitchBcfApiException("API call to BigSwitch Controller Failed", e);
    }
}

From source file:com.zimbra.qa.unittest.TestCookieReuse.java

/**
 * Verify that we CANNOT make an unauthorized admin GET request without an admin cookie
 */// w  w  w .j a  va2 s . c o m
@Test
public void testGetWithoutAdminCookie() throws Exception {
    int port = 7071;
    try {
        port = Provisioning.getInstance().getLocalServer().getIntAttr(Provisioning.A_zimbraAdminPort, 0);
    } catch (ServiceException e) {
        ZimbraLog.test.error("Unable to get admin SOAP port", e);
    }
    String getServerConfigURL = "https://localhost:" + port + "/service/collectconfig/?host="
            + Provisioning.getInstance().getLocalServer().getName();
    HttpClient eve = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    GetMethod get = new GetMethod(getServerConfigURL);
    int statusCode = HttpClientUtil.executeMethod(eve, get);
    Assert.assertEquals("This request should NOT succeed. Getting status code " + statusCode,
            HttpStatus.SC_UNAUTHORIZED, statusCode);
}

From source file:com.cloud.network.brocade.BrocadeVcsApi.java

protected HttpResponse executeMethod(HttpRequestBase method) throws BrocadeVcsApiException {
    HttpResponse response = null;/*from ww w .j  a v  a2 s.co m*/
    try {
        response = _client.execute(method);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
            method.releaseConnection();
            response = _client.execute(method);
        }
    } catch (HttpException e) {
        s_logger.error("HttpException caught while trying to connect to the Brocade Switch", e);
        method.releaseConnection();
        throw new BrocadeVcsApiException("API call to Brocade Switch Failed", e);
    } catch (IOException e) {
        s_logger.error("IOException caught while trying to connect to the Brocade Switch", e);
        method.releaseConnection();
        throw new BrocadeVcsApiException("API call to Brocade Switch Failed", e);
    }

    return response;
}