KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > WebdavServletConfig


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/WebdavServletConfig.java,v 1.13 2004/08/05 14:43:34 dflorey Exp $
3  * $Revision: 1.13 $
4  * $Date: 2004/08/05 14:43:34 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.webdav;
25
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.ServletConfig JavaDoc;
32 import javax.servlet.ServletContext JavaDoc;
33
34 import org.apache.slide.util.XMLValue;
35 import org.jdom.Element;
36 import org.jdom.JDOMException;
37
38 /**
39  * WebDAV Servlet Configuration. This class wraps around a ServletConfig object
40  * and makes the parameters conveniently accessible to the WebdavServlet and
41  * the WebdavMethod implementations.
42  *
43  * @see org.apache.slide.webdav.method.WebdavMethod#getConfig
44  *
45  */

46 public class WebdavServletConfig
47     implements ServletConfig JavaDoc {
48     
49     
50     // -------------------------------------------------------------- Constants
51

52     
53     static final String JavaDoc DEFAULT_SERVLET_PARAMETER =
54         "default-servlet";
55     
56     
57     static final String JavaDoc DEPTH_LIMIT_PARAMETER =
58         "depth-limit";
59     
60     
61     static final String JavaDoc DEFAULT_MIME_TYPE_PARAMETER =
62         "default-mime-type";
63     
64     
65     static final String JavaDoc METHOD_FACTORY_PARAMETER =
66         "method-factory";
67     
68     
69     static final String JavaDoc SCOPE_PARAMETER =
70         "scope";
71     
72     
73     // ----------------------------------------------------- Instance Variables
74

75     
76     /**
77      * The wrapped ServletConfig.
78      */

79     protected ServletConfig JavaDoc config;
80     
81     
82     /**
83      * Whether or not the servlet is mapped as default servlet of the web
84      * application. This has quite drastic consequences for the mapping of
85      * request URIs (the path-info is then returned by getServletPath() instead
86      * of getPathInfo()), but there is no safe way for the servlet to determine
87      * whether it's mapped as default servlet. For this reason we need an extra
88      * initialization parameter to tell us whether we're the default servlet
89      * or not.
90      *
91      * Because the Slide WebdavServlet is usually deployed as default servlet,
92      * this setting defaults to <code>true</code>
93      */

94     protected boolean isDefaultServlet = true;
95     
96     
97     /**
98      * Default MIME type of resources.
99      */

100     protected String JavaDoc defaultMimeType = "text/plain";
101     
102     
103     /**
104      * Depth limit. To limit tree browsing when using depth = infinity.
105      */

106     protected int depthLimit = 3;
107     
108     
109     /**
110      * Scope parameter for servlet configuration.
111      * @see #getScope()
112      */

113     protected String JavaDoc scope = "";
114     
115     
116     /**
117      * Class name of the WebdavMethodFactory.
118      */

119     protected String JavaDoc methodFactory;
120     
121     /**
122      * Maps report-root-element-name -> implementing-class-name
123      */

124     protected Map JavaDoc externalReports = new HashMap JavaDoc();
125     
126     
127     // ----------------------------------------------------------- Construction
128

129     
130     /**
131      * Constructor.
132      *
133      * @param config the ServletConfig to wrap
134      */

135     public WebdavServletConfig(ServletConfig JavaDoc config) {
136         
137         this.config = config;
138         
139         ServletContext JavaDoc context = getServletContext();
140         String JavaDoc value = null;
141         
142         // read 'scope' parameter
143
value = getInitParameter(SCOPE_PARAMETER);
144         if (value == null) {
145             value = context.getInitParameter(SCOPE_PARAMETER);
146         }
147         if (value != null) {
148             scope = value;
149         }
150         
151         // read 'depth-limit' parameter
152
value = getInitParameter(DEPTH_LIMIT_PARAMETER);
153         if (value == null) {
154             value = context.getInitParameter(DEPTH_LIMIT_PARAMETER);
155         }
156         if (value != null) {
157             depthLimit = Integer.parseInt(value);
158         }
159         
160         // read 'default-mime-type' parameter
161
value = getInitParameter(DEFAULT_MIME_TYPE_PARAMETER);
162         if (value == null) {
163             value = context.getInitParameter(DEFAULT_MIME_TYPE_PARAMETER);
164         }
165         if (value != null) {
166             defaultMimeType = value;
167         }
168         
169         // read 'default-servlet' parameter
170
value = getInitParameter(DEFAULT_SERVLET_PARAMETER);
171         if (value != null) {
172             isDefaultServlet = Boolean.valueOf(value).booleanValue();
173         }
174         
175         // read 'method-factory' parameter
176
value = getInitParameter(METHOD_FACTORY_PARAMETER);
177         if (value == null) {
178             value = context.getInitParameter(METHOD_FACTORY_PARAMETER);
179         }
180         if (value != null) {
181             methodFactory = value;
182         }
183         
184         // read external reports parameter
185
try {
186             XMLValue xv = new XMLValue(getInitParameter("external-reports"));
187             Iterator JavaDoc i = xv.iterator();
188             while (i.hasNext()) {
189                 Object JavaDoc r = i.next();
190                 if (r instanceof Element && "report".equals(((Element)r).getName())) {
191                     
192                     String JavaDoc name = ((Element)r).getAttributeValue("name");
193                     String JavaDoc clsName = ((Element)r).getTextTrim();
194                     externalReports.put(name, clsName);
195                 }
196             }
197         }
198         catch (JDOMException e) {
199             e.printStackTrace();
200         }
201     }
202     
203     
204     // ------------------------------------------- ServletConfig Implementation
205

206     
207     /**
208      * Returns a String containing the value of the named initialization
209      * parameter, or null if the parameter does not exist.
210      *
211      * @param name a String specifying the name of the initialization parameter
212      * @return a String containing the value of the initialization parameter
213      */

214     public String JavaDoc getInitParameter(String JavaDoc name) {
215         
216         return config.getInitParameter(name);
217     }
218     
219     
220     /**
221      * Returns the names of the servlet's initialization parameters as an
222      * Enumeration of String objects, or an empty Enumeration if the servlet
223      * has no initialization parameters.
224      *
225      * @return an Enumeration of String objects containing the names of the
226      * servlet's initialization parameters
227      */

228     public Enumeration JavaDoc getInitParameterNames() {
229         
230         return config.getInitParameterNames();
231     }
232     
233     
234     /**
235      * Returns a reference to the ServletContext in which the caller
236      * is executing.
237      *
238      * @return a ServletContext object, used by the caller to interact with
239      * its servlet container
240      */

241     public ServletContext JavaDoc getServletContext() {
242         
243         return config.getServletContext();
244     }
245     
246     
247     /**
248      * Returns the name of this servlet instance.
249      * The name may be provided via server administration, assigned in the
250      * web application deployment descriptor, or for an unregistered (and thus
251      * unnamed) servlet instance it will be the servlet's class name.
252      *
253      * @return the name of the servlet instance
254      */

255     public String JavaDoc getServletName() {
256         
257         return config.getServletName();
258     }
259     
260     // --------------------------------------------------------- Public Methods
261

262     
263     /**
264      * Returns the default MIME type of resources, to be used for example when
265      * clients do not specify the content-type of files uploaded with the
266      * PUT method.
267      *
268      * @return the default MIME type (for example &quot;text/plain&quot;)
269      */

270     public String JavaDoc getDefaultMimeType() {
271         
272         return defaultMimeType;
273     }
274     
275     
276     /**
277      * Returns the depth limit. The depth limit should be used to limit the
278      * depth of (for instance) PROPFIND operations, in case the client has
279      * requested &quot;Depth: infinity&quot;
280      *
281      * @return the depth limit
282      */

283     public int getDepthLimit() {
284         
285         return depthLimit;
286     }
287     
288     
289     /**
290      * Returns the name of the WebdavMethodFactory class configured by the
291      * user, or the default class name if the corresponding initialization
292      * parameter 'method-factory' was not provided.
293      *
294      * @return class name of the method factory
295      */

296     public String JavaDoc getMethodFactory() {
297         
298         return methodFactory;
299     }
300     
301     
302     /**
303      * Returns the user defined scope in the namespace that shall be exposed
304      * by the WebdavServlet. This should be treated as root directory of the
305      * WebDAV namespace. By default this will be an empty string.
306      *
307      * @return the scope URI
308      */

309     public String JavaDoc getScope() {
310         
311         return scope;
312     }
313     
314     
315     /**
316      * Returns whether the servlet is mapped as default servlet of the web
317      * application.
318      *
319      * @returns true if the servlet is configured as default servlet of the
320      * context
321      */

322     public boolean isDefaultServlet() {
323         
324         return isDefaultServlet;
325     }
326     
327     /**
328      * Returns name of the implementing class of the specified report
329      *
330      * @param name the report root element name
331      *
332      * @return a String
333      *
334      */

335     public String JavaDoc getExternalReport(String JavaDoc name) {
336         return (String JavaDoc)externalReports.get(name);
337     }
338 }
339
340
Popular Tags