JCLOUDS-457: Added deleteArchive and uploadArchive operations.

Now the Glacier client supports upload and delete archive
operations.

An static TestUtils class has been made for the archive operations
tests. This class allows us to build payloads and build ByteSources.
This commit is contained in:
Roman C. Coedo 2014-06-18 00:19:31 +02:00 committed by Andrew Gaul
parent 43ee610625
commit 2916b636cf
12 changed files with 528 additions and 0 deletions

View File

@ -24,21 +24,29 @@ import java.net.URI;
import javax.inject.Named;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import org.jclouds.Fallbacks.NullOnNotFoundOr404;
import org.jclouds.blobstore.attr.BlobScope;
import org.jclouds.glacier.binders.BindDescriptionToHeaders;
import org.jclouds.glacier.binders.BindHashesToHeaders;
import org.jclouds.glacier.domain.PaginatedVaultCollection;
import org.jclouds.glacier.domain.VaultMetadata;
import org.jclouds.glacier.fallbacks.FalseOnIllegalArgumentException;
import org.jclouds.glacier.filters.RequestAuthorizeSignature;
import org.jclouds.glacier.functions.ParseArchiveIdHeader;
import org.jclouds.glacier.functions.ParseVaultMetadataFromHttpContent;
import org.jclouds.glacier.functions.ParseVaultMetadataListFromHttpContent;
import org.jclouds.glacier.options.PaginationOptions;
import org.jclouds.glacier.predicates.validators.DescriptionValidator;
import org.jclouds.glacier.predicates.validators.PayloadValidator;
import org.jclouds.glacier.predicates.validators.VaultNameValidator;
import org.jclouds.glacier.reference.GlacierHeaders;
import org.jclouds.io.Payload;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.Headers;
import org.jclouds.rest.annotations.ParamValidators;
@ -104,4 +112,38 @@ public interface GlacierAsyncClient extends Closeable {
@Path("/-/vaults")
@ResponseParser(ParseVaultMetadataListFromHttpContent.class)
ListenableFuture<PaginatedVaultCollection> listVaults();
/**
* @see GlacierClient#uploadArchive
*/
@Named("UploadArchive")
@POST
@Path("/-/vaults/{vault}/archives")
@ResponseParser(ParseArchiveIdHeader.class)
ListenableFuture<String> uploadArchive(
@PathParam("vault") String vaultName,
@ParamValidators(PayloadValidator.class) @BinderParam(BindHashesToHeaders.class) Payload payload,
@ParamValidators(DescriptionValidator.class) @BinderParam(BindDescriptionToHeaders.class) String description);
/**
* @see GlacierClient#uploadArchive
*/
@Named("UploadArchive")
@POST
@Path("/-/vaults/{vault}/archives")
@ResponseParser(ParseArchiveIdHeader.class)
ListenableFuture<String> uploadArchive(
@PathParam("vault") String vaultName,
@ParamValidators(PayloadValidator.class) @BinderParam(BindHashesToHeaders.class) Payload payload);
/**
* @see GlacierClient#deleteArchive
*/
@Named("DeleteArchive")
@DELETE
@Path("/-/vaults/{vault}/archives/{archive}")
ListenableFuture<Boolean> deleteArchive(
@PathParam("vault") String vaultName,
@PathParam("archive") String archiveId);
}

View File

@ -22,6 +22,7 @@ import java.net.URI;
import org.jclouds.glacier.domain.PaginatedVaultCollection;
import org.jclouds.glacier.domain.VaultMetadata;
import org.jclouds.glacier.options.PaginationOptions;
import org.jclouds.io.Payload;
/**
* Provides access to Amazon Glacier resources via their REST API.
@ -78,4 +79,37 @@ public interface GlacierClient extends Closeable {
* @see GlacierClient#listVaults(PaginationOptions)
*/
PaginatedVaultCollection listVaults();
/**
* Stores an archive in a vault.
*
* @param vaultName
* Name of the Vault where the archive is being stored.
* @param payload
* Payload to be uploaded.
* @param description
* Description for the archive.
* @return A String containing the Archive identifier in Amazon Glacier.
* @see <a href="http://docs.aws.amazon.com/amazonglacier/latest/dev/api-archive-post.html" />
*/
String uploadArchive(String vaultName, Payload payload, String description);
/**
* Stores an archive in a vault.
*
* @see GlacierClient#uploadArchive
*/
String uploadArchive(String vaultName, Payload payload);
/**
* Deletes an archive from a vault.
*
* @param vaultName
* Name of the Vault where the archive is stored.
* @param archiveId
* Amazon Glacier archive identifier.
* @return False if the archive was not deleted, true otherwise.
* @see <a href="http://docs.aws.amazon.com/amazonglacier/latest/dev/api-archive-delete.html" />
*/
boolean deleteArchive(String vaultName, String archiveId);
}

