KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > actions > ShowAvailableIpRestrictionsDispatchAction


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.security.actions;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashSet 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.core.CoreAttributeConstants;
35 import com.sslexplorer.core.CoreEvent;
36 import com.sslexplorer.core.CoreEventConstants;
37 import com.sslexplorer.core.CoreServlet;
38 import com.sslexplorer.core.CoreUtil;
39 import com.sslexplorer.policyframework.Permission;
40 import com.sslexplorer.policyframework.PolicyConstants;
41 import com.sslexplorer.policyframework.PolicyUtil;
42 import com.sslexplorer.security.Constants;
43 import com.sslexplorer.security.IpRestriction;
44 import com.sslexplorer.security.SessionInfo;
45 import com.sslexplorer.security.SystemDatabase;
46 import com.sslexplorer.security.SystemDatabaseFactory;
47 import com.sslexplorer.security.forms.ShowAvailableIpRestrictionsForm;
48 import com.sslexplorer.table.actions.AbstractPagerAction;
49 import com.sslexplorer.table.forms.AbstractPagerForm;
50
51 /**
52  * Implementation of
53  * {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction} that allows
54  * an adminstrator to view all configured <i>IP Restrictions</i>
55  *
56  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
57  * @author Peter King <a HREF="mailto: peter@3sp.com">&lt;peter@3sp.com&gt;</a>
58  */

59 public class ShowAvailableIpRestrictionsDispatchAction extends AbstractPagerAction {
60
61     /**
62      * Constructor.
63      */

64     public ShowAvailableIpRestrictionsDispatchAction() {
65         super(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CREATE, PolicyConstants.PERM_DELETE });
66     }
67
68     /*
69      * (non-Javadoc)
70      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
71      * org.apache.struts.action.ActionForm,
72      * javax.servlet.http.HttpServletRequest,
73      * javax.servlet.http.HttpServletResponse)
74      */

75     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
76         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
77     }
78
79     /**
80      * List all available restrictions
81      *
82      * @param mapping mapping
83      * @param form form
84      * @param request request
85      * @param response response
86      * @return forward
87      * @throws Exception on any error
88      */

89     public ActionForward list(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
90                     throws Exception JavaDoc {
91         CoreUtil.clearFlow(request);
92         IpRestriction[] restrictions = null;
93
94         SystemDatabase sdb = SystemDatabaseFactory.getInstance();
95         restrictions = sdb.getIpRestrictions();
96         
97         ((ShowAvailableIpRestrictionsForm) form).initialize(restrictions, request.getSession());
98         return mapping.findForward("success");
99     }
100
101     /**
102      * @param mapping
103      * @param form
104      * @param request
105      * @param response
106      * @return ActionForward
107      * @throws Exception
108      */

109     public ActionForward confirmDelete(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
110                     throws Exception JavaDoc {
111
112         String JavaDoc[] id = request.getParameterValues("id");
113         if (id != null) {
114             IpRestriction[] ipRestrictions = findRemainingIpRestrictions(id);
115             SystemDatabase database = SystemDatabaseFactory.getInstance();
116             String JavaDoc remoteAddr = request.getRemoteAddr();
117             boolean isValid = database.verifyIPAddress(remoteAddr, ipRestrictions);
118             return mapping.findForward(isValid ? "confirmDelete" : "confirmDeleteWithLockout");
119         }
120         return mapping.findForward("refresh");
121     }
122     
123     private IpRestriction[] findRemainingIpRestrictions(String JavaDoc[] restrictionIds) throws Exception JavaDoc {
124         SystemDatabase database = SystemDatabaseFactory.getInstance();
125         IpRestriction[] restrictions = database.getIpRestrictions();
126         
127         Collection JavaDoc<IpRestriction> differences = new HashSet JavaDoc<IpRestriction>(Arrays.asList(restrictions));
128         for (String JavaDoc restrictionId : restrictionIds) {
129             IpRestriction ipRestriction = findIpRestriction(restrictions, Integer.valueOf(restrictionId));
130             if (ipRestriction !=null) {
131                 differences.remove(ipRestriction);
132             }
133         }
134         return differences.toArray(new IpRestriction[differences.size()]);
135     }
136     
137     /**
138      * Delete a IP restrictions
139      *
140      * @param mapping mapping
141      * @param form form
142      * @param request request
143      * @param response response
144      * @return forward
145      * @throws Exception on any error
146      */

147     public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
148                     throws Exception JavaDoc {
149         PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_DELETE, request);
150         String JavaDoc[] id = request.getParameterValues("id");
151         if (id != null) {
152             deleteIpRestrictions(request, id);
153         }
154         return mapping.findForward("refresh");
155     }
156     
157     /**
158      * Move an IP restriction down in priority by swapping the priority with
159      * the restriction below the one selected.
160      *
161      * @param mapping mapping
162      * @param form form
163      * @param request request
164      * @param response response
165      * @return ActionForward forward
166      * @throws Exception on any error
167      */

168     public ActionForward moveDown(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
169         PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_EDIT, request);
170         int id = Integer.parseInt(request.getParameter("id"));
171         SystemDatabase database = SystemDatabaseFactory.getInstance();
172         IpRestriction restriction1 = database.getIpRestriction(id);
173         String JavaDoc ipAddress = restriction1.getAddress();
174         String JavaDoc ipPermission = restriction1.getAllowed() ? "Allowed" : "Denied";
175         try {
176             List JavaDoc<IpRestriction> restrictions = Arrays.asList(database.getIpRestrictions());
177             database.swapIpRestrictions(restriction1, restrictions.get(restrictions.indexOf(restriction1) + 1));
178             fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_DOWN, ipAddress, ipPermission, CoreEvent.STATE_SUCCESSFUL);
179         } catch (Exception JavaDoc e) {
180             fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_DOWN, ipAddress, ipPermission, CoreEvent.STATE_UNSUCCESSFUL);
181             throw e;
182         }
183         return mapping.findForward("refresh");
184     }
185     
186     /**
187      * Move an IP restriction up in priority by swapping the priority with
188      * the restriction above the one selected.
189      *
190      * @param mapping mapping
191      * @param form form
192      * @param request request
193      * @param response response
194      * @return ActionForward forward
195      * @throws Exception on any error
196      */

