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