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.eclipse.winery.highlevelrestapi.LowLevelRestApi.java

/**
 * Extracts the response information from an executed HttpMethod
 * //  w ww.j  a v  a2  s.co  m
 * @param method Executed Method
 * @return Packaged response information
 */
private static HttpResponseMessage extractResponseInformation(HttpMethod method) {
    // Create and return HttpResponseMethod
    HttpResponseMessage responseMessage = new HttpResponseMessage();
    responseMessage.setStatusCode(method.getStatusCode());
    try {
        responseMessage.setResponseBody(method.getResponseBodyAsString());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseMessage;

}

From source file:org.elasticsearch.hadoop.rest.commonshttp.CommonsHttpTransport.java

@Override
public Response execute(Request request) throws IOException {
    HttpMethod http = null;

    switch (request.method()) {
    case DELETE://from   w  ww . j av a 2  s. c  o  m
        http = new DeleteMethodWithBody();
        break;
    case HEAD:
        http = new HeadMethod();
        break;
    case GET:
        http = (request.body() == null ? new GetMethod() : new GetMethodWithBody());
        break;
    case POST:
        http = new PostMethod();
        break;
    case PUT:
        http = new PutMethod();
        break;

    default:
        throw new EsHadoopTransportException("Unknown request method " + request.method());
    }

    CharSequence uri = request.uri();
    if (StringUtils.hasText(uri)) {
        http.setURI(new URI(escapeUri(uri.toString(), settings.getNetworkSSLEnabled()), false));
    }
    // NB: initialize the path _after_ the URI otherwise the path gets reset to /
    http.setPath(prefixPath(request.path().toString()));

    try {
        // validate new URI
        uri = http.getURI().toString();
    } catch (URIException uriex) {
        throw new EsHadoopTransportException("Invalid target URI " + request, uriex);
    }

    CharSequence params = request.params();
    if (StringUtils.hasText(params)) {
        http.setQueryString(params.toString());
    }

    ByteSequence ba = request.body();
    if (ba != null && ba.length() > 0) {
        if (!(http instanceof EntityEnclosingMethod)) {
            throw new IllegalStateException(String.format("Method %s cannot contain body - implementation bug",
                    request.method().name()));
        }
        EntityEnclosingMethod entityMethod = (EntityEnclosingMethod) http;
        entityMethod.setRequestEntity(new BytesArrayRequestEntity(ba));
        entityMethod.setContentChunked(false);
    }

    // when tracing, log everything
    if (log.isTraceEnabled()) {
        log.trace(String.format("Tx %s[%s]@[%s][%s] w/ payload [%s]", proxyInfo, request.method().name(),
                httpInfo, request.path(), request.body()));
    }

    long start = System.currentTimeMillis();
    try {
        client.executeMethod(http);
    } finally {
        stats.netTotalTime += (System.currentTimeMillis() - start);
    }

    if (log.isTraceEnabled()) {
        Socket sk = ReflectionUtils.invoke(GET_SOCKET, conn, (Object[]) null);
        String addr = sk.getLocalAddress().getHostAddress();
        log.trace(String.format("Rx %s@[%s] [%s-%s] [%s]", proxyInfo, addr, http.getStatusCode(),
                HttpStatus.getStatusText(http.getStatusCode()), http.getResponseBodyAsString()));
    }

    // the request URI is not set (since it is retried across hosts), so use the http info instead for source
    return new SimpleResponse(http.getStatusCode(), new ResponseInputStream(http), httpInfo);
}

From source file:org.exoplatform.bonitaextension.connectors.webdav.common.WebDAVClient.java

/**
 * To delete either a folder or a file//from  ww w. j  a  v  a 2s .c o  m
 *
 * @param uri
 * @throws Exception
 */
/* NOT TESTED
public WebDAVResponse deleteItem(String uri) throws Exception {
   if (LOGGER.isLoggable(Level.INFO)) {
 LOGGER.info("deleteItem '" + uri +"'");
   }
        
   DeleteMethod httpMethod = new DeleteMethod(uri);
   client.executeMethod(httpMethod);
        
   WebDAVResponse objResponse = processResponse(httpMethod, true);
   httpMethod.releaseConnection();
   return objResponse;
}
        
*/

/* NOT TESTED
public WebDAVResponse checkout(String uri) throws Exception {
        
   if (LOGGER.isLoggable(Level.INFO)) {
 LOGGER.info("checkout '" + uri +"'");
   }
        
   CheckoutMethod httpMethod = new CheckoutMethod(uri);
   client.executeMethod(httpMethod);
        
   WebDAVResponse objResponse = processResponse(httpMethod, true);
   httpMethod.releaseConnection();
   return objResponse;
}
 */

/* NOT TESTED
public WebDAVResponse checkin(String uri) throws Exception {
        
   if (LOGGER.isLoggable(Level.INFO)) {
 LOGGER.info("checkin '" + uri +"'");
   }
        
   CheckinMethod httpMethod = new CheckinMethod(uri);
   client.executeMethod(httpMethod);
        
   WebDAVResponse objResponse = processResponse(httpMethod, true);
   httpMethod.releaseConnection();
   return objResponse;
}
*/

/* NOT TESTED
        
public WebDAVResponse cancelCheckout(String uri) throws Exception {
        
   if (LOGGER.isLoggable(Level.INFO)) {
 LOGGER.info("cancelCheckout '" + uri +"'");
   }
        
   UncheckoutMethod httpMethod = new UncheckoutMethod(uri);
   client.executeMethod(httpMethod);
        
   WebDAVResponse objResponse = processResponse(httpMethod, true);
   httpMethod.releaseConnection();
   return objResponse;
}
*/

/* NOT TESTED
public WebDAVResponse report(String uri) throws Exception {
        
   if (LOGGER.isLoggable(Level.INFO)) {
 LOGGER.info("report '" + uri +"'");
   }
        
   ReportInfo reportInfo = new ReportInfo(ReportType.VERSION_TREE);
        
   ReportMethod httpMethod = new ReportMethod(uri, reportInfo);
   client.executeMethod(httpMethod);
        
   WebDAVResponse objResponse = processResponse(httpMethod, false);
        
   MultiStatus multiStatus = httpMethod.getResponseBodyAsMultiStatus();
   MultiStatusResponse responses[] = multiStatus.getResponses();
        
   String responseAsString = "";
   for (int i = 0; i < responses.length; i++) {
 responseAsString += responses[i].getHref() + "\n";
   }
        
   objResponse.setResponse(responseAsString);
   httpMethod.releaseConnection();
   return objResponse;
}
*/
/* NOT TESTED
public WebDAVResponse versionControl(String uri) throws Exception {
        
   if (LOGGER.isLoggable(Level.INFO)) {
 LOGGER.info("versionControl '" + uri +"'");
   }
        
   VersionControlMethod httpMethod = new VersionControlMethod(uri);
   client.executeMethod(httpMethod);
        
   WebDAVResponse objResponse = processResponse(httpMethod, true);
   httpMethod.releaseConnection();
   return objResponse;
}
*/

private WebDAVResponse processResponse(HttpMethod httpMethod, boolean getResponseAsString) {

    String statusCode = "-1";
    if (httpMethod.getStatusCode() > 0) {
        statusCode = String.valueOf(httpMethod.getStatusCode());
    }
    String statusText = httpMethod.getStatusText();

    String responseString = "";
    if (getResponseAsString) {
        try {
            responseString = httpMethod.getResponseBodyAsString();
            if (responseString == null) {
                responseString = "";
            }
        } catch (IOException e) {
            if (LOGGER.isLoggable(Level.WARNING)) {
                LOGGER.warning("IOException while getting responseAsString: " + e.getMessage());
            }
            e.printStackTrace();
        }
    }

    if (LOGGER.isLoggable(Level.INFO)) {
        LOGGER.info("status CODE: " + statusCode + ", status TEXT: " + statusText + "\n");
    }

    WebDAVResponse response = new WebDAVResponse(statusCode, statusText, responseString);
    return response;
}

From source file:org.iavante.sling.authentication.AuthenticationFilterIT.java

@org.junit.Test
public void test_get_authenticated() {

    // Try to get a collection
    authPrefs.add(AuthPolicy.DIGEST);// w w w  .  j  ava 2 s. co  m
    authPrefs.add(AuthPolicy.BASIC);
    defaultcreds = new UsernamePasswordCredentials("admin", "admin");

    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    HttpMethod get = new GetMethod(SLING_URL + GET_URL);
    try {
        client.executeMethod(get);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertEquals(get.getStatusCode(), 404);
}

From source file:org.iavante.sling.authentication.AuthenticationFilterIT.java

@org.junit.Test
public void test_get_not_authenticated() {

    // Try to get a collection
    authPrefs.add(AuthPolicy.DIGEST);/*from w w  w  . j  a v  a 2  s.co  m*/
    authPrefs.add(AuthPolicy.BASIC);
    defaultcreds = new UsernamePasswordCredentials("noauthenticated", "noauthenticated");

    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    HttpMethod get = new GetMethod(SLING_URL + GET_URL);
    try {
        client.executeMethod(get);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    assertEquals(get.getStatusCode(), 403);
}

From source file:org.iavante.sling.authentication.AuthenticationFilterIT.java

@org.junit.Test
public void test_admin_url() {

    // Try to get a collection
    authPrefs.add(AuthPolicy.DIGEST);/*from  w  w w  . j  a  v  a2 s  . co  m*/
    authPrefs.add(AuthPolicy.BASIC);
    defaultcreds = new UsernamePasswordCredentials("noauthenticated", "noauthenticated");

    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    HttpMethod get = new GetMethod(SLING_URL + ADMIN_URL);
    try {
        client.executeMethod(get);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    assertTrue((get.getStatusCode() != 403));
}

From source file:org.iavante.sling.authentication.AuthenticationFilterIT.java

@org.junit.Test
public void test_downloader_url() {

    // Try to get a collection
    authPrefs.add(AuthPolicy.DIGEST);/*from w ww  .ja va2s  .  com*/
    authPrefs.add(AuthPolicy.BASIC);
    defaultcreds = new UsernamePasswordCredentials("noauthenticated", "noauthenticated");

    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);
    client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

    HttpMethod get = new GetMethod(SLING_URL + DOWNLOADER_URL);
    try {
        client.executeMethod(get);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    assertTrue((get.getStatusCode() != 403));
}

From source file:org.iavante.sling.authentication.AuthenticationFilterIT.java

@org.junit.Test
public void test_crossdomain_url() {

    HttpMethod get = new GetMethod(SLING_URL + CROSSDOMAIN_URL);
    try {//w  w  w  . j  a  v a2s . co  m
        client.executeMethod(get);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    assertTrue((get.getStatusCode() != 403));
}

From source file:org.iavante.sling.gad.catalog.DenyRevisionServiceIT.java

public void test_get_revision_pending() {

    try {//w w  w  .j  a  v  a 2s  . com
        Thread.sleep(5000);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

    System.out.println("TEST if the pending revision is present");

    HttpMethod get_pending_revision = new GetMethod(
            SLING_URL + CATALOG_URL + PENDING_FOLDER + "/" + content_title + "/sling:resourceType");
    System.out.println("Get if the content is pending " + SLING_URL + CATALOG_URL + PENDING_FOLDER + "/"
            + content_title + "/sling:resourceType");

    try {
        this.client.executeMethod(get_pending_revision);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Response: " + get_pending_revision.getStatusCode());
    assertEquals(get_pending_revision.getStatusCode(), 404);
}

From source file:org.iavante.sling.gad.catalog.PendingServiceIT.java

public void test_source_not_in_schema() {

    try {/*from  ww  w  .j  av a  2  s  .com*/
        Thread.sleep(8000);
    } catch (InterruptedException e1) {
        e1.printStackTrace();
    }

    // Get source test not present in schema
    HttpMethod get_source = new GetMethod(SLING_URL + CATALOG_URL + PENDING_FOLDER + "/" + content_title + "/"
            + SOURCES_FOLDER + "/" + source_title);
    System.out.println("Request: " + SLING_URL + CATALOG_URL + PENDING_FOLDER + "/" + content_title + "/"
            + SOURCES_FOLDER + "/" + source_title);
    try {
        client.executeMethod(get_source);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    System.out.println("Response: " + get_source.getStatusCode());
    assertEquals(get_source.getStatusCode(), 404);
    get_source.releaseConnection();
}