2121*/
2222package com .iemr .admin .controller .version ;
2323
24- import java .io .BufferedReader ;
2524import java .io .IOException ;
2625import java .io .InputStream ;
27- import java .io .InputStreamReader ;
26+ import java .text .SimpleDateFormat ;
27+ import java .util .Date ;
28+ import java .util .Properties ;
29+ import java .util .TimeZone ;
2830
2931import org .slf4j .Logger ;
3032import org .slf4j .LoggerFactory ;
31-
33+ import org . springframework . http . MediaType ;
3234import org .springframework .web .bind .annotation .RequestMapping ;
3335import org .springframework .web .bind .annotation .RequestMethod ;
3436import org .springframework .web .bind .annotation .RestController ;
@@ -44,35 +46,115 @@ public class VersionController {
4446 private Logger logger = LoggerFactory .getLogger (this .getClass ().getSimpleName ());
4547
4648 @ Operation (summary = "Version information" )
47- @ RequestMapping (value = "/version" , method = { RequestMethod .GET })
49+ @ RequestMapping (value = "/version" , method = { RequestMethod .GET }, produces = MediaType . APPLICATION_JSON_VALUE )
4850 public String versionInformation () {
4951 OutputResponse output = new OutputResponse ();
5052 try {
5153 logger .info ("version Controller Start" );
52- output .setResponse (readGitProperties ());
54+ // Set the version information as JSON string directly
55+ output .setResponse (readGitPropertiesAsJson ());
5356 } catch (Exception e ) {
5457 output .setError (e );
5558 }
5659
5760 logger .info ("version Controller End" );
61+
62+ // Use standard toString() - no custom formatting
5863 return output .toString ();
5964 }
6065
61- private String readGitProperties () throws Exception {
62- ClassLoader classLoader = getClass ().getClassLoader ();
63- InputStream inputStream = classLoader .getResourceAsStream ("git.properties" );
64-
65- return readFromInputStream (inputStream );
66+ private String readGitPropertiesAsJson () throws Exception {
67+ StringBuilder json = new StringBuilder ();
68+ json .append ("{\n " );
69+
70+ // Read Git properties
71+ Properties gitProps = loadPropertiesFile ("git.properties" );
72+ if (gitProps != null ) {
73+ // For git.commit.id, look for both standard and abbrev versions
74+ String commitId = gitProps .getProperty ("git.commit.id" , null );
75+ if (commitId == null ) {
76+ commitId = gitProps .getProperty ("git.commit.id.abbrev" , "unknown" );
77+ }
78+ json .append (" \" git.commit.id\" : \" " ).append (commitId ).append ("\" ,\n " );
79+
80+ // For git.build.time, look for various possible property names
81+ String buildTime = gitProps .getProperty ("git.build.time" , null );
82+ if (buildTime == null ) {
83+ buildTime = gitProps .getProperty ("git.commit.time" , null );
84+ }
85+ if (buildTime == null ) {
86+ buildTime = gitProps .getProperty ("git.commit.timestamp" , "unknown" );
87+ }
88+ json .append (" \" git.build.time\" : \" " ).append (buildTime ).append ("\" ,\n " );
89+ } else {
90+ logger .warn ("git.properties file not found. Git information will be unavailable." );
91+ json .append (" \" git.commit.id\" : \" information unavailable\" ,\n " );
92+ json .append (" \" git.build.time\" : \" information unavailable\" ,\n " );
93+ }
94+
95+ // Read build properties if available
96+ Properties buildProps = loadPropertiesFile ("META-INF/build-info.properties" );
97+ if (buildProps != null ) {
98+ // Extract version - checking for both standard and nested formats
99+ String version = buildProps .getProperty ("build.version" , null );
100+ if (version == null ) {
101+ version = buildProps .getProperty ("build.version.number" , null );
102+ }
103+ if (version == null ) {
104+ version = buildProps .getProperty ("version" , "unknown" );
105+ }
106+ json .append (" \" build.version\" : \" " ).append (version ).append ("\" ,\n " );
107+
108+ // Extract time - checking for both standard and alternate formats
109+ String time = buildProps .getProperty ("build.time" , null );
110+ if (time == null ) {
111+ time = buildProps .getProperty ("build.timestamp" , null );
112+ }
113+ if (time == null ) {
114+ time = buildProps .getProperty ("timestamp" , "unknown" );
115+ }
116+ json .append (" \" build.time\" : \" " ).append (time ).append ("\" ,\n " );
117+ } else {
118+ logger .info ("build-info.properties not found, trying Maven properties" );
119+ // Fallback to maven project version
120+ Properties mavenProps = loadPropertiesFile ("META-INF/maven/com.iemr.admin/admin-api/pom.properties" );
121+ if (mavenProps != null ) {
122+ String version = mavenProps .getProperty ("version" , "unknown" );
123+ json .append (" \" build.version\" : \" " ).append (version ).append ("\" ,\n " );
124+ json .append (" \" build.time\" : \" " ).append (getCurrentIstTimeFormatted ()).append ("\" ,\n " );
125+ } else {
126+ logger .warn ("Neither build-info.properties nor Maven properties found." );
127+ json .append (" \" build.version\" : \" 3.1.0\" ,\n " ); // Default version
128+ json .append (" \" build.time\" : \" " ).append (getCurrentIstTimeFormatted ()).append ("\" ,\n " );
129+ }
130+ }
131+ json .append (" \" current.time\" : \" " ).append (getCurrentIstTimeFormatted ()).append ("\" \n " );
132+
133+ json .append (" }" );
134+ return json .toString ();
135+ }
136+
137+ /**
138+ * Get the current time formatted in Indian Standard Time (IST)
139+ * IST is UTC+5:30
140+ */
141+ private String getCurrentIstTimeFormatted () {
142+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss" );
143+ sdf .setTimeZone (TimeZone .getTimeZone ("Asia/Kolkata" ));
144+ return sdf .format (new Date ());
66145 }
67146
68- private String readFromInputStream (InputStream inputStream ) throws IOException {
69- StringBuilder resultStringBuilder = new StringBuilder ();
70- try (BufferedReader br = new BufferedReader (new InputStreamReader (inputStream ))) {
71- String line ;
72- while ((line = br .readLine ()) != null ) {
73- resultStringBuilder .append (line ).append ("\n " );
147+ private Properties loadPropertiesFile (String resourceName ) {
148+ ClassLoader classLoader = getClass ().getClassLoader ();
149+ try (InputStream inputStream = classLoader .getResourceAsStream (resourceName )) {
150+ if (inputStream != null ) {
151+ Properties props = new Properties ();
152+ props .load (inputStream );
153+ return props ;
74154 }
155+ } catch (IOException e ) {
156+ logger .warn ("Could not load properties file: " + resourceName , e );
75157 }
76- return resultStringBuilder . toString () ;
158+ return null ;
77159 }
78- }
160+ }
0 commit comments