Source:
private String getPatentMetadata(ImportedPatent patent, String securityToken, int retryCount) throws Exception { if (retryCount > maxRetriesCount) { throw new PatentServiceException("number of maximum retries exceeded: " + maxRetriesCount); } HttpRequest httpRequest = buildPatentMetaRequest(patent, securityToken, opsUriRoot); try (CloseableHttpResponse httpResponse = httpClient.execute(opsHost, httpRequest)) { int statusCode = httpResponse.getStatusLine().getStatusCode(); switch (statusCode) { case 200: { HttpEntity entity = httpResponse.getEntity(); if (entity == null) { throw new PatentServiceException( "got empty entity in response, full status: " + httpResponse.getStatusLine()); } return EntityUtils.toString(entity); } case 400: { log.info("got 400 HTTP code in response, potential reason: access token invalid or expired, " + "server response: " + EntityUtils.toString(httpResponse.getEntity())); return getPatentMetadata(patent, reauthenticate(), ++retryCount); } case 403: { log.warn("got 403 HTTP code in response, potential reason: endpoint rate limit reached. Delaying for " + throttleSleepTime + " ms, server response: " + EntityUtils.toString(httpResponse.getEntity())); Thread.sleep(throttleSleepTime); return getPatentMetadata(patent, securityToken, ++retryCount); } case 404: { throw new NoSuchElementException("unable to find element at: " + httpRequest.getRequestLine()); } default: { throw new PatentServiceException(String.format( "got unhandled HTTP status code when accessing endpoint: %d, full status: %s, server response: %s", statusCode, httpResponse.getStatusLine(), EntityUtils.toString(httpResponse.getEntity()))); } } } }

Target:
private String getPatentMetadata(ImportedPatent patent, String securityToken, int retryCount) throws Exception { if (retryCount > maxRetriesCount) { throw new PatentServiceException("number of maximum retries exceeded: " + maxRetriesCount); } HttpRequest httpRequest = buildPatentMetaRequest(patent, securityToken, opsUriRoot); try (CloseableHttpResponse httpResponse = httpClient.execute(opsHost, httpRequest)) { int statusCode = httpResponse.getStatusLine().getStatusCode(); switch (statusCode) { case 200: { HttpEntity entity = httpResponse.getEntity(); if (entity == null) { throw new PatentServiceException( "got empty entity in response, full status: " + httpResponse.getStatusLine()); } return EntityUtils.toString(entity); } case 400: { log.info("got 400 HTTP code in response, potential reason: access token invalid or expired, " + "server response: {}", EntityUtils.toString(httpResponse.getEntity())); return getPatentMetadata(patent, reauthenticate(), ++retryCount); } case 403: { log.warn("got 403 HTTP code in response, potential reason: endpoint rate limit reached. Delaying for ms, " + "server response: {}", throttleSleepTime, EntityUtils.toString(httpResponse.getEntity())); Thread.sleep(throttleSleepTime); return getPatentMetadata(patent, securityToken, ++retryCount); } case 404: { throw new NoSuchElementException("unable to find element at: " + httpRequest.getRequestLine()); } default: { throw new PatentServiceException(String.format( "got unhandled HTTP status code when accessing endpoint: %d, full status: %s, server response: %s", statusCode, httpResponse.getStatusLine(), EntityUtils.toString(httpResponse.getEntity()))); } } } }