1 48 49 package org.jpublish.servlet; 50 51 import java.io.IOException ; 52 import java.io.OutputStream ; 53 54 import javax.servlet.ServletConfig ; 55 import javax.servlet.ServletContext ; 56 import javax.servlet.ServletException ; 57 import javax.servlet.http.HttpServlet ; 58 import javax.servlet.http.HttpServletRequest ; 59 import javax.servlet.http.HttpServletResponse ; 60 import javax.servlet.http.HttpSession ; 61 62 import com.anthonyeden.lib.util.MessageUtilities; 63 import org.apache.commons.logging.Log; 64 import org.apache.commons.logging.LogFactory; 65 import org.jpublish.EntityNotFoundException; 66 import org.jpublish.JPublishEngine; 67 import org.jpublish.RequestContext; 68 import org.jpublish.SiteContext; 69 import org.jpublish.util.PathUtilities; 70 import org.jpublish.util.URLUtilities; 71 import org.jpublish.util.encoding.CharacterEncodingMap; 72 import org.jpublish.util.mime.MimeTypeMap; 73 74 83 84 public class JPublishServlet extends HttpServlet { 85 86 private static final Log log = LogFactory.getLog(JPublishServlet.class); 87 88 private JPublishEngine engine = null; 89 private SiteContext siteContext = null; 90 91 97 98 public void init(ServletConfig servletConfig) throws 99 ServletException { 100 super.init(servletConfig); 101 log.info("Initializing JPublish servlet"); 102 103 ServletContext servletContext = getServletContext(); 104 105 String configLocation = servletConfig.getInitParameter("config"); 106 if (log.isDebugEnabled()) { 107 log.debug("Config location: " + configLocation); 108 } 109 110 try { 112 log.debug("Creating site context"); 113 siteContext = new SiteContext(servletContext); 114 siteContext.loadConfiguration(configLocation); 115 116 log.debug("Creating JPublish engine"); 117 engine = new JPublishEngine(siteContext); 118 } catch (Throwable e) { 119 log.error("Error creating SiteContext: " + e.getMessage()); 120 throw new ServletException (e); 121 } 122 123 log.info("JPublish servlet initialized."); 124 } 125 126 129 130 public void destroy() { 131 engine.destroy(); 132 } 133 134 142 143 public void doGet(HttpServletRequest request, HttpServletResponse response) 144 throws ServletException , IOException { 145 doPost(request, response); 146 } 147 148 156 157 public void doPost(HttpServletRequest request, HttpServletResponse response) 158 throws ServletException , IOException { 159 String path = PathUtilities.getRealPath(siteContext, 161 request.getPathInfo()); 162 if (log.isDebugEnabled()) { 163 log.debug("Real path: " + path); 164 } 165 166 if (siteContext.getPathDispatcher().canDispatch(path)) { 168 169 String dispatcherName = siteContext.getPathDispatcher().canForwardDispatch(path); 171 if (dispatcherName != null) { 172 siteContext.getPathDispatcher().forward(request, response, dispatcherName); 173 } 174 175 dispatcherName = siteContext.getPathDispatcher().canIncludeDispatch(path); 176 if (dispatcherName != null) { 177 siteContext.getPathDispatcher().include(request, response, dispatcherName); 178 } 179 180 return; 181 } 182 183 log.debug("Creating session facade"); 185 HttpSession session = new HttpSessionFacade(request); 186 if (log.isDebugEnabled()) { 187 log.debug("Session facade: " + session); 188 } 189 190 CharacterEncodingMap characterEncodingMap = 192 siteContext.getCharacterEncodingManager().getMap(path); 193 194 if (requireVersion(2, 3)) { 196 request.setCharacterEncoding(characterEncodingMap.getRequestEncoding()); 197 } 198 199 int lastDotIndex = path.lastIndexOf("."); 201 if (lastDotIndex >= 0) { 202 String extension = path.substring(lastDotIndex + 1); 203 String mimeType = siteContext.getMimeTypeMap().getMimeType(extension); 204 String contentType = getMimeTypeWithCharset(mimeType, 205 characterEncodingMap.getResponseEncoding()); 206 response.setContentType(contentType); 207 if (log.isDebugEnabled()) { 208 log.debug("Content type for extension " + extension + " is " + 209 contentType); 210 } 211 } else { 212 response.setContentType(getMimeTypeWithCharset(MimeTypeMap.DEFAULT_MIME_TYPE, 213 characterEncodingMap.getResponseEncoding())); 214 log.debug("No extension found, using default content type."); 215 } 216 217 221 239 240 RequestContext context = RequestContext.getRequestContext(); 242 context.clear(); 244 log.debug("Cleared the context"); 245 246 context.put("path", path); 248 context.put("request", request); 249 context.put("response", response); 250 log.debug("Putting session in the context"); 251 context.put("session", session); 252 context.put("application", getServletContext()); 253 254 context.put("characterEncodingMap", characterEncodingMap); 256 257 context.put("urlUtilities", new URLUtilities(request, response)); 259 260 request.setAttribute("context", context); 262 263 OutputStream out = response.getOutputStream(); 264 try { 265 engine.render(out); 266 267 String redirect = context.getRedirect(); 268 if (redirect != null) { 269 response.sendRedirect(redirect); 272 } 273 274 } catch (EntityNotFoundException e) { 275 Object [] args = {e.getMessage()}; 276 String msg = MessageUtilities.getMessage(getClass(), 277 JPublishEngine.MESSAGE_PACKAGE, "fileNotFound", args); 278 log.error(msg); 279 if (!response.isCommitted()) { 280 response.sendError(HttpServletResponse.SC_NOT_FOUND, msg); 281 } 282 } catch (Throwable e) { 283 Object [] args = {e.getMessage()}; 284 String msg = MessageUtilities.getMessage(getClass(), 285 JPublishEngine.MESSAGE_PACKAGE, "errorRenderingPage", args); 286 log.error(msg); 287 if (!response.isCommitted()) { 288 throw new ServletException ("Error rendering page", e); 289 } 290 } finally { 291 296 } 297 } 298 299 306 307 private String getMimeTypeWithCharset(String mimeType, String charSet) { 308 313 StringBuffer buffer = new StringBuffer (); 314 buffer.append(mimeType); 315 buffer.append("; charset="); 316 buffer.append(charSet); 317 return buffer.toString(); 318 } 319 320 327 328 private boolean requireVersion(int majorVersion, int minorVersion) { 329 ServletContext servletContext = getServletContext(); 330 return ((servletContext.getMajorVersion() > majorVersion) || 331 (servletContext.getMajorVersion() == majorVersion && 332 servletContext.getMinorVersion() >= minorVersion) 333 ); 334 } 335 336 } 337 | Popular Tags |