001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/history/CmsHistoryResourceHandler.java,v $
003: * Date : $Date: 2008-02-27 12:05:25 $
004: * Version: $Revision: 1.5 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.file.history;
033:
034: import org.opencms.file.CmsObject;
035: import org.opencms.file.CmsResource;
036: import org.opencms.file.Messages;
037: import org.opencms.main.CmsException;
038: import org.opencms.main.CmsLog;
039: import org.opencms.main.CmsResourceInitException;
040: import org.opencms.main.I_CmsResourceInit;
041:
042: import javax.servlet.ServletRequest;
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045:
046: import org.apache.commons.logging.Log;
047:
048: /**
049: * Resource init handler that loads historical versions of resources.<p>
050: *
051: * @author Michael Emmerich
052: * @author Michael Moossen
053: *
054: * @version $Revision: 1.5 $
055: *
056: * @since 6.9.1
057: */
058: public class CmsHistoryResourceHandler implements I_CmsResourceInit {
059:
060: /** Constant for the historical version request attribute name. */
061: public static final String ATTRIBUTE_NAME = "org.opencms.file.history.CmsHistoryResourceHandler";
062:
063: /** The historical version handler path. */
064: public static final String HISTORY_HANDLER = "/system/shared/showversion";
065:
066: /** The static log object for this class. */
067: private static final Log LOG = CmsLog
068: .getLog(CmsHistoryResourceHandler.class);
069:
070: /** Request parameter name for the version number. */
071: public static final String PARAM_VERSION = "version";
072:
073: /** Constant for the offline project version. */
074: public static final int PROJECT_OFFLINE_VERSION = Integer.MAX_VALUE;
075:
076: /**
077: * Returns the historical version of a resource,
078: * if the given request is displaying a history version.<p>
079: *
080: * @param req the request to check
081: *
082: * @return the historical resource if the given request is displaying an historical version
083: */
084: public static I_CmsHistoryResource getHistoryResource(
085: ServletRequest req) {
086:
087: return (I_CmsHistoryResource) req.getAttribute(ATTRIBUTE_NAME);
088: }
089:
090: /**
091: * Returns <code>true</code> if the given request is displaying an historical version.<p>
092: *
093: * @param req the request to check
094: *
095: * @return <code>true</code> if the given request is displaying a historical version
096: */
097: public static boolean isHistoryRequest(ServletRequest req) {
098:
099: return (null != req.getAttribute(ATTRIBUTE_NAME));
100: }
101:
102: /**
103: * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
104: */
105: public CmsResource initResource(CmsResource resource,
106: CmsObject cms, HttpServletRequest req,
107: HttpServletResponse res) throws CmsResourceInitException {
108:
109: // we only have to check for history resources if the handler was called
110: // during a real request and NOT during a dummy-request while doing
111: // a static export
112: if (req != null) {
113: String uri = cms.getRequestContext().getUri();
114: // check if the resource starts with the HISTORY_HANDLER
115: if (uri.startsWith(HISTORY_HANDLER)) {
116: String version = req.getParameter(PARAM_VERSION);
117:
118: // only do something if the resource was not found and there was a "versionid" parameter included
119: if ((resource == null) && (version != null)) {
120:
121: // test if the current user is allowed to read historical versions of resources
122: // this can be done by trying to read the history handler resource
123: if (cms.existsResource(HISTORY_HANDLER)) {
124: String storedSiteRoot = cms.getRequestContext()
125: .getSiteRoot();
126: try {
127: // we now must switch to the root site to read the history resource
128: cms.getRequestContext().setSiteRoot("/");
129:
130: // extract the "real" resourcename
131: uri = uri.substring(HISTORY_HANDLER
132: .length(), uri.length());
133: int id = new Integer(version).intValue();
134: if (id == CmsHistoryResourceHandler.PROJECT_OFFLINE_VERSION) {
135: resource = new CmsHistoryFile(cms
136: .readFile(uri));
137: } else {
138: resource = (CmsResource) cms
139: .readResource(cms.readResource(
140: uri).getStructureId(),
141: id);
142: }
143: // store a request attribute to indicate that this is in fact a historical version
144: req.setAttribute(ATTRIBUTE_NAME, resource);
145: } catch (CmsException e) {
146: if (LOG.isErrorEnabled()) {
147: LOG
148: .error(Messages
149: .get()
150: .getBundle()
151: .key(
152: Messages.ERR_HISTORYRESOURCE_2,
153: uri, version));
154: }
155: throw new CmsResourceInitException(Messages
156: .get().container(
157: Messages.ERR_SHOWVERSION_2,
158: uri, version), e);
159: } finally {
160: // restore the siteroot and modify the uri to the one of the correct resource
161: cms.getRequestContext().setSiteRoot(
162: storedSiteRoot);
163: if (resource != null) {
164: // resource may be null in case of an error
165: cms.getRequestContext().setUri(
166: cms.getSitePath(resource));
167: }
168: }
169: }
170: }
171: }
172: }
173: return resource;
174: }
175: }
|