KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > net > GroovyTemplateServlet


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.net;
26
27 import groovy.text.SimpleTemplateEngine;
28 import groovy.text.Template;
29 import org.snipsnap.app.Application;
30 import org.snipsnap.container.Components;
31 import org.snipsnap.snip.SnipSpace;
32
33 import javax.servlet.ServletException JavaDoc;
34 import javax.servlet.http.HttpServlet JavaDoc;
35 import javax.servlet.http.HttpServletRequest JavaDoc;
36 import javax.servlet.http.HttpServletResponse JavaDoc;
37 import java.io.BufferedReader JavaDoc;
38 import java.io.IOException JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.io.InputStreamReader JavaDoc;
41
42 public class GroovyTemplateServlet extends HttpServlet JavaDoc {
43
44   SimpleTemplateEngine templateEngine = new SimpleTemplateEngine();
45
46   protected void service(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
47
48     String JavaDoc groovyFile = (String JavaDoc) request.getAttribute("javax.servlet.include.path_info");
49     if (null == groovyFile) {
50       groovyFile = request.getPathInfo();
51     }
52
53     if (groovyFile.startsWith("/")) {
54       groovyFile = groovyFile.substring(1);
55     }
56
57     String JavaDoc templateSource = getTemplateSource(groovyFile);
58     try {
59       Template groovyTemplate = templateEngine.createTemplate(templateSource);
60       groovyTemplate.make(Application.get().getParameters()).writeTo(response.getWriter());
61     } catch (Exception JavaDoc e) {
62       e.printStackTrace();
63     }
64   }
65
66   /**
67    * Read the template source from either a snip or if not existent try the
68    * jar/classpath based file read.
69    *
70    * @param name the name of the resource to load
71    * @return a string with the template source
72    * @throws IOException
73    */

74   private String JavaDoc getTemplateSource(String JavaDoc name) throws IOException JavaDoc {
75     SnipSpace space = (SnipSpace) Components.getComponent(SnipSpace.class);
76     if (space.exists(name)) {
77       return space.load(name).getContent();
78     }
79
80     InputStream JavaDoc resource = getClass().getResourceAsStream(name);
81     if (null != resource) {
82       // if there is no snip to load, try jar/classpath based file read
83
BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(resource));
84       StringBuffer JavaDoc content = new StringBuffer JavaDoc();
85       char buffer[] = new char[1024];
86       int length = 0;
87       while ((length = reader.read(buffer)) != -1) {
88         content.append(buffer, 0, length);
89       }
90       return content.toString();
91     }
92
93     throw new IOException JavaDoc("unable to load template source: '" + name + "'");
94   }
95 }
96
Popular Tags