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 ;
31- import org .springframework .web .bind .annotation .RequestMapping ;
32- import org .springframework .web .bind .annotation .RequestMethod ;
41+ import org .springframework .http .MediaType ;
42+ import org .springframework .http .ResponseEntity ;
43+ import org .springframework .web .bind .annotation .GetMapping ;
3344import org .springframework .web .bind .annotation .RestController ;
3445
35- import com .iemr .common .utils .response .OutputResponse ;
36-
3746import io .swagger .v3 .oas .annotations .Operation ;
3847
39-
4048@ RestController
4149public class VersionController {
4250
43- 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" ;
4455
45- @ Operation (summary = "Get version" )
46- @ RequestMapping (value = "/version" , method = { RequestMethod . GET } )
47- public String versionInformation () {
48- 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 <> ();
4960 try {
5061 logger .info ("version Controller Start" );
51- 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 ));
5267 } catch (Exception e ) {
53- 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 );
5473 }
55-
5674 logger .info ("version Controller End" );
57- return output .toString ();
58- }
59-
60- private String readGitProperties () throws Exception {
61- ClassLoader classLoader = getClass ().getClassLoader ();
62- InputStream inputStream = classLoader .getResourceAsStream ("git.properties" );
63-
64- return readFromInputStream (inputStream );
75+ return ResponseEntity .ok (response );
6576 }
6677
67- private String readFromInputStream ( InputStream inputStream ) throws IOException {
68- StringBuilder resultStringBuilder = new StringBuilder ();
69- try (BufferedReader br = new BufferedReader ( new InputStreamReader ( inputStream ))) {
70- String line ;
71- while (( line = br . readLine ()) != null ) {
72- resultStringBuilder . append ( line ). append ( " \n " );
78+ private Properties loadGitProperties ( ) throws IOException {
79+ Properties properties = new Properties ();
80+ try (InputStream input = getClass (). getClassLoader ()
81+ . getResourceAsStream ( "git.properties" )) {
82+ if ( input != null ) {
83+ properties . load ( input );
7384 }
7485 }
75- return resultStringBuilder . toString () ;
86+ return properties ;
7687 }
77- }
88+ }
0 commit comments