@Override public long contentLength() throws IOException { return this.cloudFile.getProperties().getLength(); }
@Override public long lastModified() throws IOException { return this.cloudFile.getProperties().getLastModified().getTime(); }
protected void updateEtagAndLastModifiedFromResponse(HttpURLConnection request) { // ETag this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG)); // Last Modified if (0 != request.getLastModified()) { final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US); lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE); lastModifiedCalendar.setTime(new Date(request.getLastModified())); this.getProperties().setLastModified(lastModifiedCalendar.getTime()); } }
protected void updateEtagAndLastModifiedFromResponse(HttpURLConnection request) { // ETag this.getProperties().setEtag(request.getHeaderField(Constants.HeaderConstants.ETAG)); // Last Modified if (0 != request.getLastModified()) { final Calendar lastModifiedCalendar = Calendar.getInstance(Utility.LOCALE_US); lastModifiedCalendar.setTimeZone(Utility.UTC_ZONE); lastModifiedCalendar.setTime(new Date(request.getLastModified())); this.getProperties().setLastModified(lastModifiedCalendar.getTime()); } }
protected void updateLengthFromResponse(HttpURLConnection request) { final String xContentLengthHeader = request.getHeaderField(FileConstants.CONTENT_LENGTH_HEADER); if (!Utility.isNullOrEmpty(xContentLengthHeader)) { this.getProperties().setLength(Long.parseLong(xContentLengthHeader)); } }
protected void updateLengthFromResponse(HttpURLConnection request) { final String xContentLengthHeader = request.getHeaderField(FileConstants.CONTENT_LENGTH_HEADER); if (!Utility.isNullOrEmpty(xContentLengthHeader)) { this.getProperties().setLength(Long.parseLong(xContentLengthHeader)); } }
public static void setFileProperties(CloudFile file) { file.getProperties().setCacheControl("no-transform"); file.getProperties().setContentDisposition("attachment"); file.getProperties().setContentEncoding("gzip"); file.getProperties().setContentLanguage("tr,en"); file.getProperties().setContentMD5("MDAwMDAwMDA="); file.getProperties().setContentType("text/html"); } }
public static void assertAreEqual(CloudFile file1, CloudFile file2) { if (file1 == null) { Assert.assertNull(file2); } else { Assert.assertNotNull(file2); Assert.assertEquals(file1.getUri(), file2.getUri()); assertAreEqual(file1.getProperties(), file2.getProperties()); } }
/** * Commits the file. * * @throws StorageException * An exception representing any error which occurred during the operation. * @throws URISyntaxException */ @DoesServiceRequest private void commit() throws StorageException, URISyntaxException { if (this.options.getStoreFileContentMD5()) { this.parentFileRef.getProperties().setContentMD5(Base64.encode(this.md5Digest.digest())); } this.parentFileRef.uploadProperties(this.accessCondition, this.options, this.opContext); }
/** * Commits the file. * * @throws StorageException * An exception representing any error which occurred during the operation. * @throws URISyntaxException */ @DoesServiceRequest private void commit() throws StorageException, URISyntaxException { if (this.options.getStoreFileContentMD5()) { this.parentFileRef.getProperties().setContentMD5(Base64.encode(this.md5Digest.digest())); } this.parentFileRef.uploadProperties(this.accessCondition, this.options, this.opContext); }
protected static void doDownloadTest(CloudFile file, int fileSize, int bufferSize, int bufferOffset) throws StorageException, IOException, URISyntaxException { final Random randGenerator = new Random(); final byte[] buffer = new byte[fileSize]; randGenerator.nextBytes(buffer); byte[] resultBuffer = new byte[bufferSize]; FileRequestOptions options = new FileRequestOptions(); file.upload(new ByteArrayInputStream(buffer), buffer.length); file.downloadToByteArray(resultBuffer, bufferOffset, null, options, null); for (int i = 0; i < file.getProperties().getLength(); i++) { Assert.assertEquals(buffer[i], resultBuffer[bufferOffset + i]); } if (bufferOffset + fileSize < bufferSize) { for (int k = bufferOffset + fileSize; k < bufferSize; k++) { Assert.assertEquals(0, resultBuffer[k]); } } }
@Test public void testFileAttributesEncryption() throws URISyntaxException, StorageException, IOException { this.file.downloadAttributes(); assertTrue(this.file.getProperties().isServerEncrypted()); CloudFile testFile = this.share.getRootDirectoryReference().getFileReference("file"); testFile.downloadText(); assertTrue(testFile.getProperties().isServerEncrypted()); }
@Override public Void preProcessResponse(CloudFile file, CloudFileClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK) { this.setNonExceptionedRetryableFailure(true); return null; } file.getProperties().setLength(size); file.updateEtagAndLastModifiedFromResponse(this.getConnection()); this.getResult().setRequestServiceEncrypted(BaseResponse.isServerRequestEncrypted(this.getConnection())); return null; } };
@Override public Void preProcessResponse(CloudFile file, CloudFileClient client, OperationContext context) throws Exception { if (this.getResult().getStatusCode() != HttpURLConnection.HTTP_OK) { this.setNonExceptionedRetryableFailure(true); return null; } file.getProperties().setLength(size); file.updateEtagAndLastModifiedFromResponse(this.getConnection()); this.getResult().setRequestServiceEncrypted(BaseResponse.isServerRequestEncrypted(this.getConnection())); return null; } };
/** * Create a file and try to download a range of its contents * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testFileDownloadRangeValidationTest() throws StorageException, URISyntaxException, IOException { final int length = 5 * 1024 * 1024; final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.upload(FileTestHelper.getRandomDataStream(length), length); //Download full file fileRef.download(new ByteArrayOutputStream()); assertEquals(length, fileRef.getProperties().getLength()); //Download file range. byte[] downloadBuffer = new byte[100]; int downloadLength = fileRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0); assertEquals(length, fileRef.getProperties().getLength()); assertEquals(100, downloadLength); }
/** * Start copying a file and then abort * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testCopyFileAbort() throws StorageException, URISyntaxException, IOException { final int length = 128; CloudFile originalFile = FileTestHelper.uploadNewFile(this.share, length, null); CloudFile copyFile = this.share.getRootDirectoryReference().getFileReference(originalFile.getName() + "copyed"); copyFile.startCopy(originalFile); try { copyFile.abortCopy(copyFile.getProperties().getCopyState().getCopyId()); fail(); } catch (StorageException e) { if (!e.getErrorCode().contains("NoPendingCopyOperation")) { throw e; } } }
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testVerifyTransactionalMD5ValidationMissingOverallMD5() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); final int length = 3*1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); FileRequestOptions options = new FileRequestOptions(); options.setDisableContentMD5Validation(true); options.setStoreFileContentMD5(false); fileRef.upload(srcStream, length, null, options, null); options.setDisableContentMD5Validation(false); options.setStoreFileContentMD5(true); options.setUseTransactionalContentMD5(true); final CloudFile fileRef2 = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef2.downloadRange(1024, (long)1024, new ByteArrayOutputStream(), null, options, null); assertNull(fileRef2.getProperties().getContentMD5()); }
/** * Test file exits method. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileExists() throws URISyntaxException, StorageException { CloudFile file = this.share.getRootDirectoryReference().getFileReference("file1"); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference("file1"); assertFalse(file2.exists()); file.create(2048); assertTrue(file2.exists()); assertEquals(2048, file2.getProperties().getLength()); file.delete(); assertFalse(file2.exists()); }
/** * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test @Category({ DevFabricTests.class, DevStoreTests.class, SlowTests.class }) public void testFileNamePlusEncoding() throws StorageException, URISyntaxException, IOException, InterruptedException { CloudFile originalFile = FileTestHelper.uploadNewFile(this.share, 1024 /* length */, null); CloudFile copyFile = this.share.getRootDirectoryReference().getFileReference(originalFile.getName() + "copyed"); copyFile.startCopy(originalFile); FileTestHelper.waitForCopy(copyFile); copyFile.downloadAttributes(); originalFile.downloadAttributes(); FileProperties prop1 = copyFile.getProperties(); FileProperties prop2 = originalFile.getProperties(); assertEquals(prop1.getCacheControl(), prop2.getCacheControl()); assertEquals(prop1.getContentEncoding(), prop2.getContentEncoding()); assertEquals(prop1.getContentDisposition(), prop2.getContentDisposition()); assertEquals(prop1.getContentLanguage(), prop2.getContentLanguage()); assertEquals(prop1.getContentMD5(), prop2.getContentMD5()); assertEquals(prop1.getContentType(), prop2.getContentType()); }
/** * Test FileOutputStream. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String fileName = FileTestHelper.generateRandomFileName(); CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); try { file.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified resource does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } file.openWriteNew(1024); file.openWriteExisting(); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference(fileName); file2.downloadAttributes(); assertEquals(1024, file2.getProperties().getLength()); }