| 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.cloudservers.config; |
| 20 | |
| 21 | import java.net.URI; |
| 22 | |
| 23 | import javax.inject.Singleton; |
| 24 | |
| 25 | import org.jclouds.cloudservers.CloudServersAsyncClient; |
| 26 | import org.jclouds.cloudservers.CloudServersClient; |
| 27 | import org.jclouds.cloudservers.ServerManagement; |
| 28 | import org.jclouds.cloudservers.handlers.ParseCloudServersErrorFromHttpResponse; |
| 29 | import org.jclouds.http.HttpErrorHandler; |
| 30 | import org.jclouds.http.RequiresHttp; |
| 31 | import org.jclouds.http.annotation.ClientError; |
| 32 | import org.jclouds.http.annotation.Redirection; |
| 33 | import org.jclouds.http.annotation.ServerError; |
| 34 | import org.jclouds.json.config.GsonModule.DateAdapter; |
| 35 | import org.jclouds.json.config.GsonModule.Iso8601DateAdapter; |
| 36 | import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse; |
| 37 | import org.jclouds.openstack.config.OpenStackAuthenticationModule; |
| 38 | import org.jclouds.openstack.reference.AuthHeaders; |
| 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 CloudServersRestClientModule extends RestClientModule<CloudServersClient, CloudServersAsyncClient> { |
| 51 | |
| 52 | private final OpenStackAuthenticationModule module; |
| 53 | |
| 54 | public CloudServersRestClientModule(OpenStackAuthenticationModule module) { |
| 55 | super(CloudServersClient.class, CloudServersAsyncClient.class); |
| 56 | this.module = module; |
| 57 | } |
| 58 | |
| 59 | public CloudServersRestClientModule() { |
| 60 | this(new OpenStackAuthenticationModule()); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected void configure() { |
| 65 | install(module); |
| 66 | bind(DateAdapter.class).to(Iso8601DateAdapter.class); |
| 67 | super.configure(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | protected void bindErrorHandlers() { |
| 72 | bind(HttpErrorHandler.class).annotatedWith(Redirection.class).to(ParseCloudServersErrorFromHttpResponse.class); |
| 73 | bind(HttpErrorHandler.class).annotatedWith(ClientError.class).to(ParseCloudServersErrorFromHttpResponse.class); |
| 74 | bind(HttpErrorHandler.class).annotatedWith(ServerError.class).to(ParseCloudServersErrorFromHttpResponse.class); |
| 75 | } |
| 76 | |
| 77 | @Provides |
| 78 | @Singleton |
| 79 | @ServerManagement |
| 80 | protected URI provideServerUrl(AuthenticationResponse response) { |
| 81 | return response.getServices().get(AuthHeaders.SERVER_MANAGEMENT_URL); |
| 82 | } |
| 83 | |
| 84 | } |