Skip to content

Commit

Permalink
1.0.0-beta
Browse files Browse the repository at this point in the history
Initial Commit
  • Loading branch information
G-Kumaran committed Jan 10, 2021
0 parents commit 97a96d6
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
HELP.md
target/
.mvn/*
!**/src/main/**/target/
!**/src/test/**/target/

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/
/application.properties
7 changes: 7 additions & 0 deletions assembly/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TRM API
trm.address=localhost
trm.port=4028

# API Server
server.address=localhost
server.port=4029
1 change: 1 addition & 0 deletions assembly/resources/start.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java -Xmx256m -jar trmhelper.jar
24 changes: 24 additions & 0 deletions assembly/zip-assembly.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">

<id>zip</id>
<includeBaseDirectory>true</includeBaseDirectory>

<formats>
<format>zip</format>
</formats>

<fileSets>
<fileSet>
<directory>${project.basedir}/assembly/resources</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
<files>
<file>
<source> ${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging}</source>
<destName>${project.artifactId}.jar</destName>
</file>
</files>
</assembly>
80 changes: 80 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath />
</parent>

<groupId>gk</groupId>
<artifactId>trmhelper</artifactId>
<version>1.0.0-beta</version>
<name>TRM_API_Wrapper</name>
<description>Wrapper for TRM</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludeDevtools>true</excludeDevtools>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>assembly\zip-assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
24 changes: 24 additions & 0 deletions src/main/java/gk/trm/TrmHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package gk.trm;

import org.springframework.boot.Banner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
public class TrmHelper
{
public static void main(String[] args)
{
new SpringApplicationBuilder(TrmHelper.class) .bannerMode(Banner.Mode.OFF)
.web(WebApplicationType.SERVLET)
.registerShutdownHook(true)
.build()
.run(args);
log.warn("Initiation : Success");
}

}
26 changes: 26 additions & 0 deletions src/main/java/gk/trm/wrapper/ApiController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gk.trm.wrapper;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
class ApiController
{
@Value("${trm.address:localhost}")
private String ip;

@Value("${trm.port:4028}")
private int port;

@GetMapping("/{command}")
public String getApi(@PathVariable
String command)
{
return ApiReader.get(ip, port, command);
}

}
51 changes: 51 additions & 0 deletions src/main/java/gk/trm/wrapper/ApiReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gk.trm.wrapper;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.Socket;

import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

@Slf4j
class ApiReader
{
static String get(final String apiIp, final int apiPort, final String command)
{
log.debug("Connecting to {}:{} - {}", apiIp, apiPort, command);

try
{
final String request = "{\"command\":\"" + command + "\"}";
final byte[] stringBytes = (request + "\n").getBytes("UTF-8");

@Cleanup
final Socket s = new Socket();
s.connect(new InetSocketAddress(apiIp, apiPort), 1000);
log.debug("Connection to {}:{} - {} : {}", apiIp, apiPort, request, s.isConnected());

@Cleanup
final DataOutputStream APIConnection = new DataOutputStream(s.getOutputStream());
APIConnection.write(stringBytes, 0, stringBytes.length);
APIConnection.flush();

@Cleanup
final BufferedReader reader = new BufferedReader(new InputStreamReader(s.getInputStream()));
final char[] a = new char[1500];
reader.read(a);

final String response = String .copyValueOf(a)
.trim();

log.debug("Receive : {}", response);
return response;

} catch (Exception e)
{
log.error("Error : ", e);
return "{\"message\":\"Error getting API at " + apiIp + ":" + apiPort + "/" + command + ". Reason - " + e.getLocalizedMessage() + "\"}";
}
}
}
12 changes: 12 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#Server
server.address=localhost
server.port=4029
server.shutdown=graceful
server.compression.enabled=true
server.compression.mime-types=application/json,application/xml,text/html,text/xml,text/plain,application/javascript,text/css,image/jpeg,image/png


#Logging
logging.level.root=ERROR
logging.level.gk.trm=INFO
logging.pattern.console=%clr(%d{ISO8601}) %clr([%-5level]) %clr(%-40.40logger{39}){cyan} %clr(:) %msg%n%clr(%throwable){red}

0 comments on commit 97a96d6

Please sign in to comment.