001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: ServletHelper.java 565388 2007-08-13 15:56:16Z rfrovarp $ */
020:
021: package org.apache.lenya.util;
022:
023: import java.io.IOException;
024: import java.util.Enumeration;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import javax.xml.parsers.ParserConfigurationException;
029: import javax.xml.transform.TransformerException;
030:
031: import org.apache.avalon.framework.service.ServiceException;
032: import org.apache.avalon.framework.service.ServiceManager;
033: import org.apache.cocoon.environment.Request;
034: import org.apache.excalibur.source.SourceNotFoundException;
035: import org.apache.lenya.cms.cocoon.source.SourceUtil;
036: import org.apache.xpath.XPathAPI;
037: import org.w3c.dom.Document;
038: import org.w3c.dom.Element;
039: import org.w3c.dom.Node;
040: import org.xml.sax.SAXException;
041:
042: /**
043: * Servlet utility class.
044: */
045: public final class ServletHelper {
046:
047: /**
048: * Ctor.
049: */
050: private ServletHelper() {
051: // do nothing
052: }
053:
054: /**
055: * Returns the URL inside the web application (without the context prefix).
056: * @param request The request.
057: * @return A string.
058: */
059: public static String getWebappURI(Request request) {
060: String context = request.getContextPath();
061: String requestUri = request.getRequestURI();
062: return getWebappURI(context, requestUri);
063: }
064:
065: /**
066: * Returns the URL inside the web application (without the context prefix).
067: * @param context The context prefix.
068: * @param requestUri The complete request URI.
069: * @return A string.
070: */
071: public static String getWebappURI(String context, String requestUri) {
072: if (context == null) {
073: context = "";
074: }
075: String url = requestUri.substring(context.length());
076: if (url.length() > 0 && !url.startsWith("/")) {
077: url = "/" + url;
078: }
079:
080: return url;
081: }
082:
083: /**
084: * Converts the request parameters to a map. If a key is mapped to multiple parameters, a string
085: * array is used as the value.
086: * @param request The request.
087: * @return A map.
088: */
089: public static Map getParameterMap(Request request) {
090: Map requestParameters = new HashMap();
091: for (Enumeration e = request.getParameterNames(); e
092: .hasMoreElements();) {
093: String key = (String) e.nextElement();
094: String[] values = request.getParameterValues(key);
095: Object value;
096: if (values.length == 1) {
097: value = values[0];
098: } else {
099: value = values;
100: }
101: requestParameters.put(key, value);
102: }
103: return requestParameters;
104: }
105:
106: private static Boolean uploadEnabled = null;
107:
108: /**
109: * Returns the value of enable-uploads in web.xml
110: * @param manager The Service Manager.
111: * @return true if enable upload is true or not set in web.xml, else false
112: */
113: public static synchronized boolean isUploadEnabled(
114: ServiceManager manager) throws SourceNotFoundException,
115: ServiceException, ParserConfigurationException,
116: SAXException, IOException, TransformerException {
117:
118: if (ServletHelper.uploadEnabled == null) {
119:
120: Node node;
121: String webappUrl = "context://WEB-INF/web.xml";
122: Document document = SourceUtil.readDOM(webappUrl, manager);
123: Element root = document.getDocumentElement();
124: node = XPathAPI
125: .selectSingleNode(root,
126: "/web-app/servlet/init-param[param-name='enable-uploads']/param-value/text()");
127:
128: if (node == null) {
129: ServletHelper.uploadEnabled = Boolean.FALSE;
130: } else {
131: boolean enabled = node.getNodeValue().equals("true");
132: ServletHelper.uploadEnabled = Boolean.valueOf(enabled);
133: }
134: }
135: return ServletHelper.uploadEnabled.booleanValue();
136: }
137: }
|