List of usage examples for org.apache.http.conn EofSensorInputStream EofSensorInputStream
public EofSensorInputStream(final InputStream in, final EofSensorWatcher watcher)
From source file:com.joyent.manta.http.EncryptedHttpHelperTest.java
/** * Builds a fully mocked {@link EncryptionHttpHelper} that is setup to * be configured for one cipher/mode and executes requests in another * cipher/mode.//from w ww .j a va2s.c o m */ private static EncryptionHttpHelper fakeEncryptionHttpHelper(String path) throws Exception { MantaConnectionContext connectionContext = mock(MantaConnectionContext.class); StandardConfigContext config = new StandardConfigContext(); SupportedCipherDetails cipherDetails = AesCbcCipherDetails.INSTANCE_192_BIT; config.setClientEncryptionEnabled(true) .setEncryptionPrivateKeyBytes(SecretKeyUtils.generate(cipherDetails).getEncoded()) .setEncryptionAlgorithm(cipherDetails.getCipherId()); EncryptionHttpHelper httpHelper = new EncryptionHttpHelper(connectionContext, new MantaHttpRequestFactory(UnitTestConstants.UNIT_TEST_URL), config); URI uri = URI.create(DEFAULT_MANTA_URL + "/" + path); CloseableHttpResponse fakeResponse = mock(CloseableHttpResponse.class); StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"); SupportedCipherDetails objectCipherDetails = AesGcmCipherDetails.INSTANCE_128_BIT; Header[] headers = new Header[] { // Notice this is a different cipher than the one set in config new BasicHeader(MantaHttpHeaders.ENCRYPTION_CIPHER, objectCipherDetails.getCipherId()) }; when(fakeResponse.getAllHeaders()).thenReturn(headers); when(fakeResponse.getStatusLine()).thenReturn(statusLine); BasicHttpEntity fakeEntity = new BasicHttpEntity(); InputStream source = IOUtils.toInputStream("I'm a stream", StandardCharsets.US_ASCII); EofSensorInputStream stream = new EofSensorInputStream(source, null); fakeEntity.setContent(stream); when(fakeResponse.getEntity()).thenReturn(fakeEntity); when(connectionContext.getHttpClient()).thenReturn(new FakeCloseableHttpClient(fakeResponse)); return httpHelper; }
From source file:ee.ria.xroad.common.util.AbstractHttpSender.java
protected void handleResponse(HttpResponse response) throws Exception { log.trace("handleResponse()"); checkResponseStatus(response);//w w w . j a v a 2 s. c om responseHeaders = getResponseHeaders(response); this.responseEntity = getResponseEntity(response); responseContentType = getResponseContentType(responseEntity, this.request instanceof HttpGet); // Wrap the response input stream in order to catch EOF errors. responseContent = new EofSensorInputStream(responseEntity.getContent(), new ResponseStreamWatcher()); }
From source file:com.joyent.manta.client.multipart.EncryptedMultipartManagerTest.java
public void canDoMultipartUpload() throws Exception { MantaMetadata metadata = new MantaMetadata(); metadata.put("m-my-key", "my value"); metadata.put("e-my-key-1", "my value 1"); metadata.put("e-my-key-2", "my value 2"); MantaHttpHeaders headers = new MantaHttpHeaders(); String path = "/user/stor/testobject"; EncryptedMultipartUpload<TestMultipartUpload> upload = manager.initiateUpload(path, 35L, metadata, headers); ArrayList<String> lines = new ArrayList<>(); lines.add("01234567890ABCDEF|}{"); lines.add("ZYXWVUTSRQPONMLKJIHG"); lines.add("!@#$%^&*()_+-=[]/,.<"); lines.add(">~`?abcdefghijklmnop"); lines.add("qrstuvxyz"); String expected = StringUtils.join(lines, ""); MantaMultipartUploadPart[] parts = new MantaMultipartUploadPart[5]; for (int i = 0; i < lines.size(); i++) { parts[i] = manager.uploadPart(upload, i + 1, lines.get(i)); }//from w w w. j av a2 s.co m Stream<MantaMultipartUploadTuple> partsStream = Stream.of(parts); manager.complete(upload, partsStream); TestMultipartUpload actualUpload = upload.getWrapped(); MantaMetadata actualMetadata = MantaObjectMapper.INSTANCE.readValue(actualUpload.getMetadata(), MantaMetadata.class); Assert.assertEquals(actualMetadata, metadata, "Metadata wasn't stored correctly"); MantaHttpHeaders actualHeaders = MantaObjectMapper.INSTANCE.readValue(actualUpload.getHeaders(), MantaHttpHeaders.class); Assert.assertEquals(actualHeaders, headers, "Headers were stored correctly"); { Cipher cipher = cipherDetails.getCipher(); byte[] iv = upload.getEncryptionState().getEncryptionContext().getCipher().getIV(); cipher.init(Cipher.ENCRYPT_MODE, secretKey, cipherDetails.getEncryptionParameterSpec(iv)); byte[] assumedCiphertext = cipher.doFinal(expected.getBytes(StandardCharsets.US_ASCII)); byte[] contents = FileUtils.readFileToByteArray(upload.getWrapped().getContents()); byte[] actualCiphertext = Arrays.copyOf(contents, contents.length - cipherDetails.getAuthenticationTagOrHmacLengthInBytes()); try { AssertJUnit.assertEquals(assumedCiphertext, actualCiphertext); } catch (AssertionError e) { System.err.println("expected: " + Hex.encodeHexString(assumedCiphertext)); System.err.println("actual : " + Hex.encodeHexString(actualCiphertext)); throw e; } } EncryptionContext encryptionContext = upload.getEncryptionState().getEncryptionContext(); MantaHttpHeaders responseHttpHeaders = new MantaHttpHeaders(); responseHttpHeaders.setContentLength(actualUpload.getContents().length()); final String hmacName = SupportedHmacsLookupMap .hmacNameFromInstance(encryptionContext.getCipherDetails().getAuthenticationHmac()); responseHttpHeaders.put(MantaHttpHeaders.ENCRYPTION_HMAC_TYPE, hmacName); responseHttpHeaders.put(MantaHttpHeaders.ENCRYPTION_IV, Base64.getEncoder().encodeToString(encryptionContext.getCipher().getIV())); responseHttpHeaders.put(MantaHttpHeaders.ENCRYPTION_KEY_ID, cipherDetails.getCipherId()); MantaObjectResponse response = new MantaObjectResponse(path, responseHttpHeaders, metadata); CloseableHttpResponse httpResponse = mock(CloseableHttpResponse.class); try (InputStream fin = new FileInputStream(actualUpload.getContents()); EofSensorInputStream eofIn = new EofSensorInputStream(fin, null); MantaObjectInputStream objIn = new MantaObjectInputStream(response, httpResponse, eofIn); InputStream in = new MantaEncryptedObjectInputStream(objIn, cipherDetails, secretKey, true)) { String actual = IOUtils.toString(in, StandardCharsets.UTF_8); Assert.assertEquals(actual, expected); } }
From source file:com.joyent.manta.client.crypto.EncryptingEntityTest.java
private void validateCiphertext(SupportedCipherDetails cipherDetails, SecretKey secretKey, byte[] plaintext, byte[] iv, byte[] ciphertext) throws IOException { MantaHttpHeaders responseHttpHeaders = new MantaHttpHeaders(); responseHttpHeaders.setContentLength((long) ciphertext.length); if (!cipherDetails.isAEADCipher()) { responseHttpHeaders.put(MantaHttpHeaders.ENCRYPTION_HMAC_TYPE, SupportedHmacsLookupMap.hmacNameFromInstance(cipherDetails.getAuthenticationHmac())); }/*from w w w .ja va 2 s. c o m*/ responseHttpHeaders.put(MantaHttpHeaders.ENCRYPTION_IV, Base64.getEncoder().encodeToString(iv)); responseHttpHeaders.put(MantaHttpHeaders.ENCRYPTION_KEY_ID, cipherDetails.getCipherId()); final MantaEncryptedObjectInputStream decryptingStream = new MantaEncryptedObjectInputStream( new MantaObjectInputStream(new MantaObjectResponse("/path", responseHttpHeaders), Mockito.mock(CloseableHttpResponse.class), new EofSensorInputStream(new ByteArrayInputStream(ciphertext), null)), cipherDetails, secretKey, true); ByteArrayOutputStream decrypted = new ByteArrayOutputStream(); IOUtils.copy(decryptingStream, decrypted); decryptingStream.close(); AssertJUnit.assertArrayEquals(plaintext, decrypted.toByteArray()); }
From source file:com.joyent.manta.client.crypto.AbstractMantaEncryptedObjectInputStreamTest.java
protected MantaEncryptedObjectInputStream createEncryptedObjectInputStream(SecretKey key, InputStream in, long ciphertextSize, SupportedCipherDetails cipherDetails, byte[] iv, boolean authenticate, long plaintextSize, Long skipBytes, Long plaintextLength, boolean unboundedEnd) { String path = String.format("/test/stor/test-%s", UUID.randomUUID()); MantaHttpHeaders headers = new MantaHttpHeaders(); headers.put(MantaHttpHeaders.ENCRYPTION_CIPHER, cipherDetails.getCipherId()); headers.put(MantaHttpHeaders.ENCRYPTION_PLAINTEXT_CONTENT_LENGTH, plaintextSize); headers.put(MantaHttpHeaders.ENCRYPTION_KEY_ID, "unit-test-key"); if (cipherDetails.isAEADCipher()) { headers.put(MantaHttpHeaders.ENCRYPTION_AEAD_TAG_LENGTH, cipherDetails.getAuthenticationTagOrHmacLengthInBytes()); } else {// www. j a v a 2 s . c om final String hmacName = SupportedHmacsLookupMap .hmacNameFromInstance(cipherDetails.getAuthenticationHmac()); headers.put(MantaHttpHeaders.ENCRYPTION_HMAC_TYPE, hmacName); } headers.put(MantaHttpHeaders.ENCRYPTION_IV, Base64.getEncoder().encodeToString(iv)); headers.setContentLength(ciphertextSize); MantaMetadata metadata = new MantaMetadata(); MantaObjectResponse response = new MantaObjectResponse(path, headers, metadata); CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class); EofSensorInputStream eofSensorInputStream = new EofSensorInputStream(in, null); MantaObjectInputStream mantaObjectInputStream = new MantaObjectInputStream(response, httpResponse, eofSensorInputStream); return new MantaEncryptedObjectInputStream(mantaObjectInputStream, cipherDetails, key, authenticate, skipBytes, plaintextLength, unboundedEnd); }
From source file:com.joyent.manta.client.crypto.MantaEncryptedObjectInputStreamTest.java
private MantaEncryptedObjectInputStream createEncryptedObjectInputStream(SecretKey key, InputStream in, long ciphertextSize, SupportedCipherDetails cipherDetails, byte[] iv, boolean authenticate, long plaintextSize, Long skipBytes, Long plaintextLength, boolean unboundedEnd) { String path = String.format("/test/stor/test-%s", UUID.randomUUID()); MantaHttpHeaders headers = new MantaHttpHeaders(); headers.put(MantaHttpHeaders.ENCRYPTION_CIPHER, cipherDetails.getCipherId()); headers.put(MantaHttpHeaders.ENCRYPTION_PLAINTEXT_CONTENT_LENGTH, plaintextSize); headers.put(MantaHttpHeaders.ENCRYPTION_KEY_ID, "unit-test-key"); if (cipherDetails.isAEADCipher()) { headers.put(MantaHttpHeaders.ENCRYPTION_AEAD_TAG_LENGTH, cipherDetails.getAuthenticationTagOrHmacLengthInBytes()); } else {/*from w w w .j a v a 2s. c om*/ final String hmacName = SupportedHmacsLookupMap .hmacNameFromInstance(cipherDetails.getAuthenticationHmac()); headers.put(MantaHttpHeaders.ENCRYPTION_HMAC_TYPE, hmacName); } headers.put(MantaHttpHeaders.ENCRYPTION_IV, Base64.getEncoder().encodeToString(iv)); headers.setContentLength(ciphertextSize); MantaMetadata metadata = new MantaMetadata(); MantaObjectResponse response = new MantaObjectResponse(path, headers, metadata); CloseableHttpResponse httpResponse = Mockito.mock(CloseableHttpResponse.class); EofSensorInputStream eofSensorInputStream = new EofSensorInputStream(in, null); MantaObjectInputStream mantaObjectInputStream = new MantaObjectInputStream(response, httpResponse, eofSensorInputStream); return new MantaEncryptedObjectInputStream(mantaObjectInputStream, cipherDetails, key, authenticate, skipBytes, plaintextLength, unboundedEnd); }