KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > j2biz > blogunity > web > servlet > VelocityViewServlet


1 /*
2  * $Id: VelocityViewServlet.java,v 1.12 2005/01/17 11:21:21 keyboardsamurai Exp $
3  *
4  * Copyright (c) 2004 j2biz Group, http://www.j2biz.com
5  * Koeln / Duesseldorf , Germany
6  *
7  * @author Max Kalina
8  *
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  *
24  */

25
26 package com.j2biz.blogunity.web.servlet;
27
28 import java.io.FileNotFoundException JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.servlet.ServletConfig JavaDoc;
36 import javax.servlet.ServletException JavaDoc;
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpServletResponse JavaDoc;
39 import javax.servlet.http.HttpSession JavaDoc;
40
41 import org.apache.commons.lang.StringUtils;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.apache.velocity.Template;
45 import org.apache.velocity.context.Context;
46 import org.apache.velocity.servlet.VelocityServlet;
47
48 import com.j2biz.blogunity.BlogunityManager;
49 import com.j2biz.blogunity.IConstants;
50 import com.j2biz.blogunity.exception.BlogunityException;
51 import com.j2biz.blogunity.i18n.I18N;
52 import com.j2biz.blogunity.i18n.I18NMessageManager;
53 import com.j2biz.blogunity.i18n.I18NStatusFactory;
54 import com.j2biz.blogunity.pojo.Blog;
55 import com.j2biz.blogunity.util.BlogUtils;
56 import com.j2biz.blogunity.web.IActionResult;
57
58 /**
59  * This servlet answers to all requests send to velocity-templates. It checks,
60  * what the velocity page was requested, merges it with the values found within
61  * the HttpRequest/HttpSession and includes resulted stream (String-instance at
62  * the moment) into the main theme's page: mainView.vm
63  *
64  * @author michelson
65  * @version $$
66  * @since 0.1
67  *
68  *
69  */

70 public class VelocityViewServlet extends VelocityServlet {
71
72     /**
73      * Comment for <code>serialVersionUID</code>
74      */

75     private static final long serialVersionUID = 3257854289644173364L;
76
77     private static final Log log = LogFactory.getLog(VelocityViewServlet.class);
78
79     private static final String JavaDoc MAIN_THEME_PAGE = "mainView.vm";
80
81     /*
82      * (non-Javadoc)
83      *
84      * @see org.apache.velocity.servlet.VelocityServlet#loadConfiguration(javax.servlet.ServletConfig)
85      */

86     protected Properties JavaDoc loadConfiguration(ServletConfig JavaDoc config) throws IOException JavaDoc,
87             FileNotFoundException JavaDoc {
88
89         Properties JavaDoc props = new Properties JavaDoc();
90         InputStream JavaDoc in = BlogunityManager.getSystemConfiguration().getVelocityPropertiesAsStream();
91         props.load(in);
92         return props;
93     }
94
95     /*
96      * (non-Javadoc)
97      *
98      * @see org.apache.velocity.servlet.VelocityServlet#handleRequest(javax.servlet.http.HttpServletRequest,
99      * javax.servlet.http.HttpServletResponse,
100      * org.apache.velocity.context.Context)
101      */

102     protected Template handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
103             Context context) throws Exception JavaDoc {
104
105         Blog b = (Blog) request.getAttribute("blog");
106         String JavaDoc blogName = b.getUrlName();
107
108         // create path to blog's theme directory relative to main "data"-dir,
109
// specified in SystemConfiguration
110
String JavaDoc blogThemeDirectory = "blogs/" + blogName + "/theme/";
111
112         if (StringUtils.isEmpty(blogName)) {
113             // re-pack exception into user-friendly one
114
BlogunityException ex = new BlogunityException(I18NStatusFactory.create(
115                     I18N.ERRORS.BLOG_NOT_FOUND, blogName));
116             throw new ServletException JavaDoc(ex);
117
118         }
119
120         // set theme directory for current request
121
// request.setAttribute(IConstants.Request.BLOG_THEME_DIRECTORY,
122
// BlogunityManager.getBase()
123
// + "/theme/" + blogName);
124

125         request.setAttribute(IConstants.Request.BLOG_THEME_DIRECTORY, BlogunityManager.getBase()
126                 + "/blogs/" + blogName + "/theme");
127
128         populateContext(request, response, context);
129
130         IActionResult forwardResult = (IActionResult) request
131                 .getAttribute(IConstants.Request.CURRENT_THEME_FILE);
132
133         String JavaDoc page = forwardResult.getPath();
134
135         if (page.charAt(0) == '/') page = page.substring(1);
136
137         Template t = getTemplate(blogThemeDirectory + page);
138         StringWriter JavaDoc sw = new StringWriter JavaDoc();
139         t.merge(context, sw);
140
141         // context.put(IConstants.Request.PAGE_CONTENT, sw.toString());
142
context.put(IConstants.Request.PAGE_CONTENT, sw.toString());
143
144         Template template = getTemplate(blogThemeDirectory + MAIN_THEME_PAGE);
145
146         return template;
147
148     }
149
150     /**
151      * Populates all attributes saved within HttpRequest or HttpSession to
152      * VelocityContext.
153      *
154      * @param request
155      * @param response
156      * @param context
157      */

158     private void populateContext(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response,
159             Context context) {
160         String JavaDoc element;
161         Enumeration JavaDoc enumer = request.getAttributeNames();
162         while (enumer.hasMoreElements()) {
163             element = (String JavaDoc) enumer.nextElement();
164             context.put(element, request.getAttribute(element));
165         }
166
167         HttpSession JavaDoc session = request.getSession(false);
168         if (session != null) {
169             enumer = session.getAttributeNames();
170             while (enumer.hasMoreElements()) {
171                 element = (String JavaDoc) enumer.nextElement();
172                 context.put(element, session.getAttribute(element));
173             }
174         }
175
176         context.put("ctx", request.getContextPath());
177         context.put("request", request);
178         context.put("response", response);
179         context.put("session", session);
180
181         // context.put("manager", new BlogunityManager());
182

183         context.put("utils", BlogUtils.getInstance());
184         context.put("i18n", I18NMessageManager.getInstance());
185         context.put("host", BlogunityManager.getHost());
186         context.put("base", BlogunityManager.getBase());
187     }
188
189 }
Popular Tags