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  
18  package org.apache.commons.jxpath.servlet;
19  
20  import java.util.Enumeration;
21  import java.util.HashSet;
22  
23  import javax.servlet.ServletRequest;
24  
25  /**
26   * Implementation of the {@link org.apache.commons.jxpath.DynamicPropertyHandler} interface that provides access to attributes and parameters of a
27   * {@link ServletRequest}.
28   */
29  public class ServletRequestHandler extends HttpSessionHandler {
30  
31      /**
32       * Constructs a new instance.
33       */
34      public ServletRequestHandler() {
35          // empty
36      }
37  
38      @Override
39      protected void collectPropertyNames(final HashSet<String> set, final Object bean) {
40          super.collectPropertyNames(set, bean);
41          final ServletRequestAndContext handle = (ServletRequestAndContext) bean;
42          final ServletRequest servletRequest = handle.getServletRequest();
43          Enumeration<String> e = servletRequest.getAttributeNames();
44          while (e.hasMoreElements()) {
45              set.add(e.nextElement());
46          }
47          e = servletRequest.getParameterNames();
48          while (e.hasMoreElements()) {
49              set.add(e.nextElement());
50          }
51      }
52  
53      @Override
54      public Object getProperty(final Object bean, final String property) {
55          final ServletRequestAndContext handle = (ServletRequestAndContext) bean;
56          final ServletRequest servletRequest = handle.getServletRequest();
57          final String[] strings = servletRequest.getParameterValues(property);
58          if (strings != null) {
59              if (strings.length == 0) {
60                  return null;
61              }
62              if (strings.length == 1) {
63                  return strings[0];
64              }
65              return strings;
66          }
67          final Object object = servletRequest.getAttribute(property);
68          if (object != null) {
69              return object;
70          }
71          return super.getProperty(bean, property);
72      }
73  
74      @Override
75      public void setProperty(final Object request, final String property, final Object value) {
76          ((ServletRequestAndContext) request).getServletRequest().setAttribute(property, value);
77      }
78  }