diff --git a/.gitignore b/.gitignore index 3df9a13..5ff6309 100644 --- a/.gitignore +++ b/.gitignore @@ -1,88 +1,38 @@ -# ---> Java -# Compiled class file -*.class +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ -# Log file -*.log +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.nar -*.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* -replay_pid* - -# ---> Eclipse -.metadata -bin/ -tmp/ -*.tmp -*.bak -*.swp -*~.nib -local.properties -.settings/ -.loadpath -.recommenders - -# External tool builders -.externalToolBuilders/ - -# Locally stored "Eclipse launch configurations" -*.launch - -# PyDev specific (Python IDE for Eclipse) -*.pydevproject - -# CDT-specific (C/C++ Development Tooling) -.cproject - -# CDT- autotools -.autotools - -# Java annotation processor (APT) +### Eclipse ### +.apt_generated +.classpath .factorypath - -# PDT-specific (PHP Development Tools) -.buildpath - -# sbteclipse plugin -.target - -# Tern plugin -.tern-project - -# TeXlipse plugin -.texlipse - -# STS (Spring Tool Suite) +.project +.settings .springBeans +.sts4-cache -# Code Recommenders -.recommenders/ +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ -# Annotation Processing -.apt_generated/ -.apt_generated_test/ - -# Scala IDE specific (Scala & Java development for Eclipse) -.cache-main -.scala_dependencies -.worksheet - -# Uncomment this line if you wish to ignore the project description file. -# Typically, this file would be tracked if it contains build/dependency configurations: -#.project +### VS Code ### +.vscode/ +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/artifacts/TerreDelPapaApiClient_jar.xml b/.idea/artifacts/TerreDelPapaApiClient_jar.xml new file mode 100644 index 0000000..8b4a823 --- /dev/null +++ b/.idea/artifacts/TerreDelPapaApiClient_jar.xml @@ -0,0 +1,8 @@ + + + $PROJECT_DIR$/out/artifacts/TerreDelPapaApiClient_jar + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..2b63946 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/artifacts/TerreDelPapaApiClient_jar/TerreDelPapaApiClient.jar b/out/artifacts/TerreDelPapaApiClient_jar/TerreDelPapaApiClient.jar new file mode 100644 index 0000000..3d6811b Binary files /dev/null and b/out/artifacts/TerreDelPapaApiClient_jar/TerreDelPapaApiClient.jar differ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..4c16a7a --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.poloinformatico + TerreDelPapaApiClient + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/com/poloinformatico/TDPApiClient.java b/src/main/java/com/poloinformatico/TDPApiClient.java new file mode 100644 index 0000000..f71e433 --- /dev/null +++ b/src/main/java/com/poloinformatico/TDPApiClient.java @@ -0,0 +1,151 @@ +package com.poloinformatico; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class TDPApiClient { + public static void main(String[] args) { + + + } + + public static ArrayList doPost(String apiUrl,String params) { + + ArrayList res=new ArrayList(); + + System.out.println(params); + + // Request parameters (key-value pairs) + Map parameters = new HashMap<>(); + + String par1 = "",par2="",par3="",par4 = ""; + + try{ + par1=params.split("#")[0]; + }catch (Exception e){} + try{ + par2=params.split("#")[1]; + }catch(Exception e){} + try{ + par3=params.split("#")[2]; + }catch(Exception e){} + try{ + par3=params.split("#")[3]; + }catch(Exception e){} + + parameters.put("KEY_EXT", par1); + parameters.put("ToPLC1", par2); + parameters.put("ToPLC2", par3); + parameters.put("ToPLC3", par4); + + // Construct URL with parameters + StringBuilder urlBuilder = new StringBuilder(apiUrl); + urlBuilder.append("?"); + urlBuilder.append(buildUrlParameters(parameters)); + + StringBuilder response = null; + try { + URL url = new URL(urlBuilder.toString()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + // Set the request method to POST + connection.setRequestMethod("POST"); + + // Get the response code + int responseCode = connection.getResponseCode(); + System.out.println("Response Code: " + responseCode); + + // Read the response from the server + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + response = new StringBuilder(); + String line; + + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + + // Print the response + System.out.println("Response Body: " + response.toString()); + + res.add(String.valueOf(responseCode)); + res.add(String.valueOf(response)); + + // Disconnect the connection + connection.disconnect(); + + } catch (Exception e) { + e.printStackTrace(); + } + return res; + } + + public static ArrayList doGet(String apiUrl){ + + ArrayList res=new ArrayList(); + + try { + URL url = new URL(apiUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + + // Set request method to GET + connection.setRequestMethod("GET"); + + // Get the response code + int responseCode = connection.getResponseCode(); + System.out.println("Response Code: " + responseCode); + + // Read the response from the server + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String line; + + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + + // Print the response + System.out.println("Response Body: " + response.toString()); + + res.add(String.valueOf(responseCode)); + res.add(String.valueOf(response)); + + // Disconnect the connection + connection.disconnect(); + + } catch (Exception e) { + e.printStackTrace(); + } + + + return res; + } + + private static String buildUrlParameters(Map params) { + StringBuilder urlParams = new StringBuilder(); + + for (Map.Entry entry : params.entrySet()) { + try { + if (urlParams.length() != 0) { + urlParams.append("&"); + } + urlParams.append(URLEncoder.encode(entry.getKey(), "UTF-8")) + .append("=") + .append(URLEncoder.encode(entry.getValue(), "UTF-8")); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); // Handle the exception according to your needs + } + } + + return urlParams.toString(); + } + +} diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..58b75db --- /dev/null +++ b/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: com.poloinformatico.TDPApiClient + diff --git a/src/test/java/test1.java b/src/test/java/test1.java new file mode 100644 index 0000000..447f80b --- /dev/null +++ b/src/test/java/test1.java @@ -0,0 +1,18 @@ +import com.poloinformatico.TDPApiClient; + +public class test1 { + + + + public static void main(String[] args) { + + System.out.println(TDPApiClient.doPost("https://jsonplaceholder.typicode.com/posts","2023_5# #0# ")); + + + System.out.println(TDPApiClient.doGet("https://jsonplaceholder.typicode.com/posts/1")); + + + } + + +}