public DDMExpressionImpl(String expressionString, Class<T> expressionClass)
      throws DDMExpressionException {

    TokenExtractor tokenExtractor = new TokenExtractor(expressionString);

    Map<String, String> variableMap = tokenExtractor.getVariableMap();

    for (Map.Entry<String, String> entry : variableMap.entrySet()) {
      String variableName = entry.getKey();

      Variable variable = new Variable(variableName);

      _variables.put(variableName, variable);

      String token = entry.getValue();

      if (token != null) {
        setStringVariableValue(variableName, token);
      }
    }

    _expressionString = tokenExtractor.getExpression();

    _expressionClass = expressionClass;
  }
 /**
  * Returns type converted to index type if needed. A index type variable in java splitter file has
  * type "int" or "int[]" instead of "long" or "long[]". This is needed if the variable or the an
  * element of the variable is used as an index to an array. This method converts the type of the
  * variable to "int_index" or "index[]" if it is used as an index to an array or an element of it
  * is used as an index to an array.
  *
  * @param type the original type of the variable.
  * @param name the name of the variable.
  * @param varInfo the VarInfo of the variable.
  * @param condition the condition in which the variable occurs.
  * @return the type converted to index type if needed.
  */
 private static String makeIndexIfNeeded(
     String type, String name, VarInfo varInfo, String condition) throws ParseException {
   if ((type.equals("int") || varInfo.type.isArray())
       && varInfo.file_rep_type != ProglangType.HASHCODE) {
     int LPAREN = 74;
     int RPAREN = 75;
     int LBRACKET = 78;
     int RBRACKET = 79;
     Stack<Boolean> inArrayIndex = new Stack<Boolean>();
     inArrayIndex.push(Boolean.FALSE);
     NodeToken[] tokens = TokenExtractor.extractTokens(condition);
     for (int i = 0; i < tokens.length; i++) {
       if (tokens[i].kind == LBRACKET) {
         inArrayIndex.push(Boolean.TRUE);
       } else if (tokens[i].kind == RBRACKET) {
         inArrayIndex.pop();
       } else if (tokens[i].kind == LPAREN) {
         inArrayIndex.push(Boolean.FALSE);
       } else if (tokens[i].kind == RPAREN) {
         inArrayIndex.pop();
       } else if (inArrayIndex.peek().booleanValue() && tokens[i].tokenImage.equals(name)) {
         if (type.equals("int") || type.equals("int_index")) {
           // Note the type can only equal "int_index" if the variable
           // was already visited by this if statement since it appears
           // more than once in the condition.
           type = "int_index";
         } else {
           type = "index[]";
         }
       }
     }
     return type;
   }
   return type;
 }
  protected VariableDependencies populateVariableDependenciesMap(
      Variable variable, Map<String, VariableDependencies> variableDependenciesMap)
      throws DDMExpressionException {

    VariableDependencies variableDependencies = variableDependenciesMap.get(variable.getName());

    if (variableDependencies != null) {
      return variableDependencies;
    }

    variableDependencies = new VariableDependencies(variable.getName());

    if (variable.getExpressionString() != null) {
      TokenExtractor tokensExtractor = new TokenExtractor(variable.getExpressionString());

      Map<String, String> variableMap = tokensExtractor.getVariableMap();

      Set<String> variableNames = variableMap.keySet();

      for (String variableName : variableNames) {
        if (!_variables.containsKey(variableName)) {
          Variable newVariable = new Variable(variableName);

          _variables.put(variableName, newVariable);

          String token = variableMap.get(variableName);

          if (token != null) {
            setStringVariableValue(variableName, token);
          }
        }

        VariableDependencies variableVariableDependencies =
            populateVariableDependenciesMap(_variables.get(variableName), variableDependenciesMap);

        variableVariableDependencies.addAffectedVariable(variableDependencies.getVariableName());
        variableDependencies.addRequiredVariable(variableVariableDependencies.getVariableName());
      }
    }

    variableDependenciesMap.put(variable.getName(), variableDependencies);

    return variableDependencies;
  }
  protected com.udojava.evalex.Expression getExpression(String expressionString)
      throws DDMExpressionException {

    com.udojava.evalex.Expression expression = new com.udojava.evalex.Expression(expressionString);

    TokenExtractor tokenExtractor = new TokenExtractor(expressionString);

    Map<String, String> variableMap = tokenExtractor.getVariableMap();

    for (String key : variableMap.keySet()) {
      Variable variable = _variables.get(key);

      if (variable != null) {
        BigDecimal variableValue = getVariableValue(variable);

        expression.setVariable(key, variableValue);
      }
    }

    return expression;
  }
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws IOException, ServletException {

    final boolean debug = logger.isDebugEnabled();
    final HttpServletRequest request = (HttpServletRequest) req;
    final HttpServletResponse response = (HttpServletResponse) res;

    try {

      Authentication authentication = tokenExtractor.extract(request);

      if (authentication == null) {
        if (debug) {
          logger.debug("No token in request, will continue chain.");
        }
      } else {
        request.setAttribute(
            OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, authentication.getPrincipal());
        if (authentication instanceof AbstractAuthenticationToken) {
          AbstractAuthenticationToken needsDetails = (AbstractAuthenticationToken) authentication;
          needsDetails.setDetails(authenticationDetailsSource.buildDetails(request));
        }
        Authentication authResult = authenticationManager.authenticate(authentication);

        if (debug) {
          logger.debug("Authentication success: " + authResult);
        }

        SecurityContextHolder.getContext().setAuthentication(authResult);
      }
    } catch (OAuth2Exception failed) {
      SecurityContextHolder.clearContext();

      if (debug) {
        logger.debug("Authentication request failed: " + failed);
      }

      authenticationEntryPoint.commence(
          request, response, new InsufficientAuthenticationException(failed.getMessage(), failed));

      return;
    }

    chain.doFilter(request, response);
  }
 /**
  * requires: condition is a string representation of a conditional
  *
  * @return a list of all possible variable variable names in condition. arrays appear with "[]" at
  *     the end if their elements or accessed in the condition.
  */
 private static List<String> findPossibleClassVariables(String condition) throws ParseException {
   NodeToken[] tokens = TokenExtractor.extractTokens(condition);
   // System.out.println("TokenExtractor.extractTokens(" + condition + ") ==> " +
   // ArraysMDE.toString(tokens));
   List<String> variables = new ArrayList<String>();
   if (tokens.length >= 1) {
     if (tokens[0].kind == IDENTIFIER && (tokens.length <= 1 || tokens[1].kind != LPAREN)) {
       variables.add(tokens[0].tokenImage);
     }
   }
   if (tokens.length >= 2) {
     if (tokens[1].kind == IDENTIFIER
         && (tokens.length <= 2 || tokens[2].kind != LPAREN)
         && (!variables.contains(tokens[1].tokenImage))) {
       variables.add(tokens[1].tokenImage);
     }
   }
   for (int i = 2; i < tokens.length - 1; i++) {
     NodeToken token = tokens[i];
     if (token.kind == IDENTIFIER
         && tokens[i - 1].kind != DOT
         && tokens[i + 1].kind != LPAREN
         && (!variables.contains(token.tokenImage))) {
       variables.add(token.tokenImage);
     }
   }
   if (tokens.length >= 3) {
     int lastIndex = tokens.length - 1;
     if (tokens[lastIndex].kind == IDENTIFIER
         && tokens[lastIndex - 1].kind != DOT
         && (!variables.contains(tokens[lastIndex].tokenImage))) {
       variables.add(tokens[lastIndex].tokenImage);
     }
   }
   // System.out.println("findPossibleClassVariables(" + condition + ") ==> " +
   // variables.toString());
   return variables;
 }