001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.lib;
025:
026: import java.io.File;
027: import java.util.HashSet;
028: import jeeves.exceptions.OperationNotAllowedEx;
029: import jeeves.server.context.ServiceContext;
030: import org.fao.geonet.GeonetContext;
031: import org.fao.geonet.constants.Geonet;
032: import org.fao.geonet.constants.Params;
033: import org.fao.geonet.kernel.AccessManager;
034:
035: //=============================================================================
036:
037: public class ResourceLib {
038: //-----------------------------------------------------------------------------
039: //---
040: //--- API methods
041: //---
042: //-----------------------------------------------------------------------------
043:
044: public String getDataDir(ServiceContext context) {
045: GeonetContext gc = (GeonetContext) context
046: .getHandlerContext(Geonet.CONTEXT_NAME);
047:
048: String dataDir = gc.getHandlerConfig().getMandatoryValue(
049: Geonet.Config.DATA_DIR);
050:
051: if (!new File(dataDir).isAbsolute())
052: dataDir = context.getAppPath() + dataDir;
053:
054: return dataDir;
055: }
056:
057: //-----------------------------------------------------------------------------
058:
059: public String getDir(ServiceContext context, String access,
060: String id) {
061: return getDir(getDataDir(context), access, id);
062: }
063:
064: //-----------------------------------------------------------------------------
065:
066: public String getDir(String dataDir, String access, String id) {
067: String group = pad(Integer.parseInt(id) / 100, 3);
068: String groupDir = group + "00-" + group + "99";
069: String subDir = (access != null && access
070: .equals(Params.Access.PUBLIC)) ? Params.Access.PUBLIC
071: : Params.Access.PRIVATE;
072:
073: return dataDir + "/" + groupDir + "/" + id + "/" + subDir + "/";
074: }
075:
076: //-----------------------------------------------------------------------------
077:
078: public void checkPrivilege(ServiceContext context, String id,
079: String operation) throws Exception {
080: GeonetContext gc = (GeonetContext) context
081: .getHandlerContext(Geonet.CONTEXT_NAME);
082:
083: AccessManager accessMan = gc.getAccessManager();
084:
085: HashSet hsOper = accessMan.getOperations(context, id, context
086: .getIpAddress());
087:
088: if (!hsOper.contains(operation))
089: throw new OperationNotAllowedEx();
090: }
091:
092: //-----------------------------------------------------------------------------
093:
094: public void checkEditPrivilege(ServiceContext context, String id)
095: throws Exception {
096: GeonetContext gc = (GeonetContext) context
097: .getHandlerContext(Geonet.CONTEXT_NAME);
098: AccessManager am = gc.getAccessManager();
099:
100: if (!am.canEdit(context, id))
101: throw new OperationNotAllowedEx();
102: }
103:
104: //-----------------------------------------------------------------------------
105:
106: public String getRemovedDir(ServiceContext context, String id) {
107: return getRemovedDir(getRemovedDir(context), id);
108: }
109:
110: //-----------------------------------------------------------------------------
111: /** @return the absolute path of the folder choosen to store all deleted metadata */
112:
113: public String getRemovedDir(ServiceContext context) {
114: GeonetContext gc = (GeonetContext) context
115: .getHandlerContext(Geonet.CONTEXT_NAME);
116:
117: String remDir = gc.getSettingManager().getValue(
118: "system/removedMetadata/dir");
119:
120: if (!new File(remDir).isAbsolute())
121: remDir = context.getAppPath() + remDir;
122:
123: return remDir;
124: }
125:
126: //-----------------------------------------------------------------------------
127: /** @return the absolute path of the folder where the given metadata should be
128: * stored when it is removed */
129:
130: public String getRemovedDir(String removedDir, String id) {
131: String group = pad(Integer.parseInt(id) / 100, 3);
132: String groupDir = group + "00-" + group + "99";
133:
134: return removedDir + "/" + groupDir + "/";
135: }
136:
137: //-----------------------------------------------------------------------------
138: //---
139: //--- Private methods
140: //---
141: //-----------------------------------------------------------------------------
142:
143: private String pad(int group, int lenght) {
144: String text = Integer.toString(group);
145:
146: while (text.length() < lenght)
147: text = "0" + text;
148:
149: return text;
150: }
151: }
152:
153: //=============================================================================
|