| 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.azure.storage.config; |
| 20 | |
| 21 | import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL; |
| 22 | |
| 23 | import java.util.concurrent.TimeUnit; |
| 24 | |
| 25 | import javax.inject.Named; |
| 26 | |
| 27 | import org.jclouds.azure.storage.handlers.AzureStorageClientErrorRetryHandler; |
| 28 | import org.jclouds.azure.storage.handlers.ParseAzureStorageErrorFromXmlContent; |
| 29 | import org.jclouds.date.DateService; |
| 30 | import org.jclouds.date.TimeStamp; |
| 31 | import org.jclouds.http.HttpErrorHandler; |
| 32 | import org.jclouds.http.HttpRetryHandler; |
| 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.rest.ConfiguresRestClient; |
| 38 | import org.jclouds.rest.config.RestClientModule; |
| 39 | |
| 40 | import com.google.common.base.Supplier; |
| 41 | import com.google.common.base.Suppliers; |
| 42 | import com.google.inject.Provides; |
| 43 | |
| 44 | /** |
| 45 | * Configures the AzureStorage connection, including logging and http transport. |
| 46 | * |
| 47 | * @author Adrian Cole |
| 48 | */ |
| 49 | @ConfiguresRestClient |
| 50 | @RequiresHttp |
| 51 | public class AzureStorageRestClientModule<S, A> extends RestClientModule<S, A> { |
| 52 | |
| 53 | public AzureStorageRestClientModule(Class<S> syncClientType, Class<A> asyncClientType) { |
| 54 | super(syncClientType, asyncClientType); |
| 55 | } |
| 56 | |
| 57 | @Provides |
| 58 | @TimeStamp |
| 59 | protected String provideTimeStamp(@TimeStamp Supplier<String> cache) { |
| 60 | return cache.get(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * borrowing concurrency code to ensure that caching takes place properly |
| 65 | */ |
| 66 | @Provides |
| 67 | @TimeStamp |
| 68 | protected Supplier<String> provideTimeStampCache(@Named(PROPERTY_SESSION_INTERVAL) long seconds, |
| 69 | final DateService dateService) { |
| 70 | return Suppliers.memoizeWithExpiration(new Supplier<String>() { |
| 71 | public String get() { |
| 72 | return dateService.rfc822DateFormat(); |
| 73 | } |
| 74 | }, seconds, TimeUnit.SECONDS); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void bindErrorHandlers() { |
| 79 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(ParseAzureStorageErrorFromXmlContent.class); |
| 80 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(ParseAzureStorageErrorFromXmlContent.class); |
| 81 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(ParseAzureStorageErrorFromXmlContent.class); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | protected void bindRetryHandlers() { |
| 86 | bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(AzureStorageClientErrorRetryHandler.class); |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | protected void configure() { |
| 91 | install(new AzureStorageParserModule()); |
| 92 | super.configure(); |
| 93 | } |
| 94 | |
| 95 | } |