/** * Reads and returns the rest of the given input stream as a string. * Caller is responsible for closing the given input stream. */ public static String toString(InputStream is) throws IOException { return new String(toByteArray(is), StringUtils.UTF8); }
/** * Static factory method to create a JsonContent object from the contents of the HttpResponse * provided */ public static JsonContent createJsonContent(HttpResponse httpResponse, JsonFactory jsonFactory) { byte[] rawJsonContent = null; try { if (httpResponse.getContent() != null) { rawJsonContent = IOUtils.toByteArray(httpResponse.getContent()); } } catch (Exception e) { LOG.debug("Unable to read HTTP response content", e); } return new JsonContent(rawJsonContent, new ObjectMapper(jsonFactory) .configure(JsonParser.Feature.ALLOW_COMMENTS, true)); }
/** * Download an object from S3. * * @param bucket * The name of the bucket * @param key * The file key of the object to upload/update. * @param result * The Hash map where data from the object are written * */ protected Status readFromStorage(String bucket, String key, Map<String, ByteIterator> result, SSECustomerKey ssecLocal) { try { S3Object object = getS3ObjectAndMetadata(bucket, key, ssecLocal); InputStream objectData = object.getObjectContent(); //consuming the stream // writing the stream to bytes and to results result.put(key, new ByteArrayByteIterator(IOUtils.toByteArray(objectData))); objectData.close(); object.close(); } catch (Exception e){ System.err.println("Not possible to get the object "+key); e.printStackTrace(); return Status.ERROR; } return Status.OK; }
/** * Creates a private key from the file given, either in RSA private key * (.pem) or pkcs8 (.der) format. Other formats will cause an exception to * be thrown. */ public static PrivateKey loadPrivateKey(File privateKeyFile) throws InvalidKeySpecException, IOException { if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".pem") ) { InputStream is = new FileInputStream(privateKeyFile); try { return PEM.readPrivateKey(is); } finally { try {is.close();} catch(IOException ignore) {} } } else if ( StringUtils.lowerCase(privateKeyFile.getAbsolutePath()).endsWith(".der") ) { InputStream is = new FileInputStream(privateKeyFile); try { return RSA.privateKeyFromPKCS8(IOUtils.toByteArray(is)); } finally { try {is.close();} catch(IOException ignore) {} } } else { throw new AmazonClientException("Unsupported file type for private key"); } }
public GetIntrospectionSchemaResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetIntrospectionSchemaResult getIntrospectionSchemaResult = new GetIntrospectionSchemaResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getIntrospectionSchemaResult.setSchema(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getIntrospectionSchemaResult; }
public DeleteThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { DeleteThingShadowResult deleteThingShadowResult = new DeleteThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { deleteThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return deleteThingShadowResult; }
public GetThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetThingShadowResult getThingShadowResult = new GetThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getThingShadowResult; }
public UpdateThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { UpdateThingShadowResult updateThingShadowResult = new UpdateThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { updateThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return updateThingShadowResult; }
@Test public void tesGetWorkflowById() throws Exception { when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(new Workflow()); Workflow workflow = executionDAOFacade.getWorkflowById("workflowId", true); assertNotNull(workflow); verify(indexDAO, never()).get(any(), any()); when(executionDAO.getWorkflow(any(), anyBoolean())).thenReturn(null); InputStream stream = ExecutionDAOFacadeTest.class.getResourceAsStream("/test.json"); byte[] bytes = IOUtils.toByteArray(stream); String jsonString = new String(bytes); when(indexDAO.get(any(), any())).thenReturn(jsonString); workflow = executionDAOFacade.getWorkflowById("workflowId", true); assertNotNull(workflow); verify(indexDAO, times(1)).get(any(), any()); }
public GetSdkResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetSdkResult getSdkResult = new GetSdkResult(); if (context.isStartOfDocument()) { if (context.getHeader("Content-Type") != null) { context.setCurrentHeader("Content-Type"); getSdkResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("Content-Disposition") != null) { context.setCurrentHeader("Content-Disposition"); getSdkResult.setContentDisposition(context.getUnmarshaller(String.class).unmarshall(context)); } } java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getSdkResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getSdkResult; }
public GetExportResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetExportResult getExportResult = new GetExportResult(); if (context.isStartOfDocument()) { if (context.getHeader("Content-Type") != null) { context.setCurrentHeader("Content-Type"); getExportResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("Content-Disposition") != null) { context.setCurrentHeader("Content-Disposition"); getExportResult.setContentDisposition(context.getUnmarshaller(String.class).unmarshall(context)); } } java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getExportResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getExportResult; }
public InvokeEndpointResult unmarshall(JsonUnmarshallerContext context) throws Exception { InvokeEndpointResult invokeEndpointResult = new InvokeEndpointResult(); if (context.isStartOfDocument()) { if (context.getHeader("Content-Type") != null) { context.setCurrentHeader("Content-Type"); invokeEndpointResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("x-Amzn-Invoked-Production-Variant") != null) { context.setCurrentHeader("x-Amzn-Invoked-Production-Variant"); invokeEndpointResult.setInvokedProductionVariant(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("X-Amzn-SageMaker-Custom-Attributes") != null) { context.setCurrentHeader("X-Amzn-SageMaker-Custom-Attributes"); invokeEndpointResult.setCustomAttributes(context.getUnmarshaller(String.class).unmarshall(context)); } } java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { invokeEndpointResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return invokeEndpointResult; }
public InvokeResult unmarshall(JsonUnmarshallerContext context) throws Exception { InvokeResult invokeResult = new InvokeResult(); if (context.isStartOfDocument()) { if (context.getHeader("X-Amz-Function-Error") != null) { context.setCurrentHeader("X-Amz-Function-Error"); invokeResult.setFunctionError(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("X-Amz-Log-Result") != null) { context.setCurrentHeader("X-Amz-Log-Result"); invokeResult.setLogResult(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("X-Amz-Executed-Version") != null) { context.setCurrentHeader("X-Amz-Executed-Version"); invokeResult.setExecutedVersion(context.getUnmarshaller(String.class).unmarshall(context)); } } invokeResult.setStatusCode(context.getHttpResponse().getStatusCode()); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { invokeResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return invokeResult; }
/** * Reads and returns the rest of the given input stream as a string, closing * the input stream afterwards. * @param is the input stream. * @return the rest of the given input stream. */ public static String toString(InputStream is) throws IOException { return new String(toByteArray(is), StringUtils.UTF8); }
/** * Reads and returns the rest of the given input stream as a string. * Caller is responsible for closing the given input stream. */ public static String toString(InputStream is) throws IOException { return new String(toByteArray(is), StringUtils.UTF8); }
public UpdateThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { UpdateThingShadowResult updateThingShadowResult = new UpdateThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { updateThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils .toByteArray(is))); } return updateThingShadowResult; }
public DeleteThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { DeleteThingShadowResult deleteThingShadowResult = new DeleteThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { deleteThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils .toByteArray(is))); } return deleteThingShadowResult; }
public GetThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetThingShadowResult getThingShadowResult = new GetThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { getThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils .toByteArray(is))); } return getThingShadowResult; }
@Test public void test() throws Exception { PutEventsRequest putEventsRequest = new PutEventsRequest(); List<Event> events = new ArrayList<Event>(); events.add(createEvent()); events.add(createEvent()); putEventsRequest.setEvents(events); PutEventsRequestMarshaller marshaller = new PutEventsRequestMarshaller(); Request<PutEventsRequest> request = marshaller.marshall(putEventsRequest); assertEquals("content encoding", "gzip", request.getHeaders().get("Content-Encoding")); byte[] content = IOUtils.toByteArray(request.getContent()); System.out.println(content.length); assertEquals("content length", request.getHeaders().get("Content-Length"), String.valueOf(content.length)); GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(content)); String str = IOUtils.toString(gis); assertTrue("data is compressed", content.length < str.length()); }
@Override public InvokeResult unmarshall(JsonUnmarshallerContext context) throws Exception { InvokeResult invokeResult = new InvokeResult(); if (context.getHeader("X-Amz-Function-Error") != null) invokeResult.setFunctionError(context.getHeader("X-Amz-Function-Error")); if (context.getHeader("X-Amz-Log-Result") != null) invokeResult.setLogResult(context.getHeader("X-Amz-Log-Result")); invokeResult.setStatusCode(context.getHttpResponse().getStatusCode()); ByteBuffer payload = EMPTY_BYTEBUFFER; InputStream content = context.getHttpResponse().getContent(); if (content != null) { payload = ByteBuffer.wrap(IOUtils.toByteArray(content)); } invokeResult.setPayload(payload); return invokeResult; }