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.server;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028: import org.fao.geonet.util.ISODate;
029: import org.fao.oaipmh.exceptions.BadArgumentException;
030: import org.fao.oaipmh.exceptions.BadVerbException;
031: import org.fao.oaipmh.requests.AbstractRequest;
032: import org.fao.oaipmh.requests.GetRecordRequest;
033: import org.fao.oaipmh.requests.IdentifyRequest;
034: import org.fao.oaipmh.requests.ListIdentifiersRequest;
035: import org.fao.oaipmh.requests.ListMetadataFormatsRequest;
036: import org.fao.oaipmh.requests.ListRecordsRequest;
037: import org.fao.oaipmh.requests.ListSetsRequest;
038: import org.jdom.Element;
039:
040: //=============================================================================
041:
042: public class OaiPmhFactory {
043: //---------------------------------------------------------------------------
044: //---
045: //--- API methods
046: //---
047: //---------------------------------------------------------------------------
048:
049: public static Map<String, String> extractParams(Element request)
050: throws BadArgumentException {
051: Map<String, String> params = new HashMap<String, String>();
052:
053: for (Object o : request.getChildren()) {
054: Element elem = (Element) o;
055: String name = elem.getName();
056: String value = elem.getText();
057:
058: if (params.containsKey(name))
059: throw new BadArgumentException("Parameter repeated : "
060: + name);
061:
062: params.put(name, value);
063: }
064:
065: return params;
066: }
067:
068: //---------------------------------------------------------------------------
069:
070: public static AbstractRequest parse(Map<String, String> params)
071: throws BadVerbException, BadArgumentException {
072: //--- duplicate parameters because the below procedure will consume them
073:
074: Map<String, String> params2 = new HashMap<String, String>();
075:
076: for (String key : params.keySet())
077: params2.put(key, params.get(key));
078:
079: params = params2;
080:
081: //--- proper processing
082:
083: String verb = consumeMan(params, "verb");
084:
085: if (verb.equals(IdentifyRequest.VERB))
086: return handleIdentify(params);
087:
088: if (verb.equals(GetRecordRequest.VERB))
089: return handleGetRecord(params);
090:
091: if (verb.equals(ListIdentifiersRequest.VERB))
092: return handleListIdentifiers(params);
093:
094: if (verb.equals(ListMetadataFormatsRequest.VERB))
095: return handleListMdFormats(params);
096:
097: if (verb.equals(ListRecordsRequest.VERB))
098: return handleListRecords(params);
099:
100: if (verb.equals(ListSetsRequest.VERB))
101: return handleListSets(params);
102:
103: throw new BadVerbException("Unknown verb : " + verb);
104: }
105:
106: //---------------------------------------------------------------------------
107: //---
108: //--- Private methods
109: //---
110: //---------------------------------------------------------------------------
111:
112: private static String consumeMan(Map<String, String> params,
113: String name) throws BadArgumentException {
114: String value = params.get(name);
115:
116: if (value == null)
117: throw new BadArgumentException("Missing '" + name
118: + "' parameter");
119:
120: if (value.trim().length() == 0)
121: throw new BadArgumentException("Empty '" + name
122: + "' parameter");
123:
124: params.remove(name);
125:
126: return value;
127: }
128:
129: //---------------------------------------------------------------------------
130:
131: private static String consumeOpt(Map<String, String> params,
132: String name) throws BadArgumentException {
133: String value = params.get(name);
134:
135: if (value == null)
136: return null;
137:
138: if (value.trim().length() == 0)
139: throw new BadArgumentException("Empty '" + name
140: + "' parameter");
141:
142: params.remove(name);
143:
144: return value;
145: }
146:
147: //---------------------------------------------------------------------------
148:
149: private static ISODate consumeDate(Map<String, String> params,
150: String name) throws BadArgumentException {
151: String date = consumeOpt(params, name);
152:
153: if (date == null)
154: return null;
155:
156: try {
157: return new ISODate(date);
158: } catch (Exception e) {
159: throw new BadArgumentException("Illegal date format : "
160: + date);
161: }
162: }
163:
164: //---------------------------------------------------------------------------
165:
166: private static void checkConsumption(Map<String, String> params)
167: throws BadArgumentException {
168: if (params.keySet().size() == 0)
169: return;
170:
171: String extraParam = params.keySet().iterator().next();
172:
173: throw new BadArgumentException("Unknown extra parameter '"
174: + extraParam + "'");
175: }
176:
177: //---------------------------------------------------------------------------
178: //---
179: //--- Dispatching methods
180: //---
181: //---------------------------------------------------------------------------
182:
183: private static IdentifyRequest handleIdentify(
184: Map<String, String> params) throws BadArgumentException {
185: checkConsumption(params);
186:
187: return new IdentifyRequest();
188: }
189:
190: //---------------------------------------------------------------------------
191:
192: private static GetRecordRequest handleGetRecord(
193: Map<String, String> params) throws BadArgumentException {
194: GetRecordRequest req = new GetRecordRequest();
195:
196: req.setIdentifier(consumeMan(params, "identifier"));
197: req.setMetadataPrefix(consumeMan(params, "metadataPrefix"));
198:
199: checkConsumption(params);
200:
201: return req;
202: }
203:
204: //---------------------------------------------------------------------------
205:
206: private static ListIdentifiersRequest handleListIdentifiers(
207: Map<String, String> params) throws BadArgumentException {
208: ListIdentifiersRequest req = new ListIdentifiersRequest();
209:
210: if (params.containsKey("resumptionToken"))
211: req
212: .setResumptionToken(consumeMan(params,
213: "resumptionToken"));
214: else {
215: req.setMetadataPrefix(consumeMan(params, "metadataPrefix"));
216: req.setSet(consumeOpt(params, "set"));
217:
218: req.setFrom(consumeDate(params, "from"));
219: req.setUntil(consumeDate(params, "until"));
220: }
221:
222: checkConsumption(params);
223:
224: return req;
225: }
226:
227: //---------------------------------------------------------------------------
228:
229: private static ListMetadataFormatsRequest handleListMdFormats(
230: Map<String, String> params) throws BadArgumentException {
231: ListMetadataFormatsRequest req = new ListMetadataFormatsRequest();
232:
233: req.setIdentifier(consumeOpt(params, "identifier"));
234: checkConsumption(params);
235:
236: return req;
237: }
238:
239: //---------------------------------------------------------------------------
240:
241: private static ListRecordsRequest handleListRecords(
242: Map<String, String> params) throws BadArgumentException {
243: ListRecordsRequest req = new ListRecordsRequest();
244:
245: if (params.containsKey("resumptionToken"))
246: req
247: .setResumptionToken(consumeMan(params,
248: "resumptionToken"));
249: else {
250: req.setMetadataPrefix(consumeMan(params, "metadataPrefix"));
251: req.setSet(consumeOpt(params, "set"));
252:
253: req.setFrom(consumeDate(params, "from"));
254: req.setUntil(consumeDate(params, "until"));
255: }
256:
257: checkConsumption(params);
258:
259: return req;
260: }
261:
262: //---------------------------------------------------------------------------
263:
264: private static ListSetsRequest handleListSets(
265: Map<String, String> params) throws BadArgumentException {
266: ListSetsRequest req = new ListSetsRequest();
267:
268: if (params.containsKey("resumptionToken"))
269: req
270: .setResumptionToken(consumeMan(params,
271: "resumptionToken"));
272:
273: checkConsumption(params);
274:
275: return req;
276: }
277: }
278:
279: //=============================================================================
|