001: /*
002: * $Id: RequestToVariableFilter.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.scripting;
022:
023: // util imports:
024: import java.util.Enumeration;
025: import java.util.Properties;
026:
027: // logging imports:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: // misc imports:
032: import javax.servlet.http.HttpServletRequest;
033: import org.apache.bsf.BSFException;
034: import org.apache.bsf.BSFManager;
035:
036: /**
037: * Takes request parameters and declares variables with them. If a variable is
038: * already exists with that name, a "_" is prepended to the name. Both Strings
039: * and arrays are recognized.
040: */
041: public class RequestToVariableFilter implements BSFManagerFilter {
042:
043: /** The logging instance. */
044: private static final Log LOG = LogFactory.getLog(TestFilter.class);
045:
046: /**
047: * Initializes the filter.
048: *
049: *@param name The name of the filter
050: *@param props The properties
051: */
052: public void init(String name, Properties props) {
053: }
054:
055: /**
056: * Applies the filter.
057: *
058: *@param mgr The bsf manager
059: *@return The bsf manager
060: */
061: public BSFManager apply(BSFManager mgr) {
062: HttpServletRequest request = (HttpServletRequest) mgr
063: .lookupBean("request");
064: String[] values = null;
065: String name = null;
066: String newName = null;
067: Object o = null;
068: for (Enumeration e = request.getParameterNames(); e
069: .hasMoreElements();) {
070: name = (String) e.nextElement();
071: o = mgr.lookupBean(name);
072: if (o == null) {
073: newName = name;
074: } else {
075: newName = "_" + name;
076: }
077:
078: values = request.getParameterValues(name);
079: try {
080: if (values.length > 1) {
081: mgr.declareBean(newName, values, values.getClass());
082: if (LOG.isDebugEnabled()) {
083: LOG.debug("creating array var " + newName);
084: }
085: } else {
086: mgr.declareBean(newName, values[0], String.class);
087: if (LOG.isDebugEnabled()) {
088: LOG.debug("creating string var " + newName);
089: }
090: }
091: } catch (BSFException ex) {
092: LOG.warn("Unable to set variable " + newName, ex);
093: }
094:
095: }
096: if (LOG.isDebugEnabled()) {
097: LOG.debug("Done filtering");
098: }
099: return mgr;
100: }
101: }
|