diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a31b98 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/assembly/resources/application.properties b/assembly/resources/application.properties new file mode 100644 index 0000000..1ae3033 --- /dev/null +++ b/assembly/resources/application.properties @@ -0,0 +1,7 @@ +# TRM API +trm.address=localhost +trm.port=4028 + +# API Server +server.address=localhost +server.port=4029 diff --git a/assembly/resources/start.bat b/assembly/resources/start.bat new file mode 100644 index 0000000..2d3053b --- /dev/null +++ b/assembly/resources/start.bat @@ -0,0 +1 @@ +java -Xmx256m -jar trmhelper.jar \ No newline at end of file diff --git a/assembly/zip-assembly.xml b/assembly/zip-assembly.xml new file mode 100644 index 0000000..0733d9b --- /dev/null +++ b/assembly/zip-assembly.xml @@ -0,0 +1,24 @@ + + + zip + true + + + zip + + + + + ${project.basedir}/assembly/resources + / + + + + + ${project.build.directory}/${project.artifactId}-${project.version}.${project.packaging} + ${project.artifactId}.jar + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..6d7fb6e --- /dev/null +++ b/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 2.4.1 + + + + gk + trmhelper + 1.0.0-beta + TRM_API_Wrapper + Wrapper for TRM + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-json + + + + + org.springframework.boot + spring-boot-devtools + true + + + org.projectlombok + lombok + true + + + + + + + maven-dependency-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + false + + assembly\zip-assembly.xml + + + + + + + + + diff --git a/src/main/java/gk/trm/TrmHelper.java b/src/main/java/gk/trm/TrmHelper.java new file mode 100644 index 0000000..94f4848 --- /dev/null +++ b/src/main/java/gk/trm/TrmHelper.java @@ -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"); + } + +} diff --git a/src/main/java/gk/trm/wrapper/ApiController.java b/src/main/java/gk/trm/wrapper/ApiController.java new file mode 100644 index 0000000..516e11f --- /dev/null +++ b/src/main/java/gk/trm/wrapper/ApiController.java @@ -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); + } + +} diff --git a/src/main/java/gk/trm/wrapper/ApiReader.java b/src/main/java/gk/trm/wrapper/ApiReader.java new file mode 100644 index 0000000..bb4dca6 --- /dev/null +++ b/src/main/java/gk/trm/wrapper/ApiReader.java @@ -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() + "\"}"; + } + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..b73577b --- /dev/null +++ b/src/main/resources/application.properties @@ -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}