Package de.odysseus.calyxo.control.conf.impl

Source Code of de.odysseus.calyxo.control.conf.impl.FilterConfigImpl

/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed 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.
*/
package de.odysseus.calyxo.control.conf.impl;

import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.el.ELException;
import javax.servlet.jsp.el.Expression;
import javax.servlet.jsp.el.VariableResolver;

import de.odysseus.calyxo.base.ModuleContext;
import de.odysseus.calyxo.base.conf.ConfigException;
import de.odysseus.calyxo.base.misc.CalyxoVariableResolver;
import de.odysseus.calyxo.control.conf.ActionConfig;
import de.odysseus.calyxo.control.conf.DispatchConfig;
import de.odysseus.calyxo.control.conf.FilterConfig;

/**
*
* @author Christoph Beck
*/
public class FilterConfigImpl extends ParamsConfigImpl implements FilterConfig {
  private String name;
  private String className;
  private String when;
  private HashMap dispatches = new HashMap(4);
 
  private Expression expression;

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getElementName()
   */
  protected String _getElementName() {
    return "filter";
  }
 
  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_addChildren(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Elements)
   */
  protected void _addChildren(Elements list) {
    super._addChildren(list);
    list.add(getDispatchConfigs());
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_putAttributes(de.odysseus.calyxo.base.conf.impl.ConfigImpl.Attributes)
   */
  protected void _putAttributes(Attributes map) {
    super._putAttributes(map);
    map.put("name", getName());
    map.put("class", getClassName());
    map.put("when", getWhen());
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_getDynamicAttributes()
   */
  protected String[] _getDynamicAttributes() {
    return new String[]{ "className", "when" };
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_evaluate(de.odysseus.calyxo.base.ModuleContext)
   */
  protected void _evaluate(ModuleContext context) throws ConfigException {
    super._evaluate(context);

    if (when != null) {
      try {
        expression = context.getExpressionEvaluator().parseExpression("${" + when + "}", Boolean.class, this);
      } catch (ELException e) {
        throw new ConfigException("Failed to parse 'when' condition in filter '" + toInlineString() + "'");
      }
    }
  }

  /* (non-Javadoc)
   * @see de.odysseus.calyxo.base.conf.impl.ConfigImpl#_init()
   */
  protected void _init() throws ConfigException {
    super._init();

    // check that name xor class is specified
    if ((name == null) == (className == null)) {
      throw new ConfigException("Exactly one of name and class must be specified in filter '" + toInlineString() + "'");
    }
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.control.conf.FilterConfig#when(javax.servlet.http.HttpServletRequest)
   */
  public boolean when(HttpServletRequest request) throws Exception {
    if (expression == null) {
      return true;
    } else {
      VariableResolver resolver = new CalyxoVariableResolver(request);
      try {
        Boolean result = (Boolean)expression.evaluate(resolver);
        return result == null ? false : result.booleanValue();
      } catch (ELException e) {
        throw new ConfigException("Could not evaluate 'when' condition '" + when + "' in filter '" + toInlineString() + "'", e);
      }
    }
  }
 
  /* (non-Javadoc)
   * @see de.odysseus.calyxo.control.conf.FilterConfig#getActionConfig()
   */
  public ActionConfig getActionConfig() {
    return (ActionConfig)_getParent();
  }

  /**
   * Add dispatch element.
   */
  public void add(DispatchConfigImpl value) throws ConfigException {
    if (dispatches.containsKey(value.getName())) {
      throw new ConfigException("Duplicate dispatch name '" + value.getName() + "' in " + toInlineString());
    }
    dispatches.put(value.getName(), value);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.control.conf.DispatchesConfig#getDispatchConfig(java.lang.String)
   */
  public DispatchConfig getDispatchConfig(String name) {
    return (DispatchConfig)dispatches.get(name);
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.control.conf.DispatchesConfig#getDispatchConfigs()
   */
  public Iterator getDispatchConfigs() {
    return dispatches.values().iterator();
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.control.conf.FilterConfig#getName()
   */
  public String getName() {
    return name;
  }

  /**
   * Set name property
   */
  public void setName(String string) {
    name = string;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.control.conf.ActionConfig#getClassName()
   */
  public String getClassName() {
    return className;
  }

  /**
   * Set filter class name
   */
  public void setClassName(String string) {
    className = string;
  }

  /*
   * (non-Javadoc)
   * @see de.odysseus.calyxo.control.conf.FilterConfig#getWhen()
   */
  public String getWhen() {
    return when;
  }

  /**
   * Set when property
   */
  public void setWhen(String string) {
    when = string;
  }
}
TOP

Related Classes of de.odysseus.calyxo.control.conf.impl.FilterConfigImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.