001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.captcha;
017:
018: import org.acegisecurity.context.SecurityContextImpl;
019:
020: /**
021: * Default CaptchaSecurityContext implementation
022: *
023: * @author mag
024: */
025: public class CaptchaSecurityContextImpl extends SecurityContextImpl
026: implements CaptchaSecurityContext {
027: //~ Instance fields ================================================================================================
028:
029: private boolean human;
030: private int humanRestrictedResourcesRequestsCount;
031: private long lastPassedCaptchaDate;
032:
033: //~ Constructors ===================================================================================================
034:
035: public CaptchaSecurityContextImpl() {
036: super ();
037: human = false;
038: lastPassedCaptchaDate = 0;
039: humanRestrictedResourcesRequestsCount = 0;
040: }
041:
042: //~ Methods ========================================================================================================
043:
044: public boolean equals(Object obj) {
045: if (obj instanceof CaptchaSecurityContextImpl) {
046: CaptchaSecurityContextImpl rhs = (CaptchaSecurityContextImpl) obj;
047:
048: if (this .isHuman() != rhs.isHuman()) {
049: return false;
050: }
051:
052: if (this .getHumanRestrictedResourcesRequestsCount() != rhs
053: .getHumanRestrictedResourcesRequestsCount()) {
054: return false;
055: }
056:
057: if (this .getLastPassedCaptchaDateInMillis() != rhs
058: .getLastPassedCaptchaDateInMillis()) {
059: return false;
060: }
061:
062: return super .equals(obj);
063: }
064:
065: return false;
066: }
067:
068: public int getHumanRestrictedResourcesRequestsCount() {
069: return humanRestrictedResourcesRequestsCount;
070: }
071:
072: public long getLastPassedCaptchaDateInMillis() {
073: return lastPassedCaptchaDate;
074: }
075:
076: public int hashCode() {
077: int code = super .hashCode();
078: code ^= this .humanRestrictedResourcesRequestsCount;
079: code ^= this .lastPassedCaptchaDate;
080:
081: if (this .isHuman()) {
082: code ^= -37;
083: }
084:
085: return code;
086: }
087:
088: /**
089: * Method to increment the human Restricted Resrouces Requests Count;
090: */
091: public void incrementHumanRestrictedRessoucesRequestsCount() {
092: humanRestrictedResourcesRequestsCount++;
093: }
094:
095: public boolean isHuman() {
096: return human;
097: }
098:
099: /**
100: * Reset the lastPassedCaptchaDate and count.
101: */
102: public void setHuman() {
103: this .human = true;
104: this .lastPassedCaptchaDate = System.currentTimeMillis();
105: this .humanRestrictedResourcesRequestsCount = 0;
106: }
107: }
|