| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.jclouds.s3.config; |
| 20 | |
| 21 | import java.util.Map; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | |
| 24 | import org.jclouds.javax.annotation.Nullable; |
| 25 | import javax.inject.Named; |
| 26 | import javax.inject.Singleton; |
| 27 | |
| 28 | import org.jclouds.Constants; |
| 29 | import org.jclouds.aws.config.AWSRestClientModule; |
| 30 | import org.jclouds.date.DateService; |
| 31 | import org.jclouds.date.TimeStamp; |
| 32 | import org.jclouds.http.HttpErrorHandler; |
| 33 | import org.jclouds.http.RequiresHttp; |
| 34 | import org.jclouds.http.annotation.ClientError; |
| 35 | import org.jclouds.http.annotation.Redirection; |
| 36 | import org.jclouds.http.annotation.ServerError; |
| 37 | import org.jclouds.location.Region; |
| 38 | import org.jclouds.rest.ConfiguresRestClient; |
| 39 | import org.jclouds.rest.RequestSigner; |
| 40 | import org.jclouds.s3.Bucket; |
| 41 | import org.jclouds.s3.S3AsyncClient; |
| 42 | import org.jclouds.s3.S3Client; |
| 43 | import org.jclouds.s3.filters.RequestAuthorizeSignature; |
| 44 | import org.jclouds.s3.handlers.ParseS3ErrorFromXmlContent; |
| 45 | |
| 46 | import com.google.common.base.Supplier; |
| 47 | import com.google.common.base.Suppliers; |
| 48 | import com.google.common.collect.Maps; |
| 49 | import com.google.inject.Provides; |
| 50 | import com.google.inject.Scopes; |
| 51 | |
| 52 | /** |
| 53 | * Configures the S3 connection, including logging and http transport. |
| 54 | * |
| 55 | * @author Adrian Cole |
| 56 | */ |
| 57 | @ConfiguresRestClient |
| 58 | @RequiresHttp |
| 59 | public class S3RestClientModule<S extends S3Client, A extends S3AsyncClient> extends AWSRestClientModule<S, A> { |
| 60 | public static S3RestClientModule<S3Client, S3AsyncClient> create() { |
| 61 | return new S3RestClientModule<S3Client, S3AsyncClient>(S3Client.class, S3AsyncClient.class); |
| 62 | } |
| 63 | |
| 64 | public S3RestClientModule(Class<S> sync, Class<A> async) { |
| 65 | super(sync, async); |
| 66 | } |
| 67 | |
| 68 | @Provides |
| 69 | @Bucket |
| 70 | @Singleton |
| 71 | protected Map<String, String> bucketToRegion() { |
| 72 | return Maps.newConcurrentMap(); |
| 73 | } |
| 74 | |
| 75 | @Provides |
| 76 | @Bucket |
| 77 | @Singleton |
| 78 | @Nullable |
| 79 | protected String defaultRegionForBucket(@Nullable @Region String defaultRegion) { |
| 80 | return defaultRegion; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | protected void configure() { |
| 85 | install(new S3ObjectModule()); |
| 86 | install(new S3ParserModule()); |
| 87 | bind(RequestAuthorizeSignature.class).in(Scopes.SINGLETON); |
| 88 | super.configure(); |
| 89 | } |
| 90 | |
| 91 | @Override |
| 92 | protected void bindErrorHandlers() { |
| 93 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(ParseS3ErrorFromXmlContent.class); |
| 94 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(ParseS3ErrorFromXmlContent.class); |
| 95 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(ParseS3ErrorFromXmlContent.class); |
| 96 | } |
| 97 | |
| 98 | @Provides |
| 99 | @Singleton |
| 100 | protected RequestSigner provideRequestSigner(RequestAuthorizeSignature in) { |
| 101 | return in; |
| 102 | } |
| 103 | |
| 104 | @Provides |
| 105 | @TimeStamp |
| 106 | protected String provideTimeStamp(@TimeStamp Supplier<String> cache) { |
| 107 | return cache.get(); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * borrowing concurrency code to ensure that caching takes place properly |
| 112 | */ |
| 113 | @Provides |
| 114 | @TimeStamp |
| 115 | @Singleton |
| 116 | protected Supplier<String> provideTimeStampCache(@Named(Constants.PROPERTY_SESSION_INTERVAL) long seconds, |
| 117 | final DateService dateService) { |
| 118 | return Suppliers.memoizeWithExpiration(new Supplier<String>() { |
| 119 | public String get() { |
| 120 | return dateService.rfc822DateFormat(); |
| 121 | } |
| 122 | }, seconds, TimeUnit.SECONDS); |
| 123 | } |
| 124 | } |