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.exceptions;
025:
026: import java.util.Map;
027: import org.fao.oaipmh.OaiPmh;
028: import org.fao.oaipmh.util.Lib;
029: import org.jdom.Element;
030:
031: //=============================================================================
032:
033: public class OaiPmhException extends Exception {
034: //---------------------------------------------------------------------------
035: //---
036: //--- Constructor
037: //---
038: //---------------------------------------------------------------------------
039:
040: public OaiPmhException(String code, String message) {
041: this (code, message, null);
042: }
043:
044: //---------------------------------------------------------------------------
045:
046: public OaiPmhException(String code, String message, Element response) {
047: super (message);
048:
049: this .code = code;
050: this .response = response;
051: }
052:
053: //---------------------------------------------------------------------------
054: //---
055: //--- API methods
056: //---
057: //---------------------------------------------------------------------------
058:
059: public String getCode() {
060: return code;
061: }
062:
063: public Element getResponse() {
064: return response;
065: }
066:
067: //---------------------------------------------------------------------------
068:
069: public String toString() {
070: return getClass().getSimpleName() + ": code=" + code
071: + ", message=" + getMessage();
072: }
073:
074: //---------------------------------------------------------------------------
075: //---
076: //--- Static marshalling methods
077: //---
078: //---------------------------------------------------------------------------
079:
080: public static Element marshal(OaiPmhException e, String reqUrl,
081: Map<String, String> reqParams) {
082: Element err = new Element("error", OaiPmh.Namespaces.OAI_PMH);
083:
084: err.setText(e.getMessage());
085: err.setAttribute("code", e.getCode());
086:
087: Element root = Lib.createOaiRoot(reqUrl, reqParams, err);
088:
089: return root;
090: }
091:
092: //---------------------------------------------------------------------------
093:
094: public static void unmarshal(Element response)
095: throws OaiPmhException {
096: Element error = response.getChild("error",
097: OaiPmh.Namespaces.OAI_PMH);
098:
099: //--- no errors: ok, skip
100: if (error == null)
101: return;
102:
103: String code = error.getAttributeValue("code");
104: String msg = error.getText();
105:
106: if (code.equals(BAD_ARGUMENT))
107: throw new BadArgumentException(msg, response);
108:
109: if (code.equals(BAD_RESUMPTION_TOKEN))
110: throw new BadResumptionTokenException(msg, response);
111:
112: if (code.equals(BAD_VERB))
113: throw new BadVerbException(msg, response);
114:
115: if (code.equals(CANNOT_DISSEMINATE_FORMAT))
116: throw new CannotDisseminateFormatException(msg, response);
117:
118: if (code.equals(ID_DOES_NOT_EXIST))
119: throw new IdDoesNotExistException(msg, response);
120:
121: if (code.equals(NO_RECORDS_MATCH))
122: throw new NoRecordsMatchException(msg, response);
123:
124: if (code.equals(NO_METADATA_FORMATS))
125: throw new NoMetadataFormatsException(msg, response);
126:
127: if (code.equals(NO_SET_HIERARCHY))
128: throw new NoSetHierarchyException(msg, response);
129:
130: //--- we should not get here
131: throw new RuntimeException("Unknown error code : " + code);
132: }
133:
134: //---------------------------------------------------------------------------
135: //---
136: //--- Variables
137: //---
138: //---------------------------------------------------------------------------
139:
140: protected static final String BAD_ARGUMENT = "badArgument";
141: protected static final String BAD_RESUMPTION_TOKEN = "badResumptionToken";
142: protected static final String BAD_VERB = "badVerb";
143: protected static final String CANNOT_DISSEMINATE_FORMAT = "cannotDisseminateFormat";
144: protected static final String ID_DOES_NOT_EXIST = "idDoesNotExist";
145: protected static final String NO_RECORDS_MATCH = "noRecordsMatch";
146: protected static final String NO_METADATA_FORMATS = "noMetadataFormats";
147: protected static final String NO_SET_HIERARCHY = "noSetHierarchy";
148:
149: //---------------------------------------------------------------------------
150:
151: private String code;
152: private Element response;
153: }
154:
155: //=============================================================================
|