View File

@ -0,0 +1,42 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import org.jclouds.glacier.reference.GlacierHeaders;
import org.jclouds.http.HttpRequest;
import org.jclouds.rest.Binder;
/**
* Binds the archive description to the request headers.
*/
public class BindDescriptionToHeaders implements Binder {
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkNotNull(request, "request");
if (input == null)
return request;
checkArgument(input instanceof String, "This binder is only valid for string");
String description = String.class.cast(input);
return (R) request.toBuilder().replaceHeader(GlacierHeaders.ARCHIVE_DESCRIPTION, description).build();
}
}

View File

@ -0,0 +1,58 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.binders;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import org.jclouds.glacier.reference.GlacierHeaders;
import org.jclouds.glacier.util.TreeHash;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpRequest;
import org.jclouds.io.Payload;
import org.jclouds.rest.Binder;
/**
* Binds the linear hash and the tree hash of payload to the request headers.
*/
public class BindHashesToHeaders implements Binder {
private HttpRequest addChecksumHeaders(HttpRequest request, Payload payload) {
try {
TreeHash hash = TreeHash.Hasher.buildTreeHashFromPayload(payload);
request = request.toBuilder()
.addHeader(GlacierHeaders.LINEAR_HASH, hash.getLinearHash().toString())
.addHeader(GlacierHeaders.TREE_HASH, hash.getTreeHash().toString())
.build();
} catch (IOException e) {
throw new HttpException("Error hashing the payload", e);
}
return request;
}
@SuppressWarnings("unchecked")
@Override
public <R extends HttpRequest> R bindToRequest(R request, Object input) {
checkArgument(checkNotNull(input, "input") instanceof Payload, "This binder is only valid for Payload");
checkNotNull(request, "request");
Payload payload = Payload.class.cast(input);
return (R) addChecksumHeaders(request, payload);
}
}

View File

@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.functions;
import org.jclouds.glacier.reference.GlacierHeaders;
import org.jclouds.http.HttpException;
import org.jclouds.http.HttpResponse;
import com.google.common.base.Function;
/**
* Parses the archiveId from the HttpResponse.
*/
public class ParseArchiveIdHeader implements Function<HttpResponse, String> {
@Override
public String apply(HttpResponse from) {
String id = from.getFirstHeaderOrNull(GlacierHeaders.ARCHIVE_ID);
if (id == null)
throw new HttpException("Did not receive ArchiveId");
return id;
}
}

View File

@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.predicates.validators;
import static com.google.common.base.Strings.isNullOrEmpty;
import org.jclouds.predicates.Validator;
import com.google.common.base.CharMatcher;
import com.google.inject.Singleton;
/**
* Validates the archive description string.
*/
@Singleton
public final class DescriptionValidator extends Validator<String> {
private static final int MAX_DESC_LENGTH = 1024;
private static final CharMatcher DESCRIPTION_ACCEPTABLE_RANGE = CharMatcher.inRange(' ', '~');
@Override
public void validate(String description) {
if (isNullOrEmpty(description))
return;
if (description.length() > MAX_DESC_LENGTH)
throw exception("Description can't be longer than " + MAX_DESC_LENGTH + " characters" + " but was " + description.length());
if (!DESCRIPTION_ACCEPTABLE_RANGE.matchesAllOf(description))
throw exception("Description should have ASCII values between 32 and 126.");
}
protected static IllegalArgumentException exception(String reason) {
return new IllegalArgumentException(
String.format(
"Description doesn't match Glacier archive description rules. "
+ "Reason: %s. For more info, please refer to http://docs.aws.amazon.com/amazonglacier/latest/dev/api-archive-post.html.",
reason));
}
}

View File

