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.oaipmh;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028: import jeeves.constants.Jeeves;
029: import jeeves.server.context.ServiceContext;
030: import jeeves.utils.Util;
031: import jeeves.utils.Xml;
032: import org.fao.geonet.constants.Geonet;
033: import org.fao.geonet.kernel.oaipmh.services.GetRecord;
034: import org.fao.geonet.kernel.oaipmh.services.Identify;
035: import org.fao.geonet.kernel.oaipmh.services.ListIdentifiers;
036: import org.fao.geonet.kernel.oaipmh.services.ListMetadataFormats;
037: import org.fao.geonet.kernel.oaipmh.services.ListRecords;
038: import org.fao.geonet.kernel.oaipmh.services.ListSets;
039: import org.fao.geonet.kernel.setting.SettingInfo;
040: import org.fao.oaipmh.exceptions.BadArgumentException;
041: import org.fao.oaipmh.exceptions.OaiPmhException;
042: import org.fao.oaipmh.requests.AbstractRequest;
043: import org.fao.oaipmh.responses.AbstractResponse;
044: import org.fao.oaipmh.server.OaiPmhFactory;
045: import org.fao.oaipmh.util.Lib;
046: import org.jdom.Element;
047:
048: //=============================================================================
049:
050: public class OaiPmhDispatcher {
051: //---------------------------------------------------------------------------
052: //---
053: //--- Constructor
054: //---
055: //---------------------------------------------------------------------------
056:
057: public OaiPmhDispatcher() {
058: register(new GetRecord());
059: register(new Identify());
060: register(new ListIdentifiers());
061: register(new ListMetadataFormats());
062: register(new ListRecords());
063: register(new ListSets());
064: }
065:
066: //---------------------------------------------------------------------------
067:
068: private void register(OaiPmhService s) {
069: hmServices.put(s.getVerb(), s);
070: }
071:
072: //---------------------------------------------------------------------------
073: //---
074: //--- API methods
075: //---
076: //---------------------------------------------------------------------------
077:
078: public Element dispatch(Element request, ServiceContext context) {
079: Element response = dispatchI(request, context);
080: validateResponse(context, response);
081:
082: return response;
083: }
084:
085: //---------------------------------------------------------------------------
086: //---
087: //--- Private method
088: //---
089: //---------------------------------------------------------------------------
090:
091: private Element dispatchI(Element request, ServiceContext context) {
092: String url = null;
093:
094: Map<String, String> params = null;
095:
096: SettingInfo si = new SettingInfo(context);
097:
098: try {
099: url = si.getSiteUrl() + context.getBaseUrl() + "/"
100: + Jeeves.Prefix.SERVICE + "/en/"
101: + context.getService();
102: params = OaiPmhFactory.extractParams(request);
103:
104: AbstractRequest req = OaiPmhFactory.parse(params);
105: OaiPmhService srv = hmServices.get(req.getVerb());
106: AbstractResponse res = srv.execute(req, context);
107:
108: Element response = res.toXml();
109:
110: return Lib.createOaiRoot(url, params, response);
111: }
112:
113: catch (OaiPmhException e) {
114: return OaiPmhException.marshal(e, url, params);
115: }
116:
117: catch (Exception e) {
118: context.info("Exception stack trace : \n"
119: + Util.getStackTrace(e));
120:
121: //--- we should use another exception type but we don't have a specific
122: //--- type to handle internal errors
123:
124: BadArgumentException ex = new BadArgumentException(
125: "Internal error : " + e.getMessage());
126:
127: return OaiPmhException.marshal(ex, url, params);
128: }
129: }
130:
131: //---------------------------------------------------------------------------
132:
133: private void validateResponse(ServiceContext context,
134: Element response) {
135: String schema = context.getAppPath()
136: + Geonet.SchemaPath.OAI_PMH;
137:
138: context.debug("Validating against : " + schema);
139:
140: try {
141: Xml.validate(schema, response);
142: } catch (Exception e) {
143: context.warning("OAI-PMH response does not validate : "
144: + e.getMessage());
145: }
146: }
147:
148: //---------------------------------------------------------------------------
149: //---
150: //--- Variables
151: //---
152: //---------------------------------------------------------------------------
153:
154: private HashMap<String, OaiPmhService> hmServices = new HashMap<String, OaiPmhService>();
155: }
156:
157: //=============================================================================
|