KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32
33 import com.sslexplorer.core.CoreAttributeConstants;
34 import com.sslexplorer.core.CoreEvent;
35 import com.sslexplorer.core.CoreEventConstants;
36 import com.sslexplorer.core.CoreServlet;
37 import com.sslexplorer.core.CoreUtil;
38 import com.sslexplorer.core.actions.AuthenticatedDispatchAction;
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.IpRestrictionForm;
48
49 /**
50  * Implementation of a
51  * {@link com.sslexplorer.core.actions.AuthenticatedDispatchAction} that allows
52  * an administrator to create an <i>IP Restrictions</i>.
53  * @author P.J.King <a HREF="mailto:peter@3sp.com">&lt;peter@3sp.com&gt;</a>
54  */

55 public class ShowIpRestrictionDispatchAction extends AuthenticatedDispatchAction {
56
57     /**
58      * Constructor.
59      */

60     public ShowIpRestrictionDispatchAction() {
61         super(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, new Permission[] { PolicyConstants.PERM_CREATE, PolicyConstants.PERM_DELETE });
62     }
63
64     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws Exception JavaDoc {
65         return mapping.findForward("display");
66     }
67
68     /**
69      * This will setup the IP Restriction Form.
70      * @param mapping
71      * @param form
72      * @param request
73      * @param response
74      * @return ActionForward
75      * @throws Exception
76      */

77     public ActionForward create(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
78                     throws Exception JavaDoc {
79         PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_CREATE, request);
80         IpRestriction[] ipRestriction = SystemDatabaseFactory.getInstance().getIpRestrictions();
81         IpRestrictionForm ipRestrictionForm = (IpRestrictionForm) form;
82         ipRestrictionForm.initialize(new IpRestriction(ipRestriction.length > 0 && ipRestriction[0].getDenied()), false);
83         ipRestrictionForm.setReferer(CoreUtil.getReferer(request));
84         CoreUtil.addRequiredFieldMessage(this, request);
85         return mapping.findForward("display");
86     }
87
88     /**
89      * This will setup the IP Restriction Form.
90      * @param mapping
91      * @param form
92      * @param request
93      * @param response
94      * @return ActionForward
95      * @throws Exception
96      */

97     public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
98                     throws Exception JavaDoc {
99         PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, PolicyConstants.PERM_EDIT, request);
100         IpRestrictionForm ipRestrictionForm = (IpRestrictionForm) form;
101         ipRestrictionForm.initialize((IpRestriction)request.getAttribute(Constants.EDITING_ITEM), true);
102         ipRestrictionForm.setReferer(CoreUtil.getReferer(request));
103         CoreUtil.addRequiredFieldMessage(this, request);
104         return mapping.findForward("display");
105     }
106
107     /**
108      * This will attempt to write the IP Restriction to the database.
109      * @param mapping
110      * @param form
111      * @param request
112      * @param response
113      * @return ActionForward
114      * @throws Exception
115      */

116     public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
117                     throws Exception JavaDoc {
118         if (isAdditionValid(request, ((IpRestrictionForm)form))) {
119             return doCommit(mapping, form, request, response);
120         }
121         else {
122             return mapping.findForward("confirmCreateWithLockout");
123         }
124     }
125     
126     /**
127      * This will attempt to write the IP Restriction to the database.
128      * @param mapping
129      * @param form
130      * @param request
131      * @param response
132      * @return ActionForward
133      * @throws Exception
134      */

135     public ActionForward doCommit(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
136                         throws Exception JavaDoc {
137         IpRestrictionForm ipRestrictionForm = (IpRestrictionForm)form;
138         ipRestrictionForm.apply();
139         Permission permission = ipRestrictionForm.isEditing() ? PolicyConstants.PERM_EDIT : PolicyConstants.PERM_CREATE;
140         PolicyUtil.checkPermission(PolicyConstants.IP_RESTRICTIONS_RESOURCE_TYPE, permission, request);
141         
142         try {
143             if(ipRestrictionForm.isEditing()) {
144                 SystemDatabaseFactory.getInstance().updateIpRestriction(ipRestrictionForm.getRestriction());
145             }
146             else {
147                 SystemDatabaseFactory.getInstance().addIpRestriction(ipRestrictionForm.getRestriction().getAddress(), ipRestrictionForm.getRestriction().getType());
148             }
149             fireCoreEvent(request, ipRestrictionForm, CoreEvent.STATE_SUCCESSFUL);
150             saveMessage(request, "editIpRestriction.message.restrictionSaved", ipRestrictionForm.getRestriction().getAddress());
151         } catch (Exception JavaDoc e) {
152             fireCoreEvent(request, ipRestrictionForm, CoreEvent.STATE_UNSUCCESSFUL);
153             throw e;
154         }
155         return cancel(mapping, form, request, response);
156     }
157     
158     private boolean isAdditionValid(HttpServletRequest JavaDoc request, IpRestrictionForm form) throws Exception JavaDoc {
159         IpRestriction[] ipRestrictions = findIpRestrictions(form.getRestriction().getAddress(), form.getType().equals(IpRestrictionForm.ALLOW_TYPE));
160         SystemDatabase database = SystemDatabaseFactory.getInstance();
161         String JavaDoc remoteAddr = request.getRemoteAddr();
162         return database.verifyIPAddress(remoteAddr, ipRestrictions);
163     }
164
165     private IpRestriction[] findIpRestrictions(String JavaDoc restriction, boolean isAllow) throws Exception JavaDoc {
166         SystemDatabase database = SystemDatabaseFactory.getInstance();
167         IpRestriction[] restrictions = database.getIpRestrictions();
168         int i = 0;
169         for(; i < restrictions.length; i++) {
170             if(restrictions[i].getAddress().equals(restriction)) {
171                 restrictions[i] = new IpRestriction(restrictions[i].getAddress(), isAllow, restrictions[i].getPriority());
172                 break;
173             }
174         }
175         if(i == restrictions.length) {
176             List JavaDoc<IpRestriction> newRestrictions = new ArrayList JavaDoc<IpRestriction>(Arrays.asList(restrictions));
177             newRestrictions.add(new IpRestriction(restriction, isAllow, Integer.MAX_VALUE));
178             return newRestrictions.toArray(new IpRestriction[newRestrictions.size()]);
179         }
180         return restrictions;
181     }
182     
183     private void fireCoreEvent(HttpServletRequest JavaDoc request, IpRestrictionForm ipRestrictionForm, int state) {
184         IpRestriction restriction = ipRestrictionForm.getRestriction();
185         int eventType = ipRestrictionForm.isEditing() ? CoreEventConstants.EDIT_IP_RESTRICTION : CoreEventConstants.CREATE_IP_RESTRICTION;
186         CoreEvent coreEvent = new CoreEvent(this, eventType, null, getSessionInfo(request), state);
187         coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_ADDRESS, restriction.getAddress());
188         coreEvent.addAttribute(CoreAttributeConstants.EVENT_ATTR_IP_RESTRICTION_IS_AUTHORIZED, String.valueOf(restriction.getAllowed()));
189         CoreServlet.getServlet().fireCoreEvent(coreEvent);
190
191     }
192     
193     /*
194      * (non-Javadoc)
195      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
196      * org.apache.struts.action.ActionForm,
197      * javax.servlet.http.HttpServletRequest,
198      * javax.servlet.http.HttpServletResponse)
199      */

200     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
201         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
202     }
203 }
Popular Tags