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

Last change on this file since 26171 was 26171, checked in by ak19, 12 years ago

Uncommitted changes from ages back to fedoraGS3 classes to get greenstone to work as an interface to fedora repository backend.

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