KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > commons > CmsSecure


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsSecure.java,v $
3  * Date : $Date: 2006/04/28 15:20:57 $
4  * Version: $Revision: 1.31 $
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.workplace.commons;
33
34 import org.opencms.file.CmsObject;
35 import org.opencms.file.CmsProject;
36 import org.opencms.file.CmsProperty;
37 import org.opencms.file.CmsPropertyDefinition;
38 import org.opencms.file.CmsResource;
39 import org.opencms.file.CmsResourceFilter;
40 import org.opencms.jsp.CmsJspActionElement;
41 import org.opencms.main.CmsException;
42 import org.opencms.main.CmsLog;
43 import org.opencms.main.OpenCms;
44 import org.opencms.security.CmsPermissionSet;
45 import org.opencms.site.CmsSite;
46 import org.opencms.site.CmsSiteManager;
47 import org.opencms.staticexport.CmsLinkManager;
48 import org.opencms.util.CmsStringUtil;
49 import org.opencms.workplace.CmsDialog;
50 import org.opencms.workplace.CmsWorkplaceSettings;
51
52 import javax.servlet.http.HttpServletRequest JavaDoc;
53 import javax.servlet.http.HttpServletResponse JavaDoc;
54 import javax.servlet.jsp.JspException JavaDoc;
55 import javax.servlet.jsp.PageContext JavaDoc;
56
57 import org.apache.commons.logging.Log;
58
59 /**
60  * Provides methods for building the security and export settings dialog.<p>
61  *
62  * The following files use this class:
63  * <ul>
64  * <li>/commons/secure.jsp
65  * </ul>
66  * <p>
67  *
68  * @author Jan Baudisch
69  *
70  * @version $Revision: 1.31 $
71  *
72  * @since 6.0.0
73  */

