<commit_before>/* 
 * Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

package com.snowplowanalytics.tomcat;

// Java
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;

// Tomcat
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;

// Get our project settings
import com.snowplowanalytics.tomcat.generated.ProjectSettings;

/**
 * A custom AccessLogValve for Tomcat to help generate CloudFront-like access logs.
 * Used in SnowPlow's Clojure Collector.
 *
 * Introduces a new pattern, 'I', to escape an incoming header
 * Introduces a new pattern, 'C', to fetch a cookie stored on the response
 * Re-implements the pattern 'i' to ensure that "" (empty string) is replaced with "-"
 * Re-implements the pattern 'q' to remove the "?" and ensure "" (empty string) is replaced with "-"
 * Overwrites the 'v' pattern, to write the version of this AccessLogValve, rather than the local server name
 *
 * This is adapted from the original AccessLogValve code here:
 * http://javasourcecode.org/html/open-source/tomcat/tomcat-7.0.29/org/apache/catalina/valves/AccessLogValve.java.html
 */
public class CfAccessLogValve extends AccessLogValve {

    private static final String cfEncoding = "UTF-8";

    /**
     * Create an AccessLogElement implementation which needs header string.
     *
     * Changes:
     * - Added 'I' pattern, to escape an incoming header
     * - Added 'C' pattern, to fetch a cookie on the response (not request)
     * - Fixed 'i' pattern, to replace "" (empty string) with "-"
     */
    @Override
    protected AccessLogElement createAccessLogElement(String header, char pattern) {

        switch (pattern) {
            // A safer header element returns a hyphen never a null/empty string
            case 'i':
                return new SaferHeaderElement(header);
            // Added EscapedHeaderElement        
            case 'I':
                return new EscapedHeaderElement(header);
            // Added EscapedHeaderElement        
            case 'C':
                return new ResponseCookieElement(header);
            // Back to AccessLogValve's handler
            default:
                return super.createAccessLogElement(header, pattern);
        }
    }

    /**
     * create an AccessLogElement implementation which doesn't need a header string.
     *
     * Changes:
     * - Fixed 'q' pattern, to remove the "?" and ensure "" (empty string) is replaced with "-"
     * - Overwrote 'v' pattern, to write the version of this AccessLogValve, rather than the local server name  
     */
    @Override
    protected AccessLogElement createAccessLogElement(char pattern) {
        switch (pattern) {
            // A better (safer, Cf-compatible) querystring element
            case 'q':
                return new BetterQueryElement();
            // Return the version of this AccessLogValve
            case 'v':
                return new AccessLogValveVersionElement();
            // Back to AccessLogValve's handler
            default:
                return super.createAccessLogElement(pattern);
        }
    }

    /**
     * We replace writing the local server name with writing
     * the version of this Tomcat AccessLogValve - %v
     */
    protected static class AccessLogValveVersionElement implements AccessLogElement {
        @Override
        public void addElement(StringBuilder buf, Date date, Request request,
                Response response, long time) {
            buf.append("tom-");
            buf.append(ProjectSettings.VERSION);
        }
    }
<commit_msg>Made VersionElement less verbose<commit_after>/* 
 * Copyright (c) 2012 SnowPlow Analytics Ltd. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

package com.snowplowanalytics.tomcat;

// Java
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;

// Tomcat
import org.apache.catalina.valves.AccessLogValve;
import org.apache.catalina.connector.Request;
import org.apache.catalina.connector.Response;

// Get our project settings
import com.snowplowanalytics.tomcat.generated.ProjectSettings;

/**
 * A custom AccessLogValve for Tomcat to help generate CloudFront-like access logs.
 * Used in SnowPlow's Clojure Collector.
 *
 * Introduces a new pattern, 'I', to escape an incoming header
 * Introduces a new pattern, 'C', to fetch a cookie stored on the response
 * Re-implements the pattern 'i' to ensure that "" (empty string) is replaced with "-"
 * Re-implements the pattern 'q' to remove the "?" and ensure "" (empty string) is replaced with "-"
 * Overwrites the 'v' pattern, to write the version of this AccessLogValve, rather than the local server name
 *
 * This is adapted from the original AccessLogValve code here:
 * http://javasourcecode.org/html/open-source/tomcat/tomcat-7.0.29/org/apache/catalina/valves/AccessLogValve.java.html
 */
public class CfAccessLogValve extends AccessLogValve {

    private static final String cfEncoding = "UTF-8";

    /**
     * Create an AccessLogElement implementation which needs header string.
     *
     * Changes:
     * - Added 'I' pattern, to escape an incoming header
     * - Added 'C' pattern, to fetch a cookie on the response (not request)
     * - Fixed 'i' pattern, to replace "" (empty string) with "-"
     */
    @Override
    protected AccessLogElement createAccessLogElement(String header, char pattern) {

        switch (pattern) {
            // A safer header element returns a hyphen never a null/empty string
            case 'i':
                return new SaferHeaderElement(header);
            // Added EscapedHeaderElement        
            case 'I':
                return new EscapedHeaderElement(header);
            // Added EscapedHeaderElement        
            case 'C':
                return new ResponseCookieElement(header);
            // Back to AccessLogValve's handler
            default:
                return super.createAccessLogElement(header, pattern);
        }
    }

    /**
     * create an AccessLogElement implementation which doesn't need a header string.
     *
     * Changes:
     * - Fixed 'q' pattern, to remove the "?" and ensure "" (empty string) is replaced with "-"
     * - Overwrote 'v' pattern, to write the version of this AccessLogValve, rather than the local server name  
     */
    @Override
    protected AccessLogElement createAccessLogElement(char pattern) {
        switch (pattern) {
            // A better (safer, Cf-compatible) querystring element
            case 'q':
                return new BetterQueryElement();
            // Return the version of this AccessLogValve
            case 'v':
                return new ValveVersionElement();
            // Back to AccessLogValve's handler
            default:
                return super.createAccessLogElement(pattern);
        }
    }

    /**
     * We replace writing the local server name with writing
     * the version of this Tomcat AccessLogValve - %v
     */
    protected static class ValveVersionElement implements AccessLogElement {
        @Override
        public void addElement(StringBuilder buf, Date date, Request request,
                Response response, long time) {
            buf.append("tom-");
            buf.append(ProjectSettings.VERSION);
        }
    }

{'fraction_non_alphanumeric': 0.07959858592769985, 'fraction_numerical': 0.004789599726308587, 'mean_word_length': 2.681780016792611, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3227469', 'n_tokens_mistral': 2309, 'n_tokens_neox': 2208, 'n_words': 861}