1 31 32 package org.opencms.main; 33 34 import org.opencms.file.CmsFile; 35 import org.opencms.file.CmsObject; 36 import org.opencms.file.CmsResourceFilter; 37 import org.opencms.i18n.CmsMessageContainer; 38 import org.opencms.staticexport.CmsStaticExportData; 39 import org.opencms.staticexport.CmsStaticExportRequest; 40 import org.opencms.util.CmsRequestUtil; 41 42 import java.io.IOException ; 43 44 import javax.servlet.ServletConfig ; 45 import javax.servlet.ServletException ; 46 import javax.servlet.http.HttpServlet ; 47 import javax.servlet.http.HttpServletRequest ; 48 import javax.servlet.http.HttpServletResponse ; 49 50 import org.apache.commons.logging.Log; 51 52 87 public class OpenCmsServlet extends HttpServlet implements I_CmsRequestHandler { 88 89 90 public static final String SERVLET_PARAM_DEFAULT_WEB_APPLICATION = "DefaultWebApplication"; 91 92 93 public static final String SERVLET_PARAM_OPEN_CMS_HOME = "OpenCmsHome"; 94 95 96 public static final String SERVLET_PARAM_OPEN_CMS_SERVLET = "OpenCmsServlet"; 97 98 99 public static final String SERVLET_PARAM_WEB_APPLICATION_CONTEXT = "WebApplicationContext"; 100 101 102 private static final String HANDLE_PATH = "/handle"; 103 104 105 private static final String HANDLE_VFS_PATH = "/system/handler" + HANDLE_PATH; 106 107 108 private static final String HANDLE_VFS_SUFFIX = ".html"; 109 110 111 private static final String [] HANDLER_NAMES = {"404"}; 112 113 114 private static final Log LOG = CmsLog.getLog(OpenCmsServlet.class); 115 116 117 private static final long serialVersionUID = 4729951599966070050L; 118 119 124 public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException , ServletException { 125 126 int runlevel = OpenCmsCore.getInstance().getRunLevel(); 128 129 res.setHeader(CmsRequestUtil.HEADER_SERVER, OpenCmsCore.getInstance().getSystemInfo().getVersion()); 131 132 if (runlevel != OpenCms.RUNLEVEL_4_SERVLET_ACCESS) { 133 if (runlevel == OpenCms.RUNLEVEL_3_SHELL_ACCESS) { 135 init(getServletConfig()); 137 } else { 138 res.sendError(HttpServletResponse.SC_FORBIDDEN); 141 return; 143 } 144 } 145 String path = req.getPathInfo(); 146 if ((path != null) && path.startsWith(HANDLE_PATH)) { 147 invokeHandler(req, res); 149 } else { 150 OpenCmsCore.getInstance().showResource(req, res); 152 } 153 } 154 155 161 public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException , ServletException { 162 163 doGet(req, res); 164 } 165 166 169 public String [] getHandlerNames() { 170 171 return HANDLER_NAMES; 172 } 173 174 177 public void handle(HttpServletRequest req, HttpServletResponse res, String name) 178 throws IOException , ServletException { 179 180 int errorCode; 181 try { 182 errorCode = Integer.valueOf(name).intValue(); 183 } catch (NumberFormatException nf) { 184 res.sendError(HttpServletResponse.SC_FORBIDDEN); 185 return; 186 } 187 switch (errorCode) { 188 case 404: 189 String path = req.getPathInfo(); 190 CmsObject cms = null; 191 CmsStaticExportData exportData = null; 192 try { 193 cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport()); 194 exportData = OpenCms.getStaticExportManager().getExportData(req, cms); 195 } catch (CmsException e) { 196 if (LOG.isWarnEnabled()) { 198 LOG.warn( 199 Messages.get().getBundle().key(Messages.LOG_INIT_CMSOBJECT_IN_HANDLER_2, name, path), 200 e); 201 } 202 } 203 if (exportData != null) { 204 try { 205 CmsStaticExportRequest exportReq = new CmsStaticExportRequest(req, exportData); 207 res.setStatus(OpenCms.getStaticExportManager().export(exportReq, res, cms, exportData)); 209 } catch (Throwable t) { 210 if (LOG.isWarnEnabled()) { 211 LOG.warn(Messages.get().getBundle().key(Messages.LOG_ERROR_EXPORT_1, exportData), t); 212 } 213 openErrorHandler(req, res, errorCode); 214 } 215 } else { 216 openErrorHandler(req, res, errorCode); 217 } 218 break; 219 default: 220 openErrorHandler(req, res, errorCode); 221 } 222 } 223 224 227 public synchronized void init(ServletConfig config) throws ServletException { 228 229 super.init(config); 230 try { 231 OpenCmsCore.getInstance().upgradeRunlevel(config.getServletContext()); 235 OpenCmsCore.getInstance().initServlet(this); 237 } catch (CmsInitException e) { 238 if (Messages.ERR_CRITICAL_INIT_WIZARD_0.equals(e.getMessageContainer().getKey())) { 239 throw new ServletException (e.getMessage()); 242 } 243 } catch (Throwable t) { 244 LOG.error(Messages.get().getBundle().key(Messages.LOG_ERROR_GENERIC_0), t); 245 } 246 } 247 248 257 protected void invokeHandler(HttpServletRequest req, HttpServletResponse res) throws IOException , ServletException { 258 259 String name = req.getPathInfo().substring(HANDLE_PATH.length()); 260 I_CmsRequestHandler handler = OpenCmsCore.getInstance().getRequestHandler(name); 261 if (handler != null) { 262 handler.handle(req, res, name); 263 } else { 264 openErrorHandler(req, res, HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 265 } 266 } 267 268 279 protected void openErrorHandler(HttpServletRequest req, HttpServletResponse res, int errorCode) 280 throws IOException , ServletException { 281 282 String handlerUri = (new StringBuffer (64)).append(HANDLE_VFS_PATH).append(errorCode).append(HANDLE_VFS_SUFFIX).toString(); 283 CmsObject cms; 284 CmsFile file; 285 try { 286 cms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserGuest()); 288 cms.getRequestContext().setUri(handlerUri); 289 file = cms.readFile(handlerUri, CmsResourceFilter.IGNORE_EXPIRATION); 291 } catch (CmsException e) { 292 CmsMessageContainer container = Messages.get().container( 294 Messages.LOG_INIT_CMSOBJECT_IN_HANDLER_2, 295 new Integer (errorCode), 296 handlerUri); 297 if (LOG.isWarnEnabled()) { 298 LOG.warn(org.opencms.jsp.Messages.getLocalizedMessage(container, req), e); 299 } 300 if (!res.isCommitted()) { 302 res.sendError(errorCode, e.getLocalizedMessage()); 304 } 305 return; 306 } 307 try { 308 req.setAttribute(CmsRequestUtil.ATTRIBUTE_ERRORCODE, new Integer (errorCode)); 310 OpenCms.getResourceManager().loadResource(cms, file, req, res); 311 } catch (CmsException e) { 312 CmsMessageContainer container = Messages.get().container( 314 Messages.ERR_SHOW_ERR_HANDLER_RESOURCE_2, 315 new Integer (errorCode), 316 handlerUri); 317 throw new ServletException (org.opencms.jsp.Messages.getLocalizedMessage(container, req), e); 318 } 319 } 320 } | Popular Tags |