74 public class CmsSecure extends CmsDialog {
75
76     /** Value for the action: change the security and export setting. */
77     public static final int ACTION_CHSECEXP = 100;
78
79     /** The dialog type. */
80     public static final String JavaDoc DIALOG_TYPE = "secure";
81
82     /** The log object for this class. */
83     private static final Log LOG = CmsLog.getLog(CmsSecure.class);
84
85     /** Export parameter. */
86     private String JavaDoc m_paramExport;
87
88     /** Exportname parameter. */
89     private String JavaDoc m_paramExportname;
90
91     /** Intern parameter. */
92     private String JavaDoc m_paramIntern;
93
94     /** Secure parameter. */
95     private String JavaDoc m_paramSecure;
96
97     /**
98      * Public constructor.<p>
99      *
100      * @param jsp an initialized JSP action element
101      */

102     public CmsSecure(CmsJspActionElement jsp) {
103
104         super(jsp);
105     }
106
107     /**
108      * Public constructor with JSP variables.<p>
109      *
110      * @param context the JSP page context
111      * @param req the JSP request
112      * @param res the JSP response
113      */

114     public CmsSecure(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
115
116         this(new CmsJspActionElement(context, req, res));
117     }
118
119     /**
120      * Performs the Security and Export Change.<p>
121      *
122      * @throws JspException if including a JSP subelement is not successful
123      */

124     public void actionChangeSecureExport() throws JspException JavaDoc {
125
126         // save initialized instance of this class in request attribute for included sub-elements
127
getJsp().getRequest().setAttribute(SESSION_WORKPLACE_CLASS, this);
128
129         String JavaDoc filename = getParamResource();
130
131         try {
132             // lock resource if autolock is enabled
133
checkLock(getParamResource());
134
135             // write the properties
136
writeProperty(CmsPropertyDefinition.PROPERTY_EXPORT, getParamExport());
137             writeProperty(CmsPropertyDefinition.PROPERTY_EXPORTNAME, getParamExportname());
138             writeProperty(CmsPropertyDefinition.PROPERTY_SECURE, getParamSecure());
139
140             // change the flag of the resource so that it is internal
141
CmsResource resource = getCms().readResource(filename, CmsResourceFilter.IGNORE_EXPIRATION);
142             if (resource.isInternal() && !Boolean.valueOf(getParamIntern()).booleanValue()) {
143                 getCms().chflags(filename, resource.getFlags() & (~CmsResource.FLAG_INTERNAL));
144             } else if (!resource.isInternal() && Boolean.valueOf(getParamIntern()).booleanValue()) {
145                 getCms().chflags(filename, resource.getFlags() | CmsResource.FLAG_INTERNAL);
146             }
147
148             actionCloseDialog();
149         } catch (Throwable JavaDoc e) {
150             // error during change of secure settings, show error dialog
151
includeErrorpage(this, e);
152         }
153
154     }
155
156     /**
157      * Builds the radio input to set the export and secure property.
158      *
159      * @param propName the name of the property to build the radio input for
160      * @return html for the radio input
161      * @throws CmsException if the reading of a property fails
162      */

163     public String JavaDoc buildRadio(String JavaDoc propName) throws CmsException {
164
165         String JavaDoc propVal = readProperty(propName);
166         StringBuffer JavaDoc result = new StringBuffer JavaDoc("<table border=\"0\"><tr>");
167         result.append("<td><input type=\"radio\" value=\"true\" onClick=\"checkNoIntern()\" name=\"").append(propName).append(
168             "\" ").append(Boolean.valueOf(propVal).booleanValue() ? "checked=\"checked\"" : "").append("/></td><td id=\"tablelabel\">").append(
169             key(Messages.GUI_LABEL_TRUE_0)).append("</td>");
170         result.append("<td><input type=\"radio\" value=\"false\" onClick=\"checkNoIntern()\" name=\"").append(propName).append(
171             "\" ").append(Boolean.valueOf(propVal).booleanValue() ? "" : "checked=\"checked\"").append("/></td><td id=\"tablelabel\">").append(
172             key(Messages.GUI_LABEL_FALSE_0)).append("</td>");
173         result.append("<td><input type=\"radio\" value=\"\" onClick=\"checkNoIntern()\" name=\"").append(propName).append(
174             "\" ").append(CmsStringUtil.isEmpty(propVal) ? "checked=\"checked\"" : "").append(
175             "/></td><td id=\"tablelabel\">").append(getPropertyInheritanceInfo(propName)).append("</td></tr></table>");
176         return result.toString();
177     }
178
179     /**
180      * Returns the value of the export parameter.<p>
181      *
182      * @return the value of the export parameter
183      */

184     public String JavaDoc getParamExport() {
185
186         return m_paramExport;
187     }
188
189     /**
190      * Returns the value of the exportname parameter.<p>
191      *
192      * @return the value of the exportname parameter
193      */

194     public String JavaDoc getParamExportname() {
195
196         return m_paramExportname;
197     }
198
199     /**
200      * Returns the value of the intern parameter.<p>
201      *
202      * @return the value of the intern parameter
203      */

204     public String JavaDoc getParamIntern() {
205
206         return m_paramIntern;
207     }
208
209     /**
210      * Returns the value of the secure parameter.<p>
211      *
212      * @return the value of the secure parameter
213      */

214     public String JavaDoc getParamSecure() {
215
216         return m_paramSecure;
217     }
218
219     /**
220      * Returns the information from which the property is inherited.<p>
221      *
222      * @param propName the name of the property
223      * @return a String containing the information from which the property is inherited and inherited value
224      * @throws CmsException if the reading of the Property fails
225      */

226     public String JavaDoc getPropertyInheritanceInfo(String JavaDoc propName) throws CmsException {
227
228         String JavaDoc folderName = CmsResource.getParentFolder(getParamResource());
229         String JavaDoc folderPropVal = null;
230         while (CmsStringUtil.isNotEmpty(folderName)) {
231             CmsProperty prop = getCms().readPropertyObject(folderName, propName, false);
232             folderPropVal = prop.getValue();
233             if (CmsStringUtil.isNotEmpty(folderPropVal)) {
234                 break;
235             }
236             folderName = CmsResource.getParentFolder(folderName);
237         }
238
239         if (CmsStringUtil.isNotEmpty(folderPropVal)) {
240             return key(Messages.GUI_SECURE_INHERIT_FROM_2, new Object JavaDoc[] {folderPropVal, folderName});
241         } else {
242             return key(Messages.GUI_SECURE_NOT_SET_0);
243         }
244     }
245
246     /**
247      * Returns the path under which the resource is accessable.
248      *
249      * @return the path under which the resource is accessable
250      */

251     public String JavaDoc getResourceUrl() {
252
253         CmsObject cms = getCms();
254         String JavaDoc uri = "";
255         String JavaDoc serverPrefix = "";
256         String JavaDoc vfsName = CmsLinkManager.getAbsoluteUri(getParamResource(), cms.getRequestContext().getUri());
257         String JavaDoc secureResource = "";
258         try {
259             if (resourceIsFolder()) {
260                 vfsName = vfsName.concat("/");
261             }
262             secureResource = getCms().readPropertyObject(
263                 getParamResource(),
264                 CmsPropertyDefinition.PROPERTY_SECURE,
265                 true).getValue();
266         } catch (CmsException e) {
267             if (LOG.isInfoEnabled()) {
268                 LOG.info(e.getLocalizedMessage());
269             }
270         }
271         CmsSite currentSite = CmsSiteManager.getCurrentSite(getCms());
272         CmsProject currentProject = cms.getRequestContext().currentProject();
273         try {
274             cms.getRequestContext().setCurrentProject(cms.readProject(CmsProject.ONLINE_PROJECT_ID));
275             uri = OpenCms.getLinkManager().substituteLink(cms, getParamResource(), currentSite.getSiteRoot());
276         } catch (CmsException e) {
277             if (LOG.isErrorEnabled()) {
278                 LOG.error(e.getLocalizedMessage());
279             }
280         } finally {
281             cms.getRequestContext().setCurrentProject(currentProject);
282         }
283         
284         if (currentSite.equals(OpenCms.getSiteManager().getDefaultSite())) {
285             serverPrefix = OpenCms.getSiteManager().getWorkplaceServer();
286         } else {
287             if (Boolean.valueOf(secureResource).booleanValue() && currentSite.hasSecureServer()) {
288                 serverPrefix = currentSite.getSecureUrl();
289             } else {
290                 serverPrefix = currentSite.getUrl();
291             }
292         }
293         if (!uri.startsWith(serverPrefix)) {
294             uri = serverPrefix + uri;
295         }
296         return uri;
297     }
298     
299     /**
300      * Returns true if the export user has read permission on a specified resource.<p>
301      *
302      * @return true, if the export user has the permission to read the resource
303      */

304     public boolean exportUserHasReadPermission() {
305         String JavaDoc vfsName = getParamResource();
306         CmsObject cms = getCms();
307         try {
308             // static export must always be checked with the export users permissions,
309
// not the current users permissions
310
CmsObject exportCms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
311             exportCms.getRequestContext().setSiteRoot(getCms().getRequestContext().getSiteRoot());
312             // let's look up if the export user has the permission to read
313
return exportCms.hasPermissions(cms.readResource(vfsName, CmsResourceFilter.IGNORE_EXPIRATION), CmsPermissionSet.ACCESS_READ);
314         } catch (CmsException e) {
315             // ignore this exception
316
}
317         return false;
318     }
319
320     /**
321      * Returns value of the the intern property of the resource.
322      *
323      * @return the value of the intern property of the resource
324      */

325     public String JavaDoc readInternProp() {
326
327         boolean internProp = false;
328         try {
329             internProp = getCms().readResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION).isInternal();
330         } catch (CmsException e) {
331             if (LOG.isInfoEnabled()) {
332                 LOG.info(e.getLocalizedMessage());
333             }
334         }
335         return String.valueOf(internProp);
336     }
337
338     /**
339      * Returns value of the property of the resource.
340      *
341      * @param propertyName the name of the property to read
342      * @return the value of the secure property of the resource
343      */

