001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 07/12/2006 20:59:12
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.entities;
044:
045: import java.io.Serializable;
046: import java.util.StringTokenizer;
047:
048: import org.apache.commons.lang.StringUtils;
049:
050: /**
051: * @author Rafael Steil
052: * @version $Id: Banlist.java,v 1.3 2006/12/10 23:08:21 rafaelsteil Exp $
053: */
054: public class Banlist implements Serializable {
055: private int id;
056: private int userId;
057: private String ip;
058: private String email;
059:
060: /**
061: * @return the id
062: */
063: public int getId() {
064: return this .id;
065: }
066:
067: /**
068: * @return the userId
069: */
070: public int getUserId() {
071: return this .userId;
072: }
073:
074: /**
075: * @return the ip
076: */
077: public String getIp() {
078: return this .ip;
079: }
080:
081: /**
082: * @return the email
083: */
084: public String getEmail() {
085: return this .email;
086: }
087:
088: /**
089: * @param id the id to set
090: */
091: public void setId(int id) {
092: this .id = id;
093: }
094:
095: /**
096: * @param userId the userId to set
097: */
098: public void setUserId(int userId) {
099: this .userId = userId;
100: }
101:
102: /**
103: * @param ip the ip to set
104: */
105: public void setIp(String ip) {
106: this .ip = ip;
107: }
108:
109: /**
110: * @param email the email to set
111: */
112: public void setEmail(String email) {
113: this .email = email;
114: }
115:
116: public boolean matches(Banlist b) {
117: boolean status = false;
118:
119: if (this .matchesUserId(b) || this .matchesEmail(b)) {
120: status = true;
121: } else if (!StringUtils.isEmpty(b.getIp())
122: && !StringUtils.isEmpty(this .getIp())) {
123: if (b.getIp().equalsIgnoreCase(this .getIp())) {
124: status = true;
125: } else {
126: status = this .matchIp(b);
127: }
128: }
129:
130: return status;
131: }
132:
133: private boolean matchesEmail(Banlist b) {
134: return (!StringUtils.isEmpty(b.getEmail()) && b.getEmail()
135: .equals(this .getEmail()));
136: }
137:
138: private boolean matchesUserId(Banlist b) {
139: return b.getUserId() > 0 && this .getUserId() > 0
140: && b.getUserId() == this .getUserId();
141: }
142:
143: private boolean matchIp(Banlist b) {
144: boolean status = false;
145:
146: StringTokenizer userToken = new StringTokenizer(b.getIp(), ".");
147: StringTokenizer this Token = new StringTokenizer(this .getIp(),
148: ".");
149:
150: if (userToken.countTokens() == this Token.countTokens()) {
151: String[] userValues = this .tokenizerAsArray(userToken);
152: String[] this Values = this .tokenizerAsArray(this Token);
153:
154: status = this .compareIpValues(userValues, this Values);
155: }
156: return status;
157: }
158:
159: private boolean compareIpValues(String[] userValues,
160: String[] this Values) {
161: boolean helperStatus = true;
162: boolean onlyStars = true;
163:
164: for (int i = 0; i < this Values.length; i++) {
165: if (this Values[i].charAt(0) != '*') {
166: onlyStars = false;
167:
168: if (!this Values[i].equals(userValues[i])) {
169: helperStatus = false;
170: }
171: }
172: }
173:
174: return helperStatus && !onlyStars;
175: }
176:
177: private String[] tokenizerAsArray(StringTokenizer token) {
178: String[] values = new String[token.countTokens()];
179:
180: for (int i = 0; token.hasMoreTokens(); i++) {
181: values[i] = token.nextToken();
182: }
183:
184: return values;
185: }
186: }
|