11/*
2- * AMRIT – Accessible Medical Records via Integrated Technology
3- * Integrated EHR (Electronic Health Records) Solution
4- *
5- * Copyright (C) "Piramal Swasthya Management and Research Institute"
6- *
7- * This file is part of AMRIT.
8- *
9- * This program is free software: you can redistribute it and/or modify
10- * it under the terms of the GNU General Public License as published by
11- * the Free Software Foundation, either version 3 of the License, or
12- * (at your option) any later version.
13- *
14- * This program is distributed in the hope that it will be useful,
15- * but WITHOUT ANY WARRANTY; without even the implied warranty of
16- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17- * GNU General Public License for more details.
18- *
19- * You should have received a copy of the GNU General Public License
20- * along with this program. If not, see https://www.gnu.org/licenses/.
21- */
2+ * AMRIT – Accessible Medical Records via Integrated Technology
3+ * Integrated EHR (Electronic Health Records) Solution
4+ *
5+ * Copyright (C) "Piramal Swasthya Management and Research Institute"
6+ *
7+ * This file is part of AMRIT.
8+ *
9+ * This program is free software: you can redistribute it and/or modify
10+ * it under the terms of the GNU General Public License as published by
11+ * the Free Software Foundation, either version 3 of the License, or
12+ * (at your option) any later version.
13+ *
14+ * This program is distributed in the hope that it will be useful,
15+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
16+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17+ * See the GNU General Public License for more details.
18+ *
19+ * You should have received a copy of the GNU General Public License
20+ * along with this program. If not, see https://www.gnu.org/licenses/.
21+ */
22+ /**
23+ * REST controller exposing application version and build metadata.
24+ * <p>
25+ * Provides the <code>/version</code> endpoint which returns Git metadata
26+ * in a standardized JSON format consistent across all AMRIT APIs.
27+ * </p>
28+ *
29+ * @author Vaishnav Bhosale
30+ */
2231package com .iemr .common .controller .version ;
2332
24- import java .io .BufferedReader ;
25- import java .io .IOException ;
2633import java .io .InputStream ;
27- import java .io .InputStreamReader ;
34+ import java .io .IOException ;
35+ import java .util .LinkedHashMap ;
36+ import java .util .Map ;
37+ import java .util .Properties ;
2838
2939import org .slf4j .Logger ;
3040import org .slf4j .LoggerFactory ;
3343import org .springframework .web .bind .annotation .GetMapping ;
3444import org .springframework .web .bind .annotation .RestController ;
3545
36- import com .iemr .common .utils .response .OutputResponse ;
37-
3846import io .swagger .v3 .oas .annotations .Operation ;
3947
40-
4148@ RestController
4249public class VersionController {
4350
44- private Logger logger = LoggerFactory .getLogger (this .getClass ().getSimpleName ());
51+ private final Logger logger =
52+ LoggerFactory .getLogger (this .getClass ().getSimpleName ());
53+
54+ private static final String UNKNOWN_VALUE = "unknown" ;
4555
46- @ Operation (summary = "Get version" )
47- @ RequestMapping (value = "/version" , method = { RequestMethod . GET } )
48- public String versionInformation () {
49- OutputResponse output = new OutputResponse ();
56+ @ Operation (summary = "Get version information " )
57+ @ GetMapping (value = "/version" , produces = MediaType . APPLICATION_JSON_VALUE )
58+ public ResponseEntity < Map < String , String >> versionInformation () {
59+ Map < String , String > response = new LinkedHashMap <> ();
5060 try {
5161 logger .info ("version Controller Start" );
52- output .setResponse (readGitProperties ());
62+ Properties gitProperties = loadGitProperties ();
63+ response .put ("buildTimestamp" , gitProperties .getProperty ("git.build.time" , UNKNOWN_VALUE ));
64+ response .put ("version" , gitProperties .getProperty ("git.build.version" , UNKNOWN_VALUE ));
65+ response .put ("branch" , gitProperties .getProperty ("git.branch" , UNKNOWN_VALUE ));
66+ response .put ("commitHash" , gitProperties .getProperty ("git.commit.id.abbrev" , UNKNOWN_VALUE ));
5367 } catch (Exception e ) {
54- output .setError (e );
68+ logger .error ("Failed to load version information" , e );
69+ response .put ("buildTimestamp" , UNKNOWN_VALUE );
70+ response .put ("version" , UNKNOWN_VALUE );
71+ response .put ("branch" , UNKNOWN_VALUE );
72+ response .put ("commitHash" , UNKNOWN_VALUE );
5573 }
5674 logger .info ("version Controller End" );
5775 return ResponseEntity .ok (response );
5876 }
5977
6078 logger .info ("version Controller End ");
61- return output.toString();
62- }
63-
64- private String readGitProperties() throws Exception {
65- ClassLoader classLoader = getClass().getClassLoader();
66- InputStream inputStream = classLoader.getResourceAsStream(" git .properties ");
67-
68- return readFromInputStream(inputStream);
79+ return ResponseEntity.ok(response);
6980 }
7081
71- private String readFromInputStream(InputStream inputStream ) throws IOException {
72- StringBuilder resultStringBuilder = new StringBuilder ();
73- try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
74- String line;
75- while ((line = br.readLine()) != null) {
76- resultStringBuilder.append(line).append(" \n " );
82+ private Properties loadGitProperties( ) throws IOException {
83+ Properties properties = new Properties ();
84+ try (InputStream input = getClass().getClassLoader()
85+ .getResourceAsStream(" git . properties ")) {
86+ if (input != null) {
87+ properties.load(input );
7788 }
7889 }
79- return resultStringBuilder.toString() ;
90+ return properties ;
8091 }
81- }
92+ }
0 commit comments