<commit_before>/* **********************************************************************
    Copyright 2007 Rensselaer Polytechnic Institute. All worldwide rights reserved.

    Redistribution and use of this distribution in source and binary forms,
    with or without modification, are permitted provided that:
       The above copyright notice and this permission notice appear in all
        copies and supporting documentation;

        The name, identifiers, and trademarks of Rensselaer Polytechnic
        Institute are not used in advertising or publicity without the
        express prior written permission of Rensselaer Polytechnic Institute;

    DISCLAIMER: The software is distributed" AS IS" without any express or
    implied warranty, including but not limited to, any implied warranties
    of merchantability or fitness for a particular purpose or any warrant)'
    of non-infringement of any current or pending patent rights. The authors
    of the software make no representations about the suitability of this
    software for any particular purpose. The entire risk as to the quality
    and performance of the software is with the user. Should the software
    prove defective, the user assumes the cost of all necessary servicing,
    repair or correction. In particular, neither Rensselaer Polytechnic
    Institute, nor the authors of the software are liable for any indirect,
    special, consequential, or incidental damages related to the software,
    to the maximum extent the law permits.
*/
package edu.rpi.sss.util.jsp;

import edu.rpi.sss.util.Util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/** Class to handle the incoming request.
 *
 * @author <NAME>
 */
public class Request implements Serializable {
  protected HttpServletRequest request;
  protected HttpServletResponse response;
  protected UtilActionForm form;

  protected boolean errFlag;

  /**
   * @param request
   * @param response
   * @param form
   */
  public Request(HttpServletRequest request,
                 HttpServletResponse response,
                 UtilActionForm form) {
    this.request = request;
    this.response = response;
    this.form = form;
  }

  /**
   * @return HttpServletRequest
   */
  public HttpServletRequest getRequest() {
    return request;
  }

  /**
   * @return HttpServletResponse
   */
  public HttpServletResponse getResponse() {
    return response;
  }

  /**
   * @return UtilActionForm
   */
  public UtilActionForm getForm() {
    return form;
  }

  /**
   * @param val
   */
  public void setErrFlag(boolean val) {
    errFlag = val;
  }

  /**
   * @return boolean
   */
  public boolean getErrFlag() {
    return errFlag;
  }

  /** Get a request parameter stripped of white space. Return null for zero
   * length.
   *
   * @param name    name of parameter
   * @return  String   value
   * @throws Throwable
   */
  public String getReqPar(String name) throws Throwable {
    return Util.checkNull(request.getParameter(name));
  }

  /** See if a request parameter is present
   *
   * @param name    name of parameter
   * @return  boolean true for present
   * @throws Throwable
   */
  public boolean present(String name) throws Throwable {
    return request.getParameter(name) != null;
  }

  /** See if a request parameter is present and not null or empty
   *
   * @param name    name of parameter
   * @return  boolean true for present and not null
   * @throws Throwable
   */
  public boolean notNull(String name) throws Throwable {
    return getReqPar(name) != null;
  }

  /** Get a multi-valued request parameter stripped of white space.
   * Return null for zero length.
   *
   * @param name    name of parameter
   * @return  Collection<String> or null
   * @throws Throwable
   */
  public Collection<String> getReqPars(String name) throws Throwable {
    String[] s = request.getParameterValues(name);
    ArrayList<String> res = null;

    if ((s == null) || (s.length == 0)) {
      return null;
    }

    for (String par: s) {
      par = Util.checkNull(par);
      if (par != null) {
        if (res == null) {
          res = new ArrayList<String>();
        }

        res.add(par);
      }
    }

    return res;
  }

  /** Get an Integer request parameter or null.
   *
   * @param name    name of parameter
   * @return  Integer   value or null
   * @throws Throwable
   */
  public Integer getIntReqPar(String name) throws Throwable {
    String reqpar = getReqPar(name);

    if (reqpar == null) {
      return null;
    }

    return Integer.valueOf(reqpar);
  }

  /** Get an Integer request parameter or null. Emit error for non-null and
   * non integer
   *
   * @param name    name of parameter
   * @param errProp
   * @return  Integer   value or null
   * @throws Throwable
   */
  public Integer getIntReqPar(String name,
                              String errProp) throws Throwable {
    String reqpar = getReqPar(name);

    if (reqpar == null) {
      return null;
    }

    try {
      return Integer.valueOf(reqpar);
    } catch (Throwable t) {
      form.getErr().emit(errProp, reqpar);
      errFlag = true;
      return null;
    }
  }

  /** Get an integer valued request parameter.
   *
   * @param name    name of parameter
   * @param defaultVal
   * @return  int   value
   * @throws Throwable
   */
  public int getIntReqPar(String name,
                          int defaultVal) throws Throwable {
    String reqpar = getReqPar(name);

    if (reqpar == null) {
      return defaultVal;
    }

    try {
      return Integer.parseInt(reqpar);
    } catch (Throwable t) {
      return defaultVal; // XXX exception?
    }
  }

