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.services.resources;
025:
026: import java.io.File;
027: import jeeves.interfaces.Service;
028: import jeeves.server.ServiceConfig;
029: import jeeves.server.context.ServiceContext;
030: import jeeves.utils.Util;
031: import org.fao.geonet.constants.Params;
032: import org.fao.geonet.lib.Lib;
033: import org.fao.geonet.services.metadata.Update;
034: import org.jdom.Element;
035:
036: //=============================================================================
037:
038: /** Handles the file upload
039: */
040:
041: public class Upload implements Service {
042: private Element config;
043: private Update update = new Update();
044:
045: //-----------------------------------------------------------------------------
046: //---
047: //--- Init
048: //---
049: //-----------------------------------------------------------------------------
050:
051: public void init(String appPath, ServiceConfig params)
052: throws Exception {
053: update.init(appPath, params);
054: }
055:
056: //-----------------------------------------------------------------------------
057: //---
058: //--- Service
059: //---
060: //-----------------------------------------------------------------------------
061:
062: public Element exec(Element params, ServiceContext context)
063: throws Exception {
064: String uploadDir = context.getUploadDir();
065:
066: String id = Util.getParam(params, Params.ID);
067: String ref = Util.getParam(params, Params.REF);
068: String fname = Util.getParam(params, Params.FNAME);
069: String access = Util.getParam(params, Params.ACCESS);
070:
071: Lib.resource.checkEditPrivilege(context, id);
072:
073: // move uploaded file to destination directory
074: // note: uploadDir and rootDir must be in the same volume
075:
076: File dir = new File(Lib.resource.getDir(context, access, id));
077: dir.mkdirs();
078:
079: // move uploaded file to destination directory
080: File oldFile = new File(uploadDir, fname);
081: File newFile = new File(dir, fname);
082:
083: if (!oldFile.renameTo(newFile)) {
084: context.warning("Cannot move uploaded file");
085: context.warning(" (C) Source : "
086: + oldFile.getAbsolutePath());
087: context.warning(" (C) Destin : "
088: + newFile.getAbsolutePath());
089: oldFile.delete();
090: throw new Exception(
091: "Unable to move uploaded file to destination directory");
092: }
093: // update the metadata
094: Element elem = new Element("_" + ref);
095: params.addContent(elem);
096: elem.setText(fname);
097: return update.exec(params, context);
098: }
099: }
100:
101: //=============================================================================
|