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 java.util.Iterator;
028: import jeeves.exceptions.ResourceNotFoundEx;
029: import jeeves.interfaces.Service;
030: import jeeves.resources.dbms.Dbms;
031: import jeeves.server.ServiceConfig;
032: import jeeves.server.context.ServiceContext;
033: import jeeves.utils.BinaryFile;
034: import jeeves.utils.Util;
035: import org.fao.geonet.GeonetContext;
036: import org.fao.geonet.constants.Geonet;
037: import org.fao.geonet.constants.Params;
038: import org.fao.geonet.kernel.AccessManager;
039: import org.fao.geonet.kernel.DataManager;
040: import org.fao.geonet.kernel.setting.SettingManager;
041: import org.fao.geonet.lib.Lib;
042: import org.fao.geonet.util.MailSender;
043: import org.jdom.Element;
044:
045: //=============================================================================
046:
047: /** Sends the resource to the client
048: */
049:
050: public class Download implements Service {
051: //-----------------------------------------------------------------------------
052: //---
053: //--- Init
054: //---
055: //-----------------------------------------------------------------------------
056:
057: public void init(String appPath, ServiceConfig params)
058: throws Exception {
059: }
060:
061: //-----------------------------------------------------------------------------
062: //---
063: //--- Service
064: //---
065: //-----------------------------------------------------------------------------
066:
067: public Element exec(Element params, ServiceContext context)
068: throws Exception {
069: String id = Util.getParam(params, Params.ID);
070: String fname = Util.getParam(params, Params.FNAME);
071: String access = Util.getParam(params, Params.ACCESS,
072: Params.Access.PUBLIC);
073:
074: boolean doNotify = false;
075:
076: if (access.equals(Params.Access.PRIVATE)) {
077: Lib.resource.checkPrivilege(context, id,
078: AccessManager.OPER_DOWNLOAD);
079: doNotify = true;
080: }
081:
082: // Build the response
083: File dir = new File(Lib.resource.getDir(context, access, id));
084: File file = new File(dir, fname);
085:
086: context.info("File is : " + file);
087:
088: if (!file.exists())
089: throw new ResourceNotFoundEx(fname);
090:
091: GeonetContext gc = (GeonetContext) context
092: .getHandlerContext(Geonet.CONTEXT_NAME);
093: SettingManager sm = gc.getSettingManager();
094: DataManager dm = gc.getDataManager();
095:
096: //--- increase metadata popularity
097:
098: Dbms dbms = (Dbms) context.getResourceManager().open(
099: Geonet.Res.MAIN_DB);
100:
101: if (access.equals(Params.Access.PRIVATE))
102: dm.increasePopularity(dbms, id);
103:
104: //--- send email notification
105:
106: if (doNotify) {
107: String host = sm
108: .getValue("system/feedback/mailServer/host");
109: String port = sm
110: .getValue("system/feedback/mailServer/port");
111: String from = sm.getValue("system/feedback/email");
112:
113: String fromDescr = "GeoNetwork administrator";
114:
115: if (host.trim().length() == 0 || from.trim().length() == 0)
116: context.debug("Skipping email notification");
117: else {
118: context.debug("Sending email notification for file : "
119: + file);
120:
121: // send emails about downloaded file to groups with notify privilege
122:
123: StringBuffer query = new StringBuffer();
124: query.append("SELECT g.id, g.name, g.email ");
125: query.append("FROM OperationAllowed oa, Groups g ");
126: query.append("WHERE oa.operationId ="
127: + AccessManager.OPER_NOTIFY + " ");
128: query.append("AND oa.metadataId = " + id + " ");
129: query.append("AND oa.groupId = g.id");
130:
131: Element groups = dbms.select(query.toString());
132:
133: for (Iterator i = groups.getChildren().iterator(); i
134: .hasNext();) {
135: Element group = (Element) i.next();
136: String name = group.getChildText("name");
137: String email = group.getChildText("email");
138:
139: if (email.trim().length() != 0) {
140: String subject = "File " + fname
141: + " has been downloaded";
142: String message = "GeoNetwork notifies you, as contact person of group "
143: + name
144: + " that data file "
145: + fname
146: + " belonging metadata "
147: + id
148: + " has beed downloaded from address "
149: + context.getIpAddress() + ".";
150:
151: try {
152: MailSender sender = new MailSender(context);
153: sender.send(host, Integer.parseInt(port),
154: from, fromDescr, email, null,
155: subject, message);
156: } catch (Exception e) {
157: e.printStackTrace();
158: }
159: }
160: }
161: }
162: }
163:
164: return BinaryFile.encode(200, file.getAbsolutePath());
165: }
166: }
167:
168: //=============================================================================
|