Source Code Cross Referenced for ArchiveStringTemplateHandler.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 java.io.IOException;
023:        import java.io.InputStream;
024:
025:        import org.antlr.stringtemplate.StringTemplate;
026:
027:        import com.sun.net.httpserver.HttpExchange;
028:
029:        import de.schlichtherle.io.File;
030:
031:        /**
032:         * Handler for StringTemplate requests from a resource in the classpath or
033:         * from an archive.
034:         * @see StringTemplateHandler
035:         * @see Templatable
036:         * @author Donald Munro
037:         */
038:        public class ArchiveStringTemplateHandler extends StringTemplateHandler
039:                implements  HttpHandleable, Postable, Templatable
040:        //==================================================================
041:        {
042:
043:            /**
044:             * Constructor with template package.
045:             * @param httpd - The Httpd instance
046:             * @param templateJavaPackage - The Java package where template processing 
047:             * classes are located.    
048:             * @param archiveFile The archive file or jar file.
049:             * @throws IOException
050:             */
051:            public ArchiveStringTemplateHandler(Httpd httpd,
052:                    String templateJavaPackage, java.io.File archiveFile)
053:                    throws IOException
054:            //----------------------------------------------------------------------
055:            {
056:                super (httpd, templateJavaPackage);
057:                File f = new File(archiveFile);
058:                m_templateGroup = new ArchiveStringTemplateGroup(
059:                        "StringTemplateCP", f);
060:            }
061:
062:            /**
063:             * Constructor with a template processor class that implements
064:             * the Templatable interface.     
065:             * @param httpd - The Httpd instance
066:             * @param templateProcessor - A Java class that implements the Templatable 
067:             * interface. 
068:             * @param archiveFile The archive file or jar file.
069:             * @throws IOException
070:             */
071:            public ArchiveStringTemplateHandler(Httpd httpd,
072:                    Templatable templateProcessor, java.io.File archiveFile)
073:                    throws IOException
074:            //----------------------------------------------------------------------
075:            {
076:                super (httpd, templateProcessor);
077:                m_templateGroup = new ArchiveStringTemplateGroup(
078:                        "StringTemplateCP", archiveFile);
079:            }
080:
081:            /**
082:             * Constructor with template package.
083:             * @param httpd - The Httpd instance
084:             * @param templateJavaPackage - The Java package where template processing 
085:             * classes are located.    
086:             * @param archiveFile The archive file or jar file.
087:             * @param archiveDir The directory within archiveFile where the content is found. 
088:             * @throws IOException
089:             */
090:            public ArchiveStringTemplateHandler(Httpd httpd,
091:                    String templateJavaPackage, java.io.File archiveFile,
092:                    String archiveDir) throws IOException
093:            //----------------------------------------------------------------------
094:            {
095:                super (httpd, templateJavaPackage);
096:                m_templateGroup = new ArchiveStringTemplateGroup(
097:                        "StringTemplateCP", archiveFile, archiveDir);
098:            }
099:
100:            /**
101:             * Constructor with a template processor class that implements
102:             * the Templatable interface.     
103:             * @param httpd - The Httpd instance
104:             * @param templateProcessor - A Java class that implements the Templatable 
105:             * interface. 
106:             * @param archiveFile The archive file or jar file.
107:             * @param archiveDir The directory within archiveFile where the content is found.
108:             * @throws IOException
109:             */
110:            public ArchiveStringTemplateHandler(Httpd httpd,
111:                    Templatable templateProcessor, java.io.File archiveFile,
112:                    String archiveDir) throws IOException
113:            //----------------------------------------------------------------------
114:            {
115:                super (httpd, templateProcessor);
116:                m_templateGroup = new ArchiveStringTemplateGroup(
117:                        "StringTemplateCP", archiveFile, archiveDir);
118:            }
119:
120:            @Override
121:            protected StringTemplate getTemplate(Request request)
122:            //--------------------------------------------------
123:            {
124:                String templateName = request.getName();
125:                File f = new File(templateName);
126:                String ext = Http.getExtension(f);
127:                int p = templateName.lastIndexOf(ext);
128:                if (p >= 0)
129:                    templateName = templateName.substring(0, p);
130:                StringTemplate template = null;
131:                String s = "";
132:                try {
133:                    s = request.getDir();
134:                    s += "/" + templateName;
135:                    template = m_templateGroup.getInstanceOf(s);
136:                } catch (Throwable e) {
137:                    try {
138:                        template = m_templateGroup.getInstanceOf(templateName);
139:                    } catch (Throwable ee) {
140:                        Httpd.Log(Httpd.LogLevel.ERROR,
141:                                "Error getting template instance:" + " Tried "
142:                                        + templateName + " and " + s, ee);
143:                        template = null;
144:                    }
145:
146:                }
147:                return template;
148:            }
149:
150:            @SuppressWarnings("unchecked")
151:            @Override
152:            protected Templatable getTemplateInstance(String templateName)
153:            //-----------------------------------------------------------
154:            {
155:                if (templateName.compareTo("--POST_GENERATED--") == 0)
156:                    return new TemplatableAdapter();
157:                String classPackage = m_templateJavaPackage;
158:                if (classPackage == null)
159:                    classPackage = "";
160:                classPackage = classPackage.trim();
161:                if (classPackage.length() > 0) {
162:                    if (!classPackage.endsWith("."))
163:                        classPackage += ".";
164:                }
165:
166:                int p = -1;
167:                p = templateName.lastIndexOf('/');
168:                if (p >= 0)
169:                    templateName = templateName.substring(p + 1);
170:                try {
171:                    String className = templateName;
172:                    Class C = _instantiateTemplateClass(classPackage
173:                            + templateName);
174:                    if (C == null) {
175:                        templateName = templateName.substring(0, 1)
176:                                .toUpperCase()
177:                                + templateName.substring(1).toLowerCase();
178:                        C = _instantiateTemplateClass(classPackage
179:                                + templateName);
180:                    }
181:                    if (C == null)
182:                        return null;
183:                    Templatable instance = (Templatable) C.newInstance();
184:                    return instance;
185:                } catch (InstantiationException e) {
186:                    Httpd.Log(Httpd.LogLevel.ERROR, "Class " + classPackage
187:                            + templateName + " could not be instantiated", e);
188:                    return null;
189:                } catch (ClassCastException e) {
190:                    Httpd.Log(Httpd.LogLevel.ERROR, "Class " + classPackage
191:                            + templateName
192:                            + " must implement the Templatable interface", e);
193:                    return null;
194:                } catch (Exception e) {
195:                    Httpd.Log(Httpd.LogLevel.ERROR, "Class " + classPackage
196:                            + templateName + " threw an exception", e);
197:                    return null;
198:                }
199:
200:            }
201:
202:            /**
203:             * Called to determine whether the resource from a request should be cached.
204:             * To implement user defined cacheing cache the file but return false and
205:             * also overide @see onGetCachedFile to return the cached file.
206:             * @param id Unique id
207:             * @param ex The exchange instance for the current HTTP transaction.
208:             * @param request The request instance
209:             * @return true to cache the result, false to not cache. 
210:             * StringTemplateHandler derived classes default to returning false to 
211:             * force no caching.
212:             */
213:            @Override
214:            public boolean onIsCacheable(long id, HttpExchange ex,
215:                    Request request)
216:            //---------------------------------------------------------------------
217:            {
218:                return m_isCacheable;
219:            }
220:
221:            @Override
222:            public File onGetCachedFile(long id, HttpExchange ex,
223:                    Request request)
224:            //--------------------------------------------------------------------
225:            {
226:                return null;
227:            }
228:
229:            @Override
230:            public boolean onPreServe(long id, HttpExchange ex, Request request)
231:            //------------------------------------------------------------------
232:            {
233:                return super .onPreServe(id, ex, request);
234:            }
235:
236:            @Override
237:            public HttpResponse onServeHeaders(long id, HttpExchange ex,
238:                    Request request)
239:            //--------------------------------------------------------------------------
240:            {
241:                return super .onServeHeaders(id, ex, request);
242:            }
243:
244:            @Override
245:            public InputStream onServeBody(long id, HttpExchange ex,
246:                    Request request)
247:            //-----------------------------------------------------------------------
248:            {
249:                return super .onServeBody(id, ex, request);
250:            }
251:
252:            @Override
253:            public void onPostServe(long id, HttpExchange ex, Request request,
254:                    boolean isOK)
255:            //-----------------------------------------------------------------
256:            {
257:                super .onPostServe(id, ex, request, isOK);
258:            }
259:
260:            @Override
261:            public Request onFileNotFound(long id, HttpExchange ex,
262:                    Request request)
263:            //----------------------------------------------------------------------
264:            {
265:                return super .onFileNotFound(id, ex, request);
266:            }
267:
268:            @Override
269:            public String onListDirectory(Request request)
270:            //--------------------------------------------
271:            {
272:                return super .onListDirectory(request);
273:            }
274:
275:            public Object onHandlePost(long id, HttpExchange ex,
276:                    Request request, HttpResponse response, File dir,
277:                    Object... extraParameters)
278:            //-----------------------------------------------------------------
279:            {
280:                return super .onHandlePost(id, ex, request, response, dir,
281:                        extraParameters);
282:            }
283:
284:            public java.io.File templateFile(StringTemplate template,
285:                    Request request, StringBuffer mimeType, java.io.File dir)
286:            //----------------------------------------------------------------
287:            {
288:                return super .templateFile(template, request, mimeType, dir);
289:            }
290:
291:            public String templateString(StringTemplate template,
292:                    Request request, StringBuffer mimeType)
293:            //--------------------------------------------------------------------
294:            {
295:                return super .templateString(template, request, mimeType);
296:            }
297:
298:            public InputStream templateStream(StringTemplate template,
299:                    Request request, StringBuffer mimeType)
300:            //-------------------------------------------------------------------------
301:            {
302:                return super.templateStream(template, request, mimeType);
303:            }
304:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.