/** * This file Copyright (c) 2010-2013 Magnolia International * Ltd. (http://www.magnolia-cms.com). All rights reserved. * * * This file is dual-licensed under both the Magnolia * Network Agreement and the GNU General Public License. * You may elect to use one or the other of these licenses. * * This file is distributed in the hope that it will be * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. * Redistribution, except as permitted by whichever of the GPL * or MNA you select, is prohibited. * * 1. For the GPL license (GPL), you can redistribute and/or * modify this file under the terms of the GNU General * Public License, Version 3, as published by the Free Software * Foundation. You should have received a copy of the GNU * General Public License, Version 3 along with this program; * if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * 2. For the Magnolia Network Agreement (MNA), this file * and the accompanying materials are made available under the * terms of the MNA which accompanies this distribution, and * is available at http://www.magnolia-cms.com/mna.html * * Any modifications to this file must keep this entire header * intact. * */ package info.magnolia.module.blossom.support; import java.util.Enumeration; import java.util.NoSuchElementException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import org.apache.commons.lang.ObjectUtils; import org.springframework.web.util.WebUtils; /** * Request wrapper for overriding special attributes. * * @since 1.2 */ public class SpecialAttributeRequestWrapper extends HttpServletRequestWrapper { private static final String[] SPECIAL_ATTRIBUTE_NAMES = new String[]{ WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, WebUtils.FORWARD_CONTEXT_PATH_ATTRIBUTE, WebUtils.FORWARD_SERVLET_PATH_ATTRIBUTE, WebUtils.FORWARD_PATH_INFO_ATTRIBUTE, WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE /*, WebUtils.ERROR_STATUS_CODE_ATTRIBUTE, WebUtils.ERROR_EXCEPTION_TYPE_ATTRIBUTE, WebUtils.ERROR_MESSAGE_ATTRIBUTE, WebUtils.ERROR_EXCEPTION_ATTRIBUTE, WebUtils.ERROR_REQUEST_URI_ATTRIBUTE, WebUtils.ERROR_SERVLET_NAME_ATTRIBUTE*/ }; private final Object[] localValues = new Object[SPECIAL_ATTRIBUTE_NAMES.length]; private final Object[] originalValues = new Object[SPECIAL_ATTRIBUTE_NAMES.length]; private boolean enabled = true; public SpecialAttributeRequestWrapper(HttpServletRequest request) { super(request); } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public boolean setSpecialAttribute(String name, Object value) { for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) { if (name.equals(SPECIAL_ATTRIBUTE_NAMES[i])) { localValues[i] = value; originalValues[i] = super.getAttribute(name); return true; } } return false; } @Override public Object getAttribute(String name) { if (enabled) { for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) { if (localValues[i] != null && SPECIAL_ATTRIBUTE_NAMES[i].equals(name)) { if (shouldMasquerade(name)) { return localValues[i]; } else { break; } } } } return super.getAttribute(name); } protected boolean shouldMasquerade(String name) { for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) { if (localValues[i] != null && !ObjectUtils.equals(super.getAttribute(SPECIAL_ATTRIBUTE_NAMES[i]), originalValues[i])) { return false; } } return true; } @Override public Enumeration getAttributeNames() { return new Enumeration() { private int pos = 0; private String next = null; private Enumeration parentEnumeration = getRequest().getAttributeNames(); @Override public boolean hasMoreElements() { if (enabled) { while (pos < localValues.length) { if (localValues[pos] != null) { return true; } pos++; } } if (next != null) { return true; } next = findNext(); return next != null; } @Override public Object nextElement() { if (enabled) { while (pos < localValues.length) { if (localValues[pos] != null) { String name = SPECIAL_ATTRIBUTE_NAMES[pos]; pos++; return name; } pos++; } } if (next == null) { next = findNext(); if (next == null) { throw new NoSuchElementException(); } } Object result = next; next = null; return result; } private String findNext() { while (parentEnumeration.hasMoreElements()) { String name = (String) parentEnumeration.nextElement(); if (!enabled || !hasLocalValue(name)) { return name; } } return null; } }; } @Override public void setAttribute(String name, Object o) { if (!hasLocalValue(name)) { super.setAttribute(name, o); } } @Override public void removeAttribute(String name) { if (!hasLocalValue(name)) { super.removeAttribute(name); } } private boolean hasLocalValue(String name) { for (int i = 0; i < SPECIAL_ATTRIBUTE_NAMES.length; i++) { if (localValues[i] != null && name.equals(SPECIAL_ATTRIBUTE_NAMES[i])) { return true; } } return false; } }