From 9a45f30ccb013c029dcb881dc101ef861ef9811f Mon Sep 17 00:00:00 2001 From: milanmajchrak <90026355+milanmajchrak@users.noreply.github.com> Date: Wed, 3 Jan 2024 08:13:53 +0100 Subject: [PATCH] internal/load-version-from-file (#488) * Load version from the specific file and show content in the root endpoint. * Fixed checkstyle issue * DO not log anything if the VERSION file does not exist. Added VERSION file path into cfg property. * Append every line into final String. --- .../app/rest/converter/RootConverter.java | 39 +++++++++++++++++++ .../org/dspace/app/rest/model/RootRest.java | 12 ++++++ dspace/config/VERSION_D.txt | 0 dspace/config/clarin-dspace.cfg | 4 ++ 4 files changed, 55 insertions(+) create mode 100644 dspace/config/VERSION_D.txt diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/RootConverter.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/RootConverter.java index 61f18a5b3c9c..fc8e996e9cd4 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/RootConverter.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/converter/RootConverter.java @@ -9,8 +9,15 @@ import static org.dspace.app.util.Util.getSourceVersion; +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +import org.apache.commons.lang3.StringUtils; import org.dspace.app.rest.model.RootRest; import org.dspace.services.ConfigurationService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -19,6 +26,7 @@ */ @Component public class RootConverter { + private static final Logger log = LoggerFactory.getLogger(RootConverter.class); @Autowired private ConfigurationService configurationService; @@ -29,6 +37,37 @@ public RootRest convert() { rootRest.setDspaceUI(configurationService.getProperty("dspace.ui.url")); rootRest.setDspaceServer(configurationService.getProperty("dspace.server.url")); rootRest.setDspaceVersion("DSpace " + getSourceVersion()); + rootRest.setBuildVersion(getBuildVersion()); return rootRest; } + + /** + * Read the build version from the `build.version.file.path` property + * + * @return content of the version file + */ + private String getBuildVersion() { + String bVersionFilePath = configurationService.getProperty("build.version.file.path"); + + if (StringUtils.isBlank(bVersionFilePath)) { + return "Unknown"; + } + + StringBuilder buildVersion = new StringBuilder(); + try { + FileReader fileReader = new FileReader(bVersionFilePath); + BufferedReader bufferedReader = new BufferedReader(fileReader); + + String line; + // Read each line from the file until the end of the file is reached + while ((line = bufferedReader.readLine()) != null) { + buildVersion.append(line); + } + + } catch (IOException e) { + // Empty - do not log anything + } + + return buildVersion.toString(); + } } diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/RootRest.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/RootRest.java index cef8965601ca..b83859898ca0 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/RootRest.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/model/RootRest.java @@ -22,6 +22,8 @@ public class RootRest extends RestAddressableModel { private String dspaceServer; private String dspaceVersion; + private String buildVersion; + public String getCategory() { return CATEGORY; } @@ -67,6 +69,14 @@ public void setDspaceVersion(String dspaceVersion) { this.dspaceVersion = dspaceVersion; } + public String getBuildVersion() { + return buildVersion; + } + + public void setBuildVersion(String buildVersion) { + this.buildVersion = buildVersion; + } + @Override public boolean equals(Object object) { return (object instanceof RootRest && @@ -76,6 +86,7 @@ public boolean equals(Object object) { .append(this.getDspaceUI(), ((RootRest) object).getDspaceUI()) .append(this.getDspaceName(), ((RootRest) object).getDspaceName()) .append(this.getDspaceServer(), ((RootRest) object).getDspaceServer()) + .append(this.getBuildVersion(), ((RootRest) object).getBuildVersion()) .isEquals()); } @@ -88,6 +99,7 @@ public int hashCode() { .append(this.getDspaceName()) .append(this.getDspaceUI()) .append(this.getDspaceServer()) + .append(this.getBuildVersion()) .toHashCode(); } } diff --git a/dspace/config/VERSION_D.txt b/dspace/config/VERSION_D.txt new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/dspace/config/clarin-dspace.cfg b/dspace/config/clarin-dspace.cfg index 478a22b9bbae..754d6422c96a 100644 --- a/dspace/config/clarin-dspace.cfg +++ b/dspace/config/clarin-dspace.cfg @@ -246,3 +246,7 @@ file.preview.enabled = false ### Storage service ### # Synchronization is NOT enabled by default sync.storage.service.enabled = true + + +### The build version is stored in the specific file ### +build.version.file.path = dspace/config/VERSION_D.txt