1 package org.onap.vid.controller;
4 import com.fasterxml.jackson.core.type.TypeReference;
5 import com.fasterxml.jackson.databind.ObjectMapper;
6 import com.google.common.collect.ImmutableMap;
7 import org.apache.commons.lang3.StringUtils;
8 import org.onap.portalsdk.core.controller.UnRestrictedBaseController;
9 import org.onap.portalsdk.core.util.SystemProperties;
10 import org.springframework.http.MediaType;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestMethod;
13 import org.springframework.web.bind.annotation.RestController;
15 import javax.inject.Inject;
16 import javax.servlet.ServletContext;
17 import java.io.IOException;
19 import java.util.HashMap;
21 import java.util.regex.Matcher;
22 import java.util.regex.Pattern;
24 import static org.apache.commons.lang3.StringUtils.substringAfterLast;
27 @RequestMapping("version")
28 public class VersionController extends UnRestrictedBaseController {
30 private final ServletContext servletContext;
33 public VersionController(ServletContext servletContext) {
34 this.servletContext = servletContext;
37 @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
38 public Map<String,String> getVersionAndFeatures()
40 String features = SystemProperties.getProperty("features.set.filename");
43 final URL resource = servletContext.getResource("/app/vid/scripts/constants/version.json");
44 HashMap<String,String> versionFile = new HashMap <>();
45 ObjectMapper mapper = new ObjectMapper();
46 versionFile.putAll(mapper.readValue(resource, new TypeReference<HashMap<String, String>>() {}));
47 version = versionFile.get("Version");
48 } catch (IOException e) {
49 version = "Error retrieving build number";
51 String majorVersion = getDisplayVersion(features, version);
52 return ImmutableMap.of("Features", features, "Build", version, "DisplayVersion", majorVersion);
55 String getDisplayVersion(String features, String build) {
56 Matcher matcher = Pattern.compile("([^/]+?)(\\.features|$)").matcher(features);
57 final String majorByFeatures = matcher.find() ? matcher.group(1) : features;
59 final String buildByVersion = StringUtils.defaultIfBlank(substringAfterLast(build, "."), build);
61 return StringUtils.join(majorByFeatures, ".", buildByVersion);