KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > attributes > actions > UserAttributesDispatchAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.properties.attributes.actions;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.servlet.http.HttpServletRequest JavaDoc;
28 import javax.servlet.http.HttpServletResponse JavaDoc;
29
30 import org.apache.struts.action.ActionForm;
31 import org.apache.struts.action.ActionForward;
32 import org.apache.struts.action.ActionMapping;
33
34 import com.sslexplorer.boot.PropertyClass;
35 import com.sslexplorer.boot.PropertyClassManager;
36 import com.sslexplorer.boot.PropertyDefinition;
37 import com.sslexplorer.core.CoreUtil;
38 import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
39 import com.sslexplorer.properties.Property;
40 import com.sslexplorer.properties.attributes.AttributeDefinition;
41 import com.sslexplorer.properties.attributes.AttributeValueItem;
42 import com.sslexplorer.properties.attributes.forms.UserAttributesForm;
43 import com.sslexplorer.properties.impl.userattributes.UserAttributeKey;
44 import com.sslexplorer.properties.impl.userattributes.UserAttributes;
45 import com.sslexplorer.security.SessionInfo;
46 import com.sslexplorer.security.User;
47
48
49 /**
50  * Implementation of {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction}
51  * that displays and allows a user to change their attributes.
52  *
53  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
54  * @see com.sslexplorer.properties.attributes.AttributeDefinition
55  */

56 public class UserAttributesDispatchAction extends AuthenticatedDispatchAction {
57     
58     /**
59      * Constructor.
60      */

61     public UserAttributesDispatchAction() {
62         super();
63     }
64
65     /* (non-Javadoc)
66      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
67      */

68     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
69                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
70         List JavaDoc<AttributeValueItem> a = new ArrayList JavaDoc<AttributeValueItem>();
71         SessionInfo sessionInfo = getSessionInfo(request);
72         PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME);
73         for(PropertyDefinition d : propertyClass.getDefinitions()) {
74             AttributeDefinition def = (AttributeDefinition)d;
75             if(def.isHidden())
76                 continue;
77             if(def.getVisibility() == AttributeDefinition.USER_OVERRIDABLE_ATTRIBUTE ||
78                             def.getVisibility() == AttributeDefinition.USER_VIEWABLE_ATTRIBUTE
79                             || def.getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) {
80                 a.add(new AttributeValueItem(def, request, Property.getProperty(new UserAttributeKey(sessionInfo.getUser(), def.getName()))));
81             }
82         }
83         Collections.sort(a);
84         ((UserAttributesForm)form).initialize(a);
85         ((UserAttributesForm)form).setReferer(CoreUtil.getReferer(request));
86         return mapping.findForward("display");
87     }
88
89     /**
90      * Commit the details to the user database.
91      *
92      * @param mapping mapping
93      * @param form form
94      * @param request request
95      * @param response response
96      * @return forward
97      * @throws Exception on any error
98      */

99     public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
100                     throws Exception JavaDoc {
101         List JavaDoc l = ((UserAttributesForm)form).getUserAttributeValueItems();
102         SessionInfo sessionInfo = getSessionInfo(request);
103         User u = sessionInfo.getUser();
104         for(Iterator JavaDoc i = l.iterator(); i.hasNext(); ) {
105             AttributeValueItem item = (AttributeValueItem)i.next();
106             if(item.getDefinition().getVisibility() == AttributeDefinition.USER_OVERRIDABLE_ATTRIBUTE ||
107                             item.getDefinition().getVisibility() == AttributeDefinition.USER_CONFIDENTIAL_ATTRIBUTE) {
108                 Property.setProperty(new UserAttributeKey(u.getPrincipalName(), item.getDefinition().getName(), u.getRealm()
109                                 .getResourceId()), item.getDefinition().formatAttributeValue(item.getValue()), sessionInfo);
110             }
111         }
112         sessionInfo.setUser(u);
113         return cleanUpAndReturnToReferer(mapping, form, request, response);
114     }
115
116     /* (non-Javadoc)
117      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
118      */

119     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
120         return SessionInfo.USER_CONSOLE_CONTEXT;
121     }
122
123     /**
124      * Reset all user attributes.
125      *
126      * @param mapping
127      * @param form
128      * @param request
129      * @param response
130      * @return forward
131      * @throws Exception
132      */

133     public ActionForward resetUserAttributes(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
134                     throws Exception JavaDoc {
135         for(Iterator JavaDoc i = ((UserAttributesForm)form).getUserAttributeValueItems().iterator(); i.hasNext(); ) {
136             AttributeValueItem v = (AttributeValueItem)i.next();
137             v.setValue(v.getDefinition().parseValue(v.getDefinition().getDefaultValue()));
138         }
139         return mapping.findForward("display");
140     }
141 }
Popular Tags