public static String toString(InputStream inputStream) throws IOException { return toString(inputStream,"UTF-8"); }
public static String toString(InputStream inputStream) throws IOException { return toString(inputStream,"UTF-8"); }
public static String toString(InputStream inputStream) throws IOException { return toString(new InputStreamReader(inputStream)); }
public static String toString(InputStream inputStream, String encoding) throws IOException { return toString(new InputStreamReader(inputStream,encoding)); }
public static String toString(InputStream inputStream, String encoding) throws IOException { return toString(new InputStreamReader(inputStream,encoding)); }
private void checkStatus(CloseableHttpResponse response) throws IOException { final int retCode = response.getStatusLine().getStatusCode(); if (retCode < 200 || retCode > 299) { String msg; if (response.getEntity() != null) { try { msg = IOUtils.toString(response.getEntity().getContent()); } catch (Exception e) { msg = ""; } } else { msg = ""; } throw new IOException("Server returned " + response.getStatusLine() + " : " + msg); } }
private void verifyStatusCode(HttpRequestBase method, CloseableHttpResponse response) throws IOException, HttpException { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode < 200 || statusCode > 299) { String errMsg; if (response.getEntity() != null && response.getEntity().getContent() != null) { errMsg = " : " + IOUtils.toString(response.getEntity().getContent()); } else { errMsg = ""; } throw new HttpException("Anypoint returned status code " + statusCode + " - url: " + method.getURI() + " - err: " + errMsg, statusCode); } }
@Override public long getTimeDifferential(HttpRequest request, HttpContext context) throws IOException { CloseableHttpClient httpClient = HttpClientBuilder.create().build(); try { CloseableHttpResponse response = httpClient.execute(new HttpGet(url)); if (response.getStatusLine().getStatusCode() != 200) { throw new IOException("Unable to sync time, server returned error code " + response.getStatusLine().getStatusCode() + " : " + response.getStatusLine().getReasonPhrase()); } long now = System.currentTimeMillis(); String serverTimestamp = IOUtils.toString(response.getEntity().getContent()); try { return now - TimeUtils.parseISOUTCDateTime(serverTimestamp).getTime(); } catch (ParseException e) { throw new IOException("Unable to sync time, invalid time returned by server: " + serverTimestamp); } } finally { IOUtils.close(httpClient); } } }
@Nullable private String doExecute(HttpRequestBase method) throws HttpException { if (auth != null && method.getFirstHeader(HEADER_AUTH) == null) { if (auth.startsWith("bearer ")) { auth = "Bearer " + auth.substring(7); } method.setHeader(HEADER_AUTH, auth); } try (CloseableHttpResponse response = httpClient.execute(method)) { verifyStatusCode(method, response); if (response.getEntity() != null && response.getEntity().getContent() != null) { String resStr = IOUtils.toString(response.getEntity().getContent()); logger.debug("RESULT CONTENT: " + resStr); return resStr; } else { return null; } } catch (IOException e) { throw new RuntimeIOException(e); } }
private InputStream getContent() throws TemplateExecutionException { try { if (StringUtils.isNotBlank(resource)) { if (!resource.startsWith("/")) { resource = "/" + resource; } resource = template.filter(resource); try (InputStream is = getClass().getResourceAsStream(resource)) { if (process == null || process) { content = IOUtils.toString(is, getEncoding()); } else { return is; } } } if (content == null) { throw new TemplateExecutionException("Content missing from " + path); } if (trim == null || trim) { content = content.trim(); } if (process == null || process) { return new ByteArrayInputStream(template.filter(content).getBytes(getEncoding())); } else { return new ByteArrayInputStream(content.getBytes(getEncoding())); } } catch (IOException e) { throw new TemplateExecutionException(e); } }