@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.predicates.validators;
import org.jclouds.io.Payload;
import org.jclouds.predicates.Validator;
import com.google.inject.Singleton;
/**
* Validates the Glacier archive payload being uploaded.
*/
@Singleton
public final class PayloadValidator extends Validator<Payload> {
private static final long MAX_CONTENT_SIZE = 1L << 32; // 4GiB
@Override
public void validate(Payload payload) {
if (payload == null)
throw exception(payload, "Archive must have a payload.");
if (payload.getContentMetadata().getContentLength() == null)
throw exception(payload, "Content length must be set.");
if (payload.getContentMetadata().getContentLength() > MAX_CONTENT_SIZE)
throw exception(payload, "Max content size is 4gb" + " but was " + payload.getContentMetadata().getContentLength());
}
protected static IllegalArgumentException exception(Payload payload, String reason) {
return new IllegalArgumentException(
String.format(
"Payload '%s' doesn't match Glacier archive upload rules. "
+ "Reason: %s. For more info, please refer to http://docs.aws.amazon.com/amazonglacier/latest/dev/api-archive-post.html.",
payload, reason));
}
}

View File

@ -25,6 +25,10 @@ public final class GlacierHeaders {
public static final String HEADER_PREFIX = "x-" + DEFAULT_AMAZON_HEADERTAG + "-";
public static final String VERSION = HEADER_PREFIX + "glacier-version";
public static final String ALTERNATE_DATE = HEADER_PREFIX + "date";
public static final String ARCHIVE_DESCRIPTION = HEADER_PREFIX + "archive-description";
public static final String LINEAR_HASH = HEADER_PREFIX + "content-sha256";
public static final String TREE_HASH = HEADER_PREFIX + "sha256-tree-hash";
public static final String ARCHIVE_ID = HEADER_PREFIX + "archive-id";
private GlacierHeaders() {
}

View File

@ -19,8 +19,10 @@ package org.jclouds.glacier;
import static com.google.common.util.concurrent.MoreExecutors.sameThreadExecutor;
import static org.jclouds.Constants.PROPERTY_MAX_RETRIES;
import static org.jclouds.Constants.PROPERTY_SO_TIMEOUT;
import static org.jclouds.glacier.util.TestUtils.buildPayload;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;
import java.io.IOException;
@ -35,6 +37,7 @@ import org.jclouds.concurrent.config.ExecutorServiceModule;
import org.jclouds.glacier.domain.PaginatedVaultCollection;
import org.jclouds.glacier.domain.VaultMetadata;
import org.jclouds.glacier.options.PaginationOptions;
import org.jclouds.glacier.reference.GlacierHeaders;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableSet;
@ -211,4 +214,58 @@ public class GlacierClientMockTest {
server.shutdown();
}
}
public void testUploadArchive() throws IOException, InterruptedException {
// Prepare the response
MockResponse mr = new MockResponse();
mr.setResponseCode(201);
String responseId = "NkbByEejwEggmBz2fTHgJrg0XBoDfjP4q6iu87-TjhqG6eGoOY9Z8i1_AUyUsuhPAdTqLHy8pTl5nfCFJmDl2yEZONi5L26Omw12vcs01MNGntHEQL8MBfGlqrEXAMPLEArchiveId";
mr.addHeader("x-amzn-RequestId", "AAABZpJrTyioDC_HsOmHae8EZp_uBSJr6cnGOLKp_XJCl-Q");
mr.addHeader("Date", "Sun, 25 Mar 2012 12:00:00 GMT");
mr.addHeader(GlacierHeaders.TREE_HASH, "beb0fe31a1c7ca8c6c04d574ea906e3f97b31fdca7571defb5b44dca89b5af60");
mr.addHeader(
"Location",
"/111122223333/vaults/examplevault/archives/NkbByEejwEggmBz2fTHgJrg0XBoDfjP4q6iu87-TjhqG6eGoOY9Z8i1_AUyUsuhPAdTqLHy8pTl5nfCFJmDl2yEZONi5L26Omw12vcs01MNGntHEQL8MBfGlqrEXAMPLEArchiveId");
mr.addHeader(GlacierHeaders.ARCHIVE_ID, responseId);
MockWebServer server = new MockWebServer();
server.enqueue(mr);
server.play();
// Send the request and check the response
try {
GlacierClient client = getGlacierClient(server.getUrl("/"));
String id = client.uploadArchive("examplevault", buildPayload(10), "test description");
RecordedRequest request = server.takeRequest();
assertEquals(id, responseId);
assertEquals(request.getRequestLine(), "POST /-/vaults/examplevault/archives HTTP/1.1");
assertEquals(request.getHeader(GlacierHeaders.ARCHIVE_DESCRIPTION), "test description");
assertNotNull(request.getHeaders(GlacierHeaders.TREE_HASH));
assertNotNull(request.getHeaders(GlacierHeaders.LINEAR_HASH));
} finally {
server.shutdown();
}
}
public void testDeleteArchive() throws IOException, InterruptedException {
// Prepare the response
MockResponse mr = new MockResponse();
mr.setResponseCode(204);
mr.addHeader("x-amzn-RequestId", "AAABZpJrTyioDC_HsOmHae8EZp_uBSJr6cnGOLKp_XJCl-Q");
mr.addHeader("Date", "Sun, 25 Mar 2012 12:00:00 GMT");
MockWebServer server = new MockWebServer();
server.enqueue(mr);
server.play();
// Send the request and check the response
try {
String id = "NkbByEejwEggmBz2fTHgJrg0XBoDfjP4q6iu87-TjhqG6eGoOY9Z8i1_AUyUsuhPAdTqLHy8pTl5nfCFJmDl2yEZONi5L26Omw12vcs01MNGntHEQL8MBfGlqrEXAMPLEArchiveId";
GlacierClient client = getGlacierClient(server.getUrl("/"));
boolean result = client.deleteArchive("examplevault", id);
RecordedRequest request = server.takeRequest();
assertEquals(request.getRequestLine(), "DELETE /-/vaults/examplevault/archives/" + id + " HTTP/1.1");
assertTrue(result);
} finally {
server.shutdown();
}
}
}