344     public String JavaDoc readProperty(String JavaDoc propertyName) {
345
346         String JavaDoc propVal = null;
347         try {
348             propVal = getCms().readPropertyObject(getParamResource(), propertyName, false).getValue();
349         } catch (CmsException e) {
350             if (LOG.isInfoEnabled()) {
351                 LOG.info(e.getLocalizedMessage());
352             }
353         }
354         if (CmsStringUtil.isEmpty(propVal)) {
355             propVal = "";
356         }
357         return propVal;
358     }
359
360     /**
361      * returns if the resource to be changed is a folder.
362      * @return true if the resource is a folder
363      * @throws CmsException if the reading of the resource fails
364      */

365     public boolean resourceIsFolder() throws CmsException {
366
367         return getCms().readResource(getParamResource(), CmsResourceFilter.IGNORE_EXPIRATION).isFolder();
368     }
369
370     /**
371      * Sets the value of the export parameter.<p>
372      *
373      * @param value for the export parameter
374      */

375     public void setParamExport(String JavaDoc value) {
376
377         m_paramExport = value;
378     }
379
380     /**
381      * Sets the value of the exportname parameter.<p>
382      *
383      * @param value for the exportname parameter
384      */

385     public void setParamExportname(String JavaDoc value) {
386
387         m_paramExportname = value;
388     }
389
390     /**
391      * Sets the value of the intern parameter.<p>
392      *
393      * @param value for the intern parameter
394      */

