From 1e50fbeb69bd3fc72ed2ce3aa5359ffbf9071164 Mon Sep 17 00:00:00 2001 From: Andrei Savu Date: Sun, 16 Sep 2012 23:08:17 +0300 Subject: [PATCH] Switched from a Provider to a Supplier as requested --- .../blobstore/SwiftBlobRequestSigner.java | 9 ++-- .../config/SwiftBlobStoreContextModule.java | 21 ++++---- .../ReturnOrFetchTemporaryUrlKey.java | 49 +++++++++++++++++++ .../swift/CommonSwiftClientTest.java | 20 +++++--- 4 files changed, 76 insertions(+), 23 deletions(-) create mode 100644 apis/swift/src/main/java/org/jclouds/openstack/swift/suppliers/ReturnOrFetchTemporaryUrlKey.java diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobRequestSigner.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobRequestSigner.java index 4da0517a2a..b14d0f87fc 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobRequestSigner.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/SwiftBlobRequestSigner.java @@ -30,6 +30,7 @@ import java.security.InvalidKeyException; import javax.inject.Inject; import javax.inject.Singleton; +import com.google.common.base.Supplier; import com.google.common.base.Throwables; import com.google.inject.Provider; import org.jclouds.blobstore.BlobRequestSigner; @@ -57,7 +58,7 @@ public class SwiftBlobRequestSigner implements BlobRequestSigner { private final Crypto crypto; private final Provider unixEpochTimestampProvider; - private final Provider temporaryUrlKeyProvider; + private final Supplier temporaryUrlKeySupplier; private final BlobToObject blobToObject; private final BlobToHttpGetOptions blob2HttpGetOptions; @@ -69,12 +70,12 @@ public class SwiftBlobRequestSigner implements BlobRequestSigner { @Inject public SwiftBlobRequestSigner(RestAnnotationProcessor processor, BlobToObject blobToObject, BlobToHttpGetOptions blob2HttpGetOptions, Crypto crypto, @TimeStamp Provider unixEpochTimestampProvider, - @TemporaryUrlKey Provider temporaryUrlKeyProvider) throws SecurityException, NoSuchMethodException { + @TemporaryUrlKey Supplier temporaryUrlKeySupplier) throws SecurityException, NoSuchMethodException { this.processor = checkNotNull(processor, "processor"); this.crypto = checkNotNull(crypto, "crypto"); this.unixEpochTimestampProvider = checkNotNull(unixEpochTimestampProvider, "unixEpochTimestampProvider"); - this.temporaryUrlKeyProvider = checkNotNull(temporaryUrlKeyProvider, "temporaryUrlKeyProvider"); + this.temporaryUrlKeySupplier = checkNotNull(temporaryUrlKeySupplier, "temporaryUrlKeyProvider"); this.blobToObject = checkNotNull(blobToObject, "blobToObject"); this.blob2HttpGetOptions = checkNotNull(blob2HttpGetOptions, "blob2HttpGetOptions"); @@ -121,7 +122,7 @@ public class SwiftBlobRequestSigner implements BlobRequestSigner { HttpRequest.Builder builder = request.toBuilder(); builder.filters(filter(request.getFilters(), instanceOf(AuthenticateRequest.class))); - String key = temporaryUrlKeyProvider.get(); + String key = temporaryUrlKeySupplier.get(); if (key == null) { throw new UnsupportedOperationException(); } diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/config/SwiftBlobStoreContextModule.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/config/SwiftBlobStoreContextModule.java index ae9ce38aa8..fc93747625 100644 --- a/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/config/SwiftBlobStoreContextModule.java +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/blobstore/config/SwiftBlobStoreContextModule.java @@ -18,7 +18,9 @@ */ package org.jclouds.openstack.swift.blobstore.config; +import com.google.common.base.Supplier; import com.google.inject.Provides; +import com.google.inject.TypeLiteral; import org.jclouds.blobstore.AsyncBlobStore; import org.jclouds.blobstore.BlobRequestSigner; import org.jclouds.blobstore.BlobStore; @@ -34,6 +36,7 @@ import com.google.inject.AbstractModule; import com.google.inject.Scopes; import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyApi; import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyAsyncApi; +import org.jclouds.openstack.swift.suppliers.ReturnOrFetchTemporaryUrlKey; import java.util.UUID; @@ -53,18 +56,6 @@ public class SwiftBlobStoreContextModule extends AbstractModule { return System.currentTimeMillis() / 1000; /* convert to seconds */ } - @Provides - @TemporaryUrlKey - protected String temporaryUrlKeyProvider(TemporaryUrlKeyApi client) { - String key = client.getTemporaryUrlKey(); - if (key == null) { - client.setTemporaryUrlKey(UUID.randomUUID().toString()); - return client.getTemporaryUrlKey(); - } - return key; - } - - @Override protected void configure() { install(new BlobStoreMapModule()); @@ -72,6 +63,12 @@ public class SwiftBlobStoreContextModule extends AbstractModule { bind(AsyncBlobStore.class).to(SwiftAsyncBlobStore.class).in(Scopes.SINGLETON); bind(BlobStore.class).to(SwiftBlobStore.class).in(Scopes.SINGLETON); bind(BlobRequestSigner.class).to(SwiftBlobRequestSigner.class); + configureTemporaryUrlExtension(); + } + + protected void configureTemporaryUrlExtension() { bindClientAndAsyncClient(binder(), TemporaryUrlKeyApi.class, TemporaryUrlKeyAsyncApi.class); + bind(new TypeLiteral>() { + }).annotatedWith(TemporaryUrlKey.class).to(ReturnOrFetchTemporaryUrlKey.class); } } diff --git a/apis/swift/src/main/java/org/jclouds/openstack/swift/suppliers/ReturnOrFetchTemporaryUrlKey.java b/apis/swift/src/main/java/org/jclouds/openstack/swift/suppliers/ReturnOrFetchTemporaryUrlKey.java new file mode 100644 index 0000000000..0c0aad2b4c --- /dev/null +++ b/apis/swift/src/main/java/org/jclouds/openstack/swift/suppliers/ReturnOrFetchTemporaryUrlKey.java @@ -0,0 +1,49 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds 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.openstack.swift.suppliers; + +import com.google.common.base.Supplier; +import com.google.inject.Inject; +import com.google.inject.Singleton; +import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyApi; + +import java.util.UUID; + +import static com.google.common.base.Preconditions.checkNotNull; + +@Singleton +public class ReturnOrFetchTemporaryUrlKey implements Supplier { + + private TemporaryUrlKeyApi client; + + @Inject + public ReturnOrFetchTemporaryUrlKey(TemporaryUrlKeyApi client) { + this.client = checkNotNull(client, "client"); + } + + @Override + public String get() { + String key = client.getTemporaryUrlKey(); + if (key == null) { + client.setTemporaryUrlKey(UUID.randomUUID().toString()); + return client.getTemporaryUrlKey(); + } + return key; + } +} diff --git a/apis/swift/src/test/java/org/jclouds/openstack/swift/CommonSwiftClientTest.java b/apis/swift/src/test/java/org/jclouds/openstack/swift/CommonSwiftClientTest.java index 6e7308eab8..667f3d0c0e 100644 --- a/apis/swift/src/test/java/org/jclouds/openstack/swift/CommonSwiftClientTest.java +++ b/apis/swift/src/test/java/org/jclouds/openstack/swift/CommonSwiftClientTest.java @@ -21,13 +21,18 @@ package org.jclouds.openstack.swift; import static org.jclouds.Constants.PROPERTY_API_VERSION; import static org.jclouds.Constants.PROPERTY_ENDPOINT; import static org.jclouds.location.reference.LocationConstants.PROPERTY_REGIONS; +import static org.jclouds.rest.config.BinderUtils.bindClientAndAsyncClient; import java.net.URI; import java.util.Properties; import javax.inject.Singleton; +import com.google.common.base.Suppliers; +import com.google.inject.*; +import com.google.inject.util.Modules; import org.jclouds.apis.ApiMetadata; +import org.jclouds.date.TimeStamp; import org.jclouds.http.HttpRequest; import org.jclouds.openstack.functions.URIFromAuthenticationResponseForService; import org.jclouds.openstack.internal.TestOpenStackAuthenticationModule; @@ -35,15 +40,14 @@ import org.jclouds.openstack.reference.AuthHeaders; import org.jclouds.openstack.swift.blobstore.config.SwiftBlobStoreContextModule; import org.jclouds.openstack.swift.config.SwiftRestClientModule; import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyApi; +import org.jclouds.openstack.swift.extensions.TemporaryUrlKeyAsyncApi; +import org.jclouds.openstack.swift.suppliers.ReturnOrFetchTemporaryUrlKey; import org.jclouds.rest.internal.BaseAsyncClientTest; import org.jclouds.rest.internal.RestAnnotationProcessor; import org.testng.annotations.Test; import com.google.common.base.Supplier; import com.google.common.collect.ImmutableSet; -import com.google.inject.Module; -import com.google.inject.Provides; -import com.google.inject.TypeLiteral; /** * Tests behavior of {@code BindSwiftObjectMetadataToRequest} @@ -79,15 +83,17 @@ public abstract class CommonSwiftClientTest extends BaseAsyncClientTest>() { + }).annotatedWith(TemporaryUrlKey.class).toInstance(Suppliers.ofInstance(TEMPORARY_URL_KEY)); } } @@ -95,7 +101,7 @@ public abstract class CommonSwiftClientTest extends BaseAsyncClientTest>of(StorageEndpointModule.class, SwiftRestClientModule.class, - FixedSwiftBlobStoreContextModule.class)).build(); + StaticTimeAndTemporaryUrlKeyModule.class)).build(); } @Override