From 3567221910f8ceb4f1a0522dfe467e6e9ed300b5 Mon Sep 17 00:00:00 2001 From: Bianca Schnalzer Date: Wed, 23 Aug 2017 11:48:53 +0200 Subject: Improved Error Handling Download-URL --- .../gui/bku/mobile/SimpleXMLTrustManager.java | 36 +++++++++++----- .../gui/utils/CertificateDownloadSource.java | 50 +++++++++++++--------- .../pdfover/gui/workflow/StateMachineImpl.java | 21 ++------- 3 files changed, 58 insertions(+), 49 deletions(-) diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java index ee2448f5..adbd05ad 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/bku/mobile/SimpleXMLTrustManager.java @@ -95,22 +95,13 @@ public class SimpleXMLTrustManager implements X509TrustManager { .newDocumentBuilder() .parse(this.getClass().getResourceAsStream(Constants.RES_CERT_LIST)); - - File added_cert = new File(Constants.RES_CERT_LIST_ADDED); - - Document doc_added = DocumentBuilderFactory.newInstance() - .newDocumentBuilder() - .parse(added_cert); - - Node certificates_added = doc_added.getFirstChild(); Node certificates = doc.getFirstChild(); - if (!certificates.getNodeName().equals("certificates") && !certificates_added.getNodeName().equals("certificates")) { //$NON-NLS-1$ + if (!certificates.getNodeName().equals("certificates")) { //$NON-NLS-1$ throw new Exception( "Used certificates xml is invalid! no certificates node"); //$NON-NLS-1$ } - NodeList certificates_added_list = certificates_added.getChildNodes(); NodeList certificateList = certificates.getChildNodes(); for (int i = 0; i < certificateList.getLength(); i++) { @@ -144,6 +135,29 @@ public class SimpleXMLTrustManager implements X509TrustManager { } } + + File added_cert = new File(Constants.RES_CERT_LIST_ADDED); + + if (added_cert.exists()) + { + Document doc_added = DocumentBuilderFactory.newInstance() + .newDocumentBuilder() + .parse(added_cert); + + Node certificates_added = doc_added.getFirstChild(); + + if (!certificates_added.getNodeName().equals("certificates")) { //$NON-NLS-1$ + throw new Exception( + "Used certificates xml is invalid! no certificates node"); //$NON-NLS-1$ + } + + + if (certificates_added.hasChildNodes()) + { + + NodeList certificates_added_list = certificates_added.getChildNodes(); + + for (int i = 0; i < certificates_added_list.getLength(); i++) { try { @@ -175,7 +189,7 @@ public class SimpleXMLTrustManager implements X509TrustManager { log.error("Failed to load certificate [" + "]", ex); //$NON-NLS-1$ //$NON-NLS-2$ } } - + }} tmf.init(myKeyStore); diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/CertificateDownloadSource.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/CertificateDownloadSource.java index f05d0034..955097fc 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/CertificateDownloadSource.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/utils/CertificateDownloadSource.java @@ -56,25 +56,33 @@ public class CertificateDownloadSource { public static void getAcceptedCertificates() { try { - + URL url = new URL(Constants.CERTIFICATE_DOWNLOAD_XML_URL+Constants.CERTIFICATE_XML_FILE); URLConnection connection = url.openConnection(); InputStream is = connection.getInputStream(); - log.info("===== Starting to download accepted certificate ====="); - BufferedInputStream bis = new BufferedInputStream(is); - FileOutputStream fis2 = new FileOutputStream(new File(Constants.RES_CERT_LIST_ADDED)); - byte[] buffer = new byte[1024]; - int count = 0; - while ((count = bis.read(buffer, 0, 1024)) != -1) { - fis2.write(buffer, 0, count); + + int b = is.read(); + if (b==-1) + { + log.info("Cannot read file"); + } + else + { + BufferedInputStream bis = new BufferedInputStream(is); + FileOutputStream fis2 = new FileOutputStream(new File(Constants.RES_CERT_LIST_ADDED)); + + + byte[] buffer = new byte[1024]; + int count = 0; + while ((count = bis.read(buffer, 0, 1024)) != -1) { + fis2.write(buffer, 0, count); + } + fis2.close(); + bis.close(); + downloadCertificatesFromServer(); } - fis2.close(); - bis.close(); - downloadCertificatesFromServer(); - } catch (IOException e) { - File f = new File(Constants.RES_CERT_LIST_ADDED); - e.printStackTrace();} + log.debug("File not found");} } @@ -89,9 +97,12 @@ public class CertificateDownloadSource { FileReader fr = null; try { + + File added_cert = new File(Constants.RES_CERT_LIST_ADDED); - + if (added_cert.exists()) + { Document doc_added = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(added_cert); @@ -99,7 +110,9 @@ public class CertificateDownloadSource { Node certificates_added = doc_added.getFirstChild(); NodeList certificates_added_list = certificates_added.getChildNodes(); - + + if (doc_added.hasChildNodes()) + { //identify the certificate that has to be downloaded for (int i = 0; i < certificates_added_list.getLength(); i++) { try { @@ -139,10 +152,7 @@ public class CertificateDownloadSource { } } - - - - + }} } catch (IOException e) { e.printStackTrace(); diff --git a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/StateMachineImpl.java b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/StateMachineImpl.java index b2367a85..6ff121bb 100644 --- a/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/StateMachineImpl.java +++ b/pdf-over-gui/src/main/java/at/asit/pdfover/gui/workflow/StateMachineImpl.java @@ -20,6 +20,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; //Imports import java.lang.reflect.Constructor; +import java.nio.file.Files; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; @@ -257,24 +258,8 @@ public class StateMachineImpl implements StateMachine, GUIProvider { // Call update to start processing ... update(); - try { - File certificates = new File (Constants.RES_CERT_LIST_ADDED); - if (!certificates.exists()) - { - - FileOutputStream fis = new FileOutputStream(new File(Constants.RES_CERT_LIST_ADDED)); - CertificateDownloadSource.getAcceptedCertificates(); - - - } - else - { - CertificateDownloadSource.getAcceptedCertificates(); - } - } catch (FileNotFoundException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + CertificateDownloadSource.getAcceptedCertificates(); + // if a user interaction is required we have a shell ... -- cgit v1.2.3