Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2012 Google Inc. | 2 * Copyright (c) 2012 Google Inc. |
3 * | 3 * |
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not u se this file except |
5 * in compliance with the License. You may obtain a copy of the License at | 5 * in compliance with the License. You may obtain a copy of the License at |
6 * | 6 * |
7 * http://www.apache.org/licenses/LICENSE-2.0 | 7 * http://www.apache.org/licenses/LICENSE-2.0 |
8 * | 8 * |
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License | 9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License |
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express | 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express |
11 * or implied. See the License for the specific language governing permissions a nd limitations under | 11 * or implied. See the License for the specific language governing permissions a nd limitations under |
12 * the License. | 12 * the License. |
13 */ | 13 */ |
14 | 14 |
15 package com.google.api.client.extensions.java6.auth.oauth2; | 15 package com.google.api.client.extensions.java6.auth.oauth2; |
16 | 16 |
17 import com.google.api.client.auth.oauth2.Credential; | 17 import com.google.api.client.auth.oauth2.Credential; |
18 import com.google.api.client.auth.oauth2.CredentialStore; | 18 import com.google.api.client.auth.oauth2.CredentialStore; |
19 import com.google.api.client.json.JsonEncoding; | |
20 import com.google.api.client.json.JsonFactory; | 19 import com.google.api.client.json.JsonFactory; |
21 import com.google.api.client.json.JsonGenerator; | 20 import com.google.api.client.json.JsonGenerator; |
21 import com.google.common.base.Charsets; | |
22 import com.google.common.base.Preconditions; | 22 import com.google.common.base.Preconditions; |
23 | 23 |
24 import java.io.File; | 24 import java.io.File; |
25 import java.io.FileInputStream; | 25 import java.io.FileInputStream; |
26 import java.io.FileOutputStream; | 26 import java.io.FileOutputStream; |
27 import java.io.IOException; | 27 import java.io.IOException; |
28 import java.util.concurrent.locks.Lock; | 28 import java.util.concurrent.locks.Lock; |
29 import java.util.concurrent.locks.ReentrantLock; | 29 import java.util.concurrent.locks.ReentrantLock; |
30 | 30 |
31 /** | 31 /** |
32 * Thread-safe file implementation of a credential store. | 32 * Thread-safe file implementation of a credential store. |
33 * | 33 * |
34 * @since 1.11 | 34 * @since 1.11 |
35 * @author Rafael Naufal | 35 * @author Rafael Naufal |
36 */ | 36 */ |
37 public class FileCredentialStore implements CredentialStore { | 37 public class FileCredentialStore implements CredentialStore { |
38 | 38 |
39 /** Json factory for serializing user credentials. */ | 39 /** Json factory for serializing user credentials. */ |
40 private final JsonFactory jsonFactory; | 40 private final JsonFactory jsonFactory; |
41 | 41 |
42 /** Lock on access to the store. */ | 42 /** Lock on access to the store. */ |
43 private final Lock lock = new ReentrantLock(); | 43 private final Lock lock = new ReentrantLock(); |
44 | 44 |
45 /** User credentials repository. */ | 45 /** User credentials repository. */ |
46 private FilePersistedCredentials credentials = new FilePersistedCredentials( ); | 46 private FilePersistedCredentials credentials = new FilePersistedCredentials(); |
47 | 47 |
48 /** File to store user credentials. */ | 48 /** File to store user credentials. */ |
49 private final File file; | 49 private final File file; |
50 | 50 |
51 /** | 51 /** |
52 * | 52 *· |
53 * @param file | 53 * @param file |
54 * File to store user credentials | 54 * File to store user credentials |
55 * @param jsonFactory | 55 * @param jsonFactory |
56 * JSON factory to serialize user credentials | 56 * JSON factory to serialize user credentials |
57 * @throws IOException | 57 */ |
58 * if some exception during file process occurs | 58 public FileCredentialStore(File file, JsonFactory jsonFactory) throws IOExcept ion { |
59 */ | 59 this.file = Preconditions.checkNotNull(file, "missing file for credentials") ; |
60 public FileCredentialStore(File file, JsonFactory jsonFactory) | 60 this.jsonFactory = Preconditions.checkNotNull(jsonFactory, "missing json fac tory"); |
61 throws IOException { | 61 if (file.exists()) { |
62 this.file = Preconditions.checkNotNull(file, | 62 loadCredentials(file); |
63 "missing file for credentials", file); | 63 } else { |
64 this.jsonFactory = Preconditions.checkNotNull(jsonFactory, | 64 file.setReadable(false, false); |
65 "missing json factory", jsonFactory); | 65 Preconditions.checkState(file.setWritable(false, false), |
66 if (this.file.exists()) { | 66 "Error when setting permissions for %s", file); |
67 loadCredentials(file); | 67 file.setExecutable(false, false); |
68 } else { | 68 file.setReadable(true); |
69 this.file.setReadable(true); | 69 file.setWritable(true); |
70 this.file.setWritable(false); | |
71 this.file.setExecutable(false); | |
72 } | |
73 } | 70 } |
71 } | |
74 | 72 |
75 public void store(String userId, Credential credential) throws IOException { | 73 @Override |
76 lock.lock(); | 74 public void store(String userId, Credential credential) throws IOException { |
77 FileOutputStream fos = null; | 75 lock.lock(); |
78 try { | 76 try { |
79 credentials.store(userId, credential); | 77 credentials.store(userId, credential); |
80 fos = new FileOutputStream(file); | 78 writeCredentials(userId, credential); |
81 JsonGenerator generator = jsonFactory.createJsonGenerator(fos, | 79 } finally { |
82 JsonEncoding.UTF8); | 80 lock.unlock(); |
83 generator.serialize(credentials); | |
84 generator.close(); | |
85 } finally { | |
86 lock.unlock(); | |
87 if (fos != null) { | |
88 fos.close(); | |
89 } | |
90 } | |
91 } | 81 } |
82 } | |
92 | 83 |
93 public void delete(String userId, Credential credential) throws IOException { | 84 @Override |
94 lock.lock(); | 85 public void delete(String userId, Credential credential) throws IOException { |
95 FileOutputStream fos = null; | 86 lock.lock(); |
96 try { | 87 try { |
97 credentials.delete(userId); | 88 credentials.delete(userId); |
98 fos = new FileOutputStream(file); | 89 writeCredentials(userId, credential); |
99 JsonGenerator generator = jsonFactory.createJsonGenerator(fos, | 90 } finally { |
100 JsonEncoding.UTF8); | 91 lock.unlock(); |
101 generator.serialize(credentials); | |
102 generator.close(); | |
103 } catch (IOException e) { | |
rmistry
2012/06/20 13:45:39
Probably better to not catch and let the IOExcepti
rafael.naufal
2012/06/29 20:19:03
Done.
rafael.naufal
2012/06/29 20:19:03
Done.
| |
104 throw new RuntimeException("Cannot store credentials on file: ", e); | |
105 } finally { | |
106 lock.unlock(); | |
107 if (fos != null) { | |
108 fos.close(); | |
109 } | |
110 } | |
111 } | 92 } |
93 } | |
112 | 94 |
113 public boolean load(String userId, Credential credential) { | 95 @Override |
114 lock.lock(); | 96 public boolean load(String userId, Credential credential) { |
115 try { | 97 lock.lock(); |
116 return credentials.load(userId, credential); | 98 try { |
117 } finally { | 99 return credentials.load(userId, credential); |
118 lock.unlock(); | 100 } finally { |
119 } | 101 lock.unlock(); |
120 } | 102 } |
103 } | |
121 | 104 |
122 private void loadCredentials(File file) throws IOException { | 105 private void loadCredentials(File file) throws IOException { |
123 FileInputStream is = null; | 106 FileInputStream is = new FileInputStream(file); |
124 try { | 107 try { |
125 is = new FileInputStream(file); | 108 this.credentials = this.jsonFactory.fromInputStream(is, FilePersistedCrede ntials.class); |
126 this.credentials = this.jsonFactory.fromInputStream(is, | 109 } finally { |
127 FilePersistedCredentials.class); | 110 is.close(); |
128 } finally { | |
129 if (is != null) { | |
130 is.close(); | |
131 } | |
132 } | |
133 Preconditions.checkArgument(this.credentials != null, | |
134 "Credentials from file may not be null."); | |
135 } | 111 } |
112 } | |
113 | |
114 private void writeCredentials(String userId, Credential credential) throws IOE xception { | |
115 FileOutputStream fos = new FileOutputStream(file); | |
116 try { | |
117 JsonGenerator generator = jsonFactory.createJsonGenerator(fos, Charsets.UT F_8); | |
118 generator.serialize(credentials); | |
119 generator.close(); | |
120 } finally { | |
121 fos.close(); | |
122 } | |
123 } | |
136 } | 124 } |
LEFT | RIGHT |