From 4164c0743ecef0bbfeae9e3ed1a61837875d6d52 Mon Sep 17 00:00:00 2001 From: Nathanael Jourdane Date: Tue, 15 Mar 2016 14:24:16 +0100 Subject: [PATCH] Use dedicated exceptions which extends SendQueryException and CantFillTableException. --- src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java | 6 +++--- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java | 44 +++++++++++++++++++++----------------------- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java | 45 ++++++++++++++++++++------------------------- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java | 252 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------- src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java | 2 +- 5 files changed, 268 insertions(+), 81 deletions(-) diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java index bfc15cb..a081b06 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java @@ -180,9 +180,9 @@ public class EpnTapController implements MainViewListener { logger.warning("Event " + event.toString() + " detected but is not implemented."); } } catch (Exception e) { - mainView.displayError("Error", "An unexpected error occured: " + e.getMessage() - + ".\nPlease report it to the developper team along with the stacktrace."); - logger.log(Level.SEVERE, "Error occured when " + event.toString(), e); + mainView.displayError("Error", e.getMessage()); + logger.log(Level.WARNING, + "Exception thrown when " + event.toString() + ": " + e.getMessage()); } } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java index 9a440d8..923638d 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableConnection.java @@ -23,7 +23,6 @@ import java.io.InputStreamReader; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; -import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.text.SimpleDateFormat; @@ -31,7 +30,12 @@ import java.util.Date; import java.util.logging.Logger; import eu.omp.irap.vespa.epntapclient.utils.Const; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.SendQueryException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.BadResponseCodeException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetErrorStream; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantGetResponseCode; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantOpenConnectionException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException.CantPrintRequestResultException; /** * This class provide HTTP connection support to send requests through POST / GET protocols. @@ -50,15 +54,14 @@ public final class VOTableConnection { } /** - * @param targetURL The url of the registry to communicate (ie. "http://reg.g-vo.org"). + * @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org"). * @param queryLanguage The language used for the queries (ie. "ADQL"). * @param query The query to ask to the registry. - * @throws HTTPRequestException Can not send the query. - * @throws BadRequestException The response code is not 200. * @return The path of the temporary XML file containing the http response. + * @throws CantSendQueryException If the query can not be sent. */ public static String sendQuery(String targetURL, String queryLanguage, - String query) throws SendQueryException { + String query) throws CantSendQueryException { SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd_hhmmss_SSS"); String voTablePath = Const.TMP_DIR + "/votable" + ft.format(new Date()) + ".xml"; @@ -70,14 +73,14 @@ public final class VOTableConnection { .replace(".", "%2E").replace("-", "%2D").replace("*", "%2A") .replace("_", "%5F"); } catch (UnsupportedEncodingException e) { - throw new SendQueryException("Can not encode URI " + uri, e); + throw new CantSendQueryException.MalformedURLException(uri, e); } logger.info("request: " + uri + "?" + parameters); try { printRequestResult(sendGet(uri, parameters), voTablePath); } catch (IOException e) { - throw new SendQueryException("Can not print the result in a file.", e); + throw new CantPrintRequestResultException(voTablePath, e); } return voTablePath; @@ -86,12 +89,11 @@ public final class VOTableConnection { /** * @param url The target URL of the request. * @param urlParameters The parameters of the request. - * @param resultFileName The name of the file where the request response content is writed. - * @throws HTTPRequestException Can not send the request. - * @throws BadRequestException If the + * @return A string builder containing the result of the query. + * @throws CantSendQueryException If the query can not be sent. */ public static StringBuilder sendGet(String url, String urlParameters) - throws SendQueryException { + throws CantSendQueryException { String inputLine; StringBuilder response = new StringBuilder(); URL obj; @@ -99,8 +101,8 @@ public final class VOTableConnection { try { obj = new URL(uri); - } catch (MalformedURLException e) { - throw new SendQueryException("The URI " + uri + " is not formated correctly.", e); + } catch (java.net.MalformedURLException e) { + throw new CantSendQueryException.MalformedURLException(uri, e); } HttpURLConnection con; @@ -109,7 +111,7 @@ public final class VOTableConnection { con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); } catch (IOException e) { - throw new SendQueryException("Can not open HTTP GET connection with uri " + uri, e); + throw new CantOpenConnectionException(uri, e); } con.setRequestProperty("User-Agent", USER_AGENT); @@ -118,11 +120,11 @@ public final class VOTableConnection { try { code = con.getResponseCode(); } catch (IOException e) { - throw new SendQueryException("Can not get the HTTP response code", e); + throw new CantGetResponseCode(uri, e); } if (code != HttpURLConnection.HTTP_OK && code != HttpURLConnection.HTTP_BAD_REQUEST) { - throw new SendQueryException("The server returned a bad response code: " + code); + throw new BadResponseCodeException(uri, code); } try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { @@ -130,7 +132,7 @@ public final class VOTableConnection { response.append(inputLine); } in.close(); - } catch (IOException e1) { + } catch (@SuppressWarnings("unused") IOException e1) { try (BufferedReader in = new BufferedReader( new InputStreamReader(con.getErrorStream()))) { while ((inputLine = in.readLine()) != null) { @@ -138,11 +140,7 @@ public final class VOTableConnection { } in.close(); } catch (IOException e2) { - throw new SendQueryException("Can not get the error stream.", e2); - } - if (code != HttpURLConnection.HTTP_BAD_REQUEST) { - throw new SendQueryException( - "The server returned a bad response code (not 200 neither 400).", e1); + throw new CantGetErrorStream(uri, e2); } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java index bc9d96f..d19f79d 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableController.java @@ -19,11 +19,12 @@ package eu.omp.irap.vespa.epntapclient.votable.controller; import java.io.IOException; import java.util.logging.Logger; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.ErrorInVOTableException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableCantFillVoTableException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableParsingException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableSeveralResourcesException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableSeveralTablesException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantFillTableException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantFillTableException.CantModifyVOTableException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantFillTableException.ErrorMessageInVOTableException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantFillTableException.SeveralResourcesException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantFillTableException.SeveralTablesException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantSendQueryException; import eu.omp.irap.vespa.epntapclient.votable.model.Table; import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; import eu.omp.irap.vespa.epntapclient.votable.view.VOTableView; @@ -52,7 +53,7 @@ public class VOTableController { * Method constructor * * @param voTablePath The path of the VOTable XML file. - * @throws VOTableException + * @throws VOTableException If something went wrong on the VOTable view or when filling table. */ public VOTableController(String voTablePath) throws VOTableException { view = new VOTableView(); @@ -62,7 +63,7 @@ public class VOTableController { /** * Method constructor * - * @param targetURL The url of the registry to communicate (ie. "http://reg.g-vo.org"). + * @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org"). * @param queryLanguage The language used for the queries (ie. "ADQL"). * @param query The query to ask to the registry. */ @@ -77,46 +78,40 @@ public class VOTableController { } /** - * @param targetURL The url of the registry to communicate (ie. "http://reg.g-vo.org"). + * @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org"). * @param queryLanguage The language used for the queries (ie. "ADQL"). * @param query The query to ask to the registry. - * @throws VOTableException Can not send the query or VOTable can not be parsed or can not fill - * the JTable. + * @throws CantSendQueryException If the query can not be sent. + * @throws CantFillTableException If the table can not be filled. @see CantFillTableException */ public void fillTable(String targetURL, String queryLanguage, String query) - throws VOTableException { - fillTable(VOTableConnection.sendQuery(targetURL, queryLanguage, query)); + throws CantFillTableException, CantSendQueryException { + String voTablePath = VOTableConnection.sendQuery(targetURL, queryLanguage, query); + fillTable(voTablePath); } /** * @param voTablePath The path of the VOTable file. - * @throws VOTableParsingException - * @throws VOTableSeveralResourcesException - * @throws ErrorInVOTableException - * @throws VOTableSeveralTablesException - * @throws VOTableCantFillVoTableException - * @throws VOTableException Can not fill the VOTable data in the JTable. + * @throws CantFillTableException If the VOTable can not be filled. */ public void fillTable(String voTablePath) - throws VOTableParsingException, VOTableSeveralResourcesException, - ErrorInVOTableException, VOTableSeveralTablesException, - VOTableCantFillVoTableException { + throws CantFillTableException { voTable = VOTableParser.parseVOTable(voTablePath); // TODO: Handle the case when there are more than 1 resource or table. if (voTable.getRESOURCE().size() > 1) { - throw new VOTableSeveralResourcesException(voTablePath); + throw new SeveralResourcesException(voTablePath); } // TODO: Iterate over all potential ERROR tags if ("ERROR".equals(voTable.getRESOURCE().get(0).getINFO().get(0).getValueAttribute())) { String errorInfo = voTable.getRESOURCE().get(0).getINFO().get(0).getValue(); - throw new ErrorInVOTableException(errorInfo); + throw new ErrorMessageInVOTableException(errorInfo); } if (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().size() > 1) { - throw new VOTableSeveralTablesException(voTablePath); + throw new SeveralTablesException(voTablePath); } Table table = (Table) (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0)); @@ -126,7 +121,7 @@ public class VOTableController { dataParser = new VOTableDataParser(table); view.fillTable(dataParser.getColumnsName(), dataParser.getDataArray()); } catch (IOException e) { - throw new VOTableCantFillVoTableException(e); + throw new CantModifyVOTableException(e); } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java index 75185b0..53b45fe 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableException.java @@ -19,60 +19,254 @@ package eu.omp.irap.vespa.epntapclient.votable.controller; import java.io.IOException; /** - * VOTable Exception class. + * VOTableException is the global exception for all the VOTable application. All other exceptions + * should extend it. * * @author N. Jourdane */ -@SuppressWarnings({ "javadoc", "serial" }) +@SuppressWarnings("serial") public abstract class VOTableException extends Exception { + /** The serial version UID (affected with a random number). */ + private static final long serialVersionUID = -5707662871529583209L; + + /** + * Method constructor + * + * @param message The message describing the exception displayed in the error dialog. + */ public VOTableException(String message) { super(message); } + /** + * Method constructor + * + * @param message The message describing the exception displayed in the error dialog. + * @param e The exception thrown. + */ public VOTableException(String message, Exception e) { super(message, e); } - public static class SendQueryException extends VOTableException { - public SendQueryException(String message) { - super("Can not send the query: " + message); + /** + * Abstract exception for when the application can not send the query. + * + * @author N. Jourdane + */ + public abstract static class CantSendQueryException extends VOTableException { + + /** + * Method constructor with a message. + * + * @param message The message describing the exception displayed in the error dialog. + */ + public CantSendQueryException(String message) { + super(message); } - public SendQueryException(String message, Exception e) { - super("Can not send the query: " + message + " because " + e.getMessage(), e); + /** + * Method constructor with a message and and exception. + * + * @param message The message describing the exception displayed in the error dialog. + * @param e The exception thrown. + */ + public CantSendQueryException(String message, Exception e) { + super(message, e); } - } - public static class VOTableParsingException extends VOTableException { - public VOTableParsingException(String message, Exception e) { - super("Can not parse the VOTable: " + message + " because " + e.getMessage(), e); + /** + * @author N. Jourdane + */ + public static class MalformedURLException extends CantSendQueryException { + /** + * Method constructor + * + * @param uri The URI sent to the server. + * @param e The exception thrown. + */ + public MalformedURLException(String uri, Exception e) { + super("The URL " + uri + " is not correctly formated.", e); + } } - } - public static class VOTableSeveralResourcesException extends VOTableException { - public VOTableSeveralResourcesException(String voTablePath) { - super("VOTable with more than one resource are not yet supported. \n" - + "See VOTable file for more informations: " + voTablePath); + /** + * @author N. Jourdane + */ + public static class CantPrintRequestResultException extends CantSendQueryException { + /** + * Method constructor + * + * @param resultFilePath The path of the file where the query results should be stored. + * @param e The exception thrown. + */ + public CantPrintRequestResultException(String resultFilePath, IOException e) { + super("Can not print the result in the file " + resultFilePath, e); + } } - } - public static class VOTableSeveralTablesException extends VOTableException { - public VOTableSeveralTablesException(String voTablePath) { - super("VOTable with more than one resource are not yet supported. \n" - + "See VOTable file for more informations: " + voTablePath); + /** + * @author N. Jourdane + */ + public static class CantOpenConnectionException extends CantSendQueryException { + /** + * Method constructor + * + * @param uri The URI sent to the server. + * @param e The exception thrown. + */ + public CantOpenConnectionException(String uri, IOException e) { + super("Can not open an HTTP connection to " + uri, e); + } + } + + /** + * @author N. Jourdane + */ + public static class CantGetResponseCode extends CantSendQueryException { + /** + * Method constructor + * + * @param uri The URI sent to the server. + * @param e The exception thrown. + */ + public CantGetResponseCode(String uri, IOException e) { + super("Can not get the server response code for the request " + uri, e); + } + } + + /** + * @author N. Jourdane + */ + public static class BadResponseCodeException extends CantSendQueryException { + /** + * Method constructor + * + * @param uri The URI sent to the server. + * @param responseCode The HTTP GET response code, which is bad. + */ + public BadResponseCodeException(String uri, int responseCode) { + super("The server returned the bad response code `" + responseCode + + "` for the request " + uri); + } } - } - public static class VOTableCantFillVoTableException extends VOTableException { - public VOTableCantFillVoTableException(IOException e) { - super("Can not fill the VOTable data in the JTable: " + e.getMessage(), e); + /** + * @author N. Jourdane + */ + public static class CantGetErrorStream extends CantSendQueryException { + /** + * Method constructor + * + * @param uri The URI sent to the server. + * @param e The exception thrown. + */ + public CantGetErrorStream(String uri, IOException e) { + super("Can not get the input stream of the result, neither the error stream " + + "for the request " + uri); + } } + } - public static class ErrorInVOTableException extends VOTableException { - public ErrorInVOTableException(String errorInfo) { - super("There is an error in the VOTable:\n" + errorInfo - + "\nPlease check your ADQL query."); + /** + * @author N. Jourdane + */ + public abstract static class CantDisplayVOTableException extends VOTableException { + + /** + * Method constructor + * + * @param message The message describing the exception displayed in the error dialog. + * @param e The exception thrown. + */ + public CantDisplayVOTableException(String message, Exception e) { + super(message, e); + } + + /** + * Method constructor + * + * @param message The message describing the exception displayed in the error dialog. + */ + public CantDisplayVOTableException(String message) { + super(message); + } + + /** + * @author N. Jourdane + */ + public static class VOTableIsNotValidException extends CantDisplayVOTableException { + + /** + * Method constructor + * + * @param e The exception thrown. + */ + public VOTableIsNotValidException(String voTablePath, Exception e) { + super("Can not parse the VOTable because it doesn't match with the VOTable schema." + + "\n See the VOTable file for more details: " + voTablePath, e); + } + } + + /** + * @author N. Jourdane + */ + public static class SeveralResourcesException extends CantDisplayVOTableException { + + /** + * Method constructor + * + * @param voTablePath + */ + public SeveralResourcesException(String voTablePath) { + super("VOTable with more than one resource are not yet supported. \n" + + "See VOTable file for more informations: " + voTablePath); + } + } + + /** + * @author N. Jourdane + */ + public static class SeveralTablesException extends CantDisplayVOTableException { + + /** + * Method constructor + * + * @param voTablePath + */ + public SeveralTablesException(String voTablePath) { + super("VOTable with more than one resource are not yet supported. \n" + + "See VOTable file for more informations: " + voTablePath); + } + } + + /** + * @author N. Jourdane + */ + public static class CantModifyVOTableException extends CantDisplayVOTableException { + /** + * Method constructor + * + * @param e The exception thrown. + */ + public CantModifyVOTableException(String voTablePath, Exception e) { + super("Can not change schema location on the VOTable file: " + voTablePath, e); + } + } + + /** + * @author N. Jourdane + */ + public static class ErrorMessageInVOTableException extends CantDisplayVOTableException { + /** + * Method constructor + * + * @param errorInfo + */ + public ErrorMessageInVOTableException(String errorInfo) { + super("There is an error in the VOTable:\n" + errorInfo + + "\nPlease check your ADQL query."); + } } } diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java index 4c6df04..266c2ce 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/votable/controller/VOTableParser.java @@ -36,7 +36,7 @@ import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.xml.sax.SAXException; -import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.VOTableParsingException; +import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException.CantFillTableException.VOTableParsingException; import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE; /** -- libgit2 0.21.2