View Javadoc

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;
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   * Represents the credential vault to the script engine
30   * This exposes credentials from the vault which are "user" managed
31   * 
32   * @author muzquiano
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       * Constructs a new ScriptCredentialVault object.
43       * 
44       * @param vault   The credential vault instance
45       * @param user    The user to whom the credential vault belongs
46       */
47      public ScriptCredentialVault(CredentialVault vault, User user)
48      {
49          this.vault = vault;
50          this.user = user;
51      }
52      
53      // --------------------------------------------------------------
54      // JavaScript Properties
55  
56      /**
57       * Returns the properties of the credential vault
58       */
59      public ScriptableMap<String, Serializable> getProperties()
60      {
61          if (this.properties == null)
62          {
63              this.properties = new ScriptableLinkedHashMap<String, Serializable>();
64              
65              // put credentials onto the map
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       * Returns the user to whom this credential vault belongs
80       */
81      public User getUser()
82      {
83          return this.user;
84      }
85      
86      /**
87       * Returns whether the given endpoint credentials are stored on this vault
88       * 
89       * @param endpointId
90       * @return
91       */
92      public boolean hasCredentials(String endpointId)
93      {
94          return getProperties().containsKey(endpointId);
95      }
96      
97      /**
98       * Creates new credentials and binds them into this vault.
99       * If the credentials already exist, the old ones will be returned
100      * 
101      * @param endpointId
102      * @return
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             // update our properties map
113             scriptCredentials = new ScriptCredentials(this.vault, creds);
114             getProperties().put(endpointId, scriptCredentials);
115         }
116         
117         return scriptCredentials;
118     }
119     
120     /**
121      * Removes credentials from the vault
122      * 
123      * @param endpointId
124      */
125     public void removeCredentials(String endpointId)
126     {
127         // remove from the actual vault
128         this.vault.remove(endpointId);
129         this.vault.save();
130         
131         // remove from our map
132         getProperties().remove(endpointId);
133     }
134     
135     /**
136      * Saves the credential vault
137      */
138     public void save()
139     {
140         // get the actual vault and clear it
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         // now walk through our properties and place them back into the vault
148         Iterator it = getProperties().keySet().iterator();
149         while (it.hasNext())
150         {
151             String endpointId = (String) it.next();
152 
153             // get the script credentials
154             ScriptCredentials scriptCredentials = (ScriptCredentials) getProperties().get(endpointId);
155             
156             // create a new actual credentials onto which we will map
157             Credentials creds = this.vault.newCredentials(endpointId);
158             
159             // now do the mapping
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             // store the creds back onto the actual vault
173             this.vault.store(creds);
174         }
175         
176         // persist the vault (if needed)
177         this.vault.save();
178         
179         // null our properties map so it reloads on next access
180         this.properties = null;
181     }
182 }