// 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;
package org.owasp.oneliner.filters;




/**
 * Author: @johnwilander
 * Date: 2010-nov-07
 */
public class CsrfProtectionFilter implements ResourceFilter, ContainerRequestFilter {
    private static final Log logger = LogFactory.getLog(CsrfProtectionFilter.class);
    public static final String SIMPLE_COOKIE_NAME = "cookieToken";
    public static final int RANDOM_COOKIE_NAME_LENGTH = 16;
    public static final int RANDOM_COOKIE_VALUE_LENGTH = 16;
    public static final String PARAM_TOKEN = "paramToken";
    private HttpServletRequest httpServletRequest;

    public CsrfProtectionFilter(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

// 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;

package org.owasp.oneliner.filters;




/**
 * Author: @johnwilander
 * Date: 2010-nov-07
 */
public class CsrfProtectionFilter implements ResourceFilter, ContainerRequestFilter {
    private static final Log logger = LogFactory.getLog(CsrfProtectionFilter.class);
    public static final String SIMPLE_COOKIE_NAME = "cookieToken";
    public static final int RANDOM_COOKIE_NAME_LENGTH = 16;
    public static final int RANDOM_COOKIE_VALUE_LENGTH = 16;
    public static final String PARAM_TOKEN = "paramToken";
    private HttpServletRequest httpServletRequest;

    public CsrfProtectionFilter(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }

    public ContainerRequest filter(ContainerRequest containerRequest) throws BadCsrfProtectionTokenException {