From b8aec1b4007cc9f34a931db96775b5ebfda0634a Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Fri, 14 Jan 2022 14:59:14 -0500 Subject: [PATCH] feat(ahc): initial commit --- ahc/.gitignore | 31 ++++ ahc/README.md | 4 + ahc/pom.xml | 107 +++++++++++++ ahc/src/main/java/de/swingbe/ahc/Main.java | 166 +++++++++++++++++++++ 4 files changed, 308 insertions(+) create mode 100644 ahc/.gitignore create mode 100644 ahc/README.md create mode 100644 ahc/pom.xml create mode 100644 ahc/src/main/java/de/swingbe/ahc/Main.java diff --git a/ahc/.gitignore b/ahc/.gitignore new file mode 100644 index 0000000..33653da --- /dev/null +++ b/ahc/.gitignore @@ -0,0 +1,31 @@ + +# Files +.classpath +.externalToolBuilders +.gradle +.project +.pydevproject +.settings +.sonar +*~ +*.ipr +*.iml +*.iws +*.swp +*.DS_Store +*.snap.debug +dependency-reduced-pom.xml + +# Directories +.idea/ +.run/ +.venv/ +_site/ +build/ +dist/ +docs/_build/ +gen-java/ +gen-javabean/ +gen-py/ +node_modules/ +target/ diff --git a/ahc/README.md b/ahc/README.md new file mode 100644 index 0000000..59d125f --- /dev/null +++ b/ahc/README.md @@ -0,0 +1,4 @@ +# Overview +This project can serve as example for an Apache HTTP client with Maven and Java. + +# Links diff --git a/ahc/pom.xml b/ahc/pom.xml new file mode 100644 index 0000000..42ffa0f --- /dev/null +++ b/ahc/pom.xml @@ -0,0 +1,107 @@ + + + 4.0.0 + + ahc + description + https://swingbe.de + de.swingbe.ahc + ahc + 0.0.1 + jar + + + + GNU General Public License + https://www.gnu.org/licenses/gpl-3.0.txt + + + + + https://github.com/Software-Ingenieur-Begerad/sandbox-java + + + + + UTF-8 + + + + + + org.apache.httpcomponents + httpclient + 4.5.13 + + + + org.apache.commons + commons-io + 1.3.2 + + + + org.json + json + 20211205 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + + 11 + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.1 + + + package + + shade + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + true + + shaded + + + + + de.swingbe.ahc.Main + + + + + + + + + + + diff --git a/ahc/src/main/java/de/swingbe/ahc/Main.java b/ahc/src/main/java/de/swingbe/ahc/Main.java new file mode 100644 index 0000000..412df81 --- /dev/null +++ b/ahc/src/main/java/de/swingbe/ahc/Main.java @@ -0,0 +1,166 @@ +package de.swingbe.ahc; + +import org.apache.http.Header; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicHeader; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.Scanner; + +public class Main { + + static String URL = "https://dedriver.org"; + static String PORT = "42001"; + static String ROUTE = "/postdata"; + + public static void main(String[] args) { + System.out.println("Hello world!"); + post("uuid", 87.263783, 52.9019052, 0, "alias", "vehicle"); + System.out.println("Done!"); + } + + static void post(final String uuid, final double latitude, final double longitude, + final long timestamp, final String alias, final String vehicle) { + + String address = URL + ":" + PORT + ROUTE; + System.out.println("address: " + address); + + //create a HTTP POST request + //use web service endpoint or web site page as url + HttpPost post = new HttpPost(address); + + //set request headers for request data in JSON format + Header[] headers = { + new BasicHeader("Content-type", "application/json"), + }; + //the request payload is in JSON format + + //set request headers + post.setHeaders(headers); + + //create payload + //create request data in JSON format + //create JSON object + JSONObject payload = new JSONObject(); + try { + payload.put("uuid", uuid); + payload.put("latitude", latitude); + payload.put("longitude", longitude); + payload.put("timestamp", timestamp); + payload.put("alias", alias); + payload.put("vehicle", vehicle); + } catch (JSONException e) { + System.out.println("ERROR: JSON error detected"); + e.printStackTrace(); + } + + //set the payload + HttpEntity entity; + entity = new ByteArrayEntity(payload.toString().getBytes(StandardCharsets.UTF_8)); + post.setEntity(entity); + + //create a HTTP client + HttpClient client = HttpClients.custom().build(); + + //send request + HttpResponse response = null; + try { + response = client.execute(post); + } catch (IOException e) { + System.out.println("ERROR: IO error detected"); + e.printStackTrace(); + } + + //read response status + Scanner sc = null; + try { + if (response != null) { + sc = new Scanner(response.getEntity().getContent()); + } + } catch (IOException e) { + System.out.println("ERROR: IO error detected"); + e.printStackTrace(); + } + + //print status line + if (response != null) { + System.out.println("status line: " + response.getStatusLine()); + } + if (sc != null) { + while (sc.hasNext()) { + System.out.println("status line: " + sc.nextLine()); + } + } + + //close interaction + try { + if (response != null) { + response.getEntity().getContent().close(); + } + } catch (IOException e) { + System.out.println("ERROR: IO error detected"); + e.printStackTrace(); + } + + //verify response + int responseCode = 0; + if (response != null) { + responseCode = response.getStatusLine().getStatusCode(); + System.out.println("responseCode: " + responseCode); + } + String statusPhrase = null; + if (response != null) { + statusPhrase = response.getStatusLine().getReasonPhrase(); + System.out.println("statusPhrase: " + statusPhrase); + } + + } +} +/** + * TODO tidy up + * if (post != null) { + * //todo string or byte entity? + * HttpEntity entity = null; + * try { + * entity = new ByteArrayEntity(postData.toString().getBytes("UTF-8")); + * } catch (UnsupportedEncodingException e) { + * Timber.e("doInBackground: HTTP entity unavailable: %s", e); + * e.printStackTrace(); + * } + *

+ * if (entity != null) { + * post.setHeader("Content-Type", "application/json"); + * post.setEntity(entity); + *

+ * HttpResponse httpResponse = null; + * try { + * httpResponse = httpClient.execute(post); + * } catch (IOException e) { + * Timber.e("doInBackground: execute post failed"); + * e.printStackTrace(); + * return "execute post failed"; + * } + * //TODO Why is it necessary to consume response? + * HttpEntity entityRsp = httpResponse.getEntity(); + * if (entityRsp != null) { + * try { + * entityRsp.consumeContent(); + * } catch (IOException e) { + * e.printStackTrace(); + * } + * } + * } else { + * Timber.w("doInBackground: HTTP entity unavailable"); + * } + * } else { + * Timber.w("doInBackground: http post instance unavailable"); + * } + */