197     public ActionForward moveUp(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
198         PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_EDIT, request);
199         int id = Integer.parseInt(request.getParameter("id"));
200         SystemDatabase database = SystemDatabaseFactory.getInstance();
201         IpRestriction restriction1 = database.getIpRestriction(id);
202         String JavaDoc ipAddress = restriction1.getAddress();
203         String JavaDoc ipPermission = restriction1.getAllowed() ? "Allowed" : "Denied";
204         try {
205             List JavaDoc<IpRestriction> restrictions = Arrays.asList(database.getIpRestrictions());
206             database.swapIpRestrictions(restriction1, restrictions.get(restrictions.indexOf(restriction1) - 1));
207             fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_UP, ipAddress, ipPermission, CoreEvent.STATE_SUCCESSFUL);
208         } catch (Exception JavaDoc e) {
209             fireCoreEvent(request, CoreEventConstants.IP_RESTRICTION_MOVE_UP, ipAddress, ipPermission, CoreEvent.STATE_UNSUCCESSFUL);
210             throw e;
211         }
212         return mapping.findForward("refresh");
213     }
214     
215     private void deleteIpRestrictions(HttpServletRequest JavaDoc request, String JavaDoc[] restrictionIds) throws Exception JavaDoc {
216         SystemDatabase database = SystemDatabaseFactory.getInstance();
217         IpRestriction[] restrictions = database.getIpRestrictions();
218         
219         for (String JavaDoc restrictionId : restrictionIds) {
220             IpRestriction ipRestriction = findIpRestriction(restrictions, Integer.valueOf(restrictionId));
221             if (ipRestriction != null) {
222                 deleteIpRestriction(request, ipRestriction);
223             }
224         }
225     }
226     
227     private void deleteIpRestriction(HttpServletRequest JavaDoc request, IpRestriction restriction) throws Exception JavaDoc {
228         String JavaDoc ipAddress = restriction.getAddress();
229         String JavaDoc ipPermission = restriction.getAllowed() ? "Allowed" : "Denied";
230         
231         try {
232             SystemDatabase database = SystemDatabaseFactory.getInstance();
233             database.removeIpRestriction(restriction.getID());
234             fireCoreEvent(request, CoreEventConstants.DELETE_IP_RESTRICTION, ipAddress, ipPermission, CoreEvent.STATE_SUCCESSFUL);
235         } catch (Exception JavaDoc e) {
236             fireCoreEvent(request, CoreEventConstants.DELETE_IP_RESTRICTION, ipAddress, ipPermission, CoreEvent.STATE_UNSUCCESSFUL);
237             throw e;
238         }
239     }
240     
241     private static IpRestriction findIpRestriction(IpRestriction[] restrictions, int id) {
242         for (IpRestriction restriction : restrictions) {
243             if (restriction.getID() == id)
244                 return restriction;
245         }
246         return null;
247     }
248
249     private void fireCoreEvent(HttpServletRequest JavaDoc request, int eventID, String JavaDoc ipAddress, String JavaDoc ipPermission, int state) {
250         CoreEvent coreEvent = new CoreEvent(this, eventID, null, getSessionInfo(request), state);
251         coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_ADDRESS, ipAddress);
252         coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_IS_AUTHORIZED, ipPermission);
253         CoreServlet.getServlet().fireCoreEvent(coreEvent);
254     }
255
256     /**
257      * Create a new restriction
258      * @param mapping mapping
259      * @param form form
260      * @param request request
261      * @param response response
262      * @return forward
263      * @throws Exception on any error
264      */

265     public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
266                     throws Exception JavaDoc {
267         return mapping.findForward("create");
268     }
269
270     /**
271      * Create an existing restriction
272      * @param mapping mapping
273      * @param form form
274      * @param request request
275      * @param response response
276      * @return forward
277      * @throws Exception on any error
278      */

279     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
280                     throws Exception JavaDoc {
281         int id = Integer.parseInt(request.getParameter("id"));
282         IpRestriction restriction = SystemDatabaseFactory.getInstance().getIpRestriction(id);
283         request.setAttribute(Constants.EDITING_ITEM, restriction);
284         return mapping.findForward("edit");
285     }
286
287     /*
288      * (non-Javadoc)
289      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
290      * org.apache.struts.action.ActionForm,
291      * javax.servlet.http.HttpServletRequest,
292      * javax.servlet.http.HttpServletResponse)
293      */

294     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
295         return list(mapping, form, request, response);
296     }
297
298     /**
299      * @param mapping
300      * @param form
301      * @param request
302      * @param response
303      * @return ActionForward
304      * @throws Exception
305      */

306     public ActionForward filter(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
307                     HttpServletResponse JavaDoc response) throws Exception JavaDoc {
308         return unspecified(mapping, form, request, response);
309     }
310 }
Popular Tags