Пример #1
0
  private void rotateTokens(HttpServletRequest request) {
    HttpSession session = request.getSession(true);

    /** rotate master token * */
    String tokenFromSession = null;

    try {
      tokenFromSession = RandomGenerator.generateRandomId(getPrng(), getTokenLength());
    } catch (Exception e) {
      throw new RuntimeException(
          String.format("unable to generate the random token - %s", e.getLocalizedMessage()), e);
    }

    session.setAttribute(getSessionKey(), tokenFromSession);

    /** rotate page token * */
    if (isTokenPerPageEnabled()) {
      @SuppressWarnings("unchecked")
      Map<String, String> pageTokens =
          (Map<String, String>) session.getAttribute(CsrfGuard.PAGE_TOKENS_KEY);

      try {
        pageTokens.put(
            request.getRequestURI(), RandomGenerator.generateRandomId(getPrng(), getTokenLength()));
      } catch (Exception e) {
        throw new RuntimeException(
            String.format("unable to generate the random token - %s", e.getLocalizedMessage()), e);
      }
    }
  }
 /**
  * Operate on the given chromosome with the given mutation rate.
  *
  * @param a_chrom chromosome to operate
  * @param a_rate mutation rate
  * @param a_generator random generator to use (must not be null)
  * @return mutated chromosome of null if no mutation has occured.
  * @author Audrius Meskauskas
  * @author Florian Hafner
  * @since 3.3.2
  */
 protected IChromosome operate(
     final IChromosome a_chrom, final int a_rate, final RandomGenerator a_generator) {
   IChromosome chromosome = null;
   // ----------------------------------------
   for (int j = m_startOffset; j < a_chrom.size(); j++) {
     // Ensure probability of 1/currentRate for applying mutation.
     // ----------------------------------------------------------
     if (a_generator.nextInt(a_rate) == 0) {
       if (chromosome == null) {
         chromosome = (IChromosome) a_chrom.clone();
         // In case monitoring is active, support it.
         // -----------------------------------------
         if (m_monitorActive) {
           chromosome.setUniqueIDTemplate(a_chrom.getUniqueID(), 1);
         }
       }
       Gene[] genes = chromosome.getGenes();
       if (m_range == 0) {
         m_range = genes.length;
       }
       Gene[] mutated = operate(a_generator, j, genes);
       // setGenes is not required for this operator, but it may
       // be needed for the derived operators.
       // ------------------------------------------------------
       try {
         chromosome.setGenes(mutated);
       } catch (InvalidConfigurationException cex) {
         throw new Error("Gene type not allowed by constraint checker", cex);
       }
     }
   }
   return chromosome;
 }
Пример #3
0
  /**
   * Create page token if it doesn't exist.
   *
   * @param pageTokens A map of tokens. If token doesn't exist it will be added.
   * @param uri The key for the tokens.
   */
  private void createPageToken(Map<String, String> pageTokens, String uri) {

    if (pageTokens == null) return;

    /** create token if it does not exist * */
    if (pageTokens.containsKey(uri)) return;
    try {
      pageTokens.put(uri, RandomGenerator.generateRandomId(getPrng(), getTokenLength()));
    } catch (Exception e) {
      throw new RuntimeException(
          String.format("unable to generate the random token - %s", e.getLocalizedMessage()), e);
    }
  }
Пример #4
0
  public void updateToken(HttpSession session) {
    String tokenValue = (String) session.getAttribute(getSessionKey());

    /** Generate a new token and store it in the session. * */
    if (tokenValue == null) {
      try {
        tokenValue = RandomGenerator.generateRandomId(getPrng(), getTokenLength());
      } catch (Exception e) {
        throw new RuntimeException(
            String.format("unable to generate the random token - %s", e.getLocalizedMessage()), e);
      }

      session.setAttribute(getSessionKey(), tokenValue);
    }
  }
  /**
   * Operate on the given array of genes. This method is only called when it is already clear that
   * the mutation must occur under the given mutation rate.
   *
   * @param a_generator a random number generator that may be needed to perform a mutation
   * @param a_target_gene an index of gene in the chromosome that will mutate
   * @param a_genes the array of all genes in the chromosome
   * @return the mutated gene array
   * @author Florian Hafner
   * @since 3.3.2
   */
  protected Gene[] operate(
      final RandomGenerator a_generator, final int a_target_gene, final Gene[] a_genes) {
    // Other needs to be an integer that is within the range of the subject
    // target gene.

    int rand = a_generator.nextInt(2 * m_range);
    int other = (a_target_gene - m_range) + rand;
    if (other < 0) {
      other = 0;
    }
    if (other >= a_genes.length) {
      other = a_genes.length - 1; // Index is -1 of length
    }
    Gene t = a_genes[a_target_gene];
    a_genes[a_target_gene] = a_genes[other];
    a_genes[other] = t;
    if (m_monitorActive) {
      a_genes[a_target_gene].setUniqueIDTemplate(a_genes[other].getUniqueID(), 1);
      a_genes[other].setUniqueIDTemplate(a_genes[a_target_gene].getUniqueID(), 1);
    }
    return a_genes;
  }