Source Code Cross Referenced for FileStringTemplateHandler.java in  » Web-Server » HttpdBase4J » net » homeip » donaldm » httpdbase4j » 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 » HttpdBase4J » net.homeip.donaldm.httpdbase4j 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:        HttpdBase4J: An embeddable Java web server framework that supports HTTP, HTTPS, 
003:        templated content and serving content from inside a jar or archive.
004:        Copyright (C) 2007 Donald Munro
005:
006:        This library is free software; you can redistribute it and/or
007:        modify it under the terms of the GNU Lesser General Public
008:        License as published by the Free Software Foundation; either
009:        version 2.1 of the License, or (at your option) any later version.
010:
011:        This library 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 GNU
014:        Lesser General Public License for more details.
015:
016:        You should have received a copy of the GNU Lesser General Public
017:        License along with this library; if not,see http://www.gnu.org/licenses/lgpl.txt
018:         */
019:
020:        package net.homeip.donaldm.httpdbase4j;
021:
022:        import com.sun.net.httpserver.HttpExchange;
023:        import java.io.File;
024:        import java.io.IOException;
025:        import java.io.InputStream;
026:        import org.antlr.stringtemplate.StringTemplate;
027:        import org.antlr.stringtemplate.StringTemplateGroup;
028:
029:        /**
030:         * Handler for StringTemplate requests from a local filesystem.
031:         * @see StringTemplateHandler
032:         * @see Templatable
033:         * @author Donald Munro
034:         */
035:        public class FileStringTemplateHandler extends StringTemplateHandler
036:                implements  HttpHandleable, Postable, Templatable
037:        //==================================================================
038:        {
039:            /**
040:             * Constructor with template package.
041:             * @param httpd - The Httpd instance
042:             * @param templateJavaPackage - The Java package where template processing 
043:             * classes are located.
044:             * @throws IOException
045:             */
046:            public FileStringTemplateHandler(Httpd httpd,
047:                    String templateJavaPackage) throws IOException
048:            //----------------------------------------------------------------------
049:            {
050:                super (httpd, templateJavaPackage);
051:                m_templateGroup = new StringTemplateGroup("StringTemplateDir",
052:                        m_httpd.getHomePath());
053:            }
054:
055:            /**
056:             * Constructor with a template processor class that implements
057:             * the Templatable interface.     
058:             * @param httpd - The Httpd instance
059:             * @param templateProcessor - A Java class that implements the Templatable 
060:             * interface. 
061:             * @throws IOException
062:             */
063:            public FileStringTemplateHandler(Httpd httpd,
064:                    Templatable templateProcessor) throws IOException
065:            //----------------------------------------------------------------------
066:            {
067:                super (httpd, templateProcessor);
068:                m_templateGroup = new StringTemplateGroup("StringTemplateDir",
069:                        m_httpd.getHomePath());
070:            }
071:
072:            /**
073:             *  @inheritDoc
074:             */
075:            @Override
076:            protected StringTemplate getTemplate(Request request)
077:            //--------------------------------------------------
078:            {
079:                String templateName = request.getName();
080:                File f = new File(templateName);
081:                String ext = Http.getExtension(f);
082:                int p = templateName.lastIndexOf(ext);
083:                if (p >= 0)
084:                    templateName = templateName.substring(0, p);
085:                StringTemplate template = null;
086:                if (request.exists())
087:                    template = m_templateGroup.getInstanceOf(templateName);
088:                return template;
089:            }
090:
091:            /**
092:             *  @inheritDoc
093:             */
094:            @SuppressWarnings("unchecked")
095:            @Override
096:            protected Templatable getTemplateInstance(String templateName)
097:            //-----------------------------------------------------------
098:            {
099:                if (templateName.compareTo("--POST_GENERATED--") == 0)
100:                    return new TemplatableAdapter();
101:                String classPackage = m_templateJavaPackage;
102:                if (classPackage == null)
103:                    classPackage = "";
104:                classPackage = classPackage.trim();
105:                if (classPackage.length() > 0) {
106:                    if (!classPackage.endsWith("."))
107:                        classPackage += ".";
108:                }
109:
110:                int p = -1;
111:                p = templateName.lastIndexOf(File.separatorChar);
112:                if (p == -1)
113:                    p = templateName.lastIndexOf('/');
114:                if (p >= 0)
115:                    templateName = templateName.substring(p + 1);
116:                try {
117:                    String className = templateName;
118:                    Class C = _instantiateTemplateClass(classPackage
119:                            + templateName);
120:
121:                    if (C == null) { // Try first letter uppercased 
122:                        templateName = templateName.substring(0, 1)
123:                                .toUpperCase()
124:                                + templateName.substring(1).toLowerCase();
125:                        C = _instantiateTemplateClass(classPackage
126:                                + templateName);
127:                    }
128:                    if (C == null)
129:                        return null;
130:                    Templatable instance = (Templatable) C.newInstance();
131:                    return instance;
132:                } catch (InstantiationException e) {
133:                    Httpd.Log(Httpd.LogLevel.ERROR, "Class " + classPackage
134:                            + templateName + " could not be instantiated", e);
135:                    return null;
136:                } catch (ClassCastException e) {
137:                    Httpd.Log(Httpd.LogLevel.ERROR, "Class " + classPackage
138:                            + templateName
139:                            + " must implement the Templatable interface", e);
140:                    return null;
141:                } catch (Exception e) {
142:                    Httpd.Log(Httpd.LogLevel.ERROR, "Class " + classPackage
143:                            + templateName + " threw an exception", e);
144:                    return null;
145:                }
146:
147:            }
148:
149:            /**
150:             * Called to determine whether the resource from a request should be cached.
151:             * To implement user defined cacheing cache the file but return false and
152:             * also overide @see onGetCachedFile to return the cached file.
153:             * @param id Unique id
154:             * @param ex The exchange instance for the current HTTP transaction.
155:             * @param request The request instance
156:             * @return true to cache the result, false to not cache. 
157:             * StringTemplateHandler derived classes default to returning false to 
158:             * force no caching.
159:             */
160:            public boolean onIsCacheable(long id, HttpExchange ex,
161:                    Request request)
162:            //---------------------------------------------------------------------
163:            {
164:                return m_isCacheable;
165:            }
166:
167:            @Override
168:            public File onGetCachedFile(long id, HttpExchange ex,
169:                    Request request)
170:            //--------------------------------------------------------------------
171:            {
172:                return null;
173:            }
174:
175:            @Override
176:            public boolean onPreServe(long id, HttpExchange ex, Request request)
177:            //------------------------------------------------------------------
178:            {
179:                return super .onPreServe(id, ex, request);
180:            }
181:
182:            @Override
183:            public HttpResponse onServeHeaders(long id, HttpExchange ex,
184:                    Request request)
185:            //--------------------------------------------------------------------------
186:            {
187:                return super .onServeHeaders(id, ex, request);
188:            }
189:
190:            @Override
191:            public InputStream onServeBody(long id, HttpExchange ex,
192:                    Request request)
193:            //-----------------------------------------------------------------------
194:            {
195:                return super .onServeBody(id, ex, request);
196:            }
197:
198:            @Override
199:            public void onPostServe(long id, HttpExchange ex, Request request,
200:                    boolean isOK)
201:            //-----------------------------------------------------------------
202:            {
203:                super .onPostServe(id, ex, request, isOK);
204:            }
205:
206:            @Override
207:            public Request onFileNotFound(long id, HttpExchange ex,
208:                    Request request)
209:            //----------------------------------------------------------------------
210:            {
211:                return super .onFileNotFound(id, ex, request);
212:            }
213:
214:            @Override
215:            public String onListDirectory(Request request)
216:            //--------------------------------------------
217:            {
218:                return super .onListDirectory(request);
219:            }
220:
221:            @Override
222:            public Object onHandlePost(long id, HttpExchange ex,
223:                    Request request, HttpResponse response, File dir,
224:                    Object... extraParameters)
225:            //-----------------------------------------------------------------
226:            {
227:                return super .onHandlePost(id, ex, request, response, dir,
228:                        extraParameters);
229:            }
230:
231:            @Override
232:            public File templateFile(StringTemplate template, Request request,
233:                    StringBuffer mimeType, File dir)
234:            //----------------------------------------------------------------
235:            {
236:                return super .templateFile(template, request, mimeType, dir);
237:            }
238:
239:            @Override
240:            public String templateString(StringTemplate template,
241:                    Request request, StringBuffer mimeType)
242:            //--------------------------------------------------------------------
243:            {
244:                return super .templateString(template, request, mimeType);
245:            }
246:
247:            @Override
248:            public InputStream templateStream(StringTemplate template,
249:                    Request request, StringBuffer mimeType)
250:            //-------------------------------------------------------------------------
251:            {
252:                return super.templateStream(template, request, mimeType);
253:            }
254:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.