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.jsp.PageContext; 024 025import org.apache.commons.jxpath.DynamicPropertyHandler; 026 027/** 028 * Implementation of the {@link DynamicPropertyHandler} interface that provides access to attributes of a {@link PageContext} in all scopes. 029 */ 030public class PageContextHandler implements DynamicPropertyHandler { 031 032 /** 033 * Constructs a new instance. 034 */ 035 public PageContextHandler() { 036 // empty 037 } 038 039 /** 040 * Returns {@code pageContext.findAttribute(property)}. 041 * 042 * @param pageContext to search 043 * @param property name 044 * @return Object value 045 */ 046 @Override 047 public Object getProperty(final Object pageContext, final String property) { 048 return ((PageContext) pageContext).findAttribute(property); 049 } 050 051 @Override 052 public String[] getPropertyNames(final Object pageContext) { 053 final HashSet<String> set = new HashSet<>(); 054 Enumeration<String> e = ((PageContext) pageContext).getAttributeNamesInScope(PageContext.PAGE_SCOPE); 055 while (e.hasMoreElements()) { 056 set.add(e.nextElement()); 057 } 058 e = ((PageContext) pageContext).getAttributeNamesInScope(PageContext.REQUEST_SCOPE); 059 while (e.hasMoreElements()) { 060 set.add(e.nextElement()); 061 } 062 e = ((PageContext) pageContext).getAttributeNamesInScope(PageContext.SESSION_SCOPE); 063 while (e.hasMoreElements()) { 064 set.add(e.nextElement()); 065 } 066 e = ((PageContext) pageContext).getAttributeNamesInScope(PageContext.APPLICATION_SCOPE); 067 while (e.hasMoreElements()) { 068 set.add(e.nextElement()); 069 } 070 return set.toArray(new String[set.size()]); 071 } 072 073 @Override 074 public void setProperty(final Object pageContext, final String property, final Object value) { 075 ((PageContext) pageContext).setAttribute(property, value, PageContext.PAGE_SCOPE); 076 } 077}