Example usage for org.apache.commons.httpclient HttpMethod getStatusCode

List of usage examples for org.apache.commons.httpclient HttpMethod getStatusCode

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod getStatusCode.

Prototype

public abstract int getStatusCode();

Source Link

Usage

From source file:org.yccheok.jstock.gui.Utils.java

private static String _getResponseBodyAsStringBasedOnProxyAuthOption(HttpClient client, String request) {
    org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(client);
    org.yccheok.jstock.gui.Utils.setHttpClientProxyCredentialsFromJStockOptions(client);

    final HttpMethod method = new GetMethod(request);
    final JStockOptions jStockOptions = JStock.instance().getJStockOptions();
    String respond = null;/*from  www  .  j  ava 2  s. c  om*/
    try {
        if (jStockOptions.isProxyAuthEnabled()) {
            method.setFollowRedirects(false);
            client.executeMethod(method);

            int statuscode = method.getStatusCode();
            if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY)
                    || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY)
                    || (statuscode == HttpStatus.SC_SEE_OTHER)
                    || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
                //Make new Request with new URL
                Header header = method.getResponseHeader("location");
                HttpMethod RedirectMethod = new GetMethod(header.getValue());
                // I assume it is OK to release method for twice. (The second
                // release will happen in finally block). We shouldn't have an
                // unreleased method, before executing another new method.
                method.releaseConnection();
                // Do RedirectMethod within try-catch-finally, so that we can have a
                // exception free way to release RedirectMethod connection.
                // #2836422
                try {
                    client.executeMethod(RedirectMethod);
                    respond = RedirectMethod.getResponseBodyAsString();
                } catch (HttpException exp) {
                    log.error(null, exp);
                    return null;
                } catch (IOException exp) {
                    log.error(null, exp);
                    return null;
                } finally {
                    RedirectMethod.releaseConnection();
                }
            } else {
                respond = method.getResponseBodyAsString();
            } // if statuscode = Redirect
        } else {
            client.executeMethod(method);
            respond = method.getResponseBodyAsString();
        } //  if jStockOptions.isProxyAuthEnabled()
    } catch (HttpException exp) {
        log.error(null, exp);
        return null;
    } catch (IOException exp) {
        log.error(null, exp);
        return null;
    } finally {
        method.releaseConnection();
    }
    return respond;
}

From source file:org.zdevra.guice.mvc.security.controller.SecureRoleControllerTest.java

@Test
public void shouldDenyRoleNotAuthenticatedAndAllowCorrectUser() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/secure/something");

    int code = method.getStatusCode();

    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_MOVED_TEMPORARILY));

    method = doRequest("http://localhost:9191/auth/authenticate");

    method = doRequest("http://localhost:9191/secure/something");
    code = method.getStatusCode();/*  w  ww .  j av  a 2s .c  om*/
    assertThat(code, is(HttpServletResponse.SC_OK));
    assertThat(method.getPath(), is("/secure/something"));
    System.out.println("code:" + code);

}

From source file:org.zdevra.guice.mvc.security.tags.TagsTest.java

@Test
public void shouldShowContentsOfAuthenticatedTagWhenUserIsAuthenticated() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/auth/authenticate");
    method = doRequest("http://localhost:9191/secure/isAuthenticated");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, containsString("AUTHENTICATED"));
    assertThat(responseBodyAsString, not(containsString("NOT")));
}

From source file:org.zdevra.guice.mvc.security.tags.TagsTest.java

@Test
public void shouldSkipContentsOfAuthenticatedTagWhenUserIsNotAuthenticated()
        throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/secure/isAuthenticated");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();

    assertThat(responseBodyAsString, not(containsString("AUTHENTICATED")));
    assertThat(responseBodyAsString, containsString("NOT"));
}

From source file:org.zdevra.guice.mvc.security.tags.TagsTest.java

@Test
public void shouldSkipContentsWhenPrincipalHasWrongRole() throws IOException, ServletException {
    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/auth/authenticate");
    method = doRequest("http://localhost:9191/secure/hasRole");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, not(containsString("ADMIN")));
    assertThat(responseBodyAsString, (containsString("MANAGER")));
}

From source file:org.zdevra.guice.mvc.security.tags.TagsTest.java

@Test
public void shouldSkipAndShowUsersByIsUserTagByTheirNames() throws IOException, ServletException {
    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/auth/authenticate");
    method = doRequest("http://localhost:9191/secure/isUser");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, not(containsString("ADMIN")));
    assertThat(responseBodyAsString, (containsString("PASS1")));
    assertThat(responseBodyAsString, (containsString("PASS2")));
}

From source file:org.zdevra.guice.mvc.security.webprincipal.WebPrincipalControllerTest.java

@Test
public void shouldShowAnnonymousUserName() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/secure/anonPrincipal");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, is("Annonymous"));
}

From source file:org.zdevra.guice.mvc.security.webprincipal.WebPrincipalControllerTest.java

@Test
public void shouldShowAuthenticatedUserName() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/auth/authenticate");
    method = doRequest("http://localhost:9191/secure/authPrincipal");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, is("SimpleName"));
}

From source file:org.zdevra.guice.mvc.security.webprincipal.WebPrincipalControllerTest.java

@Test
public void shouldProviderWebPrincipalAsRequestParameter() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/auth/authenticate");
    method = doRequest("http://localhost:9191/secure/authPrincipalParam");

    int code = method.getStatusCode();
    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_OK));

    String responseBodyAsString = method.getResponseBodyAsString();
    assertThat(responseBodyAsString, is("SimpleName"));
}

From source file:poisondog.vfs.webdav.WebdavFileFactory.java

public Integer executeNotClose(HttpMethod method) throws FileNotFoundException, IOException, DavException {
    Integer result = mMission.execute(method);
    Log.v("Http Mission: " + mMission.getClass().getName());
    Log.v("Status Code: " + method.getStatusCode());
    return result;
}