private String contentToString(InputStream content, String idString) throws Exception { try { return IOUtils.toString(content); } catch (Exception e) { log.debug(String.format("Unable to read input stream to string (%s)", idString), e); throw e; } }
/** * Tries to read all the content from the HTTP response into a string. If an IO failure occurs while reading content, * empty string is returned instead. * * @param response Response to slurp content for. * @return String containing response content, empty string if failure occurs. */ private static String trySlurpContent(HttpResponse response) { try { return IOUtils.toString(response.getEntity().getContent()); } catch (IOException e) { return ""; } } }
/** * Reads a system resource fully into a String * * @param location * Relative or absolute location of system resource. * @return String contents of resource file * @throws RuntimeException * if any error occurs */ protected String getResourceAsString(String location) { try { InputStream resourceStream = getClass().getResourceAsStream(location); String resourceAsString = IOUtils.toString(resourceStream); resourceStream.close(); return resourceAsString; } catch (Exception e) { throw new RuntimeException(e); } }
private String loadDeafultFileHeader() throws IOException { try (InputStream inputStream = getClass() .getResourceAsStream("/com/amazonaws/codegen/DefaultFileHeader.txt")) { return IOUtils.toString(inputStream) .replaceFirst("%COPYRIGHT_DATE_RANGE%", getCopyrightDateRange()); } }
/** * Reads a system resource fully into a String * * @param location * Relative or absolute location of system resource. * @return String contents of resource file * @throws RuntimeException * if any error occurs */ protected String getResourceAsString(String location) { try { InputStream resourceStream = getClass().getResourceAsStream(location); String resourceAsString = IOUtils.toString(resourceStream); resourceStream.close(); return resourceAsString; } catch (Exception e) { throw new RuntimeException(e); } }
private JmesPathExpression getAstFromArgument(String argument, Map<String, JmesPathExpression> argumentToAstMap) throws IOException { if (argument != null && !argumentToAstMap.containsKey(argument)) { final Process p = executeToAstProcess(argument); if(p.exitValue()!= 0) { throw new RuntimeException(IOUtils.toString(p.getErrorStream())); } JsonNode jsonNode = mapper.readTree(IOUtils.toString(p.getInputStream())); JmesPathExpression ast = fromAstJsonToAstJava(jsonNode); argumentToAstMap.put(argument, ast); IOUtils.closeQuietly(p.getInputStream(), null); return ast; } else if (argument != null) { return argumentToAstMap.get(argument); } return null; }
/** * Unsubscribes this endpoint from the topic. */ public void unsubscribeFromTopic() { try { HttpGet request = new HttpGet(unsubscribeUrl.toURI()); HttpResponse response = httpClient.execute(request); if (!ApacheUtils.isRequestSuccessful(response)) { throw new SdkClientException(String.format("Could not unsubscribe from %s: %d %s.%n%s", getTopicArn(), response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase(), IOUtils.toString(response.getEntity().getContent()))); } } catch (Exception e) { throw new SdkClientException(e); } }
/** * Downloads the certificate from the provided URL. Asserts that the endpoint is an SNS endpoint and that * the certificate is vended over HTTPs. * * @param certUrl URL to download certificate from. * @return String contents of certificate. * @throws SdkClientException If certificate cannot be downloaded or URL is invalid. */ private String downloadCert(URI certUrl) { try { signingCertUrlVerifier.verifyCertUrl(certUrl); HttpResponse response = client.execute(new HttpGet(certUrl)); if (ApacheUtils.isRequestSuccessful(response)) { try { return IOUtils.toString(response.getEntity().getContent()); } finally { response.getEntity().getContent().close(); } } else { throw new HttpException("Could not download the certificate from SNS", response); } } catch (IOException e) { throw new SdkClientException("Unable to download SNS certificate from " + certUrl.toString(), e); } }
/** * Download the payload from the given path * * @param path the relative path of the payload in the {@link ExternalPayloadStorage} * @return the payload object * @throws ApplicationException in case of JSON parsing errors or download errors */ @SuppressWarnings("unchecked") public Map<String, Object> downloadPayload(String path) { try (InputStream inputStream = externalPayloadStorage.download(path)) { return objectMapper.readValue(IOUtils.toString(inputStream), Map.class); } catch (IOException e) { logger.error("Unable to download payload from external storage path: {}", path, e); throw new ApplicationException(ApplicationException.Code.INTERNAL_ERROR, e); } }
public GenericApiGatewayResponse(HttpResponse httpResponse) throws IOException { this.httpResponse = httpResponse; if(httpResponse.getContent() != null) { this.body = IOUtils.toString(httpResponse.getContent()); }else { this.body = null; } }
private void handleErrorResponse(InputStream errorStream, int statusCode, String responseMessage) throws IOException { String errorCode = null; // Parse the error stream returned from the service. if(errorStream != null) { String errorResponse = IOUtils.toString(errorStream); try { JsonNode node = Jackson.jsonNodeOf(errorResponse); JsonNode code = node.get("code"); JsonNode message = node.get("message"); if (code != null && message != null) { errorCode = code.asText(); responseMessage = message.asText(); } } catch (Exception exception) { LOG.debug("Unable to parse error stream"); } } AmazonServiceException ase = new AmazonServiceException(responseMessage); ase.setStatusCode(statusCode); ase.setErrorCode(errorCode); throw ase; } }
@Override public String getObjectAsString(String bucketName, String key) throws AmazonServiceException, SdkClientException { rejectNull(bucketName, "Bucket name must be provided"); rejectNull(key, "Object key must be provided"); S3Object object = getObject(bucketName, key); try { return IOUtils.toString(object.getObjectContent()); } catch (IOException e) { throw new SdkClientException("Error streaming content from S3 during download"); } finally { IOUtils.closeQuietly(object, log); } }
return IOUtils.toString(inputStream); } else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND) {
content = IOUtils.toString(is); } catch (IOException ioe) { if (log.isDebugEnabled())
Response response = restClient.performRequest(HttpMethod.GET, resourcePath); String responseBody = IOUtils.toString(response.getEntity().getContent()); logger.info("responseBody: {}", responseBody);
Response response = restClient.performRequest(HttpMethod.GET, resourcePath); String responseBody = IOUtils.toString(response.getEntity().getContent()); TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {
@Override public String unmarshall(InputStream in) throws Exception { assertEquals("content", IOUtils.toString(in)); handleCalled.add(true); return null; } });
@Test public void test() throws Exception { String s = IOUtils.toString(new ByteArrayInputStream("Testing".getBytes(StringUtils.UTF8))); assertEquals("Testing", s); } }
@Test public void testZeroByteStream() throws Exception { String s = IOUtils.toString(new InputStream() { @Override public int read() throws IOException { return -1; } }); assertEquals("", s); }
@Test public void testContentEncodingIdentity() throws Exception { builder = HttpResponse.builder() .header("Content-Encoding", "identity") .content(content); response = builder.build(); assertFalse("Not gzip", response.getContent() instanceof GZIPInputStream); assertEquals("same content", "content", IOUtils.toString(response.getContent())); }