View File

@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.predicates.validators;
import static com.google.common.base.Charsets.UTF_8;
import static org.jclouds.glacier.util.TestUtils.buildData;
import java.io.IOException;
import org.testng.annotations.Test;
@Test(groups = "unit", testName = "DescriptionValidatorTest")
public class DescriptionValidatorTest {
private static final DescriptionValidator VALIDATOR = new DescriptionValidator();
public void testValidate() throws IOException {
VALIDATOR.validate("This is a valid description");
VALIDATOR.validate("This_is*A#valid@Description");
VALIDATOR.validate("This~Is~A~Valid~Description");
VALIDATOR.validate("&Valid$Description");
VALIDATOR.validate("");
VALIDATOR.validate(buildData(1024).asCharSource(UTF_8).read());
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testIllegalCharacter() {
VALIDATOR.validate(Character.toString((char) 31));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testIllegalCharacter2() {
VALIDATOR.validate(Character.toString((char) 127));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testDescriptionTooLong() throws IOException {
VALIDATOR.validate(buildData(1025).asCharSource(UTF_8).read());
}
}

View File

@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.predicates.validators;
import static org.jclouds.glacier.util.TestUtils.GiB;
import static org.jclouds.glacier.util.TestUtils.buildPayload;
import org.jclouds.io.Payload;
import org.testng.annotations.Test;
@Test(groups = "unit", testName = "PayloadValidatorTest")
public class PayloadValidatorTest {
private static final PayloadValidator VALIDATOR = new PayloadValidator();
public void testValidate() {
VALIDATOR.validate(buildPayload(10));
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNoContentLength() {
Payload payload = buildPayload(10);
payload.getContentMetadata().setContentLength(null);
VALIDATOR.validate(payload);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testNullPayload() {
VALIDATOR.validate(null);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testContentLengthTooBig() {
VALIDATOR.validate(buildPayload(5 * GiB));
}
}

View File

@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jclouds.glacier.util;
import static com.google.common.net.MediaType.PLAIN_TEXT_UTF_8;
import java.util.Arrays;
import org.jclouds.io.ByteSources;
import org.jclouds.io.Payload;
import org.jclouds.io.payloads.ByteSourcePayload;
import com.google.common.io.ByteSource;
public class TestUtils {
public static final long MiB = 1L << 20;
public static final long GiB = 1L << 30;
public static final long TiB = 1L << 40;
public static Payload buildPayload(long size) {
ByteSource data = buildData(size);
Payload payload = new ByteSourcePayload(data);
payload.getContentMetadata().setContentType(PLAIN_TEXT_UTF_8.toString());
payload.getContentMetadata().setContentLength(size);
return payload;
}
public static ByteSource buildData(long size) {
byte[] array = new byte[1024];
Arrays.fill(array, (byte) 'a');
return ByteSources.repeatingArrayByteSource(array).slice(0, size);
}
}