Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.engine;
 
 import java.util.Map;
 
 
 public class RequestUtil {

    
Parses a header of the form:
            Header = Token { "," Token } .
            Token = name { ";" Parameter } .
            Paramter = name [ "=" value ] .
 
"," and ";" are not allowed within name and value

Parameters:
value
Returns:
A Map indexed by the Token names where the values are Map instances indexed by parameter name
 
     public static Map<StringMap<StringString>> parserHeader(String value) {
         Map<StringMap<StringString>> result = new HashMap<StringMap<StringString>>();
         String[] tokens = value.split(",");
         for (int i = 0; i < tokens.lengthi++) {
             String[] parameters = tokens[i].split(";");
             String name = parameters[0].trim();
             Map<StringStringparMap;
             if (parameters.length > 0) {
                 parMap = new HashMap<StringString>();
                 for (int j = 1; j < parameters.lengthj++) {
                     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(nameparMap);
         }
         return result;
     }

    
Parses an Accept-* header of the form:
            Header = Token { "," Token } .
            Token = name { ";" "q" [ "=" value ] } .
            Paramter =  .
 
"," and ";" are not allowed within name and value

Parameters:
value
Returns:
A Map indexed by the Token names where the values are Double instances providing the value of the q parameter.
 
     public static Map<StringDoubleparserAcceptHeader(String value) {
         Map<StringDoubleresult = new HashMap<StringDouble>();
         String[] tokens = value.split(",");
         for (int i = 0; i < tokens.lengthi++) {
             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.lengthj++) {
                     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(nameqVal);
            }
        }
        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
    public static String getServletName(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.

Parameters:
request The request object whose attribute is to be set.
name The name of the attribute to be set.
value The new value of the attribute. If this is null the attribte is actually removed from the request.
Returns:
The previous value of the named request attribute or null if it was not set.
    public static Object setRequestAttribute(HttpServletRequest request,
            String nameObject value) {
        Object oldValue = request.getAttribute(name);
        if (value == null) {
            request.removeAttribute(name);
        } else {
            request.setAttribute(namevalue);
        }
        return oldValue;
    }
New to GrepCode? Check out our FAQ X