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.oaipmh.requests;
025:
026: import java.io.File;
027: import java.io.IOException;
028: import java.util.Map;
029: import org.fao.oaipmh.exceptions.OaiPmhException;
030: import org.fao.oaipmh.responses.AbstractResponse;
031: import org.fao.oaipmh.util.Lib;
032: import org.fao.oaipmh.util.Xml;
033: import org.jdom.Element;
034: import org.jdom.JDOMException;
035: import org.xml.sax.SAXException;
036:
037: //=============================================================================
038:
039: public abstract class AbstractRequest {
040: //---------------------------------------------------------------------------
041: //---
042: //--- API methods
043: //---
044: //---------------------------------------------------------------------------
045:
046: public void setValidationSchema(File schemaPath) {
047: this .schemaPath = schemaPath;
048: }
049:
050: //---------------------------------------------------------------------------
051:
052: public Transport getTransport() {
053: return transport;
054: }
055:
056: //---------------------------------------------------------------------------
057:
058: public void setTransport(Transport t) {
059: transport = t;
060: }
061:
062: //---------------------------------------------------------------------------
063:
064: public abstract String getVerb();
065:
066: public abstract AbstractResponse execute() throws IOException,
067: OaiPmhException, JDOMException, SAXException, Exception;
068:
069: //---------------------------------------------------------------------------
070: //---
071: //--- Protected methods
072: //---
073: //---------------------------------------------------------------------------
074:
075: protected Element sendRequest(Map<String, String> params)
076: throws IOException, OaiPmhException, JDOMException,
077: SAXException, Exception {
078: transport.clearParameters();
079:
080: for (String name : params.keySet())
081: transport.addParameter(name, params.get(name));
082:
083: transport.addParameter("verb", getVerb());
084:
085: Element response = transport.execute();
086:
087: if (!Lib.isRootValid(response))
088: throw new Exception("Response is not in OAI-PMH format");
089:
090: //--- if a schema is provided, validate the result
091:
092: if (schemaPath != null)
093: Xml.validate(schemaPath.getAbsolutePath(), response);
094:
095: //--- raises an exception if the case
096: OaiPmhException.unmarshal(response);
097:
098: return response;
099: }
100:
101: //---------------------------------------------------------------------------
102: //---
103: //--- Variables
104: //---
105: //---------------------------------------------------------------------------
106:
107: private File schemaPath;
108: private Transport transport = new Transport();
109: }
110:
111: //=============================================================================
|