source: other-projects/trunk/gs3-webservices-democlient/src/GS3Fedora/org/greenstone/fedora/services/FedoraGS3Exception.java@ 15222

Last change on this file since 15222 was 15222, checked in by ak19, 16 years ago

Greenstone3 web services demo-clientadded to GS3's other-projects

File size: 11.8 KB
Line 
1/**
2 *#########################################################################
3 * FedoraGS3Exception.java - works with the demo-client for Greenstone 3,
4 * of the Greenstone digital library suite from the New Zealand Digital
5 * Library Project at the * University of Waikato, New Zealand.
6 * <BR><BR>
7 * Copyright (C) 2008 New Zealand Digital Library Project
8 * <BR><BR>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 * <BR><BR>
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *########################################################################
19 */
20
21package org.greenstone.fedora.services;
22
23import java.io.IOException;
24import java.io.UnsupportedEncodingException;
25import java.rmi.RemoteException;
26
27import org.apache.log4j.Logger;
28import org.greenstone.gsdl3.util.GSXML;
29import org.xml.sax.SAXException;
30
31/**
32 * The exceptions that can be thrown by FedoraGS3. This class
33 * represents a general FedoraGS3 exception. It will LOG the
34 * exceptions for all the (static inner) subclasses. And, through
35 * the Cause object, also for the static inner Exception classes
36 * that do not inherit but are encapsulated in subclass
37 * FedoraGS3RunException when its constructor is called with the
38 * particular Exception type. The 'Cause' Exception classes therefore
39 * indicate the root causes for why a FedoraGS3RunException was thrown.
40 * @author ak19
41*/
42public class FedoraGS3Exception extends Exception {
43 /** Constant string message to display when a connection is refused */
44 public static final String connectionRefusedMessage
45 = "\nUnable to connect. Either the host"
46 + " and/or port contain invalid values\nOR the fedora"
47 + " server at that location is down/not yet running."
48 + "\nTry once more by changing the values for host or port.";
49 /** Constant string message to display if the sslHandshake fails
50 * when trying to connect to Fedora using https */
51 public static final String sslHandshakeExceptionMessage
52 = "SSLHandshakeException.\nThe protocol 'https' might be"
53 + "incorrect.\nTry once more. Perhaps with 'http' instead.";
54 /** Constant string message to check against if "AXIS engine
55 * could not find a target service to invoke" message is the cause
56 * for an AxisFault exception embedded in a RemoteException */
57 public static final String missingTargetService
58 = "could not find a target service to invoke";
59 /** The logging instance for this class */
60 private static final Logger LOG = Logger.getLogger(
61 FedoraGS3Exception.class.getName());
62
63 /** No argument constructor merely sets the message to
64 * this class' name. */
65 public FedoraGS3Exception(){
66 super("FedoraGS3Exception");
67 if(this.getMessage() != null) {
68 LOG.error("Message: " + this.getMessage());
69 }
70 if(this.getCause() != null) {
71 LOG.error("Cause: " + this.getCause().getMessage());
72 }
73 LOG.debug(this); // stacktrace
74 }
75
76 /** Constructs a FedoraGS3Exception with the given message.
77 * @param message is an information String of what went wrong. */
78 public FedoraGS3Exception(String message){
79 super(message);
80 if(this.getMessage() != null) {
81 LOG.error("Message: " + this.getMessage());
82 }
83 if(this.getCause() != null) {
84 LOG.error("Cause: " + this.getCause().getMessage());
85 }
86 LOG.debug(this); // stacktrace
87 }
88
89 /** Constructs a FedoraGS3Exception with the given message and cause.
90 * @param message is an information String of what went wrong.
91 * @param cause is a Throwable object (perhaps another Exception)
92 * of what caused this exception to occur. */
93 public FedoraGS3Exception(String message, Throwable cause) {
94 super(message, cause);
95 if(this.getMessage() != null) {
96 LOG.error("Message: " + this.getMessage());
97 }
98 if(this.getCause() != null) {
99 LOG.error("Cause: " + this.getCause().getMessage());
100 }
101 LOG.debug(this); // stacktrace
102 }
103
104 /** Constructs a FedoraGS3Exception with the given cause.
105 * @param cause is a Throwable object (perhaps another Exception)
106 * of what caused this exception to occur. */
107 public FedoraGS3Exception(Throwable cause) {
108 super("FedoraGS3Exception", cause);
109 if(this.getMessage() != null) {
110 LOG.error("Message: " + this.getMessage());
111 }
112 if(this.getCause() != null) {
113 LOG.error("Cause: " + this.getCause().getMessage());
114 }
115 LOG.debug(this); // stacktrace
116 }
117
118 /** Represents an exception that occurs when FedoraGS3 is running
119 * A subclass of FedoraGS3Exception */
120 public static class FedoraGS3RunException extends FedoraGS3Exception {
121 /** If a problem occurs converting the error message into XML
122 * this string is created in the format of XML stating the
123 * problem that occurred when trying to convert XML to String. */
124 protected static String xmlToStringConversionFailureResponseMsg;
125 /** Some extra information as to what when wrong */
126 protected String specifics = "";
127
128 static {
129 StringBuffer buf = new StringBuffer("<" + GSXML.MESSAGE_ELEM + ">");
130 buf.append("<" + GSXML.RESPONSE_ELEM + " "
131 + GSXML.FROM_ATT + "=\"" + "FedoraGS3\"" + ">");
132 buf.append("<" + GSXML.ERROR_ELEM + " "
133 + GSXML.ERROR_TYPE_ATT + "=\""+ GSXML.ERROR_TYPE_OTHER + "\"" + ">");
134 buf.append("(TransformerException): trouble converting original XML "
135 + "response message into String.");
136 buf.append("</" + GSXML.ERROR_ELEM + ">");
137 buf.append("</" + GSXML.RESPONSE_ELEM + ">");
138 buf.append("</" + GSXML.MESSAGE_ELEM + ">");
139 xmlToStringConversionFailureResponseMsg = buf.toString();
140 //System.err.println(xmlToStringConversionFailureResponseMsg);
141 }
142
143 public FedoraGS3RunException() {
144 super("FedoraGS3RunException");
145 }
146
147 public FedoraGS3RunException(String message){
148 super(message);
149 }
150
151 public FedoraGS3RunException(String message, Throwable cause) {
152 super("FedoraGS3RunException " + message, cause);
153 }
154
155 public FedoraGS3RunException(Throwable cause) {
156 super("Caused by "
157 + cause.getClass() + ":\n" + cause.getMessage(), cause);
158 }
159
160 public void setSpecifics(String specifics) {
161 this.specifics = specifics;
162 }
163
164 /** Overloading getMessage() to return some details as to what
165 * exactly went wrong. */
166 public String getMessage() {
167 String msg = "";
168 Throwable cause = this.getCause();
169
170 if(cause == null)
171 return super.getMessage();
172
173 if(cause instanceof RemoteException) {
174 // specifically indicate it's a RemoteException (as it encapsulates
175 // different exception classes)
176 msg = "(RemoteException): problem invoking web service, " +
177 "possibly trouble finding " + specifics;
178 } else if(cause instanceof UnsupportedEncodingException) {
179 msg = "(UnsupportedEncodingException): trouble converting "
180 + specifics + "returned "
181 + "from Fedora web services to " + FedoraConnection.UTF8;
182 } else if(cause instanceof SAXException) {
183 msg = "(SAXException): trouble parsing "
184 + specifics + " returned from web service";
185 } else if(cause instanceof IOException) {
186 msg = "(IOException): trouble parsing "
187 + specifics + " returned from web service";
188 }
189 /*else if(cause instanceof TransformerException) {
190 msg = msg + "(TransformerException) - trouble converting original XML "
191 + "response into String";
192 }*/
193 // else (other Exception, including FedoraVersionNotSupportedException)
194 // in those cases, just return it as it is.
195
196 // else msg = "";
197 if(!msg.equals(""))
198 msg = " " + msg + ". ";
199
200 return this.getClass().getCanonicalName()
201 + msg + super.getMessage();
202 }
203 }
204
205 /** Static inner class FedoraGS3InitFailureException is an Exception
206 * that is thrown when no connection can be made to the Fedora server
207 * and therefore the FedoraGS3 object construction failed.
208 * Its constructor allows other exceptions to be encapsulated within
209 * it, via the cause object (a Throwable, superclass of Exception)
210 * parameter. This (the actual exception that caused the
211 * FedoraGS3InitFailureException) can be queried by interested parties
212 * via the getCause() method. */
213 public static class FedoraGS3InitFailureException extends FedoraGS3Exception {
214 public static final String MESSAGE =
215 "Unable to instantiate FedoraToGS3Interface";
216 public FedoraGS3InitFailureException(){ super(MESSAGE); }
217 public FedoraGS3InitFailureException(String message){ super(message); }
218 public FedoraGS3InitFailureException(String message, Throwable cause) {
219 super(message, cause);
220 }
221 public FedoraGS3InitFailureException(Throwable cause) {
222 super(MESSAGE+ " due to exception "
223 + cause.getClass() + ":\n" + cause.getMessage(), cause);
224 }
225 }
226
227 //***************** FEDORA CONNECTION EXCEPTIONS**********************/
228 /**
229 * When the user chooses to cancel out of the FedoraConnection dialog of
230 * the constructor (where they have to enter authentication information),
231 * this exception is thrown, so that classes making use of FedoraConnection
232 * may choose whether to exit their program or deal with it differently.
233 * */
234 public static class CancelledException extends Exception {
235 public CancelledException() {
236 super("User cancelled out of connecting to Fedora.\n"
237 + "Cannot instantiate FedoraConnection.\n");
238 }
239 }
240
241 /**
242 * This AuthenticationFailedException can be thrown when the user enters
243 * an invalid username and password into the FedoraConnection Authentication
244 * popup dialog (displayed upon FedoraConnection object construction).
245 */
246 public static class AuthenticationFailedException extends Exception {
247 public AuthenticationFailedException() {
248 super("(401) Incorrect username and/or password. "
249 + "Please re-enter values.");
250 }
251 }
252
253 /**
254 * This ServerNotFoundException can be thrown when the user enters
255 * there is no (Fedora) server listening at a given host and port.
256 * Thrown when a 404 Not Found occurs.
257 */
258 public static class ServerNotFoundException extends Exception {
259 public ServerNotFoundException(String message) {
260 super(message);
261 }
262 }
263
264 /**
265 * This AuthenticationFailedException can be thrown when there is some
266 * server listening at the host and port values entered by the user, but
267 * when this server is (most likely) not a Fedora Server.
268 * The IOException message received in such cases is:
269 * "Unable to instantiate FedoraConnection
270 * java.io.IOException: Request failed [404 /fedora/describe]"
271 * And in such cases, we can throw this NotAFedoraServerException
272 * to deal with it more particularly than more general IOExceptions.
273 */
274 public static class NotAFedoraServerException extends Exception {
275 public static final String MESSAGE =
276 "There is a server listening at the given host and port,"
277 + "\nbut it does not seem to be a Fedora server."
278 + "\nTry changing the host and/or port values.";
279
280 public NotAFedoraServerException() {
281 super(MESSAGE);
282 }
283
284 public NotAFedoraServerException(String message) {
285 super(MESSAGE + ":\n(" + message + ")");
286 }
287 }
288
289 /**
290 * Certain functionality in Fedora - in particular fielded search - is
291 * implemented differently or uses slightly different Fedora types in older
292 * versions of Fedora (fielded search in 2.0 uses a different Condition class).
293 * In that case, the fielded search web service cannot be performed, and
294 * this FedoraVersionNotSupportedException is thrown.
295 */
296 public static class FedoraVersionNotSupportedException extends Exception {
297
298 public FedoraVersionNotSupportedException(String fedoraVersion) {
299 super("Fedora repository version is " + fedoraVersion
300 + "\nHowever, browsing/searching by field only works "
301 + "from version " + FedoraConnection.SUPPORTED_VERSION + " onwards.");
302 }
303 }
304}
Note: See TracBrowser for help on using the repository browser.