1 20 21 package com.methodhead.shim; 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.io.FilenameFilter ; 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 import java.text.Collator ; 32 33 import com.methodhead.sitecontext.SiteContextCapable; 34 import com.methodhead.sitecontext.SiteContext; 35 import javax.servlet.ServletContext ; 36 import javax.servlet.RequestDispatcher ; 37 import javax.servlet.ServletException ; 38 import javax.servlet.http.HttpServletRequest ; 39 import javax.servlet.http.HttpServletResponse ; 40 import org.apache.commons.lang.exception.ExceptionUtils; 41 42 47 public class Template 48 implements 49 SiteContextCapable { 50 51 53 55 57 60 private static class TemplateFilenameFilter 61 implements 62 FilenameFilter { 63 64 public boolean accept( 65 File dir, 66 String name ) { 67 68 return name.toLowerCase().endsWith( ".jsp" ); 69 } 70 } 71 72 74 78 public SiteContext getSiteContext() { 79 if ( siteContext_ == null ) 80 throw new ShimException( "Site context has not been set." ); 81 82 return siteContext_; 83 } 84 85 91 public void load( 92 HttpServletRequest request, 93 String template ) { 94 95 if ( ( template == null ) || ( template.equals( "" ) ) ) 99 throw new ShimException( "Invalid template \"" + template + "\"." ); 100 101 fileName_ = template; 105 106 String resource = 107 "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) + "/templates/" + 108 fileName_; 109 110 RequestDispatcher dispatcher = 111 request.getSession().getServletContext().getRequestDispatcher( resource ); 112 113 if ( dispatcher == null ) 114 throw new ShimException( 115 "Couldn't get request dispatcher for \"" + resource + "\"." ); 116 117 request.setAttribute( ShimGlobals.PANELMAP_KEY, new HashMap () ); 121 122 try { 126 dispatcher.include( request, new NullHttpServletResponse() ); 127 } 128 catch ( ServletException e ) { 129 throw new ShimException( 130 "Unexpected ServletException: " + e.getMessage() ); 131 } 132 catch ( IOException e ) { 133 throw new ShimException( 134 "Unexpected IOException: " + e.getMessage() ); 135 } 136 137 panelConfigs_ = ( Map )request.getAttribute( ShimGlobals.PANELMAP_KEY ); 138 139 request.removeAttribute( ShimGlobals.PANELMAP_KEY ); 143 } 144 145 150 public List getTemplateList( 151 HttpServletRequest request ) { 152 153 File templatesDir = 157 new File ( 158 request.getSession().getServletContext().getRealPath( 159 "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) + 160 "/templates" ) ); 161 162 if ( !templatesDir.exists() || !templatesDir.isDirectory() ) 163 throw new ShimException( 164 "Invalid templates directory \"" + templatesDir + "\"" ); 165 166 String [] fileNames = templatesDir.list( new TemplateFilenameFilter() ); 170 171 Arrays.sort( fileNames, Collator.getInstance() ); 175 176 return Arrays.asList( fileNames ); 180 } 181 182 185 public void include( 186 HttpServletRequest request, 187 HttpServletResponse response, 188 String template ) { 189 190 try { 191 RequestDispatcher dispatcher = 192 request.getSession().getServletContext().getRequestDispatcher( 193 "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) + 194 "/templates/" + template ); 195 196 dispatcher.include( request, response ); 197 } 198 catch ( Exception e ) { 199 throw new ShimException( 200 "Unexpected exception trying to include \"" + 201 "/WEB-INF/resources/" + getSiteContext().getInt( "id" ) + 202 "/templates/" + template + "\": " + 203 ExceptionUtils.getStackTrace( e ) ); 204 } 205 } 206 207 209 212 public void setSiteContext( 213 SiteContext siteContext ) { 214 siteContext_ = siteContext; 215 } 216 217 220 public String getFileName() { 221 return fileName_; 222 } 223 224 228 public Map getPanelConfigs() { 229 return panelConfigs_; 230 } 231 232 234 protected SiteContext siteContext_ = null; 235 236 protected String fileName_ = null; 237 238 protected Map panelConfigs_ = null; 239 } 240 | Popular Tags |