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.rest.config; |
20 | |
21 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | |
23 | import java.io.InputStream; |
24 | import java.util.Map; |
25 | import java.util.concurrent.ConcurrentHashMap; |
26 | |
27 | import javax.annotation.Resource; |
28 | import javax.inject.Inject; |
29 | import javax.inject.Singleton; |
30 | |
31 | import org.jclouds.collect.TransformingMap; |
32 | import org.jclouds.domain.Credentials; |
33 | import org.jclouds.io.CopyInputStreamInputSupplierMap; |
34 | import org.jclouds.json.Json; |
35 | import org.jclouds.logging.Logger; |
36 | import org.jclouds.rest.ConfiguresCredentialStore; |
37 | import org.jclouds.util.Strings2; |
38 | |
39 | import com.google.common.annotations.Beta; |
40 | import com.google.common.base.Function; |
41 | import com.google.common.io.InputSupplier; |
42 | import com.google.inject.AbstractModule; |
43 | import com.google.inject.Provides; |
44 | import com.google.inject.TypeLiteral; |
45 | |
46 | /** |
47 | * |
48 | * @author Adrian Cole |
49 | */ |
50 | @Beta |
51 | @ConfiguresCredentialStore |
52 | public class CredentialStoreModule extends AbstractModule { |
53 | private static final Map<String, InputSupplier<InputStream>> BACKING = new ConcurrentHashMap<String, InputSupplier<InputStream>>(); |
54 | private final Map<String, InputStream> backing; |
55 | |
56 | public CredentialStoreModule(Map<String, InputStream> backing) { |
57 | this.backing = backing; |
58 | } |
59 | |
60 | public CredentialStoreModule() { |
61 | this(null); |
62 | } |
63 | |
64 | @Override |
65 | protected void configure() { |
66 | bind(new TypeLiteral<Function<Credentials, InputStream>>() { |
67 | }).to(CredentialsToJsonInputStream.class); |
68 | bind(new TypeLiteral<Function<InputStream, Credentials>>() { |
69 | }).to(CredentialsFromJsonInputStream.class); |
70 | if (backing != null) { |
71 | bind(new TypeLiteral<Map<String, InputStream>>() { |
72 | }).toInstance(backing); |
73 | } else { |
74 | bind(new TypeLiteral<Map<String, InputSupplier<InputStream>>>() { |
75 | }).toInstance(BACKING); |
76 | bind(new TypeLiteral<Map<String, InputStream>>() { |
77 | }).to(new TypeLiteral<CopyInputStreamInputSupplierMap>() { |
78 | }); |
79 | } |
80 | } |
81 | |
82 | @Singleton |
83 | public static class CredentialsToJsonInputStream implements Function<Credentials, InputStream> { |
84 | private final Json json; |
85 | |
86 | @Inject |
87 | CredentialsToJsonInputStream(Json json) { |
88 | this.json = json; |
89 | } |
90 | |
91 | @Override |
92 | public InputStream apply(Credentials from) { |
93 | return Strings2.toInputStream(json.toJson(checkNotNull(from))); |
94 | } |
95 | } |
96 | |
97 | @Singleton |
98 | public static class CredentialsFromJsonInputStream implements Function<InputStream, Credentials> { |
99 | @Resource |
100 | protected Logger logger = Logger.NULL; |
101 | |
102 | private final Json json; |
103 | |
104 | @Inject |
105 | CredentialsFromJsonInputStream(Json json) { |
106 | this.json = json; |
107 | } |
108 | |
109 | private static class PrivateCredentials { |
110 | String identity; |
111 | String credential; |
112 | } |
113 | |
114 | @Override |
115 | public Credentials apply(InputStream from) { |
116 | try { |
117 | PrivateCredentials credentials = json.fromJson(Strings2.toStringAndClose(checkNotNull(from)), |
118 | PrivateCredentials.class); |
119 | return new Credentials(credentials.identity, credentials.credential); |
120 | } catch (Exception e) { |
121 | logger.warn(e, "ignoring problem retrieving credentials"); |
122 | return null; |
123 | } |
124 | } |
125 | } |
126 | |
127 | @Provides |
128 | @Singleton |
129 | protected Map<String, Credentials> provideCredentialStore(Map<String, InputStream> backing, |
130 | Function<Credentials, InputStream> credentialsSerializer, |
131 | Function<InputStream, Credentials> credentialsDeserializer) { |
132 | return new TransformingMap<String, InputStream, Credentials>(backing, credentialsDeserializer, |
133 | credentialsSerializer); |
134 | } |
135 | } |