List of usage examples for org.springframework.http.client ClientHttpResponse getRawStatusCode
int getRawStatusCode() throws IOException;
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 w w.java 2s. c o m*/ 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 */// w ww. j ava 2 s .com 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 a 2 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.fao.geonet.monitor.health.DashboardAppHealthCheck.java
public HealthCheck create(final ServiceContext context) { return new HealthCheck(this.getClass().getSimpleName()) { @Override/* ww w . jav a 2s . c o m*/ protected Result check() throws Exception { final GeonetHttpRequestFactory httpRequestFactory = context.getBean(GeonetHttpRequestFactory.class); final SettingManager settingManager = context.getBean(SettingManager.class); final EsSearchManager searchMan = context.getBean(EsSearchManager.class); final String dashboardAppUrl = searchMan.getClient().getDashboardAppUrl(); if (StringUtils.isNotEmpty(dashboardAppUrl)) { ClientHttpResponse httpResponse = null; try { String url = settingManager.getBaseURL() + "dashboards/api/status"; httpResponse = httpRequestFactory.execute(new HttpGet(url)); if (httpResponse.getRawStatusCode() == 200) { return Result.healthy(String.format("Dashboard application is running.")); } else { return Result.unhealthy("Dashboard application is not available currently. " + "This component is only required if you use dashboards."); } } catch (Throwable e) { return Result.unhealthy(e); } finally { if (httpResponse != null) { httpResponse.close(); } } } else { return Result.unhealthy("Dashboard application is not configured. " + "Update config.properties to setup Kibana to use this feature."); } } }; }
From source file:org.openflamingo.collector.handler.HttpToLocalHandler.java
/** * HTTP URL? ./*from w w w . 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; }