  /** Get a Long request parameter or null.<commit_msg>Make move calendar work in the web ui<commit_after>/* **********************************************************************
    Copyright 2007 Rensselaer Polytechnic Institute. All worldwide rights reserved.

    Redistribution and use of this distribution in source and binary forms,
    with or without modification, are permitted provided that:
       The above copyright notice and this permission notice appear in all
        copies and supporting documentation;

        The name, identifiers, and trademarks of Rensselaer Polytechnic
        Institute are not used in advertising or publicity without the
        express prior written permission of Rensselaer Polytechnic Institute;

    DISCLAIMER: The software is distributed" AS IS" without any express or
    implied warranty, including but not limited to, any implied warranties
    of merchantability or fitness for a particular purpose or any warrant)'
    of non-infringement of any current or pending patent rights. The authors
    of the software make no representations about the suitability of this
    software for any particular purpose. The entire risk as to the quality
    and performance of the software is with the user. Should the software
    prove defective, the user assumes the cost of all necessary servicing,
    repair or correction. In particular, neither Rensselaer Polytechnic
    Institute, nor the authors of the software are liable for any indirect,
    special, consequential, or incidental damages related to the software,
    to the maximum extent the law permits.
*/
package edu.rpi.sss.util.jsp;

import edu.rpi.sss.util.Util;
import edu.rpi.sss.util.log.MessageEmit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/** Class to handle the incoming request.
 *
 * @author <NAME>
 */
public class Request implements Serializable {
  protected HttpServletRequest request;
  protected HttpServletResponse response;
  protected UtilActionForm form;

  protected boolean errFlag;

  /**
   * @param request
   * @param response
   * @param form
   */
  public Request(HttpServletRequest request,
                 HttpServletResponse response,
                 UtilActionForm form) {
    this.request = request;
    this.response = response;
    this.form = form;
  }

  /**
   * @return HttpServletRequest
   */
  public HttpServletRequest getRequest() {
    return request;
  }

  /**
   * @return HttpServletResponse
   */
  public HttpServletResponse getResponse() {
    return response;
  }

  /**
   * @return UtilActionForm
   */
  public UtilActionForm getForm() {
    return form;
  }

  /**
   * @return MessageEmit
   */
  public MessageEmit getErr() {
    errFlag = true;
    return form.getErr();
  }

  /**
   * @return boolean
   */
  public boolean getErrorsEmitted() {
    return errFlag || form.getErrorsEmitted();
  }

  /**
   * @param val
   */
  public void setErrFlag(boolean val) {
    errFlag = val;
  }

  /**
   * @return boolean
   */
  public boolean getErrFlag() {
    return errFlag;
  }

  /** Get a request parameter stripped of white space. Return null for zero
   * length.
   *
   * @param name    name of parameter
   * @return  String   value
   * @throws Throwable
   */
  public String getReqPar(String name) throws Throwable {
    return Util.checkNull(request.getParameter(name));
  }

  /** See if a request parameter is present
   *
   * @param name    name of parameter
   * @return  boolean true for present
   * @throws Throwable
   */
  public boolean present(String name) throws Throwable {
    return request.getParameter(name) != null;
  }

  /** See if a request parameter is present and not null or empty
   *
   * @param name    name of parameter
   * @return  boolean true for present and not null
   * @throws Throwable
   */
  public boolean notNull(String name) throws Throwable {
    return getReqPar(name) != null;
  }

  /** Get a multi-valued request parameter stripped of white space.
   * Return null for zero length.
   *
   * @param name    name of parameter
   * @return  Collection<String> or null
   * @throws Throwable
   */
  public Collection<String> getReqPars(String name) throws Throwable {
    String[] s = request.getParameterValues(name);
    ArrayList<String> res = null;

    if ((s == null) || (s.length == 0)) {
      return null;
    }

    for (String par: s) {
      par = Util.checkNull(par);
      if (par != null) {
        if (res == null) {
          res = new ArrayList<String>();
        }

        res.add(par);
      }
    }

    return res;
  }

  /** Get an Integer request parameter or null.
   *
   * @param name    name of parameter
   * @return  Integer   value or null
   * @throws Throwable
   */
  public Integer getIntReqPar(String name) throws Throwable {
    String reqpar = getReqPar(name);

    if (reqpar == null) {
      return null;
    }

    return Integer.valueOf(reqpar);
  }

  /** Get an Integer request parameter or null. Emit error for non-null and
   * non integer
   *
   * @param name    name of parameter
   * @param errProp
   * @return  Integer   value or null
   * @throws Throwable
   */
  public Integer getIntReqPar(String name,
                              String errProp) throws Throwable {
    String reqpar = getReqPar(name);

    if (reqpar == null) {
      return null;
    }

    try {
      return Integer.valueOf(reqpar);
    } catch (Throwable t) {
      getErr().emit(errProp, reqpar);
      return null;
    }
  }

  /** Get an integer valued request parameter.
   *
   * @param name    name of parameter
   * @param defaultVal
   * @return  int   value
   * @throws Throwable
   */
  public int getIntReqPar(String name,
                          int defaultVal) throws Throwable {
    String reqpar = getReqPar(name);

    if (reqpar == null) {
      return defaultVal;
    }

    try {
      return Integer.parseInt(reqpar);
    } catch (Throwable t) {
      return defaultVal; // XXX exception?
    }
  }

  /** Get a Long request parameter or null.
{'fraction_non_alphanumeric': 0.09098462816784379, 'fraction_numerical': 0.0008309098462816784, 'mean_word_length': 2.710234278668311, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '2962053', 'n_tokens_mistral': 3326, 'n_tokens_neox': 3250, 'n_words': 1203}