395     public void setParamIntern(String JavaDoc value) {
396
397         m_paramIntern = value;
398     }
399
400     /**
401      * Sets the value of the secure parameter.<p>
402      *
403      * @param value for the secure parameter
404      */

405     public void setParamSecure(String JavaDoc value) {
406
407         m_paramSecure = value;
408     }
409
410     /**
411      * Determines whether to show the export settings dialog depending on the users settings.<p>
412      *
413      * @return true if dialogs should be shown, otherwise false
414      */

415     public boolean showExportSettings() {
416
417         return getSettings().getUserSettings().getDialogShowExportSettings();
418     }
419
420     /**
421      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
422      */

423     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
424
425         // fill the parameter values in the get/set methods
426
fillParamValues(request);
427         
428         // check the required permissions to change the resource properties
429
if (! checkResourcePermissions(CmsPermissionSet.ACCESS_WRITE, false)) {
430             // no write permissions for the resource, set cancel action to close dialog
431
setParamAction(DIALOG_CANCEL);
432         }
433         
434         // set the dialog type
435
setParamDialogtype(DIALOG_TYPE);
436         // set the action for the JSP switch
437
if (DIALOG_TYPE.equals(getParamAction())) {
438             setAction(ACTION_CHSECEXP);
439         } else if (DIALOG_CANCEL.equals(getParamAction())) {
440             setAction(ACTION_CANCEL);
441         } else {
442             setAction(ACTION_DEFAULT);
443             // build title for chnav dialog
444
setParamTitle(key(Messages.GUI_SECURE_EXPORT_RESOURCE_1, new Object JavaDoc[] {CmsResource.getName(getParamResource())}));
445         }
446     }
447
448     /**
449      * Writes a property value for a resource.<p>
450      *
451      * @param propertyName the name of the property
452      * @param propertyValue the new value of the property
453      * @throws CmsException if something goes wrong
454      */

455     protected void writeProperty(String JavaDoc propertyName, String JavaDoc propertyValue) throws CmsException {
456
457         if (CmsStringUtil.isEmpty(propertyValue)) {
458             propertyValue = CmsProperty.DELETE_VALUE;
459         }
460
461         CmsProperty newProp = new CmsProperty();
462         newProp.setName(propertyName);
463         CmsProperty oldProp = getCms().readPropertyObject(getParamResource(), propertyName, false);
464         if (oldProp.isNullProperty()) {
465             // property value was not already set
466
if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) {
467                 newProp.setStructureValue(propertyValue);
468             } else {
469                 newProp.setResourceValue(propertyValue);
470             }
471         } else {
472             if (oldProp.getStructureValue() != null) {
473                 newProp.setStructureValue(propertyValue);
474                 newProp.setResourceValue(oldProp.getResourceValue());
475             } else {
476                 newProp.setResourceValue(propertyValue);
477             }
478         }
479
480         newProp.setAutoCreatePropertyDefinition(true);
481
482         String JavaDoc oldStructureValue = oldProp.getStructureValue();
483         String JavaDoc newStructureValue = newProp.getStructureValue();
484         if (CmsStringUtil.isEmpty(oldStructureValue)) {
485             oldStructureValue = CmsProperty.DELETE_VALUE;
486         }
487         if (CmsStringUtil.isEmpty(newStructureValue)) {
488             newStructureValue = CmsProperty.DELETE_VALUE;
489         }
490
491         String JavaDoc oldResourceValue = oldProp.getResourceValue();
492         String JavaDoc newResourceValue = newProp.getResourceValue();
493         if (CmsStringUtil.isEmpty(oldResourceValue)) {
494             oldResourceValue = CmsProperty.DELETE_VALUE;
495         }
496         if (CmsStringUtil.isEmpty(newResourceValue)) {
497             newResourceValue = CmsProperty.DELETE_VALUE;
498         }
499
500         // change property only if it has been changed
501
if (!oldResourceValue.equals(newResourceValue) || !oldStructureValue.equals(newStructureValue)) {
502             getCms().writePropertyObject(getParamResource(), newProp);
503         }
504
505     }
506 }
Popular Tags