View Javadoc

    1   /*
    2    * $Id: RequestToVariableFilter.java 471754 2006-11-06 14:55:09Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts.scripting;
    22
    23  // util imports:
    24  import java.util.Enumeration;
    25  import java.util.Properties;
    26
    27  // logging imports:
    28  import org.apache.commons.logging.Log;
    29  import org.apache.commons.logging.LogFactory;
    30
    31  // misc imports:
    32  import javax.servlet.http.HttpServletRequest;
    33  import org.apache.bsf.BSFException;
    34  import org.apache.bsf.BSFManager;
    35
    36
    37  /**
    38   *  Takes request parameters and declares variables with them. If a variable is
    39   *  already exists with that name, a "_" is prepended to the name. Both Strings
    40   *  and arrays are recognized.
    41   */
    42  public class RequestToVariableFilter implements BSFManagerFilter {
    43
    44      /**  The logging instance. */
    45      private static final Log LOG = LogFactory.getLog(TestFilter.class);
    46
    47
    48      /**
    49       *  Initializes the filter.
    50       *
    51       *@param  name   The name of the filter
    52       *@param  props  The properties
    53       */
    54      public void init(String name, Properties props) { }
    55
    56
    57      /**
    58       *  Applies the filter.
    59       *
    60       *@param  mgr  The bsf manager
    61       *@return      The bsf manager
    62       */
    63      public BSFManager apply(BSFManager mgr) {
    64          HttpServletRequest request =
    65                  (HttpServletRequest) mgr.lookupBean("request");
    66          String[] values = null;
    67          String name = null;
    68          String newName = null;
    69          Object o = null;
    70          for (Enumeration e = request.getParameterNames();
    71                  e.hasMoreElements();) {
    72              name = (String) e.nextElement();
    73              o = mgr.lookupBean(name);
    74              if (o == null) {
    75                  newName = name;
    76              } else {
    77                  newName = "_" + name;
    78              }
    79
    80              values = request.getParameterValues(name);
    81              try {
    82                  if (values.length > 1) {
    83                      mgr.declareBean(newName, values, values.getClass());
    84                      if (LOG.isDebugEnabled()) {
    85                          LOG.debug("creating array var " + newName);
    86                      }
    87                  } else {
    88                      mgr.declareBean(newName, values[0], String.class);
    89                      if (LOG.isDebugEnabled()) {
    90                          LOG.debug("creating string var " + newName);
    91                      }
    92                  }
    93              } catch (BSFException ex) {
    94                  LOG.warn("Unable to set variable " + newName, ex);
    95              }
    96
    97          }
    98          if (LOG.isDebugEnabled()) {
    99              LOG.debug("Done filtering");
    100         }
    101         return mgr;
    102     }
    103 }