KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > file > CmsBackupResourceHandler


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/file/CmsBackupResourceHandler.java,v $
3  * Date : $Date: 2006/03/27 14:52:41 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.file;
33
34 import org.opencms.main.CmsException;
35 import org.opencms.main.CmsResourceInitException;
36 import org.opencms.main.I_CmsResourceInit;
37 import org.opencms.main.OpenCms;
38
39 import javax.servlet.ServletRequest JavaDoc;
40 import javax.servlet.http.HttpServletRequest JavaDoc;
41 import javax.servlet.http.HttpServletResponse JavaDoc;
42
43 /**
44  * Resource init handler that loads backup versions of resources.<p>
45  *
46  * @author Michael Emmerich
47  *
48  * @version $Revision: 1.3 $
49  *
50  * @since 6.0.1
51  */

52 public class CmsBackupResourceHandler implements I_CmsResourceInit {
53
54     /** Constant for the backup request attribute name. */
55     public static final String JavaDoc ATTRIBUTE_NAME = "org.opencms.file.CmsBackupResourceHandler";
56
57     /** The backup handler path. */
58     public static final String JavaDoc BACKUP_HANDLER = "/system/shared/showversion";
59
60     /** Request parameter name for the version id. */
61     public static final String JavaDoc PARAM_VERSIONID = "versionid";
62
63     /**
64      * Returns <code>true</code> if the given request is displaying a history backup version.<p>
65      *
66      * @param req the request to check
67      *
68      * @return <code>true</code> if the given request is displaying a history backup version
69      */

70     public static boolean isBackupRequest(ServletRequest JavaDoc req) {
71
72         return null != req.getAttribute(ATTRIBUTE_NAME);
73     }
74
75     /**
76      * @throws CmsResourceInitException
77      * @see org.opencms.main.I_CmsResourceInit#initResource(org.opencms.file.CmsResource, org.opencms.file.CmsObject, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
78      */

79     public CmsResource initResource(CmsResource resource, CmsObject cms, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res)
80     throws CmsResourceInitException {
81
82         // we only have to check for backup resources if the handler was called
83
// during a real request and NOT during a dummy-request while doing
84
// a static export
85
if (req != null) {
86             String JavaDoc versionId = req.getParameter(PARAM_VERSIONID);
87
88             // only do something if the resource was not found and there was a "versionid" parameter included
89
if (resource == null && versionId != null) {
90
91                 String JavaDoc uri = cms.getRequestContext().getUri();
92                 // check if the resource starts with the BACKUP_HANDLER
93
if (uri.startsWith(BACKUP_HANDLER)) {
94                     // test if the current user is allowed to read backup versions of resources
95
// this can be done by trying to read the backup handler resource
96
if (cms.existsResource(BACKUP_HANDLER)) {
97                         try {
98                             // extract the "real" resourcename
99
uri = uri.substring(BACKUP_HANDLER.length(), uri.length());
100                             int id = new Integer JavaDoc(versionId).intValue();
101                             // we now must switch to the root site to read the backup resource
102
cms.getRequestContext().saveSiteRoot();
103                             cms.getRequestContext().setSiteRoot("/");
104                             resource = cms.readBackupFile(uri, id);
105                             // store a request attribute to indicate that this is in fact a backup version
106
req.setAttribute(ATTRIBUTE_NAME, Boolean.TRUE);
107                         } catch (CmsException e) {
108                             if (OpenCms.getLog(this).isErrorEnabled()) {
109                                 OpenCms.getLog(this).error(
110                                     Messages.get().container(Messages.ERR_BACKUPRESOURCE_2, uri, versionId));
111                             }
112                             throw new CmsResourceInitException(Messages.get().container(
113                                 Messages.ERR_SHOWVERSION_2,
114                                 uri,
115                                 versionId), e);
116                         } finally {
117                             // restore the siteroot and modify the uri to the one of the correct resource
118
cms.getRequestContext().restoreSiteRoot();
119                             if (resource != null) {
120                                 // resource may be null in case of a
121
cms.getRequestContext().setUri(cms.getSitePath(resource));
122                             }
123                         }
124                     }
125                 }
126             }
127         }
128         return resource;
129     }
130 }
Popular Tags