KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > IPValidator


1 package org.jahia.security.license;
2
3 import java.net.InetAddress JavaDoc;
4 import java.net.UnknownHostException JavaDoc;
5 import org.jahia.resourcebundle.ResourceMessage;
6 import java.math.BigInteger JavaDoc;
7
8 /**
9  * <p>Title: </p>
10  * <p>Description: </p>
11  * <p>Copyright: Copyright (c) 2002</p>
12  * <p>Company: Jahia Ltd</p>
13  * @author Serge Huber
14  * @version 1.0
15  * @todo not finished yet.
16  */

17
18 public class IPValidator extends AbstractValidator {
19
20     private static org.apache.log4j.Logger logger =
21         org.apache.log4j.Logger.getLogger(IPValidator.class);
22
23     public IPValidator(String JavaDoc name, String JavaDoc value, License license) {
24         super(name, value, license);
25     }
26
27     public boolean assertEquals(String JavaDoc value) {
28
29         InetAddress JavaDoc inetA = null;
30         try {
31             inetA = InetAddress.getLocalHost();
32             if ( inetA == null ){
33                 logger.error("The inet address could not be found");
34                 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label");
35                 return false;
36             }
37         } catch ( UnknownHostException JavaDoc uhe ){
38             logger.error ("The inet address could not be found", uhe);
39             errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label");
40             return false;
41         }
42
43         logger.debug( "inet address hostname=" + inetA.getHostName() + ", hostAddress=" + inetA.getHostAddress());
44
45         InetAddress JavaDoc authorizedIP = null;
46         try {
47             authorizedIP = InetAddress.getByName(value);
48         } catch (UnknownHostException JavaDoc uhe) {
49             logger.error ("The inet address format is invalid", uhe);
50             errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPFormat.label", value);
51             return false;
52         }
53
54         if ( !isIPAllowed(inetA, authorizedIP) ) {
55             logger.error ("Invalid license ID <" + value + "> for host <" + inetA.getHostAddress() + ">");
56             errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPAddress.label", inetA.getHostAddress(), value);
57             return false;
58         }
59         return true;
60     }
61
62     public boolean assertInRange(String JavaDoc fromValue, String JavaDoc toValue) {
63
64         InetAddress JavaDoc inetA = null;
65         try {
66             inetA = InetAddress.getLocalHost();
67             if ( inetA == null ){
68                 logger.error("The inet address could not be found");
69                 errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label");
70                 return false;
71             }
72         } catch ( UnknownHostException JavaDoc uhe ){
73             logger.error ("The inet address could not be found", uhe);
74             errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.errorRetrievingLocalhostIP.label");
75             return false;
76         }
77
78         InetAddress JavaDoc fromIP = null;
79         try {
80             fromIP = InetAddress.getByName(fromValue);
81         } catch (UnknownHostException JavaDoc uhe) {
82             logger.error ("The inet address format is invalid", uhe);
83             errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPFormat.label", fromValue);
84             return false;
85         }
86         InetAddress JavaDoc toIP = null;
87         try {
88             toIP = InetAddress.getByName(toValue);
89         } catch (UnknownHostException JavaDoc uhe) {
90             logger.error ("The inet address format is invalid", uhe);
91             errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPFormat.label", toValue);
92             return false;
93         }
94
95         if (!isIPAllowed(inetA, fromIP, toIP)) {
96             logger.error ("Host <" + inetA.getHostAddress() + "> is not in IP range " + fromValue + " to " + toValue);
97             errorMessage = new ResourceMessage("org.jahia.security.license.IPValidator.invalidIPAddressForRange.label", inetA.getHostAddress(), fromValue, toValue);
98             return false;
99         }
100
101         return true;
102     }
103
104     private boolean isIPAllowed(InetAddress JavaDoc testIP, InetAddress JavaDoc authorizationIP) {
105         byte[] testAddress = testIP.getAddress();
106         byte[] authorizationAddress = authorizationIP.getAddress();
107
108         for (int i=0; i < authorizationAddress.length; i++) {
109             if (authorizationAddress[i] != testAddress[i]) {
110                 return false;
111             }
112         }
113         return true;
114     }
115
116     private boolean isIPAllowed(InetAddress JavaDoc testIP, InetAddress JavaDoc fromIP, InetAddress JavaDoc toIP) {
117         byte[] testAddress = testIP.getAddress();
118         byte[] fromAddress = fromIP.getAddress();
119         byte[] toAddress = toIP.getAddress();
120
121         BigInteger JavaDoc testInt = new BigInteger JavaDoc(testAddress);
122         BigInteger JavaDoc fromInt = new BigInteger JavaDoc(fromAddress);
123         BigInteger JavaDoc toInt = new BigInteger JavaDoc(toAddress);
124
125         if ((testInt.compareTo(fromInt) >= 0) && (testInt.compareTo(toInt) <= 0)) {
126             return true;
127         }
128         return false;
129     }
130
131 }
132
Popular Tags