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

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

Exception output strings changed in method FedoraGS3RunException.getMessage()

File size: 12.0 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 if(specifics != null && !specifics.equals("")) {
178 msg += ", possibly trouble finding " + specifics;
179 }
180 // Some info on what caused the Remote Exception:
181 msg += ".\nCause: " + cause.getMessage();
182 } else { // Other kind of cause to exception:
183 if(cause instanceof UnsupportedEncodingException) {
184 msg = "(UnsupportedEncodingException): trouble converting "
185 + specifics + "returned "
186 + "from Fedora web services to " + FedoraConnection.UTF8;
187 } else if(cause instanceof SAXException) {
188 msg = "(SAXException): trouble parsing "
189 + specifics + " returned from web service";
190 } else if(cause instanceof IOException) {
191 msg = "(IOException): trouble parsing "
192 + specifics + " returned from web service";
193 } /*else if(cause instanceof TransformerException) {
194 msg = msg + "(TransformerException) - trouble converting original XML "
195 + "response into String";
196 }*/
197 // else (other Exception, including FedoraVersionNotSupportedException)
198 // in those cases, just return it as it is.
199 // else msg = "";
200 if(!msg.equals(""))
201 msg = " " + msg + ". ";
202 }
203 return this.getClass().getCanonicalName()
204 + msg + super.getMessage();
205 }
206 }
207
208 /** Static inner class FedoraGS3InitFailureException is an Exception
209 * that is thrown when no connection can be made to the Fedora server
210 * and therefore the FedoraGS3 object construction failed.
211 * Its constructor allows other exceptions to be encapsulated within
212 * it, via the cause object (a Throwable, superclass of Exception)
213 * parameter. This (the actual exception that caused the
214 * FedoraGS3InitFailureException) can be queried by interested parties
215 * via the getCause() method. */
216 public static class FedoraGS3InitFailureException extends FedoraGS3Exception {
217 public static final String MESSAGE =
218 "Unable to instantiate FedoraToGS3Interface";
219 public FedoraGS3InitFailureException(){ super(MESSAGE); }
220 public FedoraGS3InitFailureException(String message){ super(message); }
221 public FedoraGS3InitFailureException(String message, Throwable cause) {
222 super(message, cause);
223 }
224 public FedoraGS3InitFailureException(Throwable cause) {
225 super(MESSAGE+ " due to exception "
226 + cause.getClass() + ":\n" + cause.getMessage(), cause);
227 }
228 }
229
230 //***************** FEDORA CONNECTION EXCEPTIONS**********************/
231 /**
232 * When the user chooses to cancel out of the FedoraConnection dialog of
233 * the constructor (where they have to enter authentication information),
234 * this exception is thrown, so that classes making use of FedoraConnection
235 * may choose whether to exit their program or deal with it differently.
236 * */
237 public static class CancelledException extends Exception {
238 public CancelledException() {
239 super("User cancelled out of connecting to Fedora.\n"
240 + "Cannot instantiate FedoraConnection.\n");
241 }
242 }
243
244 /**
245 * This AuthenticationFailedException can be thrown when the user enters
246 * an invalid username and password into the FedoraConnection Authentication
247 * popup dialog (displayed upon FedoraConnection object construction).
248 */
249 public static class AuthenticationFailedException extends Exception {
250 public AuthenticationFailedException() {
251 super("(401) Incorrect username and/or password. "
252 + "Please re-enter values.");
253 }
254 }
255
256 /**
257 * This ServerNotFoundException can be thrown when the user enters
258 * there is no (Fedora) server listening at a given host and port.
259 * Thrown when a 404 Not Found occurs.
260 */
261 public static class ServerNotFoundException extends Exception {
262 public ServerNotFoundException(String message) {
263 super(message);
264 }
265 }
266
267 /**
268 * This AuthenticationFailedException can be thrown when there is some
269 * server listening at the host and port values entered by the user, but
270 * when this server is (most likely) not a Fedora Server.
271 * The IOException message received in such cases is:
272 * "Unable to instantiate FedoraConnection
273 * java.io.IOException: Request failed [404 /fedora/describe]"
274 * And in such cases, we can throw this NotAFedoraServerException
275 * to deal with it more particularly than more general IOExceptions.
276 */
277 public static class NotAFedoraServerException extends Exception {
278 public static final String MESSAGE =
279 "There is a server listening at the given host and port,"
280 + "\nbut it does not seem to be a Fedora server."
281 + "\nTry changing the host and/or port values.";
282
283 public NotAFedoraServerException() {
284 super(MESSAGE);
285 }
286
287 public NotAFedoraServerException(String message) {
288 super(MESSAGE + ":\n(" + message + ")");
289 }
290 }
291
292 /**
293 * Certain functionality in Fedora - in particular fielded search - is
294 * implemented differently or uses slightly different Fedora types in older
295 * versions of Fedora (fielded search in 2.0 uses a different Condition class).
296 * In that case, the fielded search web service cannot be performed, and
297 * this FedoraVersionNotSupportedException is thrown.
298 */
299 public static class FedoraVersionNotSupportedException extends Exception {
300
301 public FedoraVersionNotSupportedException(String fedoraVersion) {
302 super("Fedora repository version is " + fedoraVersion
303 + "\nHowever, browsing/searching by field only works "
304 + "from version " + FedoraConnection.SUPPORTED_VERSION + " onwards.");
305 }
306 }
307}
Note: See TracBrowser for help on using the repository browser.