Source Code Cross Referenced for DirectoryHandler.java in  » Web-Server » Brazil » sunlabs » brazil » handler » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Server » Brazil » sunlabs.brazil.handler 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * DirectoryHandler.java
003:         *
004:         * Brazil project web application Framework,
005:         * export version: 1.1 
006:         * Copyright (c) 1999-2000 Sun Microsystems, Inc.
007:         *
008:         * Sun Public License Notice
009:         *
010:         * The contents of this file are subject to the Sun Public License Version 
011:         * 1.0 (the "License"). You may not use this file except in compliance with 
012:         * the License. A copy of the License is included as the file "license.terms",
013:         * and also available at http://www.sun.com/
014:         * 
015:         * The Original Code is from:
016:         *    Brazil project web application Framework release 1.1.
017:         * The Initial Developer of the Original Code is: suhler.
018:         * Portions created by suhler are Copyright (C) Sun Microsystems, Inc.
019:         * All Rights Reserved.
020:         * 
021:         * Contributor(s): cstevens, suhler.
022:         *
023:         * Version:  1.13
024:         * Created by suhler on 99/03/29
025:         * Last modified by suhler on 00/12/11 20:23:36
026:         */
027:
028:        package sunlabs.brazil.handler;
029:
030:        import sunlabs.brazil.util.http.HttpUtil;
031:
032:        import sunlabs.brazil.server.Handler;
033:        import sunlabs.brazil.server.Request;
034:        import sunlabs.brazil.server.Server;
035:        import sunlabs.brazil.server.FileHandler;
036:        import java.io.IOException;
037:        import java.io.File;
038:        import java.util.Vector;
039:        import sunlabs.brazil.util.Sort;
040:
041:        /**
042:         * Handler for directory lists.
043:         * This is a bare-bones handler for providing directory listings
044:         * for web servers.
045:         * It is designed to be placed after the
046:         * {@link sunlabs.brazil.server.FileHandler}.  If no index file is found, 
047:         * Then a simple directory listing will be produced.  Only files whose
048:         * extensions are in the mime properties will be listed.
049:         * <br>
050:         * NOTE: This handler is obsolete, as it provides no control over the
051:         *	format of the directory listing. 
052:         *	 Use the {@link sunlabs.brazil.template.DirectoryTemplate} instead.
053:         * <p>
054:         * Configuration properties used:
055:         * <dl class=props>
056:         * <dt>prefix	<dd>An initial prefix for urls that may provide listings
057:         * <dt>DirectoryName	<dd>This property is set by the
058:         *			{@link sunlabs.brazil.server.FileHandler} if the
059:         *			URL it was passed resolves to a directory, but no
060:         *			index file (e.g. index.html) was found.
061:         * <dt>setProps		<dd>If present, no content is returned.  Instead, 
062:         *			The properties "Directories" and "Files" are
063:         *			set in the request properties, so the format of
064:         *			the output may be generated dynamically.
065:         *			[Note: This feature is deprecated, use the
066:         *			{@link sunlabs.brazil.template.DirectoryTemplate}
067:         *			instead].
068:         * <dt>delim		<dd>The delimeter separating the file names. 
069:         *			Defaults to a single space.
070:         * <dt>mime.xxx		<dd>Only documents ending in ".xxx" are considered.
071:         *			more than on mime.xxx parameters may be specified.
072:         * </dl>
073:         *
074:         * @author		Stephen Uhler
075:         * @version		1.13, 00/12/11
076:         */
077:
078:        public class DirectoryHandler implements  Handler {
079:            private Server server;
080:            private String propsPrefix;
081:            private String urlPrefix;
082:            private static final String PREFIX = "prefix"; // url prefix
083:
084:            /**
085:             * Get the url prefix for this handler.
086:             */
087:
088:            public boolean init(Server server, String prefix) {
089:                this .server = server;
090:                propsPrefix = prefix;
091:                urlPrefix = server.props.getProperty(propsPrefix + PREFIX, "/");
092:                return true;
093:            }
094:
095:            /**
096:             * Display files in a directory, after being rejected by the 
097:             * FileHandler.   The output is very simple.
098:             */
099:
100:            public boolean respond(Request request) throws IOException {
101:                String directory = request.props.getProperty("DirectoryName");
102:                if (directory == null || !request.url.startsWith(urlPrefix)) {
103:                    return false;
104:                }
105:
106:                String[] list = (new File(directory)).list();
107:                Vector files = new Vector();
108:                Vector dirs = new Vector();
109:
110:                if (list != null) {
111:                    for (int i = 0; i < list.length; i++) {
112:                        String name = list[i];
113:                        int index = name.lastIndexOf(".");
114:                        if ((new File(directory, name)).isDirectory()) {
115:                            dirs.addElement(name);
116:                        } else if (index > 0
117:                                && server.props.containsKey(FileHandler.MIME
118:                                        + name.substring(index))) {
119:                            files.addElement(name);
120:                        }
121:                    }
122:                }
123:
124:                /*
125:                 * Just set properties.  We should be more flexible here.  This
126:                 * doesn't work with embedded spaces for now!
127:                 */
128:
129:                if (request.props.getProperty(propsPrefix + "setProps") != null) {
130:                    String delim = request.props.getProperty(propsPrefix
131:                            + "delim", " ");
132:                    StringBuffer dir = new StringBuffer();
133:                    for (int i = 0; i < dirs.size(); i++) {
134:                        if (i > 0) {
135:                            dir.append(delim);
136:                        }
137:                        dir.append((String) dirs.elementAt(i));
138:                    }
139:                    StringBuffer file = new StringBuffer();
140:                    for (int i = 0; i < files.size(); i++) {
141:                        if (i > 0) {
142:                            file.append(delim);
143:                        }
144:                        file.append((String) files.elementAt(i));
145:                    }
146:                    request.props.put("Directories", dir.toString());
147:                    request.props.put("Files", file.toString());
148:                    return false;
149:                }
150:
151:                /*
152:                 * simple output for now
153:                 */
154:
155:                StringBuffer result = new StringBuffer();
156:                result.append("<title>Directory Listing</title>\n");
157:                result.append("<h1>Directory Listing</h1>\n");
158:                result.append("<a href=..><b>parent directory</b></a>\n");
159:                if (dirs.size() > 0) {
160:                    Sort.qsort(dirs);
161:                    result.append("<h2>Directories</h2>\n");
162:                    list(dirs, result);
163:                }
164:                if (files.size() > 0) {
165:                    Sort.qsort(files);
166:                    result.append("<h2>Files</h2>\n");
167:                    list(files, result);
168:                }
169:                request.sendResponse(result.toString());
170:                return true;
171:            }
172:
173:            /*
174:             * List the elements of the directory in a row
175:             */
176:
177:            private void list(Vector v, StringBuffer result) {
178:                for (int i = 0; i < v.size(); i++) {
179:                    String name = (String) v.elementAt(i);
180:                    result.append("<a href=\"" + name + " \">"
181:                            + HttpUtil.htmlEncode(name) + "</a><br>\n");
182:                }
183:            }
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.