diff --git a/HIRS_Utils/src/main/java/hirs/utils/VersionHelper.java b/HIRS_Utils/src/main/java/hirs/utils/VersionHelper.java index ed92a3fa..8fc45264 100644 --- a/HIRS_Utils/src/main/java/hirs/utils/VersionHelper.java +++ b/HIRS_Utils/src/main/java/hirs/utils/VersionHelper.java @@ -2,54 +2,117 @@ package hirs.utils; import com.google.common.base.Charsets; import com.google.common.io.Resources; +import lombok.extern.log4j.Log4j2; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; import java.net.URL; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; /** * Utility class to get the current version from the VERSION file. */ +@Log4j2 public final class VersionHelper { - private static final String VERSION_FILENAME = "VERSION"; + private static final String OPT_PREFIX = "/opt"; + private static final String ETC_PREFIX = "/etc"; + private static final String VERSION = "VERSION"; private VersionHelper() { // intentionally blank, should never be instantiated } /** - * Get the current version of HIRS_Portal that is installed. + * Get the current version of HIRS_AttestationPortal that is installed. * * @return A string representing the current version. */ public static String getVersion() { - return getVersion(VERSION_FILENAME); + if (Files.exists(FileSystems.getDefault().getPath(OPT_PREFIX, + "hirs", "aca", VERSION))) { + return getVersion(FileSystems.getDefault().getPath(OPT_PREFIX, + "hirs", "aca", VERSION)); + } else if (Files.exists(FileSystems.getDefault().getPath(ETC_PREFIX, + "hirs", "aca", VERSION))) { + return getVersion(FileSystems.getDefault().getPath(ETC_PREFIX, + "hirs", "aca", VERSION)); + } + + return getVersion(VERSION); } /** - * Get the current version of HIRS_Portal that is installed. + * Get the current version of HIRS_AttestationCAPortal that is installed. * - * @param filename - * that contains the version + * @param filename that contains the version + * @return A string representing the current version. + */ + public static String getVersion(final Path filename) { + String version; + try { + version = getFileContents(filename.toString()); + } catch (IOException ioEx) { + log.error(ioEx.getMessage()); + version = ""; + } + + return version; + } + + /** + * Get the current version of HIRS_AttestationCAPortal that is installed. + * + * @param filename that contains the version * @return A string representing the current version. */ public static String getVersion(final String filename) { String version; try { - version = getFileContents(filename); - } catch (Exception e) { + version = getResourceContents(filename); + } catch (Exception ex) { version = ""; + log.error(ex.getMessage()); } + return version; } /** * Read the symbolic link to VERSION in the top level HIRS directory. + * * @param filename "VERSION" * @return the version number from the file * @throws IOException */ private static String getFileContents(final String filename) throws IOException { + final char[] buffer = new char[8192]; + final StringBuilder result = new StringBuilder(); + InputStream inputStream = new FileInputStream(filename); + + try (Reader reader = new InputStreamReader(inputStream, Charsets.UTF_8)) { + int charsRead; + while ((charsRead = reader.read(buffer, 0, buffer.length)) > 0) { + result.append(buffer, 0, charsRead); + } + } + + return result.toString(); + } + + /** + * Read the symbolic link to VERSION in the top level HIRS directory. + * + * @param filename "VERSION" + * @return the version number from the file + * @throws IOException + */ + private static String getResourceContents(final String filename) throws IOException { URL url = Resources.getResource(filename); return Resources.toString(url, Charsets.UTF_8).trim(); } diff --git a/HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java b/HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java index d2bda70d..f2cb4c34 100644 --- a/HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java +++ b/HIRS_Utils/src/test/java/hirs/utils/VersionHelperTest.java @@ -1,6 +1,7 @@ package hirs.utils; import org.junit.jupiter.api.Test; + import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -14,7 +15,6 @@ public class VersionHelperTest { */ @Test public void testGetVersionFail() { - String actual = VersionHelper.getVersion("somefile"); assertTrue(actual.startsWith("")); } @@ -24,9 +24,8 @@ public class VersionHelperTest { */ @Test public void testGetVersionDefault() { - String expected = "Test.Version"; - String actual = VersionHelper.getVersion(); + String actual = VersionHelper.getVersion("VERSION"); assertEquals(expected, actual); } }