KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > forms > IpRestrictionForm


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.forms;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23
24 import org.apache.struts.Globals;
25 import org.apache.struts.action.ActionErrors;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionMessage;
28
29 import com.sslexplorer.core.forms.CoreForm;
30 import com.sslexplorer.input.validators.IPAddressValidator;
31 import com.sslexplorer.security.IpRestriction;
32
33 /**
34  * Implementation of a {@link com.sslexplorer.core.forms.CoreForm} that allows
35  * an administrator to create an <i>IP Restrictions</i>.
36  *
37  * @author P.J.King <a HREF="mailto:peter@3sp.com">&lt;peter@3sp.com&gt;</a>
38  * @see com.sslexplorer.security.IpRestrictionItem
39  *
40  */

41 public class IpRestrictionForm extends CoreForm {
42     
43     /**
44      * String constant for allow type
45      */

46     public final static String JavaDoc ALLOW_TYPE = "allow";
47     
48     /**
49      * String constant for deny type
50      */

51     public final static String JavaDoc DENY_TYPE = "deny";
52     
53
54     private String JavaDoc type;
55     private boolean addressEnabled;
56     private boolean editing;
57     private IpRestriction restriction;
58
59     /**
60      * Sets the IP Restriction to empty strings.
61      *
62      * @param restriction restriction to edit
63      * @param editing user is editing the restriction (as opposed to creating)
64      */

65     public void initialize(IpRestriction restriction, boolean editing) {
66         this.restriction = restriction;
67         addressEnabled = !editing || !restriction.isDefault();
68         type = restriction.getAllowed() ? ALLOW_TYPE : DENY_TYPE;
69         this.editing = editing;
70     }
71     
72     /**
73      * Get the IP restriction object being edited
74      *
75      * @return IP restriction
76      */

77     public IpRestriction getRestriction() {
78         return restriction;
79     }
80     
81     /**
82      * Get if the IP restriction is being edited.
83      *
84      * @return editing
85      */

86     public boolean isEditing() {
87         return editing;
88     }
89     
90     /**
91      * Get if the address field should be enabled. This will be <code>false</code>
92      * if the IP restriction is the default rule and the user is editing.
93      *
94      * @return address enabled
95      */

96     public boolean isAddressEnabled() {
97         return addressEnabled;
98     }
99     
100     /**
101      * Apply the remaining details to the restriction object (this just
102      * sets the type according to the type value and address collected by the form)
103      */

104     public void apply() {
105         restriction.setType(IpRestriction.getType(restriction.getAddress(), type.equals(ALLOW_TYPE)));
106     }
107
108     /* (non-Javadoc)
109      * @see org.apache.struts.action.ActionForm#validate(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
110      */

111     public ActionErrors validate(ActionMapping arg0, HttpServletRequest JavaDoc arg1) {
112         ActionErrors errors = new ActionErrors();
113         if (isCommiting()) {
114             if (restriction.getAddress() == null || restriction.getAddress().equals("")) {
115                 errors.add(Globals.ERROR_KEY, new ActionMessage("editIpRestriction.error.noIpAddress"));
116             }
117             if(!isEditing() && restriction.getAddress().equals("*.*.*.*")) {
118                 errors.add(Globals.ERROR_KEY, new ActionMessage("editIpRestriction.error.usingDefaultPattern"));
119             }
120             else if(!IPAddressValidator.isIpAddressExpressionValid(restriction.getAddress())) {
121                 errors.add(Globals.ERROR_KEY, new ActionMessage("editIpRestriction.error.invalidIpAddress"));
122             }
123         }
124         return errors;
125     }
126
127     /**
128      * Gets the IP Restriction type. Will be one of {@link #ALLOW_TYPE} or
129      * {@link #DENY_TYPE}.
130      *
131      * @return type
132      */

133     public String JavaDoc getType() {
134         return type;
135     }
136
137     /**
138      * Sets the IP Restriction type. Should be one of {@link #ALLOW_TYPE} or
139      * {@link #DENY_TYPE}.
140      *
141      * @param type type
142      */

143     public void setType(String JavaDoc type) {
144         this.type = type;
145     }
146 }
Popular Tags