Source Code Cross Referenced for DynamicPageServlet.java in  » Scripting » Pnuts » pnuts » servlet » 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 » Scripting » Pnuts » pnuts.servlet 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)DynamicPageServlet.java 1.2 04/12/06
003:         *
004:         * Copyright (c) 2003 Sun Microsystems, Inc. All Rights Reserved.
005:         *
006:         * See the file "LICENSE.txt" for information on usage and redistribution
007:         * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008:         */
009:        package pnuts.servlet;
010:
011:        import javax.servlet.http.HttpServletRequest;
012:        import javax.servlet.http.HttpServletResponse;
013:        import javax.servlet.ServletException;
014:        import javax.servlet.ServletConfig;
015:        import java.io.File;
016:        import java.io.FileInputStream;
017:        import java.io.InputStreamReader;
018:        import java.io.StringReader;
019:        import java.io.StringWriter;
020:        import java.io.Reader;
021:        import java.io.FileNotFoundException;
022:        import java.io.IOException;
023:        import java.net.URL;
024:        import java.util.HashSet;
025:        import java.util.Set;
026:        import pnuts.lang.Pnuts;
027:        import pnuts.lang.Runtime;
028:        import pnuts.lang.Context;
029:        import org.pnuts.servlet.protocol.pea.Handler;
030:
031:        /**
032:         * Dynamic Web page with embeded scripts.
033:         *
034:         * This servlet converts the dynamic page to a Pnuts script, when needed, then
035:         * forward to the script.
036:         *
037:         */
038:        public class DynamicPageServlet extends PnutsServlet {
039:
040:            private String workdir;
041:
042:            public void init() throws ServletException {
043:                ServletConfig conf = getServletConfig();
044:                String workdir = conf.getInitParameter("workdir");
045:                if (workdir != null) {
046:                    this .workdir = workdir;
047:                }
048:                super .init();
049:            }
050:
051:            protected Pnuts parseFile(File file, String encoding,
052:                    PnutsServletContext psc) throws IOException {
053:                Context context = psc.context;
054:                FileInputStream fin = new FileInputStream(file);
055:                Reader reader;
056:                if (encoding != null) {
057:                    reader = new InputStreamReader(fin, encoding);
058:                } else {
059:                    reader = Runtime.getScriptReader(fin, context);
060:                }
061:                StringWriter sw = new StringWriter();
062:                Set scriptURLs = new HashSet();
063:                scriptURLs.add(file.toURL());
064:                try {
065:                    DynamicPage.convert(reader, sw, null, null, context,
066:                            scriptURLs);
067:                    psc.scriptURLs = scriptURLs;
068:                } finally {
069:                    reader.close();
070:                }
071:                URL url = new URL(null, "pea:" + file.toURL(), new Handler(
072:                        context, scriptURLs));
073:                return Pnuts.parse(new StringReader(sw.toString()), url,
074:                        context);
075:            }
076:
077:            void do_service(HttpServletRequest request,
078:                    HttpServletResponse response) throws ServletException,
079:                    IOException {
080:                if (workdir == null) {
081:                    super .do_service(request, response);
082:                    return;
083:                }
084:                try {
085:                    String s_path = request.getServletPath();
086:                    String path = "";
087:                    if (s_path != null) {
088:                        path = s_path;
089:                    }
090:                    if (path == null) {
091:                        path = request.getPathInfo();
092:                    }
093:                    String real_path = getServletContext().getRealPath(path);
094:
095:                    File file = null;
096:                    if (real_path != null) {
097:                        file = new File(real_path);
098:                    }
099:                    if (file == null || !file.exists()) {
100:                        if (debug) {
101:                            throw new FileNotFoundException(real_path);
102:                        } else {
103:                            response
104:                                    .sendError(HttpServletResponse.SC_NOT_FOUND);
105:                            return;
106:                        }
107:                    }
108:                    String filename = file.getName();
109:                    if (filename.endsWith(".pnut")) {
110:                        response.sendError(
111:                                HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
112:                                "");
113:                        return;
114:                    }
115:                    int idx = filename.lastIndexOf('.');
116:                    String s;
117:                    if (idx > 0) {
118:                        s = filename.substring(0, idx) + ".pnut";
119:                    } else {
120:                        s = filename + ".pnut";
121:                    }
122:                    File dir = file.getParentFile();
123:                    if (workdir != null) {
124:                        dir = new File(dir, workdir);
125:                        if (!dir.exists()) {
126:                            dir.mkdirs();
127:                        }
128:                    }
129:                    File outputFile = new File(dir, s);
130:
131:                    if (!outputFile.exists()
132:                            || outputFile.lastModified() < file.lastModified()) {
133:                        DynamicPage.convert(file, outputFile, encoding,
134:                                new Context());
135:                    }
136:                    request.getRequestDispatcher(s).forward(request, response);
137:
138:                } catch (Throwable e) {
139:                    if (debug) {
140:                        e.printStackTrace();
141:                    } else {
142:                        System.err.println(e);
143:                    }
144:                    response.sendError(
145:                            HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "");
146:                }
147:            }
148:
149:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.