- /*
- * Copyright 2002-2004 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.springframework.web.util;
- import java.io.File;
- import java.util.Enumeration;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.ServletContext;
- import javax.servlet.ServletRequest;
- import javax.servlet.http.Cookie;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- import org.springframework.util.StringUtils;
- /**
- * Miscellaneous utilities for web applications.
- * Used by various framework classes.
- * @author Rod Johnson
- * @author Juergen Hoeller
- */
- public abstract class WebUtils {
- /**
- * Default character encoding to use when request.getCharacterEncoding
- * returns null, according to the Servlet spec.
- * @see ServletRequest#getCharacterEncoding
- */
- public static final String DEFAULT_CHARACTER_ENCODING = "ISO-8859-1";
- /**
- * Standard Servlet spec context attribute that specifies a temporary
- * directory for the current web application, of type java.io.File
- */
- public static final String TEMP_DIR_CONTEXT_ATTRIBUTE = "javax.servlet.context.tempdir";
- /**
- * Web app root key parameter at the servlet context level
- * (i.e. web.xml): "webAppRootKey".
- */
- public static final String WEB_APP_ROOT_KEY_PARAM = "webAppRootKey";
- /** Default web app root key: "webapp.root" */
- public static final String DEFAULT_WEB_APP_ROOT_KEY = "webapp.root";
- /** Name suffixes in case of image buttons */
- public static final String[] SUBMIT_IMAGE_SUFFIXES = {".x", ".y"};
- /**
- * Set a system property to the web application root directory.
- * The key of the system property can be defined with the "webAppRootKey"
- * context-param in web.xml. Default is "webapp.root".
- * <p>Can be used for toolkits that support substition with System.getProperty
- * values, like Log4J's "${key}" syntax within log file locations.
- * @param servletContext the servlet context of the web application
- * @throws IllegalStateException if the system property is already set,
- * or if the WAR file is not expanded
- * @see #WEB_APP_ROOT_KEY_PARAM
- * @see #DEFAULT_WEB_APP_ROOT_KEY
- * @see WebAppRootListener
- * @see Log4jWebConfigurer
- */
- public static void setWebAppRootSystemProperty(ServletContext servletContext) throws IllegalStateException {
- String param = servletContext.getInitParameter(WEB_APP_ROOT_KEY_PARAM);
- String key = (param != null ? param : DEFAULT_WEB_APP_ROOT_KEY);
- String oldValue = System.getProperty(key);
- String root = servletContext.getRealPath("/");
- if (root == null) {
- throw new IllegalStateException("Cannot set web app root system property when WAR file is not expanded");
- }
- if (oldValue != null && !StringUtils.pathEquals(oldValue, root)) {
- throw new IllegalStateException("Web app root system property already set to different value: '" +
- key + "' = [" + oldValue +
- "] instead of [" + root + "]- Choose unique webAppRootKey values in your web.xml files!");
- }
- System.setProperty(key, root);
- servletContext.log("Set web app root system property: " + key + " = " + root);
- }
- /**
- * Return the temporary directory for the current web application,
- * as provided by the servlet container.
- * @param servletContext the servlet context of the web application
- * @return the File representing the temporary directory
- */
- public static File getTempDir(ServletContext servletContext) {
- return (File) servletContext.getAttribute(TEMP_DIR_CONTEXT_ATTRIBUTE);
- }
- /**
- * Check the given request for a session attribute of the given name.
- * Returns null if there is no session or if the session has no such attribute.
- * Does not create a new session if none has existed before!
- * @param request current HTTP request
- * @param name the name of the session attribute
- * @return the value of the session attribute, or null if not found
- */
- public static Object getSessionAttribute(HttpServletRequest request, String name) {
- HttpSession session = request.getSession(false);
- return (session != null ? session.getAttribute(name) : null);
- }
- /**
- * Check the given request for a session attribute of the given name.
- * Throws an exception if there is no session or if the session has no such
- * attribute. Does not create a new session if none has existed before!
- * @param request current HTTP request
- * @param name the name of the session attribute
- * @return the value of the session attribute, or null if not found
- * @throws IllegalStateException if the session attribute could not be found
- */
- public static Object getRequiredSessionAttribute(HttpServletRequest request, String name)
- throws IllegalStateException {
- Object attr = getSessionAttribute(request, name);
- if (attr == null) {
- throw new IllegalStateException("No session attribute '" + name + "' found");
- }
- return attr;
- }
- /**
- * Set the session attribute with the given name to the given value.
- * Removes the session attribute if value is null, if a session existed at all.
- * Does not create a new session if not necessary!
- * @param request current HTTP request
- * @param name the name of the session attribute
- */
- public static void setSessionAttribute(HttpServletRequest request, String name, Object value) {
- if (value != null) {
- request.getSession().setAttribute(name, value);
- }
- else {
- HttpSession session = request.getSession(false);
- if (session != null) {
- session.removeAttribute(name);
- }
- }
- }
- /**
- * Get the specified session attribute, creating and setting a new attribute if
- * no existing found. The given class needs to have a public no-arg constructor.
- * Useful for on-demand state objects in a web tier, like shopping carts.
- * @param session current HTTP session
- * @param name the name of the session attribute
- * @param clazz the class to instantiate for a new attribute
- * @return the value of the session attribute, newly created if not found
- * @throws IllegalArgumentException if the session attribute could not be instantiated
- */
- public static Object getOrCreateSessionAttribute(HttpSession session, String name, Class clazz)
- throws IllegalArgumentException {
- Object sessionObject = session.getAttribute(name);
- if (sessionObject == null) {
- try {
- sessionObject = clazz.newInstance();
- }
- catch (InstantiationException ex) {
- throw new IllegalArgumentException("Could not instantiate class [" + clazz.getName() +
- "] for session attribute '" + name + "': " + ex.getMessage());
- }
- catch (IllegalAccessException ex) {
- throw new IllegalArgumentException("Could not access default constructor of class [" + clazz.getName() +
- "] for session attribute '" + name + "': " + ex.getMessage());
- }
- session.setAttribute(name, sessionObject);
- }
- return sessionObject;
- }
- /**
- * Retrieve the first cookie with the given name. Note that multiple
- * cookies can have the same name but different paths or domains.
- * @param name cookie name
- * @return the first cookie with the given name, or null if none is found
- */
- public static Cookie getCookie(HttpServletRequest request, String name) {
- Cookie cookies[] = request.getCookies();
- if (cookies != null) {
- for (int i = 0; i < cookies.length; i++) {
- if (name.equals(cookies[i].getName()))
- return cookies[i];
- }
- }
- return null;
- }
- /**
- * Convenience method to return a map from un-prefixed property names
- * to values. E.g. with a prefix of price, price_1, price_2 produce
- * a properties object with mappings for 1, 2 to the same values.
- * Maps single values to String and multiple values to String array.
- * @param request HTTP request in which to look for parameters
- * @param base beginning of parameter name
- * (if this is null or the empty string, all parameters will match)
- * @return map containing request parameters <b>without the prefix</b>,
- * containing either a String or a String[] as values
- */
- public static Map getParametersStartingWith(ServletRequest request, String base) {
- Enumeration enum = request.getParameterNames();
- Map params = new HashMap();
- if (base == null) {
- base = "";
- }
- while (enum != null && enum.hasMoreElements()) {
- String paramName = (String) enum.nextElement();
- if (base == null || "".equals(base) || paramName.startsWith(base)) {
- String unprefixed = paramName.substring(base.length());
- String[] values = request.getParameterValues(paramName);
- if (values == null) {
- // do nothing, no values found at all
- }
- else if (values.length > 1) {
- params.put(unprefixed, values);
- }
- else {
- params.put(unprefixed, values[0]);
- }
- }
- }
- return params;
- }
- /**
- * Checks if a specific input type="submit" parameter was sent in the request,
- * either via a button (directly with name) or via an image (name + ".x" or
- * name + ".y").
- * @param request current HTTP request
- * @param name name of the parameter
- * @return if the parameter was sent
- * @see #SUBMIT_IMAGE_SUFFIXES
- */
- public static boolean hasSubmitParameter(ServletRequest request, String name) {
- if (request.getParameter(name) != null) {
- return true;
- }
- for (int i = 0; i < SUBMIT_IMAGE_SUFFIXES.length; i++) {
- String suffix = SUBMIT_IMAGE_SUFFIXES[i];
- if (request.getParameter(name + suffix) != null) {
- return true;
- }
- }
- return false;
- }
- }