001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.shim;
022:
023: import java.io.File;
024: import java.io.IOException;
025: import java.io.FilenameFilter;
026: import java.util.List;
027: import java.util.ArrayList;
028: import java.util.Arrays;
029: import java.util.Map;
030: import java.util.HashMap;
031: import java.text.Collator;
032:
033: import com.methodhead.sitecontext.SiteContextCapable;
034: import com.methodhead.sitecontext.SiteContext;
035: import javax.servlet.ServletContext;
036: import javax.servlet.RequestDispatcher;
037: import javax.servlet.ServletException;
038: import javax.servlet.http.HttpServletRequest;
039: import javax.servlet.http.HttpServletResponse;
040: import org.apache.commons.lang.exception.ExceptionUtils;
041:
042: /**
043: * A class to access and profile Shim templates. Template configuration
044: * information is embedded in templates themselves, and this class helps access
045: * that information.
046: */
047: public class Template implements SiteContextCapable {
048:
049: // constructors /////////////////////////////////////////////////////////////
050:
051: // constants ////////////////////////////////////////////////////////////////
052:
053: // classes //////////////////////////////////////////////////////////////////
054:
055: /**
056: * Returns <tt>true</tt> if file name ends in ".jsp".
057: */
058: private static class TemplateFilenameFilter implements
059: FilenameFilter {
060:
061: public boolean accept(File dir, String name) {
062:
063: return name.toLowerCase().endsWith(".jsp");
064: }
065: }
066:
067: // methods //////////////////////////////////////////////////////////////////
068:
069: /**
070: * Returns the current site context for this object, getting the default
071: * context if it has not been set.
072: */
073: public SiteContext getSiteContext() {
074: if (siteContext_ == null)
075: throw new ShimException("Site context has not been set.");
076:
077: return siteContext_;
078: }
079:
080: /**
081: * Loads template information from <tt>template</tt>. <tt>template</tt>
082: * should be a simple file name. The template's actual location is
083: * calculated using the resource root and site context. An exception is
084: * thrown if the resource root hasn't been set.
085: */
086: public void load(HttpServletRequest request, String template) {
087:
088: //
089: // make sure we got a valid template name.
090: //
091: if ((template == null) || (template.equals("")))
092: throw new ShimException("Invalid template \"" + template
093: + "\".");
094:
095: //
096: // get a dispatcher for the template
097: //
098: fileName_ = template;
099:
100: String resource = "/WEB-INF/resources/"
101: + getSiteContext().getInt("id") + "/templates/"
102: + fileName_;
103:
104: RequestDispatcher dispatcher = request.getSession()
105: .getServletContext().getRequestDispatcher(resource);
106:
107: if (dispatcher == null)
108: throw new ShimException(
109: "Couldn't get request dispatcher for \"" + resource
110: + "\".");
111:
112: //
113: // put an empty panel map in the request
114: //
115: request.setAttribute(ShimGlobals.PANELMAP_KEY, new HashMap());
116:
117: //
118: // include the template
119: //
120: try {
121: dispatcher.include(request, new NullHttpServletResponse());
122: } catch (ServletException e) {
123: throw new ShimException("Unexpected ServletException: "
124: + e.getMessage());
125: } catch (IOException e) {
126: throw new ShimException("Unexpected IOException: "
127: + e.getMessage());
128: }
129:
130: panelConfigs_ = (Map) request
131: .getAttribute(ShimGlobals.PANELMAP_KEY);
132:
133: //
134: // take the panel map out of the request (this affects PanelTag)
135: //
136: request.removeAttribute(ShimGlobals.PANELMAP_KEY);
137: }
138:
139: /**
140: * Returns a list of available templates sorted in alphabetic,
141: * case-insensitive order. Templates are JSP files located in
142: * <tt>WEB-INF/resources/<i>sitecontext_id</i>/templates</tt>.
143: */
144: public List getTemplateList(HttpServletRequest request) {
145:
146: //
147: // verify the resources directory
148: //
149: File templatesDir = new File(request.getSession()
150: .getServletContext().getRealPath(
151: "/WEB-INF/resources/"
152: + getSiteContext().getInt("id")
153: + "/templates"));
154:
155: if (!templatesDir.exists() || !templatesDir.isDirectory())
156: throw new ShimException("Invalid templates directory \""
157: + templatesDir + "\"");
158:
159: //
160: // get file list
161: //
162: String[] fileNames = templatesDir
163: .list(new TemplateFilenameFilter());
164:
165: //
166: // sort the file list
167: //
168: Arrays.sort(fileNames, Collator.getInstance());
169:
170: //
171: // return the list
172: //
173: return Arrays.asList(fileNames);
174: }
175:
176: /**
177: * Includes <tt>template</tt> in <tt>response</tt>.
178: */
179: public void include(HttpServletRequest request,
180: HttpServletResponse response, String template) {
181:
182: try {
183: RequestDispatcher dispatcher = request.getSession()
184: .getServletContext().getRequestDispatcher(
185: "/WEB-INF/resources/"
186: + getSiteContext().getInt("id")
187: + "/templates/" + template);
188:
189: dispatcher.include(request, response);
190: } catch (Exception e) {
191: throw new ShimException(
192: "Unexpected exception trying to include \""
193: + "/WEB-INF/resources/"
194: + getSiteContext().getInt("id")
195: + "/templates/" + template + "\": "
196: + ExceptionUtils.getStackTrace(e));
197: }
198: }
199:
200: // properties ///////////////////////////////////////////////////////////////
201:
202: /**
203: * Implements <tt>SiteContextCapable.setSiteContext()</tt>.
204: */
205: public void setSiteContext(SiteContext siteContext) {
206: siteContext_ = siteContext;
207: }
208:
209: /**
210: * Returns the name of the template. This is only set when {#load} is called.
211: */
212: public String getFileName() {
213: return fileName_;
214: }
215:
216: /**
217: * Returns the panel configs of the template. This is only set when {#load}
218: * is called.
219: */
220: public Map getPanelConfigs() {
221: return panelConfigs_;
222: }
223:
224: // attributes ///////////////////////////////////////////////////////////////
225:
226: protected SiteContext siteContext_ = null;
227:
228: protected String fileName_ = null;
229:
230: protected Map panelConfigs_ = null;
231: }
|