Package org.jets3t.service.impl.rest.httpclient

Examples of org.jets3t.service.impl.rest.httpclient.RestStorageService


            cleanupBucketForTest("testListBuckets");
        }
    }

    public void testBucketManagement() throws Exception {
        RestStorageService service = getStorageService(getCredentials());

        try {
            service.createBucket("");
            fail("Cannot create a bucket with empty name");
        } catch (ServiceException e) {
        }

        try {
            service.createBucket("test");
            fail("Cannot create a bucket with non-unique name");
        } catch (ServiceException e) {
        }

        String bucketName = createBucketForTest("testBucketManagement").getName();

        boolean bucketExists = service.isBucketAccessible(bucketName);
        assertTrue("Bucket should exist", bucketExists);

        try {
            service.deleteBucket((String) null);
            fail("Cannot delete a bucket with null name");
        } catch (ServiceException e) {
        }

        try {
            service.deleteBucket("");
            fail("Cannot delete a bucket with empty name");
        } catch (ServiceException e) {
        }

        try {
            service.deleteBucket("test");
            fail("Cannot delete a bucket you don't own");
        } catch (ServiceException e) {
        }

        // Ensure we can delete our bucket
        service.deleteBucket(bucketName);
    }
View Full Code Here


        service.deleteBucket(bucketName);
    }

    public void testBucketStatusLookup() throws Exception {
        String bucketName = getBucketNameForTest("testBucketStatusLookup");
        RestStorageService service = getStorageService(getCredentials());

        try {
            // Non-existent bucket
            int status = service.checkBucketStatus(bucketName);
            assertEquals(S3Service.BUCKET_STATUS__DOES_NOT_EXIST, status);

            // Bucket is owned by someone else
            // NOTE: This test will fail if you actually own the "testing" bucket,
            // or if it is owned by someone else but has been made publicly readable.
            status = service.checkBucketStatus("testing");
            assertEquals(S3Service.BUCKET_STATUS__ALREADY_CLAIMED, status);

            service.createBucket(bucketName);
            // Bucket now exists and is owned by me.
            status = service.checkBucketStatus(bucketName);
            assertEquals(S3Service.BUCKET_STATUS__MY_BUCKET, status);
        } finally {
            // Clean up
            service.deleteBucket(bucketName);
        }
    }
View Full Code Here

            service.deleteBucket(bucketName);
        }
    }

    public void testBucketLocations() throws Exception {
        RestStorageService service = getStorageService(getCredentials());
        // Create bucket in default (null) location
        String bucketName = getBucketNameForTest("testBucketLocations-test1");
        try {
            service.createBucket(bucketName);
            assertEquals(StorageService.BUCKET_STATUS__MY_BUCKET, service.checkBucketStatus(bucketName));
            String bucketLocation = (service instanceof S3Service
                ? ((S3Service) service).getBucketLocation(bucketName)
                : ((GoogleStorageService) service).getBucketLocation(bucketName) );
            if (TARGET_SERVICE_S3.equals(getTargetService())) {
                assertEquals(null, bucketLocation); // For S3, the default/null location is literally null
            } else {
                assertEquals("US", bucketLocation); // For GS, "US" is default/null location
            }
        } finally {
            service.deleteBucket(bucketName);
        }
        // Create bucket in alternate location
        bucketName = getBucketNameForTest("testBucketLocations-test2");
        try {
            StorageBucket newBucket = null;
            String targetLocation = null;
            if (TARGET_SERVICE_S3.equals(getTargetService())) {
                targetLocation = S3Bucket.LOCATION_US_WEST;
                newBucket = new S3Bucket(bucketName, targetLocation);
            } else {
                targetLocation = GSBucket.LOCATION_EUROPE;
                newBucket = new GSBucket(bucketName, targetLocation);
            }
            service.createBucket(newBucket);
            assertEquals(StorageService.BUCKET_STATUS__MY_BUCKET, service.checkBucketStatus(bucketName));
            String bucketLocation = (service instanceof S3Service
                ? ((S3Service) service).getBucketLocation(bucketName)
                : ((GoogleStorageService) service).getBucketLocation(bucketName) );
            assertEquals(targetLocation, bucketLocation);
        } finally {
            service.deleteBucket(bucketName);
        }
    }
