KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > common > actions > VelocityServlet


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.common.actions;
25
26 import java.util.HashMap JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.StringTokenizer JavaDoc;
30
31 import javax.servlet.ServletConfig JavaDoc;
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35 import javax.servlet.http.HttpSession JavaDoc;
36
37 import org.apache.velocity.Template;
38 import org.apache.velocity.context.Context;
39 import org.infoglue.cms.applications.common.Session;
40 import org.infoglue.cms.applications.common.VisualFormatter;
41 import org.infoglue.cms.exception.ConfigurationError;
42 import org.infoglue.cms.util.StringManager;
43 import org.infoglue.cms.util.StringManagerFactory;
44
45 import webwork.view.velocity.WebWorkVelocityServlet;
46
47 import com.opensymphony.module.propertyset.PropertySet;
48 import com.opensymphony.module.propertyset.PropertySetManager;
49
50 /**
51  *
52  * This class puts some things into the context object that you should
53  * be aware of (check the superclasses as well):
54  * <pre>
55  * "ui" - the StringManagerChain handling all localized strings.
56  * </pre>
57  *
58  * @author <a HREF="mailto:meat_for_the_butcher@yahoo.com">Patrik Nyborg</a>
59  */

60 public class VelocityServlet extends WebWorkVelocityServlet
61 {
62     private static final long serialVersionUID = 408929363112264207L;
63
64     private static final String JavaDoc PACKAGE_NAMES_INIT_PARAM = "packageNames";
65
66     private String JavaDoc packageNames[];
67
68     /**
69      * Performs initialization of this servlet. Called by the servlet container on loading.
70      *
71      * @param configuration The servlet configuration to apply.
72      *
73      * @exception ServletException
74      */

75     public void init(ServletConfig JavaDoc configuration) throws ServletException JavaDoc
76     {
77         super.init(configuration);
78         initializePackageNames(configuration.getInitParameter(PACKAGE_NAMES_INIT_PARAM));
79     }
80
81     /**
82      * @param commaSeparatedPackageNames comma-separareted list of package names.
83      */

84     private void initializePackageNames(String JavaDoc commaSeparatedPackageNames)
85     {
86         if (commaSeparatedPackageNames == null)
87         {
88             throw new ConfigurationError("web.xml not properly configured, did not contain the " + PACKAGE_NAMES_INIT_PARAM + " init param for the VelocityServlet.");
89         }
90
91         final StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(commaSeparatedPackageNames, ",");
92         this.packageNames = new String JavaDoc[st.countTokens()];
93         for (int i = 0; st.hasMoreTokens(); ++i)
94         {
95             this.packageNames[i] = st.nextToken();
96         }
97     }
98
99     /**
100      * @param locale
101      */

102     private StringManager getStringManagerChain(Locale JavaDoc locale)
103     {
104         return StringManagerFactory.getPresentationStringManager(this.packageNames, locale);
105     }
106
107     protected Template handleRequest(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, Context context) throws Exception JavaDoc
108     {
109         final HttpSession JavaDoc httpSession = request.getSession();
110         final Session JavaDoc session = new Session JavaDoc(httpSession);
111
112         
113         //<todo>this should definitely not be placed here
114
if(session.getLocale() == null || session.getLocale().getLanguage() == null || session.getLocale().getLanguage().equalsIgnoreCase(""))
115         {
116             session.setLocale(java.util.Locale.ENGLISH);
117         }
118         //</todo>
119

120         if(session.getLocale() == null || session.getLocale().getLanguage() == null || session.getLocale().getLanguage().equalsIgnoreCase(""))
121             context.put("ui", getStringManagerChain(java.util.Locale.ENGLISH));
122         else
123             context.put("ui", getStringManagerChain(session.getLocale()));
124         
125         context.put("formatter", new VisualFormatter());
126
127         request.setCharacterEncoding("UTF-8");
128         response.setContentType("text/html; charset=UTF-8");
129
130         return super.handleRequest(request, response, context);
131     }
132     
133     private String JavaDoc getPreferredLanguageCode(HttpServletRequest JavaDoc request)
134     {
135         Map JavaDoc args = new HashMap JavaDoc();
136         args.put("globalKey", "infoglue");
137         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
138         return ps.getString("principal_" + request.getRemoteUser() + "_languageCode");
139     }
140
141     private String JavaDoc getPreferredToolId(HttpServletRequest JavaDoc request)
142     {
143         Map JavaDoc args = new HashMap JavaDoc();
144         args.put("globalKey", "infoglue");
145         PropertySet ps = PropertySetManager.getInstance("jdbc", args);
146         
147         return ps.getString("principal_" + request.getRemoteUser() + "_defaultToolId");
148     }
149
150 }
Popular Tags