001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.jxpath.servlet;
019
020import java.util.Enumeration;
021import java.util.HashSet;
022
023import javax.servlet.ServletRequest;
024
025/**
026 * Implementation of the {@link org.apache.commons.jxpath.DynamicPropertyHandler} interface that provides access to attributes and parameters of a
027 * {@link ServletRequest}.
028 */
029public class ServletRequestHandler extends HttpSessionHandler {
030
031    /**
032     * Constructs a new instance.
033     */
034    public ServletRequestHandler() {
035        // empty
036    }
037
038    @Override
039    protected void collectPropertyNames(final HashSet<String> set, final Object bean) {
040        super.collectPropertyNames(set, bean);
041        final ServletRequestAndContext handle = (ServletRequestAndContext) bean;
042        final ServletRequest servletRequest = handle.getServletRequest();
043        Enumeration<String> e = servletRequest.getAttributeNames();
044        while (e.hasMoreElements()) {
045            set.add(e.nextElement());
046        }
047        e = servletRequest.getParameterNames();
048        while (e.hasMoreElements()) {
049            set.add(e.nextElement());
050        }
051    }
052
053    @Override
054    public Object getProperty(final Object bean, final String property) {
055        final ServletRequestAndContext handle = (ServletRequestAndContext) bean;
056        final ServletRequest servletRequest = handle.getServletRequest();
057        final String[] strings = servletRequest.getParameterValues(property);
058        if (strings != null) {
059            if (strings.length == 0) {
060                return null;
061            }
062            if (strings.length == 1) {
063                return strings[0];
064            }
065            return strings;
066        }
067        final Object object = servletRequest.getAttribute(property);
068        if (object != null) {
069            return object;
070        }
071        return super.getProperty(bean, property);
072    }
073
074    @Override
075    public void setProperty(final Object request, final String property, final Object value) {
076        ((ServletRequestAndContext) request).getServletRequest().setAttribute(property, value);
077    }
078}