View Full Code Here

        }
    }

    public void testObjectManagement() throws Exception {
        String bucketName = createBucketForTest("testObjectManagement").getName();
        RestStorageService service = getStorageService(getCredentials());

        try {
            StorageObject object = new StorageObject("TestObject");

            try {
                service.putObject((String) null, null);
                fail("Cannot create an object without a valid bucket");
            } catch (ServiceException e) {
            }

            try {
                service.putObject((String) null, object);
                fail("Cannot create an object without a valid bucket");
            } catch (ServiceException e) {
            }

            try {
                service.putObject(bucketName, new StorageObject());
                fail("Cannot create an object without a valid object");
            } catch (ServiceException e) {
            }

            // Create basic object with no content type (use the default) and no data.
            StorageObject basicObject = service.putObject(bucketName, object);

            // Ensure Content-Type is set to binary by default
            // TODO: Google Storage bug: Content type returned on initial PUT is always "text/html"
            if (!TARGET_SERVICE_GS.equals(getTargetService())) {
                assertTrue("Unexpected default content type",
                    Mimetypes.MIMETYPE_OCTET_STREAM.equals(basicObject.getContentType()));
            }

            // Re-retrieve object to ensure it was correctly created.
            basicObject = service.getObject(bucketName, object.getKey());
            assertEquals("Unexpected content type",
                Mimetypes.MIMETYPE_OCTET_STREAM, basicObject.getContentType());
            assertEquals("Unexpected size for 'empty' object", 0, basicObject.getContentLength());
            basicObject.closeDataInputStream();

            // Make sure bucket cannot be removed while it has contents.
            try {
                service.deleteBucket(bucketName);
                fail("Should not be able to delete a bucket containing objects");
            } catch (ServiceException e) {
            }

            // Update/overwrite object with real data content and some metadata.
            String contentType = "text/plain";
            String objectData = "Just some rubbish text to include as data";
            String dataMd5HashAsHex = ServiceUtils.toHex(
                ServiceUtils.computeMD5Hash(objectData.getBytes()));
            HashMap<String, Object> metadata = new HashMap<String, Object>();
            metadata.put("creator", "testObjectManagement");
            metadata.put("purpose", "For testing purposes");
            object.replaceAllMetadata(metadata);
            object.setContentType(contentType);
            object.setDataInputStream(new ByteArrayInputStream(objectData.getBytes()));
            StorageObject dataObject = service.putObject(bucketName, object);
            // TODO: Google Storage bug: Content type returned on initial PUT is always "text/html"
            if (TARGET_SERVICE_GS.equals(getTargetService())) {
                dataObject = service.getObject(bucketName, object.getKey());
            }
            assertEquals("Unexpected content type", contentType, dataObject.getContentType());
            assertEquals("Mismatching MD5 hex hash", dataMd5HashAsHex, dataObject.getETag());

            // Retrieve data object to ensure it was correctly created, the server-side hash matches
            // what we expect, and we get our metadata back.
            dataObject = service.getObject(bucketName, object.getKey());
            assertEquals("Unexpected default content type", "text/plain", dataObject.getContentType());
            // TODO: Google Storage doesn't reliably return Content-Length in a GET
            if (!TARGET_SERVICE_GS.equals(getTargetService())) {
                assertEquals("Unexpected content-length for object",
                    objectData.length(), dataObject.getContentLength());
            }
            assertEquals("Mismatching hash", dataMd5HashAsHex, dataObject.getETag());

            // Check user's data are available in basic metadata map
            assertEquals("Missing creator metadata", "testObjectManagement",
                dataObject.getMetadata("creator"));
            assertEquals("Missing purpose metadata", "For testing purposes",
                dataObject.getMetadata("purpose"));

            // Check data are available in user metadata map
            assertEquals("Missing creator user metadata",
                "testObjectManagement", dataObject.getUserMetadataMap().get("creator"));
            assertEquals("Missing purpose user metadata",
                "For testing purposes", dataObject.getUserMetadataMap().get("purpose"));
            assertNotNull("Expected data input stream to be available", dataObject.getDataInputStream());

            // Check data are available in service metadata map
            assertNotNull(dataObject.getServiceMetadataMap().get("request-id"));

            // Ensure we can get the data from S3.
            StringBuffer sb = new StringBuffer();
            int b = -1;
            while ((b = dataObject.getDataInputStream().read()) != -1) {
                sb.append((char) b);
            }
            dataObject.closeDataInputStream();
            assertEquals("Mismatching data", objectData, sb.toString());

            // Retrieve only HEAD of data object (all metadata is available, but not the object content
            // data input stream)
            dataObject = service.getObjectDetails(bucketName, object.getKey());
            assertEquals("Unexpected default content type", "text/plain", dataObject.getContentType());
            assertEquals("Mismatching hash", dataMd5HashAsHex, dataObject.getETag());
            assertEquals("Missing creator metadata", "testObjectManagement",
                dataObject.getMetadata("creator"));
            assertEquals("Missing purpose metadata", "For testing purposes",
                dataObject.getMetadata("purpose"));
            assertNull("Expected data input stream to be unavailable", dataObject.getDataInputStream());
            assertEquals("Unexpected size for object", objectData.length(), dataObject.getContentLength());

            // Test object GET constraints.
            Calendar objectCreationTimeCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.US);
            objectCreationTimeCal.setTime(dataObject.getLastModifiedDate());

            Calendar yesterday = (Calendar) objectCreationTimeCal.clone();
            yesterday.add(Calendar.DAY_OF_YEAR, -1);
            Calendar tomorrow = (Calendar) objectCreationTimeCal.clone();
            tomorrow.add(Calendar.DAY_OF_YEAR, +2);

            // Precondition: Modified since yesterday
            service.getObjectDetails(bucketName, object.getKey(), yesterday, null, null, null);
            // Precondition: Mot modified since after creation date.
            try {
                service.getObjectDetails(bucketName, object.getKey(), objectCreationTimeCal, null, null, null);
                fail("Cannot have been modified since object was created");
            } catch (ServiceException e) { }
            // Precondition: Not modified since yesterday
            try {
                service.getObjectDetails(bucketName, object.getKey(), null, yesterday, null, null);
                fail("Cannot be unmodified since yesterday");
            } catch (ServiceException e) { }
            // Precondition: Not modified since tomorrow
            service.getObjectDetails(bucketName, object.getKey(), null, tomorrow, null, null);
            // Precondition: matches correct hash
            service.getObjectDetails(bucketName, object.getKey(), null, null, new String[] {dataMd5HashAsHex}, null);
            // Precondition: doesn't match incorrect hash
            try {
                service.getObjectDetails(bucketName, object.getKey(), null, null,
                    new String[] {"__" + dataMd5HashAsHex.substring(2)}, null);
                fail("Hash values should not match");
            } catch (ServiceException e) {
            }
            // Precondition: doesn't match correct hash
            try {
                service.getObjectDetails(bucketName, object.getKey(), null, null, null, new String[] {dataMd5HashAsHex});
                fail("Hash values should mis-match");
            } catch (ServiceException e) {
            }
            // Precondition: doesn't match incorrect hash
            service.getObjectDetails(bucketName, object.getKey(), null, null, null,
                new String[] {"__" + dataMd5HashAsHex.substring(2)});

            // Retrieve only a limited byte-range of the data, with a start and end.
            Long byteRangeStart = new Long(3);
            Long byteRangeEnd = new Long(12);
            dataObject = service.getObject(bucketName, object.getKey(), null, null, null, null, byteRangeStart, byteRangeEnd);
            String dataReceived = ServiceUtils.readInputStreamToString(
                dataObject.getDataInputStream(), Constants.DEFAULT_ENCODING);
            String dataExpected = objectData.substring(byteRangeStart.intValue(), byteRangeEnd.intValue() + 1);
            assertEquals("Mismatching data from range precondition", dataExpected, dataReceived);

            // Retrieve only a limited byte-range of the data, with a start range only.
            byteRangeStart = new Long(7);
            byteRangeEnd = null;
            dataObject = service.getObject(bucketName, object.getKey(), null, null, null, null, byteRangeStart, byteRangeEnd);
            dataReceived = ServiceUtils.readInputStreamToString(
                dataObject.getDataInputStream(), Constants.DEFAULT_ENCODING);
            dataExpected = objectData.substring(byteRangeStart.intValue());
            assertEquals("Mismatching data from range precondition", dataExpected, dataReceived);

            // Retrieve only a limited byte-range of the data, with an end range only.
            byteRangeStart = null;
            byteRangeEnd = new Long(13);
            dataObject = service.getObject(bucketName, object.getKey(), null, null, null, null, byteRangeStart, byteRangeEnd);
            dataReceived = ServiceUtils.readInputStreamToString(
                dataObject.getDataInputStream(), Constants.DEFAULT_ENCODING);
            dataExpected = objectData.substring(objectData.length() - byteRangeEnd.intValue());
            assertEquals("Mismatching data from range precondition", dataExpected, dataReceived);

            // Clean-up.
            service.deleteObject(bucketName, object.getKey());

            // Create object with tricky key.
            String trickyKey = "http://example.site.com/some/path/document name.html?param1=a@b#c$d&param2=(089)";
            StorageObject trickyObject = service.putObject(bucketName,
                new StorageObject(trickyKey, "Some test data"));
            assertEquals("Tricky key name mistmatch", trickyKey, trickyObject.getKey());

            // Make sure the tricky named object really exists with its full name.
            StorageObject[] objects = service.listObjects(bucketName);
            boolean trickyNamedObjectExists = false;
            for (int i = 0; !trickyNamedObjectExists && i < objects.length; i++) {
                if (trickyKey.equals(objects[i].getKey())) {
                    trickyNamedObjectExists = true;
                }
            }
            assertTrue("Tricky key name object does not exist with its full name", trickyNamedObjectExists);

            // Delete object with tricky key.
            service.deleteObject(bucketName, trickyObject.getKey());

        } finally {
            cleanupBucketForTest("testObjectManagement");
        }
    }
