/*
 * 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.core;

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

public class RequestUtil {

    /**
     * Parses a header of the form:
     *
     * <pre>
     *            Header = Token { &quot;,&quot; Token } .
     *            Token = name { &quot;;&quot; Parameter } .
     *            Paramter = name [ &quot;=&quot; value ] .
     * </pre>
     *
     * "," and ";" are not allowed within name and value
     *
     * @param value
     * @return A Map indexed by the Token names where the values are Map
     *         instances indexed by parameter name
     */
    public static Map<String, Map<String, String>> parserHeader(String value) {
        Map<String, Map<String, String>> result = new HashMap<String, Map<String, String>>();
        String[] tokens = value.split(",");
        for (int i = 0; i < tokens.length; i++) {
            String[] parameters = tokens[i].split(";");
            String name = parameters[0].trim();
            Map<String, String> parMap;
            if (parameters.length > 0) {
                parMap = new HashMap<String, String>();
                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 <code>Accept-*</code> header of the form:
     *
     * <pre>
     *            Header = Token { &quot;,&quot; Token } .
     *            Token = name { &quot;;&quot; &quot;q&quot; [ &quot;=&quot; value ] } .
     *            Paramter =  .
     * </pre>
     *
     * "," and ";" are not allowed within name and value
     *
     * @param value
     * @return A Map indexed by the Token names where the values are
     *         <code>Double</code> instances providing the value of the
     *         <code>q</code> parameter.
     */
    public static Map<String, Double> parserAcceptHeader(String value) {
        Map<String, Double> result = new HashMap<String, Double>();
        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;
    }
}

@@ -912,16 +912,47 @@
 l.Map;%0A%0A
+import javax.servlet.Servlet;%0A%0A
 public c
@@ -1013,32 +1013,33 @@
 the form:%0A     *
+ 
 %0A     * %3Cpre%3E%0A  
@@ -1228,32 +1228,33 @@
  * %3C/pre%3E%0A     *
+ 
 %0A     * %22,%22 and 
@@ -1293,32 +1293,33 @@
 and value%0A     *
+ 
 %0A     * @param v
@@ -2588,24 +2588,25 @@
 form:%0A     *
+ 
 %0A     * %3Cpre
@@ -2803,24 +2803,25 @@
 /pre%3E%0A     *
+ 
 %0A     * %22,%22 
@@ -2872,16 +2872,17 @@
 e%0A     *
+ 
 %0A     * 
@@ -4067,32 +4067,32 @@
     %7D%0A        %7D%0A
-
         return r
@@ -4103,11 +4103,897 @@
 t;%0A    %7D
+%0A%0A    /**%0A     * Utility method to return a name for the given servlet. This method%0A     * applies the following algorithm to find a non-%3Ccode%3Enull%3C/code%3E,%0A     * non-empty name:%0A     * %3Col%3E%0A     * %3Cli%3EIf the servlet has a servlet config, the servlet name from the%0A     * servlet config is taken.%0A     * %3Cli%3EOtherwise check the servlet info%0A     * %3Cli%3EOtherwise use the fully qualified name of the servlet class%0A     * %3C/ol%3E%0A     */%0A    public static String getServletName(Servlet servlet) %7B%0A        String name = null;%0A%0A        if (servlet.getServletConfig() != null) %7B%0A            name = servlet.getServletConfig().getServletName();%0A        %7D%0A        if (name == null %7C%7C name.length() == 0) %7B%0A            name = servlet.getServletInfo();%0A        %7D%0A        if (name == null %7C%7C name.length() == 0) %7B%0A            name = servlet.getClass().getName();%0A        %7D%0A%0A        return name;%0A    %7D
 %0A%7D%0A

Add utility method to return the name of a Servlet