try {
statusCode = verifyContentToWebSite(delegator, webSiteId, contentId);
} catch (GeneralException e) {
Debug.logError(e, module);
throw new GeneralRuntimeException(e.getMessage(), e);
}
// We try to find a specific Error page for this website concerning the status code
if (statusCode != HttpServletResponse.SC_OK) {
List<GenericValue> errorContainers = null;
try {
errorContainers = delegator.findByAndCache("WebSiteContent",
UtilMisc.toMap("webSiteId", webSiteId, "webSiteContentTypeId", "ERROR_ROOT"),
UtilMisc.toList("-fromDate"));
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
errorContainers = EntityUtil.filterByDate(errorContainers);
if (UtilValidate.isNotEmpty(errorContainers)) {
if (Debug.verboseOn()) Debug.logVerbose("Found error containers: " + errorContainers, module);
GenericValue errorContainer = EntityUtil.getFirst(errorContainers);
List<GenericValue> errorPages = null;
try {
errorPages = delegator.findByAnd("ContentAssocViewTo", UtilMisc.toMap("contentIdStart", errorContainer.getString("contentId"), "caContentAssocTypeId", "TREE_CHILD", "contentTypeId", "DOCUMENT", "caMapKey", String.valueOf(statusCode)));
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
errorPages = EntityUtil.filterByDate(errorPages);
if (UtilValidate.isNotEmpty(errorPages)) {
if (Debug.verboseOn()) Debug.logVerbose("Found error pages " + statusCode + " : " + errorPages, module);
contentId = EntityUtil.getFirst(errorPages).getString("contentId");
} else {
if (Debug.verboseOn()) Debug.logVerbose("No specific error page, falling back to the Error Container for " + statusCode, module);
contentId = errorContainer.getString("contentId");
}
mapKey = null;
hasErrorPage=true;
}
// We try to find a generic content Error page concerning the status code
if (!hasErrorPage) {
try {
GenericValue errorPage = delegator.findByPrimaryKeyCache("Content", UtilMisc.toMap("contentId", "CONTENT_ERROR_" + statusCode));
if (errorPage != null) {
Debug.logVerbose("Found generic page " + statusCode, module);
contentId = errorPage.getString("contentId");
mapKey = null;
hasErrorPage = true;
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
}
if (statusCode == HttpServletResponse.SC_OK || hasErrorPage) {
// create the template map
MapStack<String> templateMap = MapStack.create();
ScreenRenderer.populateContextForRequest(templateMap, null, request, response, servletContext);
templateMap.put("statusCode", statusCode);
// make the link prefix
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
templateMap.put("_REQUEST_HANDLER_", rh);
response.setStatus(statusCode);
// NOTE DEJ20080817: this is done in the ContentMapFacade class now to avoid problems with the jsessionid being in the middle of the URL and such
//String contextLinkPrefix = rh.makeLink(request, response, "", true, false, true);
//templateMap.put("_CONTEXT_LINK_PREFIX_", contextLinkPrefix);
Writer writer;
try {
// use UtilJ2eeCompat to get this setup properly
boolean useOutputStreamNotWriter = false;
if (servletContext != null) {
useOutputStreamNotWriter = UtilJ2eeCompat.useOutputStreamNotWriter(servletContext);
}
if (useOutputStreamNotWriter) {
ServletOutputStream ros = response.getOutputStream();
writer = new OutputStreamWriter(ros, "UTF-8");
} else {
writer = response.getWriter();
}
// TODO: replace "screen" to support dynamic rendering of different output
FormStringRenderer formStringRenderer = new MacroFormRenderer(UtilProperties.getPropertyValue("widget", "screen.formrenderer"), request, response);
templateMap.put("formStringRenderer", formStringRenderer);
//include DOCTYPE for cms screens
writer.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
// if use web analytics
List<GenericValue> webAnalytics = delegator.findByAnd("WebAnalyticsConfig", UtilMisc.toMap("webSiteId", webSiteId));
// render
if (UtilValidate.isNotEmpty(webAnalytics) && hasErrorPage) {
ContentWorker.renderContentAsText(dispatcher, delegator, contentId, writer, templateMap, locale, "text/html", null, null, true, webAnalytics);
} else if (UtilValidate.isEmpty(mapKey)) {
ContentWorker.renderContentAsText(dispatcher, delegator, contentId, writer, templateMap, locale, "text/html", null, null, true);
} else {
ContentWorker.renderSubContentAsText(dispatcher, delegator, contentId, writer, mapKey, templateMap, locale, "text/html", true);
}
} catch (TemplateException e) {
throw new GeneralRuntimeException("Error creating form renderer", e);
} catch (IOException e) {
throw new GeneralRuntimeException("Error in the response writer/output stream: " + e.toString(), e);
} catch (GeneralException e) {
throw new GeneralRuntimeException("Error rendering content: " + e.toString(), e);
}
return "success";
} else {
String contentName = null;