Example usage for org.springframework.http.client ClientHttpResponse getStatusText

List of usage examples for org.springframework.http.client ClientHttpResponse getStatusText

Introduction

In this page you can find the example usage for org.springframework.http.client ClientHttpResponse getStatusText.

Prototype

String getStatusText() throws IOException;

Source Link

Document

Return the HTTP status text of the response.

Usage

From source file:org.fao.geonet.doi.client.DoiClient.java

public void deleteDoi(String doi) throws DoiClientException {

    ClientHttpResponse httpResponse = null;
    HttpDelete deleteMethod = null;//from   w  ww. j a v  a 2  s.  c  om

    try {
        Log.debug(LOGGER_NAME, "   -- URL: " + this.serverUrl + "/metadata");

        deleteMethod = new HttpDelete(createUrl("doi/" + doi));

        httpResponse = requestFactory.execute(deleteMethod, new UsernamePasswordCredentials(username, password),
                AuthScope.ANY);
        int status = httpResponse.getRawStatusCode();

        Log.debug(LOGGER_NAME, "   -- Request status code: " + status);

        // Ignore NOT FOUND (trying to delete a non existing doi metadata)
        if ((status != HttpStatus.SC_NOT_FOUND) && (status != HttpStatus.SC_OK)) {
            Log.info(LOGGER_NAME, "Delete DOI end -- Error: " + httpResponse.getStatusText());

            throw new DoiClientException(httpResponse.getStatusText());
        } else {
            Log.info(LOGGER_NAME, "DeleteDOI end");
        }

    } catch (Exception ex) {
        Log.error(LOGGER_NAME, "   -- Error (exception): " + ex.getMessage());
        throw new DoiClientException(ex.getMessage());

    } finally {
        if (deleteMethod != null) {
            deleteMethod.releaseConnection();
        }
        // Release the connection.
        IOUtils.closeQuietly(httpResponse);
    }
}

From source file:org.fao.geonet.doi.client.DoiClient.java

/**
 * See https://support.datacite.org/docs/mds-api-guide#section-register-metadata
 *//*from  w w w. j  a v  a  2s.co m*/
private void create(String url, String body, String contentType, String entity) throws DoiClientException {

    ClientHttpResponse httpResponse = null;
    HttpPost postMethod = null;

    try {
        Log.debug(LOGGER_NAME, "   -- URL: " + url);

        postMethod = new HttpPost(url);

        ((HttpUriRequest) postMethod)
                .addHeader(new BasicHeader("Content-Type", contentType + ";charset=UTF-8"));
        Log.debug(LOGGER_NAME, "   -- Request body: " + body);

        StringEntity requestEntity = new StringEntity(body, contentType, "UTF-8");

        postMethod.setEntity(requestEntity);

        httpResponse = requestFactory.execute(postMethod, new UsernamePasswordCredentials(username, password),
                AuthScope.ANY);
        int status = httpResponse.getRawStatusCode();

        Log.debug(LOGGER_NAME, "   -- Request status code: " + status);

        if (status != HttpStatus.SC_CREATED) {
            String message = String.format("Failed to create '%s' with '%s'. Status is %d. Error is %s.", url,
                    body, status, httpResponse.getStatusText());

            Log.info(LOGGER_NAME, message);
            throw new DoiClientException(message);
        } else {
            Log.info(LOGGER_NAME, String.format("DOI metadata created at %s.", url));
        }
    } catch (Exception ex) {
        Log.error(LOGGER_NAME, "   -- Error (exception): " + ex.getMessage());
        throw new DoiClientException(ex.getMessage());

    } finally {
        if (postMethod != null) {
            postMethod.releaseConnection();
        }
        // Release the connection.
        IOUtils.closeQuietly(httpResponse);
    }
}

From source file:org.fao.geonet.doi.client.DoiClient.java

private String retrieve(String url, String entity) throws DoiClientException {

    ClientHttpResponse httpResponse = null;
    HttpGet getMethod = null;/*from  w  w  w. j a  v a2  s  .  c  o m*/

    try {
        Log.debug(LOGGER_NAME, "   -- URL: " + url);

        getMethod = new HttpGet(url);

        httpResponse = requestFactory.execute(getMethod, new UsernamePasswordCredentials(username, password),
                AuthScope.ANY);
        int status = httpResponse.getRawStatusCode();

        Log.debug(LOGGER_NAME, "   -- Request status code: " + status);

        if (status == HttpStatus.SC_OK) {
            return CharStreams.toString(new InputStreamReader(httpResponse.getBody()));
        } else if (status == HttpStatus.SC_NO_CONTENT) {
            return null; // Not found
        } else if (status == HttpStatus.SC_NOT_FOUND) {
            return null; // Not found
        } else {
            Log.info(LOGGER_NAME, "Retrieve DOI metadata end -- Error: " + httpResponse.getStatusText());

            throw new DoiClientException(httpResponse.getStatusText()
                    + CharStreams.toString(new InputStreamReader(httpResponse.getBody())));
        }

    } catch (Exception ex) {
        Log.error(LOGGER_NAME, "   -- Error (exception): " + ex.getMessage());
        throw new DoiClientException(ex.getMessage());

    } finally {
        if (getMethod != null) {
            getMethod.releaseConnection();
        }
        // Release the connection.
        IOUtils.closeQuietly(httpResponse);
    }
}

From source file:org.openflamingo.collector.handler.HttpToLocalHandler.java

/**
 * HTTP  URL? ./*from www  . j  a  va 2  s .  c om*/
 *
 * @param http HTTP
 * @return HTTP Response String
 * @throws Exception HTTP ? ?? ? ? 
 */
private String getResponse(FromHttp http) throws Exception {
    logger.info("HTTP URL?    ?  .");

    String url = jobContext.getValue(http.getUrl().trim());
    String method = jobContext.getValue(http.getMethod().getType());

    logger.info("HTTP URL Information :");
    logger.info("   URL = {}", url);
    logger.info("   Method = {}", method);

    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    ClientHttpRequest request = null;
    if ("POST".equals(method)) {
        request = factory.createRequest(new java.net.URI(url), POST);
    } else {
        request = factory.createRequest(new java.net.URI(url), GET);
    }

    if (http.getHeaders() != null && http.getHeaders().getHeader().size() > 0) {
        List<Header> header = http.getHeaders().getHeader();
        logger.info("HTTP Header :", new String[] {});
        for (Header h : header) {
            String name = h.getName();
            String value = jobContext.getValue(h.getValue());
            request.getHeaders().add(name, value);
            logger.info("\t{} = {}", name, value);
        }
    }

    String responseBodyAsString = null;
    ClientHttpResponse response = null;
    try {
        response = request.execute();
        responseBodyAsString = new String(FileCopyUtils.copyToByteArray(response.getBody()),
                Charset.defaultCharset());
        logger.debug("HTTP ? ?  ? .\n{}", responseBodyAsString);
        logger.info("HTTP ? . ?  '{}({})'.",
                response.getStatusText(), response.getRawStatusCode());
        if (response.getRawStatusCode() != HttpStatus.OK.value()) {
            throw new SystemException(ExceptionUtils.getMessage(
                    "HTTP URL ? . ? OK  '{}({})'  ? .",
                    response.getStatusText(), response.getRawStatusCode()));
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        throw new SystemException(
                ExceptionUtils.getMessage("HTTP URL ? . ? : {}",
                        ExceptionUtils.getRootCause(ex).getMessage()),
                ex);
    } finally {
        try {
            response.close();
        } catch (Exception ex) {
            // Ignored
        }
    }
    return responseBodyAsString;
}