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.harvesting;
025:
026: import java.io.File;
027: import java.io.FileFilter;
028: import java.net.URL;
029: import java.util.Iterator;
030: import jeeves.exceptions.BadInputEx;
031: import jeeves.exceptions.BadParameterEx;
032: import jeeves.exceptions.BadXmlResponseEx;
033: import jeeves.exceptions.JeevesException;
034: import jeeves.exceptions.MissingParameterEx;
035: import jeeves.interfaces.Service;
036: import jeeves.server.ServiceConfig;
037: import jeeves.server.context.ServiceContext;
038: import org.fao.geonet.lib.Lib;
039: import org.fao.oaipmh.exceptions.NoSetHierarchyException;
040: import org.fao.oaipmh.exceptions.OaiPmhException;
041: import org.fao.oaipmh.requests.ListMetadataFormatsRequest;
042: import org.fao.oaipmh.requests.ListSetsRequest;
043: import org.fao.oaipmh.responses.ListMetadataFormatsResponse;
044: import org.fao.oaipmh.responses.ListSetsResponse;
045: import org.fao.oaipmh.responses.MetadataFormat;
046: import org.fao.oaipmh.responses.SetInfo;
047: import org.jdom.Element;
048: import org.jdom.JDOMException;
049: import org.xml.sax.SAXException;
050:
051: //=============================================================================
052:
053: public class Info implements Service {
054: //--------------------------------------------------------------------------
055: //---
056: //--- Init
057: //---
058: //--------------------------------------------------------------------------
059:
060: public void init(String appPath, ServiceConfig config)
061: throws Exception {
062: iconPath = new File(appPath + "/images/harvesting");
063: oaiSchema = new File(appPath
064: + "/xml/validation/oai/OAI-PMH.xsd");
065: }
066:
067: //--------------------------------------------------------------------------
068: //---
069: //--- Service
070: //---
071: //--------------------------------------------------------------------------
072:
073: public Element exec(Element params, ServiceContext context)
074: throws Exception {
075: Element result = new Element("root");
076:
077: for (Iterator i = params.getChildren().iterator(); i.hasNext();) {
078: Element el = (Element) i.next();
079:
080: String name = el.getName();
081: String type = el.getText();
082:
083: if (!name.equals("type"))
084: throw new BadParameterEx(name, type);
085:
086: if (type.equals("icons"))
087: result.addContent(getIcons());
088:
089: else if (type.equals("oaiPmhServer"))
090: result.addContent(getOaiPmhServer(el));
091:
092: else
093: throw new BadParameterEx("type", type);
094: }
095:
096: return result;
097: }
098:
099: //--------------------------------------------------------------------------
100: //---
101: //--- Private methods
102: //---
103: //--------------------------------------------------------------------------
104:
105: private Element getIcons() {
106: File icons[] = iconPath.listFiles(iconFilter);
107:
108: Element result = new Element("icons");
109:
110: if (icons != null)
111: for (File icon : icons)
112: result.addContent(new Element("icon").setText(icon
113: .getName()));
114:
115: return result;
116: }
117:
118: //--------------------------------------------------------------------------
119:
120: private FileFilter iconFilter = new FileFilter() {
121: public boolean accept(File icon) {
122: if (!icon.isFile())
123: return false;
124:
125: String name = icon.getName();
126:
127: for (String ext : iconExt)
128: if (name.endsWith(ext))
129: return true;
130:
131: return false;
132: }
133: };
134:
135: //--------------------------------------------------------------------------
136: //--- OaiPmhServer
137: //--------------------------------------------------------------------------
138:
139: private Element getOaiPmhServer(Element el) throws BadInputEx {
140: String url = el.getAttributeValue("url");
141:
142: if (url == null)
143: throw new MissingParameterEx("attribute:url", el);
144:
145: if (!Lib.net.isUrlValid(url))
146: throw new BadParameterEx("attribute:url", el);
147:
148: Element res = new Element("oaiPmhServer");
149:
150: try {
151: res.addContent(getMdFormats(url));
152: res.addContent(getSets(url));
153: } catch (JDOMException e) {
154: res.setContent(JeevesException
155: .toElement(new BadXmlResponseEx(e.getMessage())));
156: } catch (SAXException e) {
157: res.setContent(JeevesException
158: .toElement(new BadXmlResponseEx(e.getMessage())));
159: } catch (OaiPmhException e) {
160: res.setContent(org.fao.geonet.kernel.oaipmh.Lib
161: .toJeevesException(e));
162: } catch (Exception e) {
163: res.setContent(JeevesException.toElement(e));
164: }
165:
166: return res;
167: }
168:
169: //--------------------------------------------------------------------------
170:
171: private Element getMdFormats(String url) throws Exception {
172: ListMetadataFormatsRequest req = new ListMetadataFormatsRequest();
173: req.setValidationSchema(oaiSchema);
174: req.getTransport().setUrl(new URL(url));
175: ListMetadataFormatsResponse res = req.execute();
176:
177: //--- build response
178:
179: Element root = new Element("formats");
180:
181: for (MetadataFormat mf : res.getFormats())
182: root.addContent(new Element("format").setText(mf.prefix));
183:
184: return root;
185: }
186:
187: //--------------------------------------------------------------------------
188:
189: private Element getSets(String url) throws Exception {
190: Element root = new Element("sets");
191:
192: try {
193: ListSetsRequest req = new ListSetsRequest();
194: req.setValidationSchema(oaiSchema);
195: req.getTransport().setUrl(new URL(url));
196: ListSetsResponse res = req.execute();
197:
198: //--- build response
199:
200: while (res.hasNext()) {
201: SetInfo si = res.next();
202:
203: Element el = new Element("set");
204:
205: el
206: .addContent(new Element("name").setText(si
207: .getSpec()));
208: el.addContent(new Element("label")
209: .setText(si.getName()));
210:
211: root.addContent(el);
212: }
213: } catch (NoSetHierarchyException e) {
214: //--- if the server does not support sets, simply returns an empty set
215: }
216:
217: return root;
218: }
219:
220: //--------------------------------------------------------------------------
221: //---
222: //--- Variables
223: //---
224: //--------------------------------------------------------------------------
225:
226: private File iconPath;
227: private File oaiSchema;
228:
229: private static final String iconExt[] = { ".gif", ".png", ".jpg",
230: ".jpeg" };
231: }
232:
233: //=============================================================================
|