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  
23  import org.springframework.extensions.webscripts.connector.CredentialVault;
24  import org.springframework.extensions.webscripts.connector.Credentials;
25  
26  /*
27   * @author muzquiano
28   */
29  public final class ScriptCredentials implements Serializable
30  {
31      final private CredentialVault vault;
32      final private Credentials credentials;
33      final private boolean hideNonPersistent;
34      
35      protected ScriptableMap<String, Serializable> properties;
36  
37      public ScriptCredentials(CredentialVault vault, Credentials credentials)
38      {
39          this(vault, credentials, false);
40      }
41      
42      public ScriptCredentials(CredentialVault vault, Credentials credentials, boolean hideNonPersistent)
43      {
44          this.vault = vault;
45          this.credentials = credentials;
46          this.hideNonPersistent = hideNonPersistent;
47      }
48  
49      /**
50       * Returns the properties of the credentials
51       */
52      public ScriptableMap<String, Serializable> getProperties()
53      {
54          if (this.properties == null)
55          {
56              this.properties = new ScriptableLinkedHashMap<String, Serializable>();
57              
58              // show either persistent credentials
59              // or non-persistent credentials (when persistentOnly = false)
60              if (!isHidden())
61              {            
62                  // put credentials properties onto the map
63                  String[] keys = this.credentials.getPropertyKeys();
64                  for(int i = 0; i < keys.length; i++)
65                  {
66                      Object propertyValue = this.credentials.getProperty(keys[i]);
67                      this.properties.put(keys[i], (Serializable)propertyValue);
68                  }
69              }
70          }
71          
72          return this.properties;
73      }
74      
75      
76      // --------------------------------------------------------------
77      // JavaScript Properties
78      
79      public boolean isHidden()
80      {
81          return !isPersistent() && hideNonPersistent;
82      }
83      
84      public boolean isPersistent()
85      {
86          return credentials.isPersistent();
87      }
88  }