<commit_before>package gov.nih.nci.evs.browser.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.regex.Pattern;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 * HTTP Utility methods
 *
 * @author garciawa2
 *
 */
public class HTTPUtils {

  /**
   * Remove potentially bad XSS syntax
   *
   * @param value
   * @return
   */
  public static String cleanXSS(String value) {

    if (value == null || value.length() < 1)
      return value;

    try {
      value = URLDecoder.decode(value, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      // Do nothing, just use the input
    } catch (IllegalArgumentException e) {
      // Do nothing, just use the input
      // Note: The following exception was triggered:
      // java.lang.IllegalArgumentException: URLDecoder: Illegal hex
      // characters in escape (%) pattern - For input string: "^&".
    }

    // Remove XSS attacks
    value = replaceAll(value, "<\\s*script\\s*>.*</\\s*script\\s*>", "");
    value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    value = value.replaceAll("\\(", "&#40;").replaceAll("\\)", "&#41;");
    value = value.replaceAll("'", "&#39;");
    value = value.replaceAll("eval\\((.*)\\)", "");
    value = replaceAll(value, "[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
    value = value.replaceAll("\"", "&quot;");
    return value;

  }

  /**
   * @param string
   * @param regex
   * @param replaceWith
   * @return
   */
  public static String replaceAll(String string, String regex, String replaceWith) {

    Pattern myPattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    string = myPattern.matcher(string).replaceAll(replaceWith);
    return string;

  }

  @SuppressWarnings("unchecked")
  public static Object getBean(String name, String classPath) {
      try {
          Map<Object, Object> map =
              FacesContext.getCurrentInstance().getExternalContext()
                  .getSessionMap();

          Object bean = map.get(name);
          if (bean == null) {
              Class klass = Class.forName(classPath);
              bean = klass.newInstance();
              map.put(name, bean);
          }
          return bean;
      } catch (Exception e) {
          return null;
      }
  }
  public static HttpServletRequest getRequest() {
    return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
  }

  public static String getAttribute(HttpServletRequest request, String attributeName, String defaultValue) {
    String value = (String) request.getAttribute(attributeName);
    return getValue(value, defaultValue);
  }

  public static String getSessionAttribute(HttpServletRequest request, String attributeName, String defaultValue) {
    String value = (String) request.getSession().getAttribute(attributeName);
    return getValue(value, defaultValue);
  }

  public static String getValue(String value, String defaultValue) {
    if (value == null || value.trim().length() <= 0 || value.equals("null"))
      return defaultValue;
    return value;
  }

  /**
   * @param request
   * @return
   */
  public static String getRefererParmEncode(HttpServletRequest request) {
    String iref = request.getHeader("Referer");
    String referer = "N/A";
    if (iref != null)
      try {
        referer = URLEncoder.encode(iref,"UTF-8");
      } catch (UnsupportedEncodingException e) {
        // return N/A if encoding is not supported.
      }
    return cleanXSS(referer);
  }

  /**
   * @param request
   * @return
   */
  public static String getRefererParmDecode(HttpServletRequest request) {
    String refurl = "N/A";
    try {
      String iref = request.getParameter("referer");
      if (iref != null)
        refurl = URLDecoder.decode(request.getParameter("referer"),
            "UTF-8");
    } catch (UnsupportedEncodingException e) {
      // return N/A if encoding is not supported.
    }
    return cleanXSS(refurl);
  }

	public static String convertJSPString(String t) {
	// Convert problem characters to JavaScript Escaped values
	  if (t == null) {
		return "";
	  }

	  if (t.compareTo("") == 0) {
		return "";
	  }

	  String sigleQuoteChar = "'";
	  String doubleQuoteChar = "\"";

	  String dq = "&quot;";

	  t = t.replaceAll(sigleQuoteChar,"\\" + sigleQuoteChar);
	  t = t.replaceAll(doubleQuoteChar,"\\" + dq);
	  t = t.replaceAll("\r","\\r");  // replace CR with \r;
	  t = t.replaceAll("\n","\\n");  // replace LF with \n;

	  return t;
	}<commit_msg>New clearRefererParm method to clear Referer parm.<commit_after>package gov.nih.nci.evs.browser.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.regex.Pattern;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 * HTTP Utility methods
 *
 * @author garciawa2
 *
 */
public class HTTPUtils {

  /**
   * Remove potentially bad XSS syntax
   *
   * @param value
   * @return
   */
  public static String cleanXSS(String value) {

    if (value == null || value.length() < 1)
      return value;

    try {
      value = URLDecoder.decode(value, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      // Do nothing, just use the input
    } catch (IllegalArgumentException e) {
      // Do nothing, just use the input
      // Note: The following exception was triggered:
      // java.lang.IllegalArgumentException: URLDecoder: Illegal hex
      // characters in escape (%) pattern - For input string: "^&".
    }

    // Remove XSS attacks
    value = replaceAll(value, "<\\s*script\\s*>.*</\\s*script\\s*>", "");
    value = value.replaceAll("<", "&lt;").replaceAll(">", "&gt;");
    value = value.replaceAll("\\(", "&#40;").replaceAll("\\)", "&#41;");
    value = value.replaceAll("'", "&#39;");
    value = value.replaceAll("eval\\((.*)\\)", "");
    value = replaceAll(value, "[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
    value = value.replaceAll("\"", "&quot;");
    return value;

  }

  /**
   * @param string
   * @param regex
   * @param replaceWith
   * @return
   */
  public static String replaceAll(String string, String regex, String replaceWith) {

    Pattern myPattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
    string = myPattern.matcher(string).replaceAll(replaceWith);
    return string;

  }

  /**
   * @param name
   * @param classPath
   * @return
   */
  @SuppressWarnings("unchecked")
  public static Object getBean(String name, String classPath) {
      try {
          Map<Object, Object> map =
              FacesContext.getCurrentInstance().getExternalContext()
                  .getSessionMap();

          Object bean = map.get(name);
          if (bean == null) {
              Class klass = Class.forName(classPath);
              bean = klass.newInstance();
              map.put(name, bean);
          }
          return bean;
      } catch (Exception e) {
          return null;
      }
  }
  /**
   * @return
   */
  public static HttpServletRequest getRequest() {
    return (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
  }

  /**
   * @param request
   * @param attributeName
   * @param defaultValue
   * @return
   */
  public static String getAttribute(HttpServletRequest request, String attributeName, String defaultValue) {
    String value = (String) request.getAttribute(attributeName);
    return getValue(value, defaultValue);
  }

  /**
   * @param request
   * @param attributeName
   * @param defaultValue
   * @return
   */
  public static String getSessionAttribute(HttpServletRequest request, String attributeName, String defaultValue) {
    String value = (String) request.getSession().getAttribute(attributeName);
    return getValue(value, defaultValue);
  }

  /**
   * @param value
   * @param defaultValue
   * @return
   */
  public static String getValue(String value, String defaultValue) {
    if (value == null || value.trim().length() <= 0 || value.equals("null"))
      return defaultValue;
    return value;
  }

  /**
   * @param request
   * @return
   */
  public static String getRefererParmEncode(HttpServletRequest request) {
    String iref = request.getHeader("Referer");
    String referer = "N/A";
    if (iref != null)
      try {
        referer = URLEncoder.encode(iref,"UTF-8");
      } catch (UnsupportedEncodingException e) {
        // return N/A if encoding is not supported.
      }
    return cleanXSS(referer);
  }

  /**
   * @param request
   * @return
   */
  public static String getRefererParmDecode(HttpServletRequest request) {
    String refurl = "N/A";
    try {
      String iref = request.getParameter("referer");
      if (iref != null)
        refurl = URLDecoder.decode(request.getParameter("referer"),
            "UTF-8");
    } catch (UnsupportedEncodingException e) {
      // return N/A if encoding is not supported.
    }
    return cleanXSS(refurl);
  }

  /**
   * @param request
   */
  public static void clearRefererParm(HttpServletRequest request) {
    request.setAttribute("referer", null);
  }    
  
	/**
	 * @param t
	 * @return
	 */
	public static String convertJSPString(String t) {
	// Convert problem characters to JavaScript Escaped values
	  if (t == null) {
		return "";
	  }

	  if (t.compareTo("") == 0) {
		return "";
	  }

	  String sigleQuoteChar = "'";
	  String doubleQuoteChar = "\"";

	  String dq = "&quot;";

	  t = t.replaceAll(sigleQuoteChar,"\\" + sigleQuoteChar);
	  t = t.replaceAll(doubleQuoteChar,"\\" + dq);
	  t = t.replaceAll("\r","\\r");  // replace CR with \r;
	  t = t.replaceAll("\n","\\n");  // replace LF with \n;

	  return t;
	}
{'fraction_non_alphanumeric': 0.14654734058781654, 'fraction_numerical': 0.002644157429065392, 'mean_word_length': 2.796911196911197, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1920777', 'n_tokens_mistral': 3026, 'n_tokens_neox': 2971, 'n_words': 715}