View Full Code Here

        }
    }

    public void testDirectoryPlaceholderObjects() throws Exception {
        String bucketName = createBucketForTest("testDirectoryPlaceholderObjects").getName();
        RestStorageService service = getStorageService(getCredentials());

        try {
            // Create new-style place-holder object (compatible with Amazon's AWS Console
            // and Panic's Transmit) -- note trailing slash
            StorageObject requestObject = new StorageObject("DirPlaceholderObject/");
            requestObject.setContentLength(0);
            requestObject.setContentType(Mimetypes.MIMETYPE_BINARY_OCTET_STREAM);
            assertTrue(requestObject.isDirectoryPlaceholder());

            service.putObject(bucketName, requestObject);
            StorageObject resultObject = service.getObjectDetails(bucketName, requestObject.getKey());
            assertTrue(resultObject.isDirectoryPlaceholder());

            // Create legacy-style place-holder object (compatible with objects stored using
            // JetS3t applications prior to version 0.8.0) -- note content type
            requestObject = new StorageObject("LegacyDirPlaceholderObject");
            requestObject.setContentLength(0);
            requestObject.setContentType(Mimetypes.MIMETYPE_JETS3T_DIRECTORY);
            assertTrue(requestObject.isDirectoryPlaceholder());

            service.putObject(bucketName, requestObject);
            resultObject = service.getObjectDetails(bucketName, requestObject.getKey());
            assertTrue(resultObject.isDirectoryPlaceholder());

            // Create place-holder object compatible with the S3 Organizer Firefox extension
            // -- note object name suffix.
            requestObject = new StorageObject("S3OrganizerDirPlaceholderObject_$folder$");
            requestObject.setContentLength(0);
            assertTrue(requestObject.isDirectoryPlaceholder());

            service.putObject(bucketName, requestObject);
            resultObject = service.getObjectDetails(bucketName, requestObject.getKey());
            assertTrue(resultObject.isDirectoryPlaceholder());
        } finally {
            cleanupBucketForTest("testDirectoryPlaceholderObjects");
        }
    }
