<commit_before>package org.apache.velocity.tools.view;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.servlet.http.HttpServletRequest;
import org.apache.velocity.tools.config.DefaultKey;
import org.apache.velocity.tools.view.ViewContext;
import org.apache.velocity.tools.config.InvalidScope;

/**
 *  <p>browser-sniffing tool (session or request scope requested, session scope advised).</p>
 *  <p></p>
 * <p><b>Usage:</b></p>
 * <p>BrowserTool defines properties that are used to test the client browser, operating system, device...
 * Apart from properties related to versioning, all properties are booleans.</p>
 * <p>The following properties are available:</p>
 * <ul>
 * <li><i>Versioning:</i>version majorVersion minorVersion geckoVersion</li>
 * <li><i>Browser:</i>mosaic netscape nav2 nav3 nav4 nav4up nav45 nav45up nav6 nav6up navgold firefox safari
 * ie ie3 ie4 ie4up ie5 ie5up ie55 ie55up ie6 opera opera3 opera4 opera5 opera6 opera7 lynx links
 * aol aol3 aol4 aol5 aol6 neoplanet neoplanet2 amaya icab avantgo emacs mozilla gecko webtv staroffice
 * lotusnotes konqueror</li>
 * <li><i>Operating systems:</i>win16 win3x win31 win95 win98 winnt windows win32 win2k winxp winme dotnet
 * mac macosx mac68k macppc os2 unix sun sun4 sun5 suni86 irix irix5 irix6 hpux hpux9 hpux10 aix aix1 aix2 aix3 aix4
 * linux sco unixware mpras reliant dec sinix freebsd bsd vms x11 amiga</li>
 * <li><i>Devices:</i>palm audrey iopener wap blackberry</li>
 * <li><i>Features:</i>javascript css css1 css2 dom0 dom1 dom2</li>
 * <li><i>Special:</i>robot (true if the page is requested by a robot, i.e. when one of the following properties is true:
 * wget getright yahoo altavista lycos infoseek lwp webcrawler linkexchange slurp google java)
 * </ul>
 *
 * Thanks to <NAME> (<EMAIL>), the author of the HTTP::BrowserDetect Perl module.
 * See also:
 * * http://www.zytrax.com/tech/web/browser_ids.htm
 * * http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
 * * http://en.wikipedia.org/wiki/User_agent
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since VelocityTools 1.2
 * @version $Revision$ $Date$
 */
@DefaultKey("browser")
@InvalidScope("application")
public class BrowserTool implements java.io.Serializable
{
    private static final long serialVersionUID = 1734529350532353339L;

    private String userAgent = null;
    private String version = null;
    private int majorVersion = -1;
    private int minorVersion = -1;
    private String geckoVersion = null;
    private int geckoMajorVersion = -1;
    private int geckoMinorVersion = -1;

    /**
     * Initializes this tool by setting the current {@link HttpServletRequest}.
     * This is required for this tool to operate and will throw a
     * NullPointerException if this is not set or is set to {@code null}.
     */
    public void setRequest(HttpServletRequest request)
    {
        if (request == null)
        {
            throw new NullPointerException("request should not be null");
        }
        userAgent = request.getHeader("User-Agent").toLowerCase();
    }

    /* Generic getter for unknown tests
     */
    public boolean get(String key)
    {
        return test(key);
    }

    /* Versioning */

    public String getVersion()
    {
        parseVersion();
        return version;
    }

    public int getMajorVersion()
    {
        parseVersion();
        return majorVersion;
    }

    public int getMinorVersion()
    {
        parseVersion();
        return minorVersion;
    }
<commit_msg>protect BrowserTool from requests without user agent<commit_after>package org.apache.velocity.tools.view;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.servlet.http.HttpServletRequest;
import org.apache.velocity.tools.config.DefaultKey;
import org.apache.velocity.tools.view.ViewContext;
import org.apache.velocity.tools.config.InvalidScope;

/**
 *  <p>browser-sniffing tool (session or request scope requested, session scope advised).</p>
 *  <p></p>
 * <p><b>Usage:</b></p>
 * <p>BrowserTool defines properties that are used to test the client browser, operating system, device...
 * Apart from properties related to versioning, all properties are booleans.</p>
 * <p>The following properties are available:</p>
 * <ul>
 * <li><i>Versioning:</i>version majorVersion minorVersion geckoVersion</li>
 * <li><i>Browser:</i>mosaic netscape nav2 nav3 nav4 nav4up nav45 nav45up nav6 nav6up navgold firefox safari
 * ie ie3 ie4 ie4up ie5 ie5up ie55 ie55up ie6 opera opera3 opera4 opera5 opera6 opera7 lynx links
 * aol aol3 aol4 aol5 aol6 neoplanet neoplanet2 amaya icab avantgo emacs mozilla gecko webtv staroffice
 * lotusnotes konqueror</li>
 * <li><i>Operating systems:</i>win16 win3x win31 win95 win98 winnt windows win32 win2k winxp winme dotnet
 * mac macosx mac68k macppc os2 unix sun sun4 sun5 suni86 irix irix5 irix6 hpux hpux9 hpux10 aix aix1 aix2 aix3 aix4
 * linux sco unixware mpras reliant dec sinix freebsd bsd vms x11 amiga</li>
 * <li><i>Devices:</i>palm audrey iopener wap blackberry</li>
 * <li><i>Features:</i>javascript css css1 css2 dom0 dom1 dom2</li>
 * <li><i>Special:</i>robot (true if the page is requested by a robot, i.e. when one of the following properties is true:
 * wget getright yahoo altavista lycos infoseek lwp webcrawler linkexchange slurp google java)
 * </ul>
 *
 * Thanks to <NAME> (<EMAIL>), the author of the HTTP::BrowserDetect Perl module.
 * See also:
 * * http://www.zytrax.com/tech/web/browser_ids.htm
 * * http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
 * * http://en.wikipedia.org/wiki/User_agent
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @since VelocityTools 1.2
 * @version $Revision$ $Date$
 */
@DefaultKey("browser")
@InvalidScope("application")
public class BrowserTool implements java.io.Serializable
{
    private static final long serialVersionUID = 1734529350532353339L;

    private String userAgent = null;
    private String version = null;
    private int majorVersion = -1;
    private int minorVersion = -1;
    private String geckoVersion = null;
    private int geckoMajorVersion = -1;
    private int geckoMinorVersion = -1;

    /**
     * Initializes this tool by setting the current {@link HttpServletRequest}.
     * This is required for this tool to operate and will throw a
     * NullPointerException if this is not set or is set to {@code null}.
     */
    public void setRequest(HttpServletRequest request)
    {
        if (request == null)
        {
            throw new NullPointerException("request should not be null");
        }
        userAgent = request.getHeader("User-Agent");
        if(userAgent == null) {
            userAgent = "";
        } else {
            userAgent = userAgent.toLowerCase();
        }
    }

    /* Generic getter for unknown tests
     */
    public boolean get(String key)
    {
        return test(key);
    }

    /* Versioning */

    public String getVersion()
    {
        parseVersion();
        return version;
    }

    public int getMajorVersion()
    {
        parseVersion();
        return majorVersion;
    }

    public int getMinorVersion()
    {
        parseVersion();
        return minorVersion;
    }

{'fraction_non_alphanumeric': 0.09509989954235964, 'fraction_numerical': 0.020984484875544145, 'mean_word_length': 3.5275391611925215, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '5450236', 'n_tokens_mistral': 2756, 'n_tokens_neox': 2635, 'n_words': 924}