KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > server > auth > acl > IpAddressAccessControlParser


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.server.auth.acl;
18
19 import org.alfresco.config.ConfigElement;
20 import org.alfresco.filesys.util.IPAddress;
21
22 /**
23  * Ip Address Access Control Parser Class
24  */

25 public class IpAddressAccessControlParser extends AccessControlParser
26 {
27
28     /**
29      * Default constructor
30      */

31     public IpAddressAccessControlParser()
32     {
33     }
34
35     /**
36      * Return the parser type
37      *
38      * @return String
39      */

40     public String JavaDoc getType()
41     {
42         return "address";
43     }
44
45     /**
46      * Validate the parameters and create an address access control
47      *
48      * @param params ConfigElement
49      * @return AccessControl
50      * @throws ACLParseException
51      */

52     public AccessControl createAccessControl(ConfigElement params) throws ACLParseException
53     {
54
55         // Get the access type
56

57         int access = parseAccessType(params);
58
59         // Check if the single IP address format has been specified
60

61         String JavaDoc ipAddr = params.getAttribute("ip");
62         if (ipAddr != null)
63         {
64
65             // Validate the parameters
66

67             if (ipAddr.length() == 0 || IPAddress.isNumericAddress(ipAddr) == false)
68                 throw new ACLParseException("Invalid IP address, " + ipAddr);
69
70             if (params.getAttributeCount() != 2)
71                 throw new ACLParseException("Invalid parameter(s) specified for address");
72
73             // Create a single TCP/IP address access control rule
74

75             return new IpAddressAccessControl(ipAddr, null, getType(), access);
76         }
77
78         // Check if a subnet address and mask have been specified
79

80         String JavaDoc subnet = params.getAttribute("subnet");
81         if (subnet != null)
82         {
83
84             // Get the network mask parameter
85

86             String JavaDoc netmask = params.getAttribute("mask");
87
88             // Validate the parameters
89

90             if (subnet.length() == 0 || netmask == null || netmask.length() == 0)
91                 throw new ACLParseException("Invalid subnet/mask parameter");
92
93             if (IPAddress.isNumericAddress(subnet) == false)
94                 throw new ACLParseException("Invalid subnet parameter, " + subnet);
95
96             if (IPAddress.isNumericAddress(netmask) == false)
97                 throw new ACLParseException("Invalid mask parameter, " + netmask);
98
99             // Create a subnet address access control rule
100

101             return new IpAddressAccessControl(subnet, netmask, getType(), access);
102         }
103
104         // Invalid parameters
105

106         throw new ACLParseException("Unknown address parameter(s)");
107     }
108 }
109
Popular Tags