Create application structure

api
├── getHosts.sh
├── pom.xml
├── src
│   └── main
│   ├── java
│   │   └── com
│   │   └── cba
│   │   └── web
│   │   ├── host.java
│   │   └── hosts.java
│   └── webapp
│   ├── css
│   │   └── cba.css
│   ├── index.html
│   ├── META-INF
│   │   └── context.xml
│   └── WEB-INF
│   ├── cgi
│   └── web.xml

create a pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.cba.web</groupId>
<artifactId>api</artifactId>
<version>1</version>
<packaging>war</packaging>

<name>api</name>
<url>https://www.custom-built-apps.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<gson.version>2.9.0</gson.version>
<javax.servlet.version>3.1.0</javax.servlet.version>
</properties>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/java/com/cba/web/</sourceDirectory>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Create class to hold the data for serialiazation/deserialiazation

// File : host.java
// Author : Boris Alexandrov
// Date : Tue Jul 5 21:09:02 EDT 2022
// Purpose : class to use for serialzation and deserializtion of JSON
///////////////////////////////////////////////////

package com.cba.web;
import java.util.*;
import java.io.*;
import java.lang.*;


public class host implements Serializable
{
     String id;
     String hostname;
     String OS;
     String IpAddress;
public host(String id, String hostname, String OS, String IpAddress)
{
      this.id=id;
      this.hostname=hostname;
      this.OS=OS;
      this.IpAddress=IpAddress;
}

    public String getId() { return id; }
    public String getOS() {return OS; }
    public String getHostname() { return hostname;}
    public String returnIpAddress() {return IpAddress;}
};


Create the servlet

// Author : Boris Alexandrov
// Date : Tue Jul 5 21:09:02 EDT 2022
// Purpose : Servlet for Tomcat webservices
///////////////////////////////////////////////////

package com.cba.web;
import java.sql.*;
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.*;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;


public class hosts extends HttpServlet
{
Connection conn = null;

public void init(ServletConfig config) throws ServletException
{
super.init(config);
}

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{

String url = "jdbc:postgresql://192.168.2.30:5432/cbaadmin";
Properties props = new Properties();
props.setProperty("user","cbaadmin");
props.setProperty("password","pWdHeRe");
PrintWriter out = response.getWriter();
try
{
Connection conn = DriverManager.getConnection(url, props);
String strSQL="select h.id,h.hostname,o.name,h.ip_address from public.hosts h join public.os o on o.id=h.os_id";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(strSQL);
conn.close();
Vector<host> hs= new Vector<host>();

while(rs.next())
{
    hs.add(new host(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4)));
}


GsonBuilder builder = new GsonBuilder();
builder.setPrettyPrinting();
Gson gson = builder.create();

String hostsJsonString = gson.toJson(hs);

response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(hostsJsonString);
out.flush();

}
catch(SQLException e)
{
out.println(e.getMessage());
}

}

};

create web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">

<display-name>webservice api</display-name>

<servlet>
<servlet-name>hosts</servlet-name>
<servlet-class>com.cba.web.hosts</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>cgi</servlet-name>
<servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class>
<init-param>
<param-name>cgiPathPrefix</param-name>
<param-value>WEB-INF/cgi</param-value>
</init-param>
<init-param>
<param-name>executable</param-name>
<param-value></param-value>
</init-param>

<init-param>
<param-name>passShellEnvironment</param-name>
<param-value>"true"</param-value>
</init-param>

<init-param>
<param-name>environment-variable-LD_LIBRARY_PATH</param-name>
<param-value>/usr/local/lib64:/usr/lib64:/usr/lib64/graphviz:/app/hadoop/lib/native:/usr/pgsql-10/lib</param-value>
</init-param>

<load-on-startup>5</load-on-startup>
</servlet>


<servlet-mapping>
<servlet-name>cgi</servlet-name>
<url-pattern>/cgi-bin/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>hosts</servlet-name>
<url-pattern>/getHosts</url-pattern>
</servlet-mapping>

</web-app>

Build and deploy the application

from api directory(root)

mvn package 

copy the war to /tmp

cp target/api-1.war /tmp/api.war
chmod 777 /tmp/api.war

deploy from tomcat application manager as tomcat user


Create a curl shell script

getHosts.sh

curl http://r02edge.custom-built-apps.com:1962/api/getHosts


Run the curl


[dataexplorer1@r02edge api]$ ./getHosts.sh
[
{
"id": "1",
"hostname": "r02edge",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.30"
},
{
"id": "2",
"hostname": "r01edge",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.22"
},
{
"id": "3",
"hostname": "datanode1",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.23"
},
{
"id": "4",
"hostname": "datanode2",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.24"
},
{
"id": "5",
"hostname": "datanode3",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.27"
},
{
"id": "6",
"hostname": "datanode4",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.28"
},
{
"id": "7",
"hostname": "datanode5",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.29"
},
{
"id": "8",
"hostname": "namenode1",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.25"
},
{
"id": "9",
"hostname": "namenode2",
"OS": "CentOS Linux release 7.8.2003 (Core)",
"IpAddress": "192.168.2.26"
}
]



Attachments: