/*
 * Copyright  1999-2004 The Apache Software Foundation
 *
 *  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.
 *
 */

/* $Id$  */

package org.apache.lenya.util;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.cocoon.environment.Request;
import org.apache.excalibur.source.SourceNotFoundException;
import org.apache.lenya.cms.cocoon.source.SourceUtil;
import org.apache.xpath.XPathAPI;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;


/**
 * Servlet utility class.
 */
public final class ServletHelper {
	
    /**
     * Ctor.
     */
    private ServletHelper() {
        // do nothing
    }

    /**
     * Returns the URL inside the web application (without the context prefix). 
     * @param request The request.
     * @return A string.
     */
    public static String getWebappURI(Request request) {
        String context = request.getContextPath();
        String requestUri = request.getRequestURI();
        return getWebappURI(context, requestUri);
    }

    /**
     * Returns the URL inside the web application (without the context prefix). 
     * @param context The context prefix.
     * @param requestUri The complete request URI.
     * @return A string.
     */
    public static String getWebappURI(String context, String requestUri) {
        if (context == null) {
            context = "";
        }
        String url = requestUri.substring(context.length());
		if (url.length() > 0 && !url.startsWith("/")) {
			url = "/" + url;
		}
		
        return url;
    }

    /**
     * Converts the request parameters to a map.
     * If a key is mapped to multiple parameters, a string array is used as the value.
     * @param request The request.
     * @return A map.
     */
    public static Map getParameterMap(Request request) {
        Map requestParameters = new HashMap();
        for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
            String key = (String) e.nextElement();
            String[] values = request.getParameterValues(key);
            Object value;
            if (values.length == 1) {
                value = values[0];
            }
            else {
                value = values;
            }
            requestParameters.put(key, value);
        }
        return requestParameters;
    }
    
    /**
     * Returns the value of enable-uploads in web.xml
     * @param manager The Service Manager.
     * @return true if enable upload is true or not set in web.xml, else false
     */
    public static boolean isUploadEnabled(ServiceManager manager)
            throws SourceNotFoundException, ServiceException,
            ParserConfigurationException, SAXException, IOException,
            TransformerException {

        Node node;
        String webappUrl = "context://WEB-INF/web.xml";
        Document document = SourceUtil.readDOM(webappUrl, manager);
        Element root = document.getDocumentElement();
        node = XPathAPI.selectSingleNode(root,"/web-app/servlet/init-param[param-name='enable-uploads']/param-value/text()");
        
        if (node == null) {
            return false;
        }
        return node.getNodeValue().equals("true");
    }
}

@@ -1313,17 +1313,16 @@
 ption;%0A%0A
-%0A
 /**%0A * S
@@ -1382,17 +1382,16 @@
 elper %7B%0A
-%09
 %0A    /**
@@ -1550,33 +1550,32 @@
 context prefix).
- 
 %0A     * @param r
@@ -1932,17 +1932,16 @@
 prefix).
- 
 %0A     * 
@@ -2262,18 +2262,24 @@
 gth());%0A
-%09%09
+        
 if (url.
@@ -2322,11 +2322,20 @@
 ) %7B%0A
-%09%09%09
+            
 url 
@@ -2351,14 +2351,18 @@
 rl;%0A
-%09%09%7D%0A%09%09
+        %7D%0A
 %0A   
@@ -2441,23 +2441,16 @@
 o a map.
-%0A     *
  If a ke
@@ -2493,16 +2493,23 @@
 a string
+%0A     *
  array i
@@ -3006,28 +3006,16 @@
        %7D
-%0A           
  else %7B%0A
@@ -3157,20 +3157,66 @@
 ;%0A    %7D%0A
+%0A
     
+private static Boolean uploadEnabled = null;%0A
 %0A    /**
@@ -3418,16 +3418,29 @@
  static 
+synchronized 
 boolean 
@@ -3540,28 +3540,16 @@
 ception,
-%0A           
  ParserC
@@ -3570,16 +3570,28 @@
 ception,
+%0A           
  SAXExce
@@ -3609,28 +3609,16 @@
 ception,
-%0A           
  Transfo
@@ -3630,24 +3630,80 @@
 xception %7B%0A%0A
+        if (ServletHelper.uploadEnabled == null) %7B%0A%0A    
         Node
@@ -3709,16 +3709,20 @@
 e node;%0A
+    
         
@@ -3777,16 +3777,20 @@
         
+   
+ 
 Document
@@ -3849,16 +3849,20 @@
         
+    
 Element 
@@ -3907,16 +3907,20 @@
         
+ 
+   
 node = X
@@ -3949,16 +3949,37 @@
 de(root,
+%0A                    
 %22/web-ap
@@ -4054,17 +4054,13 @@
 %22);%0A
-    
+%0A
     
-%0A
     
@@ -4099,80 +4099,271 @@
     
-return false;%0A        %7D%0A        return node.getNodeValue().equals(%22true%22
+    ServletHelper.uploadEnabled = Boolean.FALSE;%0A            %7D%0A            boolean enabled = node.getNodeValue().equals(%22true%22);%0A            ServletHelper.uploadEnabled = Boolean.valueOf(enabled);%0A        %7D%0A        return ServletHelper.uploadEnabled.booleanValue(
 );%0A 

Use lazy loading in ServletHelper.isUploadEnabled()