    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.http.HttpSession;
    024
    025import org.apache.commons.jxpath.JXPathException;
    026
    027/**
    028 * Implementation of the {@link org.apache.commons.jxpath.DynamicPropertyHandler} interface that provides access to attributes of a @{link HttpSession}.
    029 */
    030public class HttpSessionHandler extends ServletContextHandler {
    031
    032    /**
    033     * Constructs a new instance.
    034     */
    035    public HttpSessionHandler() {
    036        // empty
    037    }
    038
    039    @Override
    040    protected void collectPropertyNames(final HashSet<String> set, final Object bean) {
    041        final HttpSessionAndServletContext handle = (HttpSessionAndServletContext) bean;
    042        super.collectPropertyNames(set, handle.getServletContext());
    043        final HttpSession session = handle.getSession();
    044        if (session != null) {
    045            final Enumeration<String> e = session.getAttributeNames();
    046            while (e.hasMoreElements()) {
    047                set.add(e.nextElement());
    048            }
    049        }
    050    }
    051
    052    @Override
    053    public Object getProperty(final Object bean, final String property) {
    054        final HttpSessionAndServletContext handle = (HttpSessionAndServletContext) bean;
    055        final HttpSession session = handle.getSession();
    056        if (session != null) {
    057            final Object object = session.getAttribute(property);
    058            if (object != null) {
    059                return object;
    060            }
    061        }
    062        return super.getProperty(handle.getServletContext(), property);
    063    }
    064
    065    @Override
    066    public void setProperty(final Object bean, final String property, final Object value) {
    067        final HttpSessionAndServletContext handle = (HttpSessionAndServletContext) bean;
    068        final HttpSession session = handle.getSession();
    069        if (session == null) {
    070            throw new JXPathException("Cannot set session attribute: " + "there is no session");
    071        }
    072        session.setAttribute(property, value);
    073    }
    074}