feat(ahc): initial commit
This commit is contained in:
parent
bf2a3af044
commit
b8aec1b400
|
@ -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/
|
|
@ -0,0 +1,4 @@
|
||||||
|
# Overview
|
||||||
|
This project can serve as example for an Apache HTTP client with Maven and Java.
|
||||||
|
|
||||||
|
# Links
|
|
@ -0,0 +1,107 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<name>ahc</name>
|
||||||
|
<description>description</description>
|
||||||
|
<url>https://swingbe.de</url>
|
||||||
|
<groupId>de.swingbe.ahc</groupId>
|
||||||
|
<artifactId>ahc</artifactId>
|
||||||
|
<version>0.0.1</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>GNU General Public License</name>
|
||||||
|
<url>https://www.gnu.org/licenses/gpl-3.0.txt</url>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<url>https://github.com/Software-Ingenieur-Begerad/sandbox-java</url>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- Other properties -->
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.httpcomponents</groupId>
|
||||||
|
<artifactId>httpclient</artifactId>
|
||||||
|
<version>4.5.13</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-io</artifactId>
|
||||||
|
<version>1.3.2</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.json/json -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.json</groupId>
|
||||||
|
<artifactId>json</artifactId>
|
||||||
|
<version>20211205</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.1</version>
|
||||||
|
<configuration>
|
||||||
|
<!-- Target Java versions -->
|
||||||
|
<release>11</release>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<filters>
|
||||||
|
<filter>
|
||||||
|
<!-- exclude signatures from merged JAR to avoid invalid signature messages -->
|
||||||
|
<artifact>*:*</artifact>
|
||||||
|
<excludes>
|
||||||
|
<exclude>META-INF/*.SF</exclude>
|
||||||
|
<exclude>META-INF/*.DSA</exclude>
|
||||||
|
<exclude>META-INF/*.RSA</exclude>
|
||||||
|
</excludes>
|
||||||
|
</filter>
|
||||||
|
</filters>
|
||||||
|
<!-- The shaded JAR will not be the main artifact for the project, it will be attached
|
||||||
|
for deployment in the way source and docs are. -->
|
||||||
|
<shadedArtifactAttached>true
|
||||||
|
</shadedArtifactAttached>
|
||||||
|
<shadedClassifierName>shaded</shadedClassifierName>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
|
||||||
|
<transformer
|
||||||
|
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||||
|
<manifestEntries>
|
||||||
|
<Main-Class>de.swingbe.ahc.Main
|
||||||
|
</Main-Class>
|
||||||
|
</manifestEntries>
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
|
@ -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();
|
||||||
|
* }
|
||||||
|
* <p>
|
||||||
|
* if (entity != null) {
|
||||||
|
* post.setHeader("Content-Type", "application/json");
|
||||||
|
* post.setEntity(entity);
|
||||||
|
* <p>
|
||||||
|
* 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");
|
||||||
|
* }
|
||||||
|
*/
|
Loading…
Reference in New Issue