View Full Code Here

    }

    public void testCopyObjects() throws Exception {
        String sourceBucketName = createBucketForTest("testCopyObjects-source").getName();
        String targetBucketName = createBucketForTest("testCopyObjects-target").getName();
        RestStorageService service = getStorageService(getCredentials());
        try {
            // Create source objects to copy, with potentially troublesome names
            String[] objectNames = new String[] {
                "testing.txt",
                "test me.txt",
                "virtual-path/testing.txt",
                "vîrtüál-πå†h/tés†ing.txt"
            };

            for (int i = 0; i < objectNames.length; i++) {
                StorageObject object = new StorageObject(
                    objectNames[i], "A little data");
                object.addMetadata("object-offset", "" + i);
                service.putObject(sourceBucketName, object);
            }

            // Copy objects within bucket, retaining metadata
            String targetPath = "copies/";
            for (String objectName: objectNames) {
                StorageObject targetObject = new StorageObject(
                    targetPath + objectName);
                service.copyObject(sourceBucketName, objectName,
                    sourceBucketName, targetObject,
                    false // replaceMetadata
                    );
            }
            // Ensure objects are in target location and have the same metadata
            for (int i = 0; i < objectNames.length; i++) {
                StorageObject object = service.getObjectDetails(
                    sourceBucketName, targetPath + objectNames[i]);
                assertEquals("" + i, object.getMetadata("object-offset"));
            }

            // Copy object within bucket, replacing metadata
            StorageObject targetObject = new StorageObject(
                targetPath + objectNames[0]);
            targetObject.addMetadata("replaced-metadata", "booyah!");
            service.copyObject(sourceBucketName, objectNames[0],
                sourceBucketName, targetObject,
                true // replaceMetadata
                );
            StorageObject copiedObject = service.getObjectDetails(
                sourceBucketName, targetObject.getName());
            assertNull(copiedObject.getMetadata("object-offset"));
            assertEquals("booyah!", copiedObject.getMetadata("replaced-metadata"));

            // Copy objects between buckets
            List<Map<String, Object>> copyResults =
                new ArrayList<Map<String, Object>>();
            for (String objectName: objectNames) {
                targetObject = new StorageObject(objectName);
                Map<String, Object> copyResult = service.copyObject(
                    sourceBucketName, objectName,
                    targetBucketName, targetObject,
                    false // replaceMetadata
                    );
                copyResults.add(copyResult);
                copiedObject = service.getObjectDetails(
                    targetBucketName, targetObject.getName());
            }
            assertEquals(4, service.listObjects(targetBucketName).length);
            // Check results map from copy operation
            Map<String, Object> firstCopyResult = copyResults.get(0);
            assertEquals("\"5d11eb8a313fc3e205fef245b7be06c7\"",
                firstCopyResult.get("ETag"));
            assertEquals(sourceBucketName,
                firstCopyResult.get("X-JetS3t-SourceBucketName"));
            assertEquals(objectNames[0],
                firstCopyResult.get("X-JetS3t-SourceObjectKey"));
            assertEquals(targetBucketName,
                firstCopyResult.get("X-JetS3t-DestinationBucketName"));
            assertEquals(objectNames[0], // Note same source and target key
                firstCopyResult.get("X-JetS3t-DestinationObjectKey"));
            assertNull(
                firstCopyResult.get("X-JetS3t-VersionId"));
            assertNull(
                firstCopyResult.get("X-JetS3t-DestinationObjectStorageClass"));
            assertNull(
                firstCopyResult.get("X-JetS3t-DestinationObjectServerSideEncryptionAlgorithm"));

            // Rename convenience method
            int objectOffset = 3;
            targetObject = new StorageObject("my-new-name");
            service.renameObject(
                sourceBucketName, objectNames[objectOffset], targetObject);
            copiedObject = service.getObjectDetails(
                sourceBucketName, targetObject.getName());
            // Ensure we have a new object with the same metadata
            assertEquals("my-new-name", copiedObject.getKey());
            assertEquals("" + objectOffset, copiedObject.getMetadata("object-offset"));

            // Update metadata convenience method
            objectOffset = 2;
            targetObject = new StorageObject(objectNames[objectOffset]);
            targetObject.addMetadata("object-offset", "" + objectOffset); // Unchanged
            targetObject.addMetadata("was-i-updated", "yes!");
            service.updateObjectMetadata(sourceBucketName, targetObject);
            copiedObject = service.getObjectDetails(
                sourceBucketName, targetObject.getName());
            // Ensure we have the same object with updated metadata
            assertEquals("yes!", copiedObject.getUserMetadataMap().get("was-i-updated"));

            // Move object convenience method - retain metadata
            objectOffset = 0;
            targetObject = new StorageObject(objectNames[objectOffset]);
            service.moveObject(sourceBucketName, objectNames[objectOffset],
                targetBucketName, targetObject, false);
            try {
                service.getObjectDetails(
                    sourceBucketName, objectNames[objectOffset]);
                fail("Source object should be moved");
            } catch (ServiceException e) {
                // Expected
            }
            copiedObject = service.getObjectDetails(
                targetBucketName, targetObject.getName());
            assertEquals("" + objectOffset, copiedObject.getUserMetadataMap().get("object-offset"));

            // Move object convenience method - replace metadata
            objectOffset = 1;
            targetObject = new StorageObject(objectNames[objectOffset]);
            targetObject.addMetadata("was-i-moved-with-new-metadata", "yes!");
            service.moveObject(sourceBucketName, objectNames[objectOffset],
                targetBucketName, targetObject, true);
            try {
                service.getObjectDetails(
                    sourceBucketName, objectNames[objectOffset]);
                fail("Source object should be moved");
            } catch (ServiceException e) {
                // Expected
            }
            copiedObject = service.getObjectDetails(
                targetBucketName, targetObject.getName());
            assertNull(copiedObject.getMetadata("object-offset"));
            assertEquals("yes!", copiedObject.getMetadata("was-i-moved-with-new-metadata"));
        } finally {
            cleanupBucketForTest("testCopyObjects-source");
View Full Code Here

        }
    }

    public void testUnicodeData() throws Exception {
        String bucketName = createBucketForTest("testUnicodeData").getName();
        RestStorageService service = getStorageService(getCredentials());

        try {
            // Unicode object name
            String unicodeText = "テストオブジェクト";
            StorageObject requestObject = new StorageObject("1." + unicodeText);
            service.putObject(bucketName, requestObject);
            StorageObject resultObject = service.getObjectDetails(bucketName, requestObject.getKey());
            assertEquals("1." + unicodeText, resultObject.getKey());

            // Unicode data content
            requestObject = new StorageObject("2." + unicodeText, unicodeText);
            service.putObject(bucketName, requestObject);
            resultObject = service.getObject(bucketName, requestObject.getKey());
            String data = ServiceUtils.readInputStreamToString(
                resultObject.getDataInputStream(), "UTF-8");
            assertEquals(unicodeText, data);

            // Unicode metadata values are not supported
            requestObject = new StorageObject("3." + unicodeText);
            requestObject.addMetadata("testing", unicodeText);
            try {
                service.putObject(bucketName, requestObject);
            } catch (ServiceException e) {
            }

            // Unicode metadata values can be encoded
            requestObject = new StorageObject("4." + unicodeText);
            requestObject.addMetadata("testing", URLEncoder.encode(unicodeText, "UTF-8"));
            service.putObject(bucketName, requestObject);
            resultObject = service.getObjectDetails(bucketName, requestObject.getKey());
            assertEquals(unicodeText, URLDecoder.decode(
                (String) resultObject.getMetadata("testing"), "UTF-8"));

            // Unicode metadata names are not possible with HTTP
            requestObject = new StorageObject("5." + unicodeText);
            requestObject.addMetadata(unicodeText, "value");
            try {
                service.putObject(bucketName, requestObject);
                fail("Illegal to use non-ASCII characters in HTTP headers");
            } catch (ServiceException e) {
            }

            // Unicode HTTP headers (via RFC 5987 encoding) -- not working...
View Full Code Here

TOP

Related Classes of org.jets3t.service.impl.rest.httpclient.RestStorageService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.