JSPDispatcher.java [plain text]
package org.blojsom.dispatcher;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.blojsom.BlojsomException;
import org.blojsom.filter.PermalinkFilter;
import org.blojsom.blog.BlogUser;
import org.blojsom.blog.BlojsomConfiguration;
import org.blojsom.util.BlojsomUtils;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
public class JSPDispatcher implements BlojsomDispatcher {
private Log _logger = LogFactory.getLog(JSPDispatcher.class);
private ServletContext _context;
private String _templatesDirectory;
private String _baseConfigurationDirectory;
public JSPDispatcher() {
}
public void init(ServletConfig servletConfig, BlojsomConfiguration blojsomConfiguration) throws BlojsomException {
_context = servletConfig.getServletContext();
_baseConfigurationDirectory = blojsomConfiguration.getBaseConfigurationDirectory();
_templatesDirectory = blojsomConfiguration.getTemplatesDirectory();
_logger.debug("Using templates directory: " + _templatesDirectory);
_logger.debug("Initialized JSP dispatcher");
}
public void dispatch(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse,
BlogUser user,
Map context,
String flavorTemplate,
String flavorContentType)
throws IOException, ServletException {
httpServletResponse.setContentType(flavorContentType);
if (!flavorTemplate.startsWith("/")) {
flavorTemplate = '/' + flavorTemplate;
}
String flavorTemplateForPage = null;
if (BlojsomUtils.getRequestValue(PAGE_PARAM, httpServletRequest) != null) {
flavorTemplateForPage = BlojsomUtils.getTemplateForPage(flavorTemplate, BlojsomUtils.getRequestValue(PAGE_PARAM, httpServletRequest));
_logger.debug("Retrieved template for page: " + flavorTemplateForPage);
}
Iterator contextIterator = context.keySet().iterator();
while (contextIterator.hasNext()) {
String contextKey = (String) contextIterator.next();
httpServletRequest.setAttribute(contextKey, context.get(contextKey));
}
if (httpServletRequest instanceof PermalinkFilter.PermalinkRequest) {
PermalinkFilter.PermalinkRequest permalinkRequest = (PermalinkFilter.PermalinkRequest) httpServletRequest;
permalinkRequest.setPathInfo(null);
}
if (flavorTemplateForPage != null) {
String templateToLoad = _baseConfigurationDirectory + user.getId() + _templatesDirectory + BlojsomUtils.removeInitialSlash(flavorTemplateForPage);
if (_context.getResource(templateToLoad) != null) {
httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
httpServletResponse.getWriter().flush();
_logger.debug("Dispatched to flavor page template for user: " + templateToLoad);
return;
} else {
templateToLoad = _baseConfigurationDirectory + BlojsomUtils.removeInitialSlash(_templatesDirectory) + BlojsomUtils.removeInitialSlash(flavorTemplateForPage);
if (_context.getResource(templateToLoad) != null) {
httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
httpServletResponse.getWriter().flush();
_logger.debug("Dispatched to flavor page template: " + templateToLoad);
return;
} else {
_logger.error("Unable to dispatch to flavor page template: " + templateToLoad);
}
}
} else {
String templateToLoad = _baseConfigurationDirectory + user.getId() + _templatesDirectory + BlojsomUtils.removeInitialSlash(flavorTemplate);
if (_context.getResource(templateToLoad) != null) {
httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
httpServletResponse.getWriter().flush();
_logger.debug("Dispatched to flavor template for user: " + templateToLoad);
return;
} else {
templateToLoad = _baseConfigurationDirectory + BlojsomUtils.removeInitialSlash(_templatesDirectory) + BlojsomUtils.removeInitialSlash(flavorTemplate);
if (_context.getResource(templateToLoad) != null) {
httpServletRequest.getRequestDispatcher(templateToLoad).forward(httpServletRequest, httpServletResponse);
httpServletResponse.getWriter().flush();
_logger.debug("Dispatched to flavor template: " + templateToLoad);
return;
} else {
_logger.error("Unable to dispatch to flavor template: " + templateToLoad);
}
}
}
}
}