1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import java.io.Serializable;
22 import java.util.Iterator;
23
24 import org.springframework.extensions.webscripts.connector.CredentialVault;
25 import org.springframework.extensions.webscripts.connector.Credentials;
26 import org.springframework.extensions.webscripts.connector.User;
27
28
29
30
31
32
33
34 public final class ScriptCredentialVault
35 {
36 final private CredentialVault vault;
37 final private User user;
38
39 protected ScriptableMap<String, Serializable> properties;
40
41
42
43
44
45
46
47 public ScriptCredentialVault(CredentialVault vault, User user)
48 {
49 this.vault = vault;
50 this.user = user;
51 }
52
53
54
55
56
57
58
59 public ScriptableMap<String, Serializable> getProperties()
60 {
61 if (this.properties == null)
62 {
63 this.properties = new ScriptableLinkedHashMap<String, Serializable>();
64
65
66 String[] ids = this.vault.getStoredIds();
67 for(int i = 0; i < ids.length; i++)
68 {
69 Credentials credentials = this.vault.retrieve(ids[i]);
70 ScriptCredentials scriptCredentials = new ScriptCredentials(this.vault, credentials, true);
71 this.properties.put(ids[i], scriptCredentials);
72 }
73 }
74
75 return this.properties;
76 }
77
78
79
80
81 public User getUser()
82 {
83 return this.user;
84 }
85
86
87
88
89
90
91
92 public boolean hasCredentials(String endpointId)
93 {
94 return getProperties().containsKey(endpointId);
95 }
96
97
98
99
100
101
102
103
104 public ScriptCredentials newCredentials(String endpointId)
105 {
106 ScriptCredentials scriptCredentials = (ScriptCredentials) getProperties().get(endpointId);
107 if (scriptCredentials == null)
108 {
109 Credentials creds = this.vault.newCredentials(endpointId);
110 this.vault.save();
111
112
113 scriptCredentials = new ScriptCredentials(this.vault, creds);
114 getProperties().put(endpointId, scriptCredentials);
115 }
116
117 return scriptCredentials;
118 }
119
120
121
122
123
124
125 public void removeCredentials(String endpointId)
126 {
127
128 this.vault.remove(endpointId);
129 this.vault.save();
130
131
132 getProperties().remove(endpointId);
133 }
134
135
136
137
138 public void save()
139 {
140
141 String[] storedIds = this.vault.getStoredIds();
142 for(int i = 0; i < storedIds.length; i++)
143 {
144 this.vault.remove(storedIds[i]);
145 }
146
147
148 Iterator it = getProperties().keySet().iterator();
149 while (it.hasNext())
150 {
151 String endpointId = (String) it.next();
152
153
154 ScriptCredentials scriptCredentials = (ScriptCredentials) getProperties().get(endpointId);
155
156
157 Credentials creds = this.vault.newCredentials(endpointId);
158
159
160 Iterator it2 = scriptCredentials.getProperties().keySet().iterator();
161 while (it2.hasNext())
162 {
163 String propertyKey = (String) it2.next();
164 Object propertyValue = scriptCredentials.getProperties().get(propertyKey);
165
166 if (propertyValue != null)
167 {
168 creds.setProperty(propertyKey, propertyValue);
169 }
170 }
171
172
173 this.vault.store(creds);
174 }
175
176
177 this.vault.save();
178
179
180 this.properties = null;
181 }
182 }