View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.impl;
18  
19  import org.apache.jetspeed.security.BasePrincipal;
20  
21  /***
22   * <p>
23   * {@link BasePrincipal} interface implementation.
24   * </p>
25   * 
26   * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>
27   */
28  public abstract class BasePrincipalImpl implements BasePrincipal
29  {
30      
31      /*** The version uid. */
32      private static final long serialVersionUID = 5687385387290144541L;
33  
34      /*** The principal name. */
35      private final String name;
36  
37      /*** The full path. */
38      private final String fullPath;
39  
40      /*** is this principal enabled **/
41      private boolean enabled = true;
42      
43      /*** is this principal a mapping **/
44      private boolean isMapping = false;
45      
46      /***
47       * <p>
48       * Principal constructor given a name and preferences root.
49       * </p>
50       * 
51       * @param name The principal name.
52       * @param prefsRoot The preferences root node.
53       */
54      public BasePrincipalImpl(String name, String prefsRoot, boolean hiearchicalNames)
55      {
56          this(name, prefsRoot, hiearchicalNames, true, false);
57      }
58      
59      public BasePrincipalImpl(String name, String prefsRoot, boolean hiearchicalNames, boolean isEnabled, boolean isMapping)
60      {
61          this.name = name;
62          this.fullPath = getFullPathFromPrincipalName(name, prefsRoot, hiearchicalNames);
63          this.enabled = isEnabled;
64          this.isMapping = isMapping;
65      }
66  
67      /***
68       * @see org.apache.jetspeed.security.BasePrincipal#getFullPath()
69       */
70      public String getFullPath()
71      {
72          return this.fullPath;
73      }
74  
75      /***
76       * @see java.security.Principal#getName()
77       */
78      public String getName()
79      {
80          return this.name;
81      }
82  
83      /***
84       * @see java.lang.Object#hashCode()
85       */
86      public int hashCode()
87      {
88          return this.name.hashCode();
89      }
90  
91      /***
92       * <p>
93       * Returns a string representation of this principal.
94       * </p>
95       * 
96       * @return A string representation of this principal.
97       */
98      public String toString()
99      {
100         return this.name;
101     }
102 
103     /***
104      * <p>
105      * Gets the principal implementation full path from the principal name.
106      * </p>
107      * <p>
108      * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
109      * separator for hierarchical elements.
110      * </p>
111      * <p>
112      * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
113      * </p>
114      * 
115      * @param name The principal name.
116      * @param prefsRoot The preferences root node.
117      * @param hiearchicalNames indicator if hierarchy encoding (replacing '.' with '/') should be done
118      * @return The preferences full path / principal name.
119      */
120     public static String getFullPathFromPrincipalName(String name, String prefsRoot, boolean hiearchicalNames)
121     {
122         String fullPath = name;
123         if (null != name )
124         {
125             fullPath = prefsRoot + (hiearchicalNames ? name.replace('.','/') : name );
126         }
127         return fullPath;
128     }
129 
130     /***
131      * <p>
132      * Gets the principal implementation full path from the principal name.
133      * </p>
134      * <p>
135      * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
136      * separator for hierarchical elements.
137      * </p>
138      * <p>
139      * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
140      * </p>
141      * 
142      * @param name The principal name.
143      * @param prefsRoot The preferences root node.
144      * @return The preferences full path / principal name.
145      */        
146 
147     /***
148      * <p>
149      * Gets the principal name from the principal implementation full path.
150      * </p>
151      * <p>
152      * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
153      * separator for hierarchical elements.
154      * </p>
155      * <p>
156      * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
157      * </p>
158      * 
159      * @param fullPath The principal full path.
160      * @param prefsRoot The preferences root node.
161      * @param hiearchicalNames indicator if hierarchical decoding (replacing '/' with '.') should be done
162      * @return The principal name.
163      */
164     public static String getPrincipalNameFromFullPath(String fullPath, String prefsRoot, boolean hiearchicalNames)
165     {
166         String name = fullPath;
167         if (null != name)
168         {
169             name = name.substring(prefsRoot.length(), name.length());
170             if ( hiearchicalNames )
171             {
172                 name = name.replace('/', '.');
173             }
174         }
175         return name;
176     }
177 
178     /***
179      * <p>
180      * Gets the principal name from the principal implementation full path.
181      * </p>
182      * <p>
183      * Hierarchical principal names should follow: {principal}.{subprincipal}. "." is used as the
184      * separator for hierarchical elements.
185      * </p>
186      * <p>
187      * The implementation path follow /PREFS_{PRINCIPAL}_ROOT/{principal}/{subprincipal}.
188      * </p>
189      * 
190      * @param fullPath The principal full path.
191      * @param prefsRoot The preferences root node.
192      * @return The principal name.
193      */
194 // MOVED TO DERVICED CLASSES    
195 //    public static String getPrincipalNameFromFullPath(String fullPath, String prefsRoot)
196 //    {
197 //        return getPrincipalNameFromFullPath(fullPath, prefsRoot, true);
198 //    }
199 
200     /***
201      * @see org.apache.jetspeed.security.BasePrincipal#isEnabled()
202      */
203     public boolean isEnabled()
204     {
205         return enabled;
206     }
207 
208     /***
209      * @see org.apache.jetspeed.security.BasePrincipal#setEnabled(boolean)
210      */
211     public void setEnabled(boolean enabled)
212     {
213         this.enabled = enabled;
214     }
215    
216     public boolean isMapping()
217     {
218         return isMapping;
219     }
220     
221 }