public static String getFromHTTP(String url, Time timeout) throws Exception { final URL u = new URL(url); LOG.info("Accessing URL " + url + " as URL: " + u); final long deadline = timeout.toMilliseconds() + System.currentTimeMillis(); while (System.currentTimeMillis() <= deadline) { HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setConnectTimeout(100000); connection.connect(); if (Objects.equals(HttpResponseStatus.SERVICE_UNAVAILABLE, HttpResponseStatus.valueOf(connection.getResponseCode()))) { // service not available --> Sleep and retry LOG.debug("Web service currently not available. Retrying the request in a bit."); Thread.sleep(100L); } else { InputStream is; if (connection.getResponseCode() >= 400) { // error! LOG.warn("HTTP Response code when connecting to {} was {}", url, connection.getResponseCode()); is = connection.getErrorStream(); } else { is = connection.getInputStream(); } return IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET); } } throw new TimeoutException("Could not get HTTP response in time since the service is still unavailable."); }
private static Predicate<Throwable> isServiceUnavailable() { return httpExceptionCodePredicate(code -> code == HttpResponseStatus.SERVICE_UNAVAILABLE.code()); }
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpResponse && ((HttpResponse) msg).status().equals(REQUEST_ENTITY_TOO_LARGE)) { jsonFuture.completeExceptionally( new RestClientException( String.format( REQUEST_ENTITY_TOO_LARGE + ". Try to raise [%s]", RestOptions.CLIENT_MAX_CONTENT_LENGTH.key()), ((HttpResponse) msg).status())); } else if (msg instanceof FullHttpResponse) { readRawResponse((FullHttpResponse) msg); } else { LOG.error("Implementation error: Received a response that wasn't a FullHttpResponse."); if (msg instanceof HttpResponse) { jsonFuture.completeExceptionally( new RestClientException( "Implementation error: Received a response that wasn't a FullHttpResponse.", ((HttpResponse) msg).getStatus())); } else { jsonFuture.completeExceptionally( new RestClientException( "Implementation error: Received a response that wasn't a FullHttpResponse.", HttpResponseStatus.INTERNAL_SERVER_ERROR)); } } ctx.close(); }
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { if (msg instanceof HttpResponse && ((HttpResponse) msg).status().equals(REQUEST_ENTITY_TOO_LARGE)) { jsonFuture.completeExceptionally( new RestClientException( String.format( REQUEST_ENTITY_TOO_LARGE + ". Try to raise [%s]", RestOptions.CLIENT_MAX_CONTENT_LENGTH.key()), ((HttpResponse) msg).status())); } else if (msg instanceof FullHttpResponse) { readRawResponse((FullHttpResponse) msg); } else { LOG.error("Implementation error: Received a response that wasn't a FullHttpResponse."); if (msg instanceof HttpResponse) { jsonFuture.completeExceptionally( new RestClientException( "Implementation error: Received a response that wasn't a FullHttpResponse.", ((HttpResponse) msg).getStatus())); } else { jsonFuture.completeExceptionally( new RestClientException( "Implementation error: Received a response that wasn't a FullHttpResponse.", HttpResponseStatus.INTERNAL_SERVER_ERROR)); } } ctx.close(); }
private static Predicate<Throwable> httpExceptionCodePredicate(Predicate<Integer> statusCodePredicate) { return (throwable) -> ExceptionUtils.findThrowable(throwable, RestClientException.class) .map(restClientException -> { final int code = restClientException.getHttpResponseStatus().code(); return statusCodePredicate.test(code); }) .orElse(false); }
public HttpResponseStatus getHttpResponseStatus() { return HttpResponseStatus.valueOf(responseCode); } }
public RestHandlerException(String errorMessage, HttpResponseStatus httpResponseStatus, Throwable cause) { super(errorMessage, cause); this.responseCode = httpResponseStatus.code(); }
public HttpResponseStatus getHttpResponseStatus() { return HttpResponseStatus.valueOf(responseCode); } }
public RestClientException(String message, Throwable cause, HttpResponseStatus responseStatus) { super(message, cause); responseCode = responseStatus.code(); }
public HttpResponseStatus getHttpResponseStatus() { return HttpResponseStatus.valueOf(responseCode); } }
public RestHandlerException(String errorMessage, HttpResponseStatus httpResponseStatus, Throwable cause) { super(errorMessage, cause); this.responseCode = httpResponseStatus.code(); }
public HttpResponseStatus getHttpResponseStatus() { return HttpResponseStatus.valueOf(responseCode); } }
public RestClientException(String message, Throwable cause, HttpResponseStatus responseStatus) { super(message, cause); responseCode = responseStatus.code(); }
public HttpResponseStatus getHttpResponseStatus() { return HttpResponseStatus.valueOf(responseCode); } }
public RestClientException(String message, Throwable cause, HttpResponseStatus responseStatus) { super(message, cause); responseCode = responseStatus.code(); }
public HttpResponseStatus getHttpResponseStatus() { return HttpResponseStatus.valueOf(responseCode); } }
public RestHandlerException(String errorMessage, HttpResponseStatus httpResponseStatus) { super(errorMessage); this.responseCode = httpResponseStatus.code(); }
public static String getFromHTTP(String url, Time timeout) throws Exception { final URL u = new URL(url); LOG.info("Accessing URL " + url + " as URL: " + u); final long deadline = timeout.toMilliseconds() + System.currentTimeMillis(); while (System.currentTimeMillis() <= deadline) { HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setConnectTimeout(100000); connection.connect(); if (Objects.equals(HttpResponseStatus.SERVICE_UNAVAILABLE, HttpResponseStatus.valueOf(connection.getResponseCode()))) { // service not available --> Sleep and retry LOG.debug("Web service currently not available. Retrying the request in a bit."); Thread.sleep(100L); } else { InputStream is; if (connection.getResponseCode() >= 400) { // error! LOG.warn("HTTP Response code when connecting to {} was {}", url, connection.getResponseCode()); is = connection.getErrorStream(); } else { is = connection.getInputStream(); } return IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET); } } throw new TimeoutException("Could not get HTTP response in time since the service is still unavailable."); }
public RestHandlerException(String errorMessage, HttpResponseStatus httpResponseStatus) { super(errorMessage); this.responseCode = httpResponseStatus.code(); }
public static String getFromHTTP(String url, Time timeout) throws Exception { final URL u = new URL(url); LOG.info("Accessing URL " + url + " as URL: " + u); final long deadline = timeout.toMilliseconds() + System.currentTimeMillis(); while (System.currentTimeMillis() <= deadline) { HttpURLConnection connection = (HttpURLConnection) u.openConnection(); connection.setConnectTimeout(100000); connection.connect(); if (Objects.equals(HttpResponseStatus.SERVICE_UNAVAILABLE, HttpResponseStatus.valueOf(connection.getResponseCode()))) { // service not available --> Sleep and retry LOG.debug("Web service currently not available. Retrying the request in a bit."); Thread.sleep(100L); } else { InputStream is; if (connection.getResponseCode() >= 400) { // error! LOG.warn("HTTP Response code when connecting to {} was {}", url, connection.getResponseCode()); is = connection.getErrorStream(); } else { is = connection.getInputStream(); } return IOUtils.toString(is, ConfigConstants.DEFAULT_CHARSET); } } throw new TimeoutException("Could not get HTTP response in time since the service is still unavailable."); }