From 645e943e998ca7cb337a16d2d47fce9d30544a83 Mon Sep 17 00:00:00 2001 From: mbcoder Date: Mon, 24 Apr 2023 15:32:03 +0100 Subject: [PATCH] Initial experiments --- src/main/java/com/mycompany/app/App.java | 33 ++++++---- src/main/java/com/mycompany/app/oAuth.java | 70 ++++++++++++++++++++++ src/main/java/module-info.java | 1 + 3 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/mycompany/app/oAuth.java diff --git a/src/main/java/com/mycompany/app/App.java b/src/main/java/com/mycompany/app/App.java index 9ceec1d..67fec58 100644 --- a/src/main/java/com/mycompany/app/App.java +++ b/src/main/java/com/mycompany/app/App.java @@ -20,8 +20,16 @@ import com.esri.arcgisruntime.mapping.BasemapStyle; import com.esri.arcgisruntime.mapping.ArcGISMap; import com.esri.arcgisruntime.mapping.view.MapView; +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; import javafx.application.Application; import javafx.scene.Scene; +import javafx.scene.control.Button; import javafx.scene.layout.StackPane; import javafx.stage.Stage; @@ -48,22 +56,21 @@ public void start(Stage stage) { Scene scene = new Scene(stackPane); stage.setScene(scene); - // Note: it is not best practice to store API keys in source code. - // An API key is required to enable access to services, web maps, and web scenes hosted in ArcGIS Online. - // If you haven't already, go to your developer dashboard to get your API key. - // Please refer to https://developers.arcgis.com/java/get-started/ for more information - String yourApiKey = "YOUR_API_KEY"; - ArcGISRuntimeEnvironment.setApiKey(yourApiKey); + Button button = new Button("Login"); + button.setOnAction(event -> { - // create a MapView to display the map and add it to the stack pane - mapView = new MapView(); - stackPane.getChildren().add(mapView); - // create an ArcGISMap with an imagery basemap - ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_IMAGERY); + System.out.println("pressed login"); - // display the map by setting the map on the map view - mapView.setMap(map); + String clientID = "55QgK81zBgA3q7Zw"; + String response_type = "code"; + String redirect = "http://localhost:1234/auth"; + + oAuth oAuth = new oAuth(clientID, redirect, "https://www.arcgis.com", stage, getHostServices()); + oAuth.authenticate(); + }); + + stackPane.getChildren().add(button); } /** diff --git a/src/main/java/com/mycompany/app/oAuth.java b/src/main/java/com/mycompany/app/oAuth.java new file mode 100644 index 0000000..12719d7 --- /dev/null +++ b/src/main/java/com/mycompany/app/oAuth.java @@ -0,0 +1,70 @@ +package com.mycompany.app; + +import com.sun.net.httpserver.HttpContext; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpServer; +import java.io.IOException; +import java.io.OutputStream; +import java.net.InetSocketAddress; +import javafx.application.HostServices; +import javafx.stage.Stage; + +public class oAuth { + private String clientID; + private String redirectURI; + private String baseUrl; + private HostServices hostServices; + private HttpServer httpServer; + + private Stage stage; + + public oAuth (String clientID, String redirectURI, String url, Stage stage, HostServices hostServices) { + this.clientID = clientID; + this.redirectURI = redirectURI; + this.baseUrl = url; + this.stage = stage; + this.hostServices = hostServices; + } + + public void authenticate() { + + // set up a http server to listen to redirect + try { + httpServer = HttpServer.create(new InetSocketAddress("localhost", 1234), 0); + HttpContext context = httpServer.createContext("/auth"); + context.setHandler(exchange -> handleRequest(exchange)); + httpServer.start(); + + // open up default browser with login page + String loginURL = baseUrl + "/sharing/oauth2/authorize?client_id=" + + clientID + "&response_type=code&redirect_uri=" + redirectURI; + + hostServices.showDocument(loginURL); + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void handleRequest(HttpExchange exchange) throws IOException { + + // crude capture of the auth code needed to get the token + System.out.println("message : " + exchange.getRequestURI().toString()); + + // output something in the web browser to say go back to your app + String response = "Please return to your app"; + exchange.sendResponseHeaders(200, response.getBytes().length);//response code and length + OutputStream os = exchange.getResponseBody(); + os.write(response.getBytes()); + os.close(); + + // stop the server + this.httpServer.stop(0); + + // attempt to return focus to the JavaFX app, but this doesn't work + this.stage.requestFocus(); + } + + + +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 24d021f..32006a4 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -19,6 +19,7 @@ requires com.esri.arcgisruntime; requires javafx.graphics; requires org.slf4j.nop; + requires jdk.httpserver; exports com.mycompany.app; }