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

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

Introduction

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

Prototype

int SC_OK

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

Click Source Link

Document

<tt>200 OK</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from   ww  w  .  j  av a2 s .  c  o  m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        response = new String(responseBody);

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:it.geosolutions.httpproxy.service.BaseProxyServiceTest.java

/**
 * Test IProxyService execute as HTTP GET
 *///from  w ww .  ja  v  a2s.c  o m
public void executeGet() {
    try {
        // Generate mocked request and response
        MockHttpServletRequest mockRequest = new MockHttpServletRequest("GET", "/proxy/");
        mockRequest.addParameter("url", testUrl);
        MockHttpServletResponse mockResponse = new MockHttpServletResponse();

        // Call proxy execute
        proxy.execute(mockRequest, mockResponse);

        // Assert the response
        assertNotNull(mockResponse);
        assertEquals(mockResponse.getStatus(), HttpStatus.SC_OK);
        assertNotNull(mockResponse.getOutputStream());
        assertNotNull(mockResponse.getContentType());
        assertTrue(mockResponse.getContentType().contains("text/xml"));

        LOGGER.info("Success proxy GET in '" + testUrl + "'");
        LOGGER.info("************************ Response ************************");
        LOGGER.info(mockResponse.getContentAsString());
        LOGGER.info("********************** EoF Response **********************");

    } catch (Exception e) {
        fail("Exception executing proxy");
    }
}

From source file:com.owncloud.android.authenticator.AuthenticationRunnable.java

@Override
public void run() {
    Uri uri;/*from w  w  w . j av a  2  s. c  o  m*/
    uri = Uri.parse(mUrl.toString());
    WebdavClient wdc = OwnCloudClientUtils.createOwnCloudClient(uri, mUsername, mPassword, mContext);
    int login_result = wdc.tryToLogin();
    switch (login_result) {
    case HttpStatus.SC_OK:
        postResult(true, uri.toString());
        break;
    case HttpStatus.SC_UNAUTHORIZED:
        postResult(false, mContext.getString(R.string.auth_unauthorized));
        break;
    case HttpStatus.SC_NOT_FOUND:
        postResult(false, mContext.getString(R.string.auth_not_found));
        break;
    default:
        postResult(false, String.format(mContext.getString(R.string.auth_internal), login_result));
    }
}

From source file:com.comcast.cats.service.util.HttpClientUtil.java

public static synchronized Object getForObject(String uri, Map<String, String> paramMap) {
    Object responseObject = new Object();

    HttpMethod httpMethod = new GetMethod(uri);

    if ((null != paramMap) && (!paramMap.isEmpty())) {
        httpMethod.setQueryString(getNameValuePair(paramMap));
    }//w  w w . j  a v a2s. c o m

    Yaml yaml = new Yaml();

    HttpClient client = new HttpClient();
    InputStream responseStream = null;
    Reader inputStreamReader = null;

    try {
        int responseCode = client.executeMethod(httpMethod);

        if (HttpStatus.SC_OK != responseCode) {
            logger.error("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.error("[ METHOD   ] " + httpMethod.getName());
            logger.error("[ STATUS   ] " + responseCode);
        } else {
            logger.trace("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.trace("[ METHOD   ] " + httpMethod.getName());
            logger.trace("[ STATUS   ] " + responseCode);
        }

        responseStream = httpMethod.getResponseBodyAsStream();
        inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF);
        responseObject = yaml.load(inputStreamReader);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        cleanUp(inputStreamReader, responseStream, httpMethod);
    }

    return responseObject;
}

From source file:com.unicauca.braim.http.HttpBraimClient.java

public Token GET_Token(String email_or_username, String password) throws IOException, Exception {
    Token token = null;//from ww  w.jav  a 2  s  .c o m
    Gson gson = new Gson();

    String data = "data=" + email_or_username + "&password=" + password;
    GetMethod method = new GetMethod(api_url + "/api/v1/token?" + data);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
        throw new Exception("The username or password are invalid");
    }

    byte[] responseBody = method.getResponseBody();
    String response = new String(responseBody, "UTF-8");
    token = gson.fromJson(response, Token.class);
    System.out.println("algo");

    return token;
}

From source file:com.voidsearch.voidbase.client.VoidBaseHttpClient.java

public byte[] get(String query) throws Exception {

    GetMethod method = new GetMethod(query);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    StringBuilder sb = new StringBuilder();

    // TODO : add a content dependent fetch

    try {/*from   w ww .j  av  a 2  s. c  o m*/
        int statusCode = client.executeMethod(method);

        if (statusCode == HttpStatus.SC_OK) {
            InputStreamReader is = new InputStreamReader(method.getResponseBodyAsStream());
            BufferedReader in = new BufferedReader(is);

            String line = null;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
        }

    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }

    return (sb.toString()).getBytes();

}

From source file:at.ait.dme.yuma.suite.apps.map.server.geo.MapMetadataServiceImpl.java

@Override
public MapMetadata getMetadata(String url) {
    try {//from ww  w. j ava2 s. co  m
        GetMethod getMetadata = new GetMethod(url);
        int statusCode = new HttpClient().executeMethod(getMetadata);
        if (statusCode == HttpStatus.SC_OK) {
            return fromXML(getMetadata.getResponseBodyAsStream());
        }
    } catch (Exception e) {
        logger.info("No metadata found for " + url + " (" + e.getClass() + ")");
    }
    return null;
}

From source file:com.mythesis.userbehaviouranalysis.AnnotationClient.java

public String request(HttpMethod method) throws AnnotationException {

    String response = null;/*from  w  w  w  . j  ava 2  s. co m*/

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            LOG.error("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        //byte[] responseBody = method.getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
        InputStream responseBodyAsStream = method.getResponseBodyAsStream();
        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        StringWriter writer = new StringWriter();
        IOUtils.copy(responseBodyAsStream, writer, "utf-8");
        response = writer.toString();

    } catch (HttpException e) {
        LOG.error("Fatal protocol violation: " + e.getMessage());
        throw new AnnotationException("Protocol error executing HTTP request.", e);
    } catch (IOException e) {
        LOG.error("Fatal transport error: " + e.getMessage());
        LOG.error(method.getQueryString());
        throw new AnnotationException("Transport error executing HTTP request.", e);
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;

}

From source file:de.linsin.alterego.notification.AppNotificationService.java

/**
 * @see NotificationService#notify(String, String)
 *//*  w ww . ja  v  a 2  s. c  o  m*/
public boolean notify(String argTitle, String argMessage) {
    HttpClient client;
    PostMethod method = null;
    try {
        client = setUp();
        method = setUp(argTitle, argMessage);
        int returnCode = client.executeMethod(method);

        if (returnCode > HttpStatus.SC_OK) {
            logger.warning("Couldn't send notification! Return code: " + returnCode);
            return false;
        } else {
            logger.info("Sent notification!");
            return true;
        }
    } catch (Exception e) {
        logger.severe(e.toString());
        throw new AppNotificationException();
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.wfreitas.camelsoap.util.ClientWsdlLoader.java

public InputStream load(String url) throws Exception {
    GetMethod httpGetMethod;/*w  w  w  . ja  v a 2  s  .co  m*/

    if (url.startsWith("file")) {
        return new URL(url).openStream();
    }

    // Authentication is not being overridden on the method.  It needs
    // to be present on the supplied HttpClient instance!
    httpGetMethod = new GetMethod(url);
    httpGetMethod.setDoAuthentication(true);

    try {
        int result = httpClient.executeMethod(httpGetMethod);

        if (result != HttpStatus.SC_OK) {
            if (result < 200 || result > 299) {
                throw new HttpException(
                        "Received status code '" + result + "' on WSDL HTTP (GET) request: '" + url + "'.");
            } else {
                logger.warn("Received status code '" + result + "' on WSDL HTTP (GET) request: '" + url + "'.");
            }
        }

        return new ByteArrayInputStream(httpGetMethod.getResponseBody());
    } finally {
        httpGetMethod.releaseConnection();
    }
}