KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > navigation > actions > ShowFavoritesAction


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.navigation.actions;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionForward;
27 import org.apache.struts.action.ActionMapping;
28
29 import com.sslexplorer.boot.Util;
30 import com.sslexplorer.core.CoreUtil;
31 import com.sslexplorer.navigation.FavoriteResourceType;
32 import com.sslexplorer.navigation.forms.FavoritesForm;
33 import com.sslexplorer.policyframework.Permission;
34 import com.sslexplorer.policyframework.PolicyConstants;
35 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
36 import com.sslexplorer.policyframework.Resource;
37 import com.sslexplorer.policyframework.ResourceType;
38 import com.sslexplorer.policyframework.ResourceUtil;
39 import com.sslexplorer.policyframework.forms.AbstractResourcesForm;
40 import com.sslexplorer.security.Constants;
41 import com.sslexplorer.security.SessionInfo;
42 import com.sslexplorer.security.SystemDatabaseFactory;
43 import com.sslexplorer.table.actions.AbstractPagerAction;
44
45 /**
46  * Implenetation of an
47  * {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction} that lists
48  * the users current favorites.
49  *
50  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
51  */

52 public class ShowFavoritesAction extends AbstractPagerAction {
53
54     /**
55      * Constructor
56      */

57     public ShowFavoritesAction() {
58         super(PolicyConstants.FAVORITES_RESOURCE_TYPE, new Permission[] {
59             PolicyConstants.PERM_USE
60         });
61     }
62
63     /**
64      * Default dispatch method.
65      *
66      * @param mapping mapping
67      * @param form form
68      * @param request request
69      * @param response response
70      * @return forward forward
71      * @throws Exception on any error
72      */

73     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
74                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
75         Util.noCache(response);
76         ((FavoritesForm) form).initialize(request, "name");
77         ((FavoritesForm) form).checkSelectedView(request, response);
78         return mapping.findForward("display");
79     }
80
81     /**
82      * Confirm removal of favorite
83      *
84      * @param mapping mapping
85      * @param form form
86      * @param request request
87      * @param response response
88      * @return forward
89      * @throws Exception on any error
90      */

91     public ActionForward confirmRemove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
92                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
93         return mapping.findForward("confirmRemove");
94     }
95
96     /**
97      * Remove selected favorite
98      *
99      * @param mapping mapping
100      * @param form form
101      * @param request request
102      * @param response response
103      * @return forward forward
104      * @throws Exception on any error
105      */

106     public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
107                     throws Exception JavaDoc {
108         String JavaDoc id = ((FavoritesForm) form).getSelectedItem();
109         int idx = id.indexOf('_');
110         int type = Integer.parseInt(id.substring(0, idx));
111         int key = Integer.parseInt(id.substring(idx + 1));
112         ResourceType ty = PolicyDatabaseFactory.getInstance().getResourceType(type);
113         if(ty != null && ty instanceof FavoriteResourceType) {
114             FavoriteResourceType frt = (FavoriteResourceType)ty;
115             Resource r = frt.getResourceById(key);
116             if(ResourceUtil.isResourceGlobalFavorite(r)) {
117                 throw new Exception JavaDoc("Cannot remove policy favorites here.");
118             }
119             else {
120                 ResourceUtil.checkResourceAccessRights(r, getSessionInfo(request));
121             }
122             SystemDatabaseFactory.getInstance().removeFavorite(type, key, getSessionInfo(request).getUser().getPrincipalName());
123         }
124         return mapping.findForward("refresh");
125     }
126
127     /**
128      * Show information about the resource
129      *
130      * @param mapping mapping
131      * @param form form
132      * @param request request
133      * @param response response
134      * @return forward forward
135      * @throws Exception on any error
136      */

137     public ActionForward information(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
138                     throws Exception JavaDoc {
139         String JavaDoc id = ((FavoritesForm) form).getSelectedItem();
140         int idx = id.indexOf('_');
141         int type = Integer.parseInt(id.substring(0, idx));
142         int key = Integer.parseInt(id.substring(idx + 1));
143         ResourceType ty = PolicyDatabaseFactory.getInstance().getResourceType(type);
144         if(ty != null && ty instanceof FavoriteResourceType) {
145             FavoriteResourceType frt = (FavoriteResourceType)ty;
146             Resource r = frt.getResourceById(key);
147             request.setAttribute(Constants.REQ_ATTR_INFO_RESOURCE, r);
148         }
149         return mapping.findForward("resourceInformation");
150     }
151
152     /**
153      * Change the selected view to
154      * {@link AbstractResourcesForm#ICONS_VIEW}.
155      *
156      * @param mapping mapping
157      * @param form form
158      * @param request request
159      * @param response response
160      * @return forward
161      * @throws Exception on any error
162      */

163     public ActionForward viewIcons(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
164                     throws Exception JavaDoc {
165         ((FavoritesForm) form).setSelectedView(AbstractResourcesForm.ICONS_VIEW);
166         CoreUtil.storeUIState("ui_view_favorites_" + getSessionInfo(request).getNavigationContext(), AbstractResourcesForm.ICONS_VIEW, request, response);
167         return mapping.findForward("display");
168     }
169
170     /**
171      * Change the selected view to
172      * {@link AbstractResourcesForm#LIST_VIEW}.
173      *
174      * @param mapping mapping
175      * @param form form
176      * @param request request
177      * @param response response
178      * @return forward
179      * @throws Exception on any error
180      */

181     public ActionForward viewList(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
182                     throws Exception JavaDoc {
183         ((FavoritesForm) form).setSelectedView(AbstractResourcesForm.LIST_VIEW);
184         CoreUtil.storeUIState("ui_view_favorites" + getSessionInfo(request).getNavigationContext(), AbstractResourcesForm.LIST_VIEW, request, response);
185         return mapping.findForward("display");
186     }
187
188     /*
189      * (non-Javadoc)
190      *
191      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
192      * org.apache.struts.action.ActionForm,
193      * javax.servlet.http.HttpServletRequest,
194      * javax.servlet.http.HttpServletResponse)
195      */

196     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
197         return SessionInfo.USER_CONSOLE_CONTEXT;
198     }
199 }
200
Popular Tags