1 | /** |
2 | * |
3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
4 | * |
5 | * ==================================================================== |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * ==================================================================== |
18 | */ |
19 | package org.jclouds.openstack.swift.config; |
20 | |
21 | import java.net.URI; |
22 | |
23 | import javax.inject.Singleton; |
24 | |
25 | import org.jclouds.http.HttpErrorHandler; |
26 | import org.jclouds.http.RequiresHttp; |
27 | import org.jclouds.http.annotation.ClientError; |
28 | import org.jclouds.http.annotation.Redirection; |
29 | import org.jclouds.http.annotation.ServerError; |
30 | import org.jclouds.json.config.GsonModule.DateAdapter; |
31 | import org.jclouds.json.config.GsonModule.Iso8601DateAdapter; |
32 | import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse; |
33 | import org.jclouds.openstack.config.OpenStackAuthenticationModule; |
34 | import org.jclouds.openstack.reference.AuthHeaders; |
35 | import org.jclouds.openstack.swift.CommonSwiftAsyncClient; |
36 | import org.jclouds.openstack.swift.CommonSwiftClient; |
37 | import org.jclouds.openstack.swift.Storage; |
38 | import org.jclouds.openstack.swift.handlers.ParseSwiftErrorFromHttpResponse; |
39 | import org.jclouds.rest.ConfiguresRestClient; |
40 | import org.jclouds.rest.config.RestClientModule; |
41 | |
42 | import com.google.inject.Provides; |
43 | |
44 | /** |
45 | * |
46 | * @author Adrian Cole |
47 | */ |
48 | @ConfiguresRestClient |
49 | @RequiresHttp |
50 | public class BaseSwiftRestClientModule<S extends CommonSwiftClient, A extends CommonSwiftAsyncClient> extends |
51 | RestClientModule<S, A> { |
52 | private final OpenStackAuthenticationModule module; |
53 | |
54 | public BaseSwiftRestClientModule(Class<S> syncClientType, Class<A> asyncClientType) { |
55 | this(new OpenStackAuthenticationModule(), syncClientType, asyncClientType); |
56 | } |
57 | |
58 | public BaseSwiftRestClientModule(OpenStackAuthenticationModule module, Class<S> syncClientType, |
59 | Class<A> asyncClientType) { |
60 | super(syncClientType, asyncClientType); |
61 | this.module = module; |
62 | } |
63 | |
64 | @Override |
65 | protected void configure() { |
66 | install(module); |
67 | install(new SwiftObjectModule()); |
68 | bind(DateAdapter.class).to(Iso8601DateAdapter.class); |
69 | super.configure(); |
70 | } |
71 | |
72 | @Override |
73 | protected void bindErrorHandlers() { |
74 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(ParseSwiftErrorFromHttpResponse.class); |
75 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(ParseSwiftErrorFromHttpResponse.class); |
76 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(ParseSwiftErrorFromHttpResponse.class); |
77 | } |
78 | |
79 | @Provides |
80 | @Singleton |
81 | @Storage |
82 | protected URI provideStorageUrl(AuthenticationResponse response) { |
83 | return response.getServices().get(AuthHeaders.STORAGE_URL); |
84 | } |
85 | } |