1 /**
2 * Copyright (C) 2005-2009 Alfresco Software Limited.
3 *
4 * This file is part of the Spring Surf Extension project.
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.springframework.extensions.webscripts.connector;
20
21 import java.io.Serializable;
22 import java.util.HashMap;
23 import java.util.Map;
24
25 /**
26 * A simple implementation of a credential vault that does not persist anything
27 * to disk or database.
28 * <p>
29 * Credentials can be stored and retrieved from this vault but they will be lost
30 * when the server is restarted.
31 * <p>
32 * That said, this implementation will likely be very useable for any situations
33 * where you wish to explicitly challenge the end user but only challenge them
34 * once.
35 *
36 * @author muzquiano
37 */
38 public class SimpleCredentialVault implements CredentialVault, Serializable
39 {
40 public String id;
41 public Map<String, Credentials> credentialsMap = new HashMap<String, Credentials>(8, 1.0f);
42
43 /**
44 * Instantiates a new simple credential vault.
45 *
46 * @param id the id
47 */
48 public SimpleCredentialVault(String id)
49 {
50 this.id = id;
51 }
52
53 /* (non-Javadoc)
54 * @see org.alfresco.connector.CredentialVault#store(java.lang.String, java.lang.String, org.alfresco.connector.Credentials)
55 */
56 public void store(Credentials credentials)
57 {
58 credentialsMap.put(credentials.getEndpointId(), credentials);
59 }
60
61 /* (non-Javadoc)
62 * @see org.alfresco.connector.CredentialVault#retrieve(java.lang.String, java.lang.String)
63 */
64 public Credentials retrieve(String endpointId)
65 {
66 return (Credentials) credentialsMap.get(endpointId);
67 }
68
69 /* (non-Javadoc)
70 * @see org.alfresco.connector.CredentialVault#remove(java.lang.String)
71 */
72 public void remove(String endpointId)
73 {
74 credentialsMap.remove(endpointId);
75 }
76
77 /* (non-Javadoc)
78 * @see org.alfresco.connector.CredentialVault#hasCredentials(java.lang.String, java.lang.String)
79 */
80 public boolean hasCredentials(String endpointId)
81 {
82 return (retrieve(endpointId) != null);
83 }
84
85 /* (non-Javadoc)
86 * @see org.alfresco.connector.CredentialVault#getStoredIds()
87 */
88 public String[] getStoredIds()
89 {
90 return this.credentialsMap.keySet().toArray(new String[this.credentialsMap.size()]);
91 }
92
93 /* (non-Javadoc)
94 * @see org.alfresco.connector.CredentialVault#newCredentials(java.lang.String)
95 */
96 public Credentials newCredentials(String endpointId)
97 {
98 CredentialsImpl credentials = new CredentialsImpl(endpointId);
99 store(credentials);
100
101 return credentials;
102 }
103
104 /* (non-Javadoc)
105 * @see org.alfresco.connector.CredentialVault#load()
106 */
107 public boolean load()
108 {
109 return true;
110 }
111
112 /* (non-Javadoc)
113 * @see org.alfresco.connector.CredentialVault#save()
114 */
115 public boolean save()
116 {
117 return true;
118 }
119
120 /* (non-Javadoc)
121 * @see java.lang.Object#toString()
122 */
123 @Override
124 public String toString()
125 {
126 return "SimpleCredentialVault - " + this.id;
127 }
128 }