View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.jxpath.servlet;
18  
19  import java.util.Enumeration;
20  import java.util.HashSet;
21  import javax.servlet.ServletRequest;
22  
23  /**
24   * Implementation of the {@link org.apache.commons.jxpath.DynamicPropertyHandler}
25   * interface that provides access to attributes and parameters
26   * of a {@link ServletRequest}.
27   *
28   * @author Dmitri Plotnikov
29   * @version $Revision: 652848 $ $Date: 2008-05-02 19:53:50 +0200 (Fr, 02 Mai 2008) $
30   */
31  public class ServletRequestHandler extends HttpSessionHandler {
32  
33      protected void collectPropertyNames(HashSet set, Object bean) {
34          super.collectPropertyNames(set, bean);
35          ServletRequestAndContext handle = (ServletRequestAndContext) bean;
36          ServletRequest servletRequest = handle.getServletRequest();
37          Enumeration e = servletRequest.getAttributeNames();
38          while (e.hasMoreElements()) {
39              set.add(e.nextElement());
40          }
41          e = servletRequest.getParameterNames();
42          while (e.hasMoreElements()) {
43              set.add(e.nextElement());
44          }
45      }
46  
47      public Object getProperty(Object bean, String property) {
48          ServletRequestAndContext handle = (ServletRequestAndContext) bean;
49          ServletRequest servletRequest = handle.getServletRequest();
50          String[] strings = servletRequest.getParameterValues(property);
51  
52          if (strings != null) {
53              if (strings.length == 0) {
54                  return null;
55              }
56              if (strings.length == 1) {
57                  return strings[0];
58              }
59              return strings;
60          }
61  
62          Object object = servletRequest.getAttribute(property);
63          if (object != null) {
64              return object;
65          }
66  
67          return super.getProperty(bean, property);
68      }
69  
70      public void setProperty(Object request, String property, Object value) {
71          ((ServletRequestAndContext) request).getServletRequest().setAttribute(property, value);
72      }
73  }