Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/main/java/com/mycompany/app/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

/**
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/mycompany/app/oAuth.java
Original file line number Diff line number Diff line change
@@ -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();
}



}
1 change: 1 addition & 0 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
requires com.esri.arcgisruntime;
requires javafx.graphics;
requires org.slf4j.nop;
requires jdk.httpserver;

exports com.mycompany.app;
}