// Path: src/main/java/org/owasp/oneliner/exceptions/BadCsrfProtectionTokenException.java
// public class BadCsrfProtectionTokenException extends RuntimeException {
// 
//     public static final BadCsrfProtectionTokenException INSTANCE = new BadCsrfProtectionTokenException();
//     public final ReasonWrapper reasonWrapper;
//     public final Reason reason;
// 
//     private BadCsrfProtectionTokenException() {
//         this.reason = Reason.FORBIDDEN;
//         this.reasonWrapper = new ReasonWrapper(reason);
//         this.setStackTrace(new StackTraceElement[]{});  // Empty stack trace since this is a signaling exception
//     }
// 
//     public enum Reason {
//         FORBIDDEN (Response.Status.FORBIDDEN);
// 
//         public final Response.Status status;
//         Reason(Response.Status status) {
//             this.status = status;
//         }
//     }
// 
//     @XmlRootElement
//     public static class ReasonWrapper {
//         public final Reason reason;
//         public ReasonWrapper(Reason reason) { this.reason = reason; }
//         private ReasonWrapper() { this.reason = null; } // here for JAXB
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/util/RandomAlphaNumericString.java
// public static boolean isValidAlphaNumericString(String string) {
//     int length = string.length();
//     if (length > MAX_SIZE) {
//         return false;
//     }
//     for (int index=0; index < length; index++) {
//         if (CHARS.indexOf(string.charAt(index)) == -1) {
//             if (logger.isDebugEnabled()) { logger.debug("Found illegal cookie character: " + string.charAt(index)); }
//             return false;
//         }
//     }
//     return true;
// }


import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.sun.jersey.spi.container.ContainerResponseFilter;
import com.sun.jersey.spi.container.ResourceFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.owasp.oneliner.exceptions.BadCsrfProtectionTokenException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import static org.owasp.oneliner.util.RandomAlphaNumericString.isValidAlphaNumericString;
            throw BadCsrfProtectionTokenException.INSTANCE;
        }
    }

    public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    public ContainerRequestFilter getRequestFilter() {
        return this;
    }

    public ContainerResponseFilter getResponseFilter() {
        return null;  // No response filter
    }

    // Private help methods

    private String getCsrfProtectionTokenCookie() {
        int numOfSimpleCookies = 0, numOfRandomCookies = 0;
        boolean foundRandomCookie = false;
        Cookie[] cookies = httpServletRequest.getCookies();
        String csrfProtectionCookieValue = null;
        for (Cookie cookie : cookies) {
            String cookieName = cookie.getName();
            if (logger.isDebugEnabled()) { logger.debug("Checking cookie: " + cookieName); }
            if (!foundRandomCookie && SIMPLE_COOKIE_NAME.equals(cookieName)) {
                csrfProtectionCookieValue = cookie.getValue();
                numOfSimpleCookies++;
            } else if (cookieName.length() == RANDOM_COOKIE_NAME_LENGTH &&
// Path: src/main/java/org/owasp/oneliner/exceptions/BadCsrfProtectionTokenException.java
// public class BadCsrfProtectionTokenException extends RuntimeException {
// 
//     public static final BadCsrfProtectionTokenException INSTANCE = new BadCsrfProtectionTokenException();
//     public final ReasonWrapper reasonWrapper;
//     public final Reason reason;
// 
//     private BadCsrfProtectionTokenException() {
//         this.reason = Reason.FORBIDDEN;
//         this.reasonWrapper = new ReasonWrapper(reason);
//         this.setStackTrace(new StackTraceElement[]{});  // Empty stack trace since this is a signaling exception
//     }
// 
//     public enum Reason {
//         FORBIDDEN (Response.Status.FORBIDDEN);
// 
//         public final Response.Status status;
//         Reason(Response.Status status) {
//             this.status = status;
//         }
//     }
// 
//     @XmlRootElement
//     public static class ReasonWrapper {
//         public final Reason reason;
//         public ReasonWrapper(Reason reason) { this.reason = reason; }
//         private ReasonWrapper() { this.reason = null; } // here for JAXB
//     }
// }
// 
// Path: src/main/java/org/owasp/oneliner/util/RandomAlphaNumericString.java
// public static boolean isValidAlphaNumericString(String string) {
//     int length = string.length();
//     if (length > MAX_SIZE) {
//         return false;
//     }
//     for (int index=0; index < length; index++) {
//         if (CHARS.indexOf(string.charAt(index)) == -1) {
//             if (logger.isDebugEnabled()) { logger.debug("Found illegal cookie character: " + string.charAt(index)); }
//             return false;
//         }
//     }
//     return true;
// }


// Path: src/main/java/org/owasp/oneliner/filters/CsrfProtectionFilter.java
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
import com.sun.jersey.spi.container.ContainerResponseFilter;
import com.sun.jersey.spi.container.ResourceFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.owasp.oneliner.exceptions.BadCsrfProtectionTokenException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import static org.owasp.oneliner.util.RandomAlphaNumericString.isValidAlphaNumericString;

            throw BadCsrfProtectionTokenException.INSTANCE;
        }
    }

    public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    public ContainerRequestFilter getRequestFilter() {
        return this;
    }

    public ContainerResponseFilter getResponseFilter() {
        return null;  // No response filter
    }

    // Private help methods

    private String getCsrfProtectionTokenCookie() {
        int numOfSimpleCookies = 0, numOfRandomCookies = 0;
        boolean foundRandomCookie = false;
        Cookie[] cookies = httpServletRequest.getCookies();
        String csrfProtectionCookieValue = null;
        for (Cookie cookie : cookies) {
            String cookieName = cookie.getName();
            if (logger.isDebugEnabled()) { logger.debug("Checking cookie: " + cookieName); }
            if (!foundRandomCookie && SIMPLE_COOKIE_NAME.equals(cookieName)) {
                csrfProtectionCookieValue = cookie.getValue();
                numOfSimpleCookies++;
            } else if (cookieName.length() == RANDOM_COOKIE_NAME_LENGTH &&
                    isValidAlphaNumericString(cookieName)) {