2121*/
2222package com .iemr .admin .controller .version ;
2323
24- import java .io .IOException ;
25- import java .io .InputStream ;
26- import java .text .SimpleDateFormat ;
27- import java .util .Date ;
28- import java .util .Properties ;
29- import java .util .TimeZone ;
30-
3124import org .slf4j .Logger ;
3225import org .slf4j .LoggerFactory ;
26+ import org .springframework .beans .factory .annotation .Autowired ;
3327import org .springframework .http .MediaType ;
3428import org .springframework .web .bind .annotation .RequestMapping ;
3529import org .springframework .web .bind .annotation .RequestMethod ;
3630import org .springframework .web .bind .annotation .RestController ;
3731
32+ import com .iemr .admin .service .version .VersionService ;
3833import com .iemr .admin .utils .response .OutputResponse ;
3934
4035import io .swagger .v3 .oas .annotations .Operation ;
4136
42-
4337@ RestController
4438public class VersionController {
4539
46- private Logger logger = LoggerFactory .getLogger (this .getClass ().getSimpleName ());
47-
48- @ Operation (summary = "Version information" )
49- @ RequestMapping (value = "/version" , method = { RequestMethod .GET }, produces = MediaType .APPLICATION_JSON_VALUE )
50- public String versionInformation () {
51- OutputResponse output = new OutputResponse ();
52- try {
53- logger .info ("version Controller Start" );
54- // Set the version information as JSON string directly
55- output .setResponse (readGitPropertiesAsJson ());
56- } catch (Exception e ) {
57- output .setError (e );
58- }
59-
60- logger .info ("version Controller End" );
61-
62- // Use standard toString() - no custom formatting
63- return output .toString ();
64- }
65-
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 ());
145- }
146-
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 ;
154- }
155- } catch (IOException e ) {
156- logger .warn ("Could not load properties file: " + resourceName , e );
157- }
158- return null ;
159- }
40+ private static final Logger logger = LoggerFactory .getLogger (VersionController .class );
41+
42+ @ Autowired
43+ private VersionService versionService ;
44+
45+ @ Operation (summary = "Version information" )
46+ @ RequestMapping (value = "/version" , method = { RequestMethod .GET }, produces = MediaType .APPLICATION_JSON_VALUE )
47+ public String versionInformation () {
48+ OutputResponse output = new OutputResponse ();
49+ try {
50+ logger .info ("version Controller Start" );
51+ output .setResponse (versionService .getVersionInformation ());
52+ } catch (Exception e ) {
53+ logger .error ("Error in version controller" , e );
54+ output .setError (e );
55+ }
56+
57+ logger .info ("version Controller End" );
58+ return output .toString ();
59+ }
16060}
0 commit comments