All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sling.api.request.RequestUtil Maven / Gradle / Ivy

Go to download

The Apache Sling API defines an extension to the Servlet API 2.4 to provide access to content and unified access to request parameters hiding the differences between the different methods of transferring parameters from client to server. Note that the Apache Sling API bundle does not include the Servlet API but instead requires the API to be provided by the Servlet container in which the Apache Sling framework is running or by another bundle.

There is a newer version: 2.9.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.sling.api.request;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.servlets.HttpConstants;

/**
 * Request related utility methods.
 * 

* This class is not intended to be extended or instantiated because it just * provides static utility methods not intended to be overwritten. * * @since 2.1 (Sling API Bundle 2.1.0) */ public class RequestUtil { /** * Parses a header of the form: * *

     *            Header = Token { "," Token } .
     *            Token = name { ";" Parameter } .
     *            Parameter = name [ "=" value ] .
     * 
* * "," and ";" are not allowed within name and value * * @param value The header value * @return A Map indexed by the Token names where the values are Map * instances indexed by parameter name */ public static @Nonnull Map> parserHeader(@Nonnull String value) { Map> result = new HashMap>(); String[] tokens = value.split(","); for (int i = 0; i < tokens.length; i++) { String[] parameters = tokens[i].split(";"); String name = parameters[0].trim(); Map parMap; if (parameters.length > 0) { parMap = new HashMap(); for (int j = 1; j < parameters.length; j++) { String[] content = parameters[j].split("=", 2); if (content.length > 1) { parMap.put(content[0].trim(), content[1].trim()); } else { parMap.put(content[0].trim(), content[0].trim()); } } } else { parMap = Collections.emptyMap(); } result.put(name, parMap); } return result; } /** * Parses an Accept-* header of the form: * *
     *            Header = Token { "," Token } .
     *            Token = name { ";" "q" [ "=" value ] } .
     *            Parameter =  .
     * 
* * "," and ";" are not allowed within name and value * * @param value The header value * @return A Map indexed by the Token names where the values are * Double instances providing the value of the * q parameter. */ public static @Nonnull Map parserAcceptHeader(@Nonnull String value) { Map result = new HashMap(); String[] tokens = value.split(","); for (int i = 0; i < tokens.length; i++) { String[] parameters = tokens[i].split(";"); String name = parameters[0]; Double qVal = new Double(1.0); if (parameters.length > 1) { for (int j = 1; j < parameters.length; j++) { String[] content = parameters[j].split("=", 2); if (content.length > 1 && "q".equals(content[0])) { try { qVal = Double.valueOf(content[1]); } catch (NumberFormatException nfe) { // don't care } } } } if (qVal != null) { result.put(name, qVal); } } return result; } /** * Utility method to return a name for the given servlet. This method * applies the following algorithm to find a non-null, * non-empty name: *
    *
  1. If the servlet has a servlet config, the servlet name from the * servlet config is taken. *
  2. Otherwise check the servlet info *
  3. Otherwise use the fully qualified name of the servlet class *
* * @param servlet The servlet * @return The name of the servlet. */ public static @Nonnull String getServletName(@Nonnull Servlet servlet) { String name = null; if (servlet.getServletConfig() != null) { name = servlet.getServletConfig().getServletName(); } if (name == null || name.length() == 0) { name = servlet.getServletInfo(); } if (name == null || name.length() == 0) { name = servlet.getClass().getName(); } return name; } /** * Sets the named request attribute to the new value and returns the * previous value. * * @param request The request object whose attribute is to be set. * @param name The name of the attribute to be set. * @param value The new value of the attribute. If this is null * the attribte is actually removed from the request. * @return The previous value of the named request attribute or * null if it was not set. */ public static @CheckForNull Object setRequestAttribute(@Nonnull HttpServletRequest request, @Nonnull String name, Object value) { Object oldValue = request.getAttribute(name); if (value == null) { request.removeAttribute(name); } else { request.setAttribute(name, value); } return oldValue; } /** * Checks if the request contains a if-last-modified-since header and if the the * request's underlying resource has a jcr:lastModified property. if the properties were modified * before the header a 304 is sent otherwise the response last modified header is set. * @param req the request * @param resp the response * @return true if the response was set */ public static boolean handleIfModifiedSince(@Nonnull SlingHttpServletRequest req, @Nonnull HttpServletResponse resp){ boolean responseSet=false; long lastModified=req.getResource().getResourceMetadata().getModificationTime(); if (lastModified!=-1){ long modifiedTime = lastModified/1000; //seconds long ims = req.getDateHeader(HttpConstants.HEADER_IF_MODIFIED_SINCE)/1000; //seconds if (modifiedTime <= ims) { resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED); responseSet=true; } resp.setDateHeader(HttpConstants.HEADER_LAST_MODIFIED, lastModified); } return responseSet; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy