KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > webdav > util > DirectoryIndexGenerator


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/webdav/server/org/apache/slide/webdav/util/DirectoryIndexGenerator.java,v 1.8.2.3 2004/12/11 04:22:39 masonjm Exp $
3  * $Revision: 1.8.2.3 $
4  * $Date: 2004/12/11 04:22:39 $
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.util;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.text.DateFormat JavaDoc;
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31 import java.util.Enumeration JavaDoc;
32
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 import org.apache.slide.common.NamespaceAccessToken;
37 import org.apache.slide.common.SlideException;
38 import org.apache.slide.common.SlideToken;
39 import org.apache.slide.content.Content;
40 import org.apache.slide.content.NodeRevisionDescriptor;
41 import org.apache.slide.content.NodeRevisionDescriptors;
42 import org.apache.slide.lock.Lock;
43 import org.apache.slide.lock.NodeLock;
44 import org.apache.slide.security.NodePermission;
45 import org.apache.slide.security.Security;
46 import org.apache.slide.structure.ObjectNode;
47 import org.apache.slide.structure.Structure;
48 import org.apache.slide.util.Messages;
49 import org.apache.slide.webdav.WebdavServletConfig;
50
51 /**
52  * Utility class that encapsulates the generation of HTML directory index
53  * pages.
54  *
55  * @version $Revision: 1.8.2.3 $
56  */

57 public class DirectoryIndexGenerator {
58     
59     
60     // -------------------------------------------------------------- Constants
61

62     
63     /**
64      * HTTP Date format pattern (RFC 2068, 822, 1123).
65      */

66     public static final String JavaDoc DATE_FORMAT =
67         "EEE, d MMM yyyy kk:mm:ss z";
68     
69     
70     /**
71      * Date formatter.
72      */

73     private static final DateFormat JavaDoc formatter =
74         new SimpleDateFormat JavaDoc(DATE_FORMAT);
75     
76     
77     // ----------------------------------------------------- Instance Variables
78

79     
80     /**
81      * Access token to the namespace.
82      */

83     protected NamespaceAccessToken nat;
84     
85     
86     /**
87      * Configuration of the WebDAV servlet.
88      */

89     protected WebdavServletConfig config;
90     
91     
92     // ----------------------------------------------------------- Constructors
93

94     
95     /**
96      * Constructor.
97      *
98      * @param nat the namespace access token
99      */

100     public DirectoryIndexGenerator(NamespaceAccessToken nat,
101                                    WebdavServletConfig config) {
102         
103         if (nat == null) {
104             throw new IllegalArgumentException JavaDoc(
105                 "NamespaceAccessToken must not be null");
106         }
107         this.nat = nat;
108         if (config == null) {
109             throw new IllegalArgumentException JavaDoc(
110                 "WebdavServletConfig must not be null");
111         }
112         this.config = config;
113     }
114     // Little helpers to create character refrences
115
private String JavaDoc stringToCharacterRef(String JavaDoc val) {
116         StringBuffer JavaDoc result = new StringBuffer JavaDoc(val.length() * 8);
117         for (int i = 0; i < val.length(); i++) {
118             result.append(charToCharacterRef(val.charAt(i)));
119         }
120         return result.toString();
121     }
122     
123     private String JavaDoc charToCharacterRef(char val) {
124         StringBuffer JavaDoc result = new StringBuffer JavaDoc(8);
125         result.append("&#x").append(Integer.toHexString((int) val).toUpperCase()).append(";");
126         return result.toString();
127     }
128     
129     
130     // --------------------------------------------------------- Public Methods
131

132     
133     /**
134      * Display a directory browsing page.
135      *
136      * @param req the HTTP request
137      * @param res the HTTP response
138      * @throw IOException if an IO exception occurrs while writing the
139      * response
140      * @throw SlideException if an exception occurrs accessing Slide
141      */

142     public void generate(HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res, SlideToken slideToken)
143         throws IOException JavaDoc, SlideException {
144         
145         res.setContentType("text/html; charset=\"UTF-8\"");
146         
147         // get the helpers
148
Content content = nat.getContentHelper();
149         Lock lock = nat.getLockHelper();
150         Security security = nat.getSecurityHelper();
151         Structure structure = nat.getStructureHelper();
152         
153 // SlideToken slideToken = WebdavUtils.getSlideToken(req);
154
String JavaDoc resourcePath = WebdavUtils.getRelativePath(req, config);
155         ObjectNode object = structure.retrieve(slideToken, resourcePath);
156         String JavaDoc name = object.getUri();
157         
158         // Number of characters to trim from the beginnings of filenames
159
int trim = name.length();
160         if (!name.endsWith("/"))
161             trim += 1;
162         if (name.equals("/"))
163             trim = 1;
164         
165         PrintWriter JavaDoc writer = new PrintWriter JavaDoc(res.getWriter());
166         
167         // Render the page header
168
writer.print("<html>\r\n");
169         writer.print("<head>\r\n");
170         writer.print("<meta http-equiv=\"Content-type\" content=\"text/html; charset=UTF-8\" >\r\n");
171         writer.print("</meta>\r\n");
172         writer.print("<title>");
173         writer.print
174             (Messages.format
175                  ("org.apache.slide.webdav.GetMethod.directorylistingfor",
176                   name));
177         writer.print("</title>\r\n</head>\r\n");
178         writer.print("<body bgcolor=\"white\">\r\n");
179         writer.print("<table width=\"90%\" cellspacing=\"0\"" +
180                          " cellpadding=\"5\" align=\"center\">\r\n");
181         
182         // Render the in-page title
183
writer.print("<tr><td colspan=\"3\"><font size=\"+2\">\r\n<strong>");
184         writer.print
185             (Messages.format
186                  ("org.apache.slide.webdav.GetMethod.directorylistingfor",
187                   name));
188         writer.print("</strong>\r\n</font></td></tr>\r\n");
189         
190         // Render the link to our parent (if required)
191
String JavaDoc parentDirectory = name;
192         if (parentDirectory.endsWith("/")) {
193             parentDirectory =
194                 parentDirectory.substring(0, parentDirectory.length() - 1);
195         }
196         String JavaDoc scope = config.getScope();
197         parentDirectory = parentDirectory.substring(scope.length());
198         if (parentDirectory.lastIndexOf("/") >= 0) {
199             parentDirectory = parentDirectory.substring(0, parentDirectory.lastIndexOf("/"));
200             writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">\r\n");
201             writer.print("<a HREF=\"");
202             writer.print(WebdavUtils.getAbsolutePath(scope, req, config));
203             if (parentDirectory.equals(""))
204                 parentDirectory = "/";
205             writer.print(parentDirectory); // I18N chars
206
writer.print("\">");
207             writer.print(Messages.format
208                              ("org.apache.slide.webdav.GetMethod.parent",
209                               parentDirectory));
210             writer.print("</a>\r\n");
211             writer.print("</td></tr>\r\n");
212         }
213         
214         Enumeration JavaDoc permissionsList = null;
215         Enumeration JavaDoc locksList = null;
216         try {
217             permissionsList =
218                 security.enumeratePermissions(slideToken, object.getUri());
219             locksList = lock.enumerateLocks(slideToken, object.getUri(), false);
220         } catch (SlideException e) {
221             // Any security based exception will be trapped here
222
// Any locking based exception will be trapped here
223
}
224         
225         // Displaying ACL info
226
if (org.apache.slide.util.Configuration.useIntegratedSecurity()) {
227             displayPermissions(permissionsList, writer, false);
228         }
229         
230         // Displaying lock info
231
displayLocks(locksList, writer, false);
232         
233         writer.print("<tr><td colspan=\"5\" bgcolor=\"#ffffff\">");
234         writer.print("&nbsp;");
235         writer.print("</td></tr>\r\n");
236         
237         // Render the column headings
238
writer.print("<tr bgcolor=\"#cccccc\">\r\n");
239         writer.print("<td align=\"left\" colspan=\"3\">");
240         writer.print("<font size=\"+1\"><strong>");
241         writer.print(Messages.message
242                          ("org.apache.slide.webdav.GetMethod.filename"));
243         writer.print("</strong></font></td>\r\n");
244         writer.print("<td align=\"center\"><font size=\"+1\"><strong>");
245         writer.print(Messages.message
246                          ("org.apache.slide.webdav.GetMethod.size"));
247         writer.print("</strong></font></td>\r\n");
248         writer.print("<td align=\"right\"><font size=\"+1\"><strong>");
249         writer.print(Messages.message
250                          ("org.apache.slide.webdav.GetMethod.lastModified"));
251         writer.print("</strong></font></td>\r\n");
252         writer.print("</tr>\r\n");
253         
254         Enumeration JavaDoc resources = structure.getChildren(slideToken,object);
255         boolean shade = false;
256         
257         while (resources.hasMoreElements()) {
258             String JavaDoc currentResource = ((ObjectNode)resources.nextElement()).getPath().toString();
259             NodeRevisionDescriptor currentDescriptor = null;
260             permissionsList = null;
261             locksList = null;
262             try {
263                 NodeRevisionDescriptors revisionDescriptors =
264                     content.retrieve(slideToken, currentResource);
265                 // Retrieve latest revision descriptor
266
currentDescriptor =
267                     content.retrieve(slideToken, revisionDescriptors);
268             } catch (SlideException e) {
269                 // Silent exception : Objects without any revision are
270
// considered collections, and do not have any attributes
271
// Any security based exception will be trapped here
272
// Any locking based exception will be trapped here
273
}
274             
275             try {
276                 permissionsList =
277                     security.enumeratePermissions(slideToken, currentResource);
278                 locksList = lock.enumerateLocks(slideToken, currentResource, false);
279             } catch (SlideException e) {
280                 // Any security based exception will be trapped here
281
// Any locking based exception will be trapped here
282
}
283             
284             String JavaDoc trimmed = currentResource.substring(trim);
285             if (trimmed.equalsIgnoreCase("WEB-INF") ||
286                 trimmed.equalsIgnoreCase("META-INF")) {
287                 continue;
288             }
289             
290             writer.print("<tr");
291             if (shade) {
292                 writer.print(" bgcolor=\"dddddd\"");
293             } else {
294                 writer.print(" bgcolor=\"eeeeee\"");
295             }
296             writer.print(">\r\n");
297             shade = !shade;
298             
299             writer.print("<td align=\"left\" colspan=\"3\">&nbsp;&nbsp;\r\n");
300             writer.print("<a HREF=\"");
301             writer.print(WebdavUtils.getAbsolutePath(currentResource, req, config));
302             writer.print("\"><tt>");
303             writer.print(stringToCharacterRef(trimmed)); // I18N chars
304
if (WebdavUtils.isCollection(currentDescriptor)) {
305                 writer.print("/");
306             }
307             writer.print("</tt></a></td>\r\n");
308             
309             writer.print("<td align=\"right\"><tt>");
310             if (WebdavUtils.isCollection(currentDescriptor)) {
311                 writer.print("&nbsp;");
312             }
313             else {
314                 writer.print(renderSize(currentDescriptor.getContentLength()));
315             }
316             writer.print("</tt></td>\r\n");
317             
318             writer.print("<td align=\"right\"><tt>");
319             if (currentDescriptor != null) {
320                 writer.print(currentDescriptor.getLastModified());
321             } else {
322                 writer.print("&nbsp;");
323             }
324             writer.print("</tt></td>\r\n");
325             
326             writer.print("</tr>\r\n");
327             
328             // Displaying ACL info
329
if (org.apache.slide.util.Configuration.useIntegratedSecurity()) {
330                 displayPermissions(permissionsList, writer, shade);
331             }
332             
333             // Displaying lock info
334
displayLocks(locksList, writer, shade);
335         }
336         
337         // Render the page footer
338
writer.print("<tr><td colspan=\"5\">&nbsp;</td></tr>\r\n");
339         writer.print("<tr><td colspan=\"3\" bgcolor=\"#cccccc\">");
340         writer.print("<font size=\"-1\">");
341         writer.print(Messages.message
342                          ("org.apache.slide.webdav.GetMethod.version"));
343         writer.print("</font></td>\r\n");
344         writer.print("<td colspan=\"2\" align=\"right\" bgcolor=\"#cccccc\">");
345         writer.print("<font size=\"-1\">");
346         writer.print(formatter.format(new Date JavaDoc()));
347         writer.print("</font></td></tr>\r\n");
348         writer.print("</table>\r\n");
349         writer.print("</body>\r\n");
350         writer.print("</html>\r\n");
351         
352         // Return an input stream to the underlying bytes
353
writer.flush();
354     }
355     
356     
357     // ------------------------------------------------------ Protected Methods
358

359     
360     /**
361      * Display an ACL list.
362      *
363      * @param permissionsList the list of NodePermission objects
364      * @param writer the output will be appended to this writer
365      * @param shade whether the row should be displayed darker
366      */

367     protected void displayPermissions(Enumeration JavaDoc permissionsList,
368                                       PrintWriter JavaDoc writer,
369                                       boolean shade)
370         throws IOException JavaDoc {
371         
372         boolean hideAcl = false;
373         String JavaDoc hideAclStr = config.getInitParameter( "directory-browsing-hide-acl" );
374         if( "true".equalsIgnoreCase(hideAclStr) )
375             hideAcl = true;
376         
377         if( !hideAcl ) {
378             if ((permissionsList != null) && (permissionsList.hasMoreElements())) {
379                 writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
380                                           : " bgcolor=\"dddddd\"") +
381                                  ">\r\n");
382                 writer.print("<td align=\"left\" colspan=\"5\"><tt><b>");
383                 writer.print(Messages.message
384                                  ("org.apache.slide.webdav.GetMethod.aclinfo"));
385                 writer.print("</b></tt></td>\r\n");
386                 writer.print("</tr>\r\n");
387                 writer.print("<tr");
388                 if (!shade) {
389                     writer.print(" bgcolor=\"dddddd\"");
390                 } else {
391                     writer.print(" bgcolor=\"eeeeee\"");
392                 }
393                 writer.print(">\r\n");
394                 writer.print("<td align=\"left\" colspan=\"2\"><tt><b>");
395                 writer.print(Messages.message
396                                  ("org.apache.slide.webdav.GetMethod.subject"));
397                 writer.print("</b></tt></td>\r\n");
398                 writer.print("<td align=\"left\"><tt><b>");
399                 writer.print(Messages.message
400                                  ("org.apache.slide.webdav.GetMethod.action"));
401                 writer.print("</b></tt></td>\r\n");
402                 writer.print("<td align=\"right\"><tt><b>");
403                 writer.print(Messages.message
404                                  ("org.apache.slide.webdav.GetMethod.inheritable"));
405                 writer.print("</b></tt></td>\r\n");
406                 writer.print("<td align=\"right\"><tt><b>");
407                 writer.print(Messages.message
408                                  ("org.apache.slide.webdav.GetMethod.deny"));
409                 writer.print("</b></tt></td>\r\n");
410                 writer.print("</tr>\r\n");
411                 
412                 while (permissionsList.hasMoreElements()) {
413                     writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
414                                               : " bgcolor=\"dddddd\"") +
415                                      ">\r\n");
416                     NodePermission currentPermission =
417                         (NodePermission) permissionsList.nextElement();
418                     writer.print("<td align=\"left\" colspan=\"2\"><tt>");
419                     writer.print(currentPermission.getSubjectUri());
420                     writer.print("</tt></td>\r\n");
421                     writer.print("<td align=\"left\"><tt>");
422                     writer.print(currentPermission.getActionUri());
423                     writer.print("</tt></td>\r\n");
424                     writer.print("<td align=\"right\"><tt>");
425                     writer.print(currentPermission.isInheritable());
426                     writer.print("</tt></td>\r\n");
427                     writer.print("<td align=\"right\"><tt>");
428                     writer.print(currentPermission.isNegative());
429                     writer.print("</tt></td>\r\n");
430                     writer.print("</tr>\r\n");
431                 }
432             }
433         }
434     }
435     
436     
437     /**
438      * Display a lock list.
439      *
440      * @param permissionsList the list of NodePermission objects
441      * @param writer the output will be appended to this writer
442      * @param shade whether the row should be displayed darker
443      */

444     protected void displayLocks(Enumeration JavaDoc locksList, PrintWriter JavaDoc writer,
445                                 boolean shade)
446         throws IOException JavaDoc {
447         
448         boolean hideLocks = false;
449         String JavaDoc hideLocksStr = config.getInitParameter( "directory-browsing-hide-locks" );
450         if( "true".equalsIgnoreCase(hideLocksStr) )
451             hideLocks = true;
452         
453         if( !hideLocks ) {
454             if ((locksList != null) && (locksList.hasMoreElements())) {
455                 writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
456                                           : " bgcolor=\"dddddd\"") +
457                                  ">\r\n");
458                 writer.print("<td align=\"left\" colspan=\"5\"><tt><b>");
459                 writer.print(Messages.message
460                                  ("org.apache.slide.webdav.GetMethod.locksinfo"));
461                 writer.print("</b></tt></td>\r\n");
462                 writer.print("</tr>\r\n");
463                 writer.print("<tr");
464                 if (!shade) {
465                     writer.print(" bgcolor=\"dddddd\"");
466                 } else {
467                     writer.print(" bgcolor=\"eeeeee\"");
468                 }
469                 writer.print(">\r\n");
470                 writer.print("<td align=\"left\"><tt><b>");
471                 writer.print(Messages.message
472                                  ("org.apache.slide.webdav.GetMethod.subject"));
473                 writer.print("</b></tt></td>\r\n");
474                 writer.print("<td align=\"left\"><tt><b>");
475                 writer.print(Messages.message
476                                  ("org.apache.slide.webdav.GetMethod.type"));
477                 writer.print("</b></tt></td>\r\n");
478                 writer.print("<td align=\"right\"><tt><b>");
479                 writer.print(Messages.message
480                                  ("org.apache.slide.webdav.GetMethod.expiration"));
481                 writer.print("</b></tt></td>\r\n");
482                 writer.print("<td align=\"right\"><tt><b>");
483                 writer.print(Messages.message
484                                  ("org.apache.slide.webdav.GetMethod.inheritable"));
485                 writer.print("</b></tt></td>\r\n");
486                 writer.print("<td align=\"right\"><tt><b>");
487                 writer.print(Messages.message
488                                  ("org.apache.slide.webdav.GetMethod.exclusive"));
489                 writer.print("</b></tt></td>\r\n");
490                 writer.print("</tr>\r\n");
491                 
492                 while (locksList.hasMoreElements()) {
493                     writer.print("<tr" + (shade ? " bgcolor=\"eeeeee\""
494                                               : " bgcolor=\"dddddd\"") +
495                                      ">\r\n");
496                     NodeLock currentLock = (NodeLock) locksList.nextElement();
497                     writer.print("<td align=\"left\"><tt>");
498                     writer.print(currentLock.getSubjectUri());
499                     writer.print("</tt></td>\r\n");
500                     writer.print("<td align=\"left\"><tt>");
501                     writer.print(currentLock.getTypeUri());
502                     writer.print("</tt></td>\r\n");
503                     writer.print("<td align=\"right\"><tt>");
504                     writer.print
505                         (formatter.format(currentLock.getExpirationDate()));
506                     writer.print("</tt></td>\r\n");
507                     writer.print("<td align=\"right\"><tt>");
508                     writer.print(currentLock.isInheritable());
509                     writer.print("</tt></td>\r\n");
510                     writer.print("<td align=\"right\"><tt>");
511                     writer.print(currentLock.isExclusive());
512                     writer.print("</tt></td>\r\n");
513                 }
514             }
515         }
516     }
517     
518     
519     /**
520      * Render the specified file size (in bytes).
521      *
522      * @param size File size (in bytes)
523      */

524     protected String JavaDoc renderSize(long size) {
525         
526         long leftSide = size / 1024;
527         long rightSide = (size % 1024) / 103; // Makes 1 digit
528
if ((leftSide == 0) && (rightSide == 0) && (size > 0))
529             rightSide = 1;
530         
531         return ("" + leftSide + "." + rightSide + " kb");
532     }
533     
534     
535 }
536
537
Popular Tags