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.kernel.harvest.harvester.webdav;
025:
026: import java.io.IOException;
027: import java.util.ArrayList;
028: import java.util.List;
029: import jeeves.interfaces.Logger;
030: import jeeves.server.context.ServiceContext;
031: import org.apache.commons.httpclient.HttpException;
032: import org.apache.commons.httpclient.HttpStatus;
033: import org.apache.commons.httpclient.HttpURL;
034: import org.apache.commons.httpclient.HttpsURL;
035: import org.apache.webdav.lib.WebdavResource;
036: import org.fao.geonet.GeonetContext;
037: import org.fao.geonet.constants.Geonet;
038: import org.fao.geonet.kernel.setting.SettingManager;
039: import org.fao.geonet.lib.Lib;
040:
041: //=============================================================================
042:
043: class WebDavRetriever implements RemoteRetriever {
044: //--------------------------------------------------------------------------
045: //---
046: //--- RemoteRetriever interface
047: //---
048: //--------------------------------------------------------------------------
049:
050: public void init(Logger log, ServiceContext context,
051: WebDavParams params) {
052: this .log = log;
053: this .context = context;
054: this .params = params;
055: }
056:
057: //---------------------------------------------------------------------------
058:
059: public List<RemoteFile> retrieve() throws Exception {
060: davRes = open(params.url);
061:
062: files.clear();
063: retrieveFiles(davRes);
064:
065: return files;
066: }
067:
068: //---------------------------------------------------------------------------
069:
070: public void destroy() {
071: try {
072: davRes.close();
073: } catch (Exception e) {
074: log.warning("Cannot close resource : " + e.getMessage());
075: }
076: }
077:
078: //---------------------------------------------------------------------------
079: //---
080: //--- Private methods
081: //---
082: //---------------------------------------------------------------------------
083:
084: private WebdavResource open(String url) throws Exception {
085: if (!url.endsWith("/"))
086: url += "/";
087:
088: try {
089: log.info("Connecting to webdav url for node : "
090: + params.name);
091:
092: WebdavResource wr = createResource(params.url);
093:
094: log.info("Connected to webdav resource at : " + url);
095:
096: //--- we are interested only in folders
097: if (!wr.isCollection()) {
098: log.error("Resource url is not a collection : " + url);
099: wr.close();
100:
101: throw new Exception(
102: "Resource url is not a collection : " + url);
103: }
104:
105: log.info("Resource path is : " + wr.getPath());
106:
107: return wr;
108: }
109:
110: catch (HttpException e) {
111: int code = e.getReasonCode();
112:
113: if (code == HttpStatus.SC_METHOD_NOT_ALLOWED)
114: throw new Exception("Server does not support WebDAV");
115:
116: if (code == HttpStatus.SC_UNAUTHORIZED)
117: throw new Exception("Unauthorized to access resource");
118:
119: throw new Exception(
120: "Received unknown response from server : "
121: + e.getReason());
122: }
123: }
124:
125: //---------------------------------------------------------------------------
126:
127: private WebdavResource createResource(String url) throws Exception {
128: HttpURL http = url.startsWith("https") ? new HttpsURL(url)
129: : new HttpURL(url);
130:
131: if (params.useAccount)
132: http.setUserinfo(params.username, params.password);
133:
134: //--- setup proxy, if the case
135:
136: GeonetContext gc = (GeonetContext) context
137: .getHandlerContext(Geonet.CONTEXT_NAME);
138: SettingManager sm = gc.getSettingManager();
139:
140: boolean enabled = sm.getValueAsBool("system/proxy/use", false);
141: String host = sm.getValue("system/proxy/host");
142: String port = sm.getValue("system/proxy/port");
143:
144: if (!enabled)
145: return new WebdavResource(http);
146:
147: if (!Lib.type.isInteger(port))
148: throw new Exception("Proxy port is not an integer : "
149: + port);
150:
151: return new WebdavResource(http, host, Integer.parseInt(port));
152: }
153:
154: //---------------------------------------------------------------------------
155:
156: private void retrieveFiles(WebdavResource wr) throws HttpException,
157: IOException {
158: String path = wr.getPath();
159:
160: log.debug("Scanning resource : " + path);
161:
162: String[] children = wr.list();
163:
164: if (children == null)
165: log.warning("Cannot scan resource. Error is : "
166: + wr.getStatusMessage());
167: else {
168: int startSize = files.size();
169:
170: for (String name : children) {
171: wr.setPath(path + "/" + name);
172:
173: if (wr.exists()) {
174: if (wr.isCollection()) {
175: //--- this test is needed to fix a slide bug
176:
177: if (!wr.getPath().equals(path))
178: if (params.recurse)
179: retrieveFiles(wr);
180: }
181:
182: else if (wr.getName().toLowerCase()
183: .endsWith(".xml"))
184: files.add(new WebDavRemoteFile(wr));
185: }
186: }
187:
188: int endSize = files.size();
189: int added = endSize - startSize;
190:
191: if (added == 0)
192: log.debug("No xml files found in path : " + path);
193: else
194: log.debug("Found " + added + " xml file(s) in path : "
195: + path);
196: }
197: }
198:
199: //---------------------------------------------------------------------------
200: //---
201: //--- Variables
202: //---
203: //---------------------------------------------------------------------------
204:
205: private Logger log;
206: private ServiceContext context;
207: private WebDavParams params;
208: private WebdavResource davRes;
209:
210: private List<RemoteFile> files = new ArrayList<RemoteFile>();
211: }
212:
213: //=============================================================================
|