Skip to content

Commit

Permalink
Fix checking updates in gui
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximPlusov committed Oct 25, 2023
1 parent 9b923cc commit e9f73a2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 54 deletions.
72 changes: 24 additions & 48 deletions gui/src/main/java/org/verapdf/apps/SoftwareUpdaterImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.verapdf.ReleaseDetails;
import org.verapdf.version.SemanticVersionNumber;
import org.verapdf.version.Versions;
import org.xml.sax.Attributes;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
* @author <a href="mailto:[email protected]">Carl Wilson</a>
Expand All @@ -30,12 +32,8 @@

public class SoftwareUpdaterImpl implements SoftwareUpdater {
private final static Logger logger = Logger.getLogger(SoftwareUpdaterImpl.class.getCanonicalName());
private final static SAXParserFactory saxFact = SAXParserFactory.newInstance();
private final static String jenkinsRoot = "http://jenkins.openpreservation.org/job/veraPDF-apps"; //$NON-NLS-1$
private final static String jenkinsApiPath = "/lastStableBuild/api/xml"; //$NON-NLS-1$
private final static String latestGF = jenkinsRoot + jenkinsApiPath;
private final static String latestPDFBox = jenkinsRoot + "-" + Versions.PDFBOX_BUILD_INFO.toLowerCase() //$NON-NLS-1$
+ jenkinsApiPath;
private final static String latestGF = "https://search.maven.org/solrsearch/select?q=g:org.verapdf.apps+AND+a:greenfield-apps&core=gav&rows=20&wt=xml";
private final static String latestPDFBox = "https://search.maven.org/solrsearch/select?q=g:org.verapdf.apps+AND+a:pdfbox-apps&core=gav&rows=20&wt=xml";
private final String currentVersion = Applications.getAppDetails().getVersion();

/**
Expand All @@ -47,17 +45,18 @@ public class SoftwareUpdaterImpl implements SoftwareUpdater {

@Override
public boolean isOnline() {
String stringURL = getEndpointForVersion(this.currentVersion);
try {
URL url = new URL(getEndpointForVersion(this.currentVersion));
URL url = new URL(stringURL);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("HEAD"); //$NON-NLS-1$
huc.setRequestMethod("GET"); //$NON-NLS-1$
huc.connect();
if (huc.getResponseCode() == 200) {
url.openStream();
return true;
}
} catch (MalformedURLException excep) {
throw new IllegalStateException(String.format("Problem parsing hard coded URL %s", jenkinsRoot), excep); //$NON-NLS-1$
throw new IllegalStateException(String.format("Problem parsing hard coded URL %s", stringURL), excep); //$NON-NLS-1$
} catch (IOException excep) {
logger.log(Level.INFO, "Couldn't get latest version info from Jenkins.", excep); //$NON-NLS-1$
}
Expand All @@ -81,8 +80,9 @@ public boolean isUpdateAvailable() {

@Override
public boolean isUpdateAvailable(final String versionString) {
if (!this.isOnline())
if (!this.isOnline()) {
return false;
}
SemanticVersionNumber current = Versions.fromString(versionString);
String endpoint = getEndpointForVersion(versionString);
SemanticVersionNumber available = getLatestVersionFromUrl(endpoint);
Expand All @@ -108,47 +108,23 @@ public String getLatestVersion(final ReleaseDetails details) {
return getLatestVersion(details.getVersion());
}

private static final SemanticVersionNumber getLatestVersionFromUrl(final String endpoint) {
private static SemanticVersionNumber getLatestVersionFromUrl(final String endpoint) {
try {
URL url = new URL(endpoint);
SAXParser saxParser = saxFact.newSAXParser();
VersionParser versionParser = new VersionParser();
saxParser.parse(new InputSource(url.openStream()), versionParser);
return versionParser.getVersion();
} catch (IOException | ParserConfigurationException | SAXException excep) {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
builder.setErrorHandler(null);
Document doc = builder.parse(new InputSource(url.openStream()));
XPath path = XPathFactory.newInstance().newXPath();
NodeList versions = ((NodeList) path.evaluate("//str[@name='v']", doc, XPathConstants.NODESET));
return Versions.fromString(versions.item(0).getFirstChild().getNodeValue());
} catch (IOException | ParserConfigurationException | SAXException | XPathExpressionException excep) {
excep.printStackTrace();
throw new IllegalStateException(String.format("Problem parsing version number from URL %s", endpoint), //$NON-NLS-1$
excep);
}
}

private static final String getEndpointForVersion(final String versionString) {
private static String getEndpointForVersion(final String versionString) {
return versionString.endsWith(Versions.PDFBOX_BUILD_INFO) ? latestPDFBox : latestGF;
}

static class VersionParser extends DefaultHandler {
private static final String verQName = "displayName"; //$NON-NLS-1$
private SemanticVersionNumber versionNumber = Versions.fromInts(0, 0, 0);
private boolean isVersion = false;

public SemanticVersionNumber getVersion() {
return this.versionNumber;
}

@Override
public void startElement(final String namespaceURI, final String localName, final String qName,
final Attributes atts) {
this.isVersion = qName.equalsIgnoreCase(verQName);

}

@Override
public void characters(final char ch[], final int start, final int length) {
if (!this.isVersion)
return;
String version = new String(ch, start, length);
this.versionNumber = Versions.fromString(version);
this.isVersion = false;
}
}
}
12 changes: 7 additions & 5 deletions gui/src/main/java/org/verapdf/cli/FormatterHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
import org.verapdf.features.FeatureObjectType;
import org.verapdf.pdfa.flavours.PDFAFlavour;
import org.verapdf.pdfa.validation.profiles.Profiles;
import org.verapdf.processor.FormatOption;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import java.util.*;

public class FormatterHelper extends DefaultUsageFormatter {

Expand Down Expand Up @@ -74,7 +72,11 @@ public void appendAllParametersDetails(StringBuilder out, int indentCount, Strin
flavours.add(PDFAFlavour.NO_FLAVOUR);
}
valueList = flavours.toString();
} else {
} else if (FormatOption.class.getCanonicalName().equals(type.getName())) {
List<FormatOption> formatOptions = new LinkedList<>(Arrays.asList(FormatOption.values()));
formatOptions.remove(FormatOption.MRR);
valueList = formatOptions.toString();
}else {
valueList = EnumSet.allOf((Class<? extends Enum>) type).toString();
}
String possibleValues = "Possible Values: " + valueList;
Expand Down
2 changes: 1 addition & 1 deletion gui/src/main/java/org/verapdf/gui/utils/GUIConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public final class GUIConstants {
public static final String ERROR_SAVING_REPORT = "Unresolvable problem occured when saving the %S report.";
public static final String IOEXCEP_OPENING_REPORT = "Could not open %s using the default Desktop application for %S file type. Please, try opening this file manually.";
public static final String IOEXCEP_SAVING_REPORT = "IOException when saving the %S report.";
public static final String LABEL_TEXT = " Please choose a PDF and a PDF flavour or custom validation profile, then press \"" + VALIDATE_BUTTON_TEXT + "\"";
public static final String LABEL_TEXT = " Choose a PDF and a PDF flavour or a custom validation profile, then press \"" + VALIDATE_BUTTON_TEXT + "\"";

public static final String LABEL_TOOL_TIP = "<html>PDF flavour: the PDF/A or PDF/UA part and conformance level<br>" +
"Validation profile: custom collection of validation rules</html>";
Expand Down

0 comments on commit e9f73a2

Please sign in to comment.