1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * http://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */17packageorg.apache.jetspeed.security.impl;
1819import org.apache.jetspeed.security.BasePrincipal;
2021/***22 * <p>23 * {@link BasePrincipal} interface implementation.24 * </p>25 * 26 * @author <a href="mailto:dlestrat@apache.org">David Le Strat</a>27 */28publicabstractclassBasePrincipalImpl implements BasePrincipal
29 {
3031/*** The version uid. */32privatestaticfinallong serialVersionUID = 5687385387290144541L;
3334/*** The principal name. */35privatefinal String name;
3637/*** The full path. */38privatefinal String fullPath;
3940/*** is this principal enabled **/41privateboolean enabled = true;
4243/*** is this principal a mapping **/44privateboolean isMapping = false;
4546/***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 */54publicBasePrincipalImpl(String name, String prefsRoot, boolean hiearchicalNames)
55 {
56this(name, prefsRoot, hiearchicalNames, true, false);
57 }
5859publicBasePrincipalImpl(String name, String prefsRoot, boolean hiearchicalNames, boolean isEnabled, boolean isMapping)
60 {
61this.name = name;
62this.fullPath = getFullPathFromPrincipalName(name, prefsRoot, hiearchicalNames);
63this.enabled = isEnabled;
64this.isMapping = isMapping;
65 }
6667/***68 * @see org.apache.jetspeed.security.BasePrincipal#getFullPath()69 */70public String getFullPath()
71 {
72returnthis.fullPath;
73 }
7475/***76 * @see java.security.Principal#getName()77 */78public String getName()
79 {
80returnthis.name;
81 }
8283/***84 * @see java.lang.Object#hashCode()85 */86publicint hashCode()
87 {
88returnthis.name.hashCode();
89 }
9091/***92 * <p>93 * Returns a string representation of this principal.94 * </p>95 * 96 * @return A string representation of this principal.97 */98public String toString()
99 {
100returnthis.name;
101 }
102103/***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 the109 * 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 done118 * @return The preferences full path / principal name.119 */120publicstatic String getFullPathFromPrincipalName(String name, String prefsRoot, boolean hiearchicalNames)
121 {
122 String fullPath = name;
123if (null != name )
124 {
125 fullPath = prefsRoot + (hiearchicalNames ? name.replace('.','/') : name );
126 }
127return fullPath;
128 }
129130/***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 the136 * 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 */146147/***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 the153 * 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 done162 * @return The principal name.163 */164publicstatic String getPrincipalNameFromFullPath(String fullPath, String prefsRoot, boolean hiearchicalNames)
165 {
166 String name = fullPath;
167if (null != name)
168 {
169 name = name.substring(prefsRoot.length(), name.length());
170if ( hiearchicalNames )
171 {
172 name = name.replace('/', '.');
173 }
174 }
175return name;
176 }
177178/***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 the184 * 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// }199200/***201 * @see org.apache.jetspeed.security.BasePrincipal#isEnabled()202 */203publicboolean isEnabled()
204 {
205return enabled;
206 }
207208/***209 * @see org.apache.jetspeed.security.BasePrincipal#setEnabled(boolean)210 */211publicvoid setEnabled(boolean enabled)
212 {
213this.enabled = enabled;
214 }
215216publicboolean isMapping()
217 {
218return isMapping;
219 }
220221 }