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.csw.common.exceptions;
025:
026: import java.util.List;
027: import jeeves.utils.Xml;
028: import org.fao.geonet.csw.common.Csw;
029: import org.jdom.Element;
030: import org.jdom.Namespace;
031:
032: //=============================================================================
033:
034: public abstract class CatalogException extends Exception {
035: protected static final String INVALID_PARAMETER_VALUE = "InvalidParameterValue";
036: protected static final String INVALID_UPDATE_SEQUENCE = "InvalidUpdateSequence";
037: protected static final String MISSING_PARAMETER_VALUE = "MissingParameterValue";
038: protected static final String NO_APPLICABLE_CODE = "NoApplicableCode";
039: protected static final String OPERATION_NOT_SUPPORTED = "OperationNotSupported";
040: protected static final String VERSION_NEGOTIATION_FAILED = "VersionNegotiationFailed";
041:
042: //---------------------------------------------------------------------------
043:
044: private String code;
045: private String locator;
046:
047: //---------------------------------------------------------------------------
048: //---
049: //--- Constructor
050: //---
051: //---------------------------------------------------------------------------
052:
053: public CatalogException(String code, String message, String locator) {
054: super (message);
055:
056: this .code = code;
057: this .locator = locator;
058: }
059:
060: //---------------------------------------------------------------------------
061:
062: public CatalogException(String code, String message,
063: String locator, CatalogException cause) {
064: super (message, cause);
065:
066: this .code = code;
067: this .locator = locator;
068: }
069:
070: //---------------------------------------------------------------------------
071: //---
072: //--- API methods
073: //---
074: //---------------------------------------------------------------------------
075:
076: public String getCode() {
077: return code;
078: }
079:
080: public String getLocator() {
081: return locator;
082: }
083:
084: //---------------------------------------------------------------------------
085:
086: public String toString() {
087: String clazz = getClass().getName();
088: return clazz + ": code=" + code + ", locator=" + locator
089: + ", message=" + getMessage();
090: }
091:
092: //---------------------------------------------------------------------------
093:
094: public static Element marshal(CatalogException e) {
095: //--- setup root node
096:
097: Element root = new Element("ExceptionReport", Csw.NAMESPACE_OWS);
098: root.setAttribute("version", Csw.OWS_VERSION);
099:
100: while (e != null) {
101: Element exc = new Element("Exception", Csw.NAMESPACE_OWS);
102: exc.setAttribute("exceptionCode", e.getCode());
103:
104: if (e.getMessage() != null)
105: exc.addContent(new Element("ExceptionText",
106: Csw.NAMESPACE_OWS).setText(e.getMessage()));
107:
108: if (e.getLocator() != null)
109: exc.setAttribute("locator", e.getLocator());
110:
111: root.addContent(exc);
112:
113: e = (CatalogException) e.getCause();
114: }
115:
116: return root;
117: }
118:
119: //---------------------------------------------------------------------------
120:
121: public static void unmarshal(Element response)
122: throws CatalogException, Exception {
123: if (!response.getName().equals("ExceptionReport"))
124: return;
125:
126: Namespace ns = response.getNamespace();
127:
128: List exceptions = response.getChildren("Exception", ns);
129:
130: if (exceptions.size() == 0)
131: throw new Exception(
132: "Bad exception (no 'Exception' elem) : \n"
133: + Xml.getString(response));
134:
135: CatalogException e = null;
136:
137: for (int i = exceptions.size() - 1; i >= 0; i--) {
138: Element ex = (Element) exceptions.get(i);
139:
140: e = createException(ex, response, e);
141: }
142:
143: throw e;
144: }
145:
146: //---------------------------------------------------------------------------
147:
148: private static CatalogException createException(Element ex,
149: Element response, CatalogException prev) throws Exception {
150: Namespace ns = response.getNamespace();
151:
152: String code = ex.getAttributeValue("exceptionCode");
153:
154: if (code == null)
155: throw new Exception(
156: "Bad exception (no 'exceptionCode' attr) : \n"
157: + Xml.getString(response));
158:
159: Element text = ex.getChild("ExceptionText", ns);
160:
161: String locator = ex.getAttributeValue("locator");
162: String message = (text == null) ? null : text.getText();
163:
164: CatalogException e = null;
165:
166: if (code.equals(INVALID_PARAMETER_VALUE))
167: e = new InvalidParameterValueEx(null, message, prev);
168:
169: if (code.equals(INVALID_UPDATE_SEQUENCE))
170: e = new InvalidUpdateSequenceEx(message, prev);
171:
172: if (code.equals(MISSING_PARAMETER_VALUE))
173: e = new MissingParameterValueEx(null, prev);
174:
175: if (code.equals(NO_APPLICABLE_CODE))
176: e = new NoApplicableCodeEx(message, prev);
177:
178: if (code.equals(OPERATION_NOT_SUPPORTED))
179: e = new OperationNotSupportedEx(null, prev);
180:
181: if (code.equals(VERSION_NEGOTIATION_FAILED))
182: e = new VersionNegotiationFailedEx(message, prev);
183:
184: if (e == null)
185: throw new Exception(
186: "Bad exception (unknown 'exceptionCode') : \n"
187: + Xml.getString(response));
188:
189: e.locator = locator;
190:
191: return e;
192: }
193: }
194:
195: //=============================================================================
196: /*
197:
198: il Content-Type deve essere "application/soap+xml; charset="utf-8" "
199: il Content-Length deve essere settato
200:
201: <?xml version="1.0" ?>
202:
203: <env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
204: <env:Body>
205: <env:Fault>
206: <env:Code>
207: <env:Value>env:Sender|env:Receiver</env:Value>
208: [ <env:Subcode>
209: <env:Value>gn:xxx</env:Value>
210: [ <env:Subcode>...</env:Subcode> ]
211: </env:Subcode> ]
212: </env:Code>
213:
214: <env:Reason>
215: <env:Text xml:lang="en">...human readable...</env:Text>
216: ...
217: </env:Reason>
218:
219: <env:Detail>
220: <... env:encodingStyle="geonet.org/encoding/error">
221: ...altri figli con pi� info
222: </...>
223: </env:Detail>
224: </env:Fault>
225: </env:Body>
226: </env:Envelope>
227:
228:
229:
230: <env:Code>
231: <env:Value>env:Sender|env:Receiver</env:Value>
232: [ <env:Subcode>
233: <env:Value>gn:xxx</env:Value>
234: [ <env:Subcode>...</env:Subcode> ]
235: </env:Subcode> ]
236: </env:Code>
237:
238: <env:Reason>
239: <env:Text xml:lang="en">...human readable...</env:Text>
240: ...
241: </env:Reason>
242:
243: <env:Detail>
244: <... env:encodingStyle="geonet.org/encoding/error">
245: ...altri figli con pi� info
246: </...>
247: </env:Detail>
248:
249:
250:
251:
252: <env:faultcode>env:Server</env:faultcode>
253: <env:faultstring>...human readable...</env:faultstring>
254:
255: <env:detail>
256: <ows:ExceptionReport>...
257: </env:detail>
258:
259: */
|