Source Code Cross Referenced for ResourceServlet.java in  » Workflow-Engines » wfmopen-2.1.1 » de » danet » an » util » web » 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 » Workflow Engines » wfmopen 2.1.1 » de.danet.an.util.web 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file is part of the WfMOpen project.
003:         * Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
004:         * All rights reserved.
005:         *
006:         * This program is free software; you can redistribute it and/or modify
007:         * it under the terms of the GNU General Public License as published by
008:         * the Free Software Foundation; either version 2 of the License, or
009:         * (at your option) any later version.
010:         *
011:         * This program is distributed in the hope that it will be useful,
012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014:         * GNU General Public License for more details.
015:         *
016:         * You should have received a copy of the GNU General Public License
017:         * along with this program; if not, write to the Free Software
018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
019:         *
020:         * $Id: ResourceServlet.java,v 1.2 2006/09/29 12:32:08 drmlipp Exp $
021:         *
022:         * $Log: ResourceServlet.java,v $
023:         * Revision 1.2  2006/09/29 12:32:08  drmlipp
024:         * Consistently using WfMOpen as projct name now.
025:         *
026:         * Revision 1.1.1.1  2004/08/18 15:17:35  drmlipp
027:         * Update to 1.2
028:         *
029:         * Revision 1.3  2004/03/24 15:15:02  lipp
030:         * Added development support feature.
031:         *
032:         * Revision 1.2  2004/03/15 12:52:33  lipp
033:         * Changed resource retrieval.
034:         *
035:         * Revision 1.1  2004/03/04 13:37:45  lipp
036:         * Moved resource servlet up.
037:         *
038:         * Revision 1.3  2004/03/01 21:15:01  lipp
039:         * Returns result now.
040:         *
041:         * Revision 1.2  2004/02/27 14:39:00  lipp
042:         * Progressing.
043:         *
044:         * Revision 1.1  2004/02/26 15:46:44  lipp
045:         * Renamed package.
046:         *
047:         * Revision 1.1  2004/02/25 16:51:58  lipp
048:         * Getting started.
049:         *
050:         */
051:        package de.danet.an.util.web;
052:
053:        import java.io.File;
054:        import java.io.IOException;
055:        import java.io.InputStream;
056:        import java.io.OutputStream;
057:
058:        import java.net.URL;
059:        import java.net.URLConnection;
060:
061:        import javax.servlet.ServletConfig;
062:        import javax.servlet.ServletContext;
063:        import javax.servlet.ServletException;
064:        import javax.servlet.http.HttpServlet;
065:        import javax.servlet.http.HttpServletRequest;
066:        import javax.servlet.http.HttpServletResponse;
067:
068:        /**
069:         * This class provides a servlet for accessing resources. The servlet
070:         * uses the extra path info, prepends a configurable prefix, and looks
071:         * up a resource with the resulting name. The prefix defaults to
072:         * "<code>web-resource</code>". It can be altered by setting the
073:         * initialization parameter "<code>lookupPrefix</code>".<P>
074:         *
075:         * If the requested resource is found, the resource (i.e. its content)
076:         * is returned. The content type of the response is derived from the
077:         * resource's suffix. If the resource cannot be found, error 404 is
078:         * returned.<P>
079:         *
080:         * To facilitate development, an initialization parameter
081:         * "<code>resourceDirectory</code>" may be set. If set, the servlet
082:         * will first look for the requested resource in the specified
083:         * directory (not prepending the lookup prefix!).
084:         *
085:         * @author <a href="mailto:mnl@mnl.de">Michael N. Lipp</a>
086:         * @version $Revision: 1.2 $
087:         */
088:
089:        public class ResourceServlet extends HttpServlet {
090:
091:            private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
092:                    .getLog(ResourceServlet.class);
093:
094:            // getServletConfig does not work, let's remember it ourselves
095:            private ServletConfig myServletConfig = null;
096:            private String lookupPrefix = "web-resource";
097:            private String resourceDirectory = null;
098:
099:            /**
100:             * Default init method.
101:             *
102:             * @param servletConfig a <code>ServletConfig</code> value
103:             * @exception ServletException if an error occurs
104:             */
105:            public void init(ServletConfig servletConfig)
106:                    throws ServletException {
107:                myServletConfig = servletConfig;
108:                String prefix = servletConfig.getInitParameter("lookupPrefix");
109:                if (prefix != null) {
110:                    lookupPrefix = prefix;
111:                }
112:                resourceDirectory = servletConfig
113:                        .getInitParameter("resourceDirectory");
114:            }
115:
116:            /**
117:             * Lookup the requested resource and return it.
118:             *
119:             * @param request a <code>HttpServletRequest</code> value
120:             * @param response a <code>HttpServletResponse</code> value
121:             * @exception ServletException if an error occurs
122:             * @exception IOException if an error occurs
123:             */
124:            public void doGet(HttpServletRequest request,
125:                    HttpServletResponse response) throws ServletException,
126:                    IOException {
127:                String xtra = request.getPathInfo();
128:                if (xtra == null || xtra.length() == 0) {
129:                    response.sendError(HttpServletResponse.SC_NOT_FOUND,
130:                            "No resource specified");
131:                    return;
132:                }
133:                String res = "/" + lookupPrefix + xtra;
134:                ServletContext ctx = myServletConfig.getServletContext();
135:                URL resUrl = null;
136:                if (resourceDirectory != null) {
137:                    File file = new File(resourceDirectory + xtra);
138:                    logger.debug("Trying request for \"" + xtra + "\" as \""
139:                            + file + "\"");
140:                    if (file.exists()) {
141:                        logger.debug("Request for \"" + xtra
142:                                + "\" resolved as \"" + file + "\"");
143:                        resUrl = file.toURL();
144:                    }
145:                }
146:                if (resUrl == null) {
147:                    if (logger.isDebugEnabled()) {
148:                        logger.debug("Request for \"" + xtra
149:                                + "\", will be resolved as \"" + res + "\"");
150:                    }
151:                    resUrl = ctx.getResource(res);
152:                }
153:                if (resUrl == null) {
154:                    response.sendError(HttpServletResponse.SC_NOT_FOUND,
155:                            "No such resource: " + res);
156:                    return;
157:                }
158:                if (logger.isDebugEnabled()) {
159:                    logger.debug("Retrieving from \"" + resUrl.toExternalForm()
160:                            + "\"");
161:                }
162:                URLConnection conn = resUrl.openConnection();
163:                int length = conn.getContentLength();
164:                if (logger.isDebugEnabled()) {
165:                    logger.debug("Content length of \""
166:                            + resUrl.toExternalForm() + "\" is " + length);
167:                }
168:                if (length >= 0) {
169:                    response.setContentLength(length);
170:                }
171:                String mimeType = ctx.getMimeType(res);
172:                if (mimeType != null) {
173:                    if (logger.isDebugEnabled()) {
174:                        logger.debug("Mime type of response is " + mimeType);
175:                    }
176:                    response.setContentType(mimeType);
177:                }
178:                InputStream in = conn.getInputStream();
179:                OutputStream out = response.getOutputStream();
180:                byte[] buf = new byte[2048];
181:                while (true) {
182:                    int cnt = in.read(buf);
183:                    if (cnt < 0) {
184:                        break;
185:                    }
186:                    out.write(buf, 0, cnt);
187:                }
188:                out.close();
189:            }
190:
191:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.