package org.ihtsdo.buildcloud.controller;

import org.ihtsdo.buildcloud.controller.helper.HypermediaGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/version")
public class VersionController {

	public static final String VERSION_FILE_PATH = "/var/opt/snomed-release-service-api/version.txt";

	@Autowired
	private HypermediaGenerator hypermediaGenerator;

	private String versionString;

	@RequestMapping
	@ResponseBody
	public Map<String, Object> getVersion(HttpServletRequest request) throws IOException {
		Map<String, String> entity = new HashMap<>();
		entity.put("package_version", getVersionString());
		return hypermediaGenerator.getEntityHypermedia(entity, true, request, new String[]{});
	}

	private String getVersionString() throws IOException {
		if (this.versionString == null) {
			String versionString = "";
			File file = new File(VERSION_FILE_PATH);
			if (file.isFile()) {
				try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file))) {
					versionString = bufferedReader.readLine();
				}
			}
			this.versionString = versionString;
		}
		return versionString;
	}

}

@@ -591,30 +591,11 @@
 opt/
-snomed-release-service
+rvf
 -api
@@ -1010,16 +1010,17 @@
 String%5B%5D
+ 
 %7B%7D);%0A%09%7D%0A
@@ -1349,16 +1349,82 @@
 ;%0A%09%09%09%09%7D%0A
+%09%09%09%7D else %7B%0A%09%09%09%09versionString = %22Version information not found.%22;%0A
 %09%09%09%7D%0A%09%09%09

Improve robustness if version.txt file should go missing.