ObrienlabsDev / rest-client-java

A java SE REST client
Apache License 2.0
0 stars 0 forks source link

Java SE REST Client #1

Open obriensystems opened 12 months ago

obriensystems commented 12 months ago

https://github.com/obrienlabs/magellan/issues/26

public class RestClient {

    // from 

    static Logger logger = Logger.getLogger(RestClient.class.getName());

    private static final String URL_CREATE_RECORD =
                "http://biometric.elasticbeanstalk.com/FrontController?action=activeid";

    public static void restCall() {     
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(URL_CREATE_RECORD))
                .GET()
                .build();

    //HttpClient client = HttpClient.newBuilder().build();
    HttpClient client = HttpClient.newBuilder()
            .version(Version.HTTP_1_1)
            .followRedirects(Redirect.NORMAL)
            .connectTimeout(Duration.ofSeconds(20))
            //.proxy(ProxySelector.of(new InetSocketAddress("proxy.example.com", 80)))
            //.authenticator(Authenticator.getDefault())
            .build();
    try {
        HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
        String body = response.body();
        logger.info("Response: " + body);

        System.out.println(response.statusCode());
        System.out.println(response.body()); 
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (InterruptedException ie) {
        ie.printStackTrace();
    }   
    }

    public static void main( String[] args )
    {
        RestClient app = new RestClient();
        app.restCall();
    }

}

200
{ "id" : 202311035}

Nov 07, 2023 11:08:38 AM dev.obrienlabs.RestClient restCall
INFO: Response: { "id" : 202311035}
obriensystems commented 12 months ago

https target endpoint Use the following GCP Google Cloud Functions https endpoint (org: oldev - proj: dev/eventstream-dev list parameter is input into a random selector https://northamerica-northeast1-eventstream-dev.cloudfunctions.net/random2?list=first,second,third,forth {key: 1, value: second}

Retrofit for old school spring boot https://www.thomasvitale.com/https-spring-boot-ssl-certificate/ https://www.baeldung.com/spring-boot-https-self-signed-certificate

obriensystems commented 12 months ago

java -cp rest-client-java-1.0-SNAPSHOT.jar dev.obrienlabs.RestClient

-jar when executable jar

obriensystems commented 12 months ago
michaelobrien@mbp7 rest-client-java % mvn clean install -U
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------< dev.obrienlabs:rest-client-java >-------------------
[INFO] Building rest-client-java 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ rest-client-java ---
[INFO] Deleting /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/target
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ rest-client-java ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ rest-client-java ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ rest-client-java ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ rest-client-java ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ rest-client-java ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running dev.obrienlabs.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.011 s - in dev.obrienlabs.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ rest-client-java ---
[INFO] Building jar: /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/target/rest-client-java-1.0-SNAPSHOT.jar
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ rest-client-java ---
[INFO] Installing /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/target/rest-client-java-1.0-SNAPSHOT.jar to /Users/michaelobrien/.m2/repository/dev/obrienlabs/rest-client-java/1.0-SNAPSHOT/rest-client-java-1.0-SNAPSHOT.jar
[INFO] Installing /Users/michaelobrien/wse_github/ObrienlabsDev/rest-client-java/pom.xml to /Users/michaelobrien/.m2/repository/dev/obrienlabs/rest-client-java/1.0-SNAPSHOT/rest-client-java-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.349 s
[INFO] Finished at: 2023-11-07T16:17:38-05:00
[INFO] ------------------------------------------------------------------------
michaelobrien@mbp7 rest-client-java % cd target 
michaelobrien@mbp7 target % java -cp rest-client-java-1.0-SNAPSHOT.jar dev.obrienlabs.RestClient
Nov 07, 2023 4:17:44 PM dev.obrienlabs.RestClient restCall
INFO: Response: { "id" : 202311035}

200
{ "id" : 202311035}

with proxy
 java -Dhttp.proxyHost=1.2.3.4 -Dhttp.proxyPort=8192 -cp rest-client-java-1.0-SNAPSHOT.jar dev.obrienlabs.RestClient

https
 java -Dhttps.proxyHost=1.2.3.4 -Dhttps.proxyPort=8192 -cp rest-client-java-1.0-SNAPSHOT.jar dev.obrienlabs.RestClient

and note double -- needs no spaces if mixed with single -D
fmichaelobrien commented 12 months ago

switch to -Dhttps not http option 0: edit https -D's and put them in the tomcat jvm options

fmichaelobrien commented 11 months ago

https proxy settings must be at the war/jar classloader level to allow for AD authentication via System.setProperty()