01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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.portlets.security.users;
18:
19: import java.security.Principal;
20: import java.util.Iterator;
21: import java.util.LinkedHashMap;
22: import java.util.Map;
23: import java.util.prefs.BackingStoreException;
24: import java.util.prefs.Preferences;
25:
26: import javax.security.auth.Subject;
27:
28: import org.apache.jetspeed.security.User;
29: import org.apache.jetspeed.security.UserPrincipal;
30:
31: /**
32: * User state.
33: *
34: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
35: * @version $Id: JetspeedUserBean.java 348264 2005-11-22 22:06:45Z taylor $
36: */
37: public class JetspeedUserBean {
38: private String principal;
39: private Map attributes = new LinkedHashMap();
40:
41: public JetspeedUserBean(User user) {
42: Principal userPrincipal = createPrincipal(user.getSubject(),
43: UserPrincipal.class);
44: this .principal = userPrincipal.getName();
45: try {
46: Preferences userAttributes = user.getUserAttributes();
47: String[] keys = userAttributes.keys();
48: for (int ix = 0; ix < keys.length; ix++) {
49: attributes.put(keys[ix], userAttributes.get(keys[ix],
50: null));
51: }
52: } catch (BackingStoreException e) {
53: }
54: }
55:
56: /**
57: * @return Returns the principal.
58: */
59: public String getPrincipal() {
60: return principal;
61: }
62:
63: /**
64: * @param principal The principal to set.
65: */
66: public void setPrincipal(String principal) {
67: this .principal = principal;
68: }
69:
70: public Principal createPrincipal(Subject subject, Class classe) {
71: Principal principal = null;
72: Iterator principals = subject.getPrincipals().iterator();
73: while (principals.hasNext()) {
74: Principal p = (Principal) principals.next();
75: if (classe.isInstance(p)) {
76: principal = p;
77: break;
78: }
79: }
80: return principal;
81: }
82:
83: /**
84: * @return Returns the attributes.
85: */
86: public Map getAttributes() {
87: return attributes;
88: }
89: }
|