Skip to content

Commit f006cc8

Browse files
JCJC
authored andcommitted
Added check that authentication went through
1 parent 98bd0cd commit f006cc8

File tree

4 files changed

+78
-78
lines changed

4 files changed

+78
-78
lines changed

src/main/java/com/esri/samples/map/create_and_save_map/AuthenticationDialog.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,11 @@
3030
*/
3131
class AuthenticationDialog extends Dialog<OAuthConfiguration> {
3232

33-
@FXML
34-
private TextField portalURL;
35-
@FXML
36-
private TextField clientID;
37-
@FXML
38-
private TextField redirectURI;
39-
@FXML
40-
private ButtonType cancelButton;
41-
@FXML
42-
private ButtonType continueButton;
33+
@FXML private TextField portalURL;
34+
@FXML private TextField clientID;
35+
@FXML private TextField redirectURI;
36+
@FXML private ButtonType cancelButton;
37+
@FXML private ButtonType continueButton;
4338

4439
AuthenticationDialog() {
4540
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/create_and_save_map_auth_dialog.fxml"));
@@ -60,18 +55,20 @@ class AuthenticationDialog extends Dialog<OAuthConfiguration> {
6055
try {
6156
return new OAuthConfiguration(portalURL.getText(), clientID.getText(), redirectURI.getText());
6257
} catch (Exception e) {
63-
Alert alert = new Alert(Alert.AlertType.ERROR);
64-
alert.setContentText(e.getMessage());
65-
alert.show();
58+
showMessage(e.getMessage());
6659
}
6760
} else {
68-
Alert alert = new Alert(Alert.AlertType.ERROR);
69-
alert.setContentText("missing credentials");
70-
alert.show();
61+
showMessage("missing credentials");
7162
}
7263
}
7364
return null;
7465
});
7566
}
7667

77-
}
68+
private void showMessage(String message) {
69+
Alert alert = new Alert(Alert.AlertType.ERROR);
70+
alert.setContentText(message);
71+
alert.show();
72+
}
73+
74+
}

src/main/java/com/esri/samples/map/create_and_save_map/CreateAndSaveMapController.java

Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
import java.util.List;
2121
import java.util.concurrent.ExecutionException;
2222

23+
import javafx.fxml.FXML;
24+
import javafx.scene.control.Alert;
25+
import javafx.scene.control.Button;
26+
import javafx.scene.control.ComboBox;
27+
import javafx.scene.control.ListCell;
28+
import javafx.scene.control.ListView;
29+
import javafx.scene.control.ProgressIndicator;
30+
import javafx.scene.control.SelectionMode;
31+
import javafx.scene.control.TextArea;
32+
import javafx.scene.control.TextField;
33+
import javafx.util.StringConverter;
34+
2335
import com.esri.arcgisruntime.concurrent.ListenableFuture;
2436
import com.esri.arcgisruntime.layers.ArcGISMapImageLayer;
2537
import com.esri.arcgisruntime.layers.Layer;
@@ -34,22 +46,8 @@
3446
import com.esri.arcgisruntime.security.AuthenticationManager;
3547
import com.esri.arcgisruntime.security.OAuthConfiguration;
3648

37-
import javafx.fxml.FXML;
38-
import javafx.scene.control.Alert;
39-
import javafx.scene.control.Button;
40-
import javafx.scene.control.ComboBox;
41-
import javafx.scene.control.Dialog;
42-
import javafx.scene.control.ListCell;
43-
import javafx.scene.control.ListView;
44-
import javafx.scene.control.ProgressIndicator;
45-
import javafx.scene.control.SelectionMode;
46-
import javafx.scene.control.TextArea;
47-
import javafx.scene.control.TextField;
48-
import javafx.util.StringConverter;
49-
5049
public class CreateAndSaveMapController {
5150

52-
5351
@FXML private MapView mapView;
5452
@FXML private TextField title;
5553
@FXML private TextField tags;
@@ -72,9 +70,8 @@ private void initialize() {
7270

7371
// update basemap when selection changes
7472
basemapList.getSelectionModel().select(0);
75-
basemapList.getSelectionModel().selectedItemProperty().addListener(o ->
76-
map.setBasemap(basemapList.getSelectionModel().getSelectedItem())
77-
);
73+
basemapList.getSelectionModel().selectedItemProperty()
74+
.addListener(o -> map.setBasemap(basemapList.getSelectionModel().getSelectedItem()));
7875

7976
basemapList.setCellFactory(c -> new BasemapCell());
8077

@@ -83,7 +80,8 @@ private void initialize() {
8380
mapView.setMap(map);
8481

8582
// set operational layer options
86-
String worldElevationService = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer";
83+
String worldElevationService =
84+
"http://sampleserver6.arcgisonline.com/arcgis/rest/services/WorldTimeZones/MapServer";
8785
ArcGISMapImageLayer worldElevation = new ArcGISMapImageLayer(worldElevationService);
8886
worldElevation.loadAsync();
8987

@@ -103,6 +101,7 @@ private void initialize() {
103101

104102
// set portal folder title converter
105103
folderList.setConverter(new StringConverter<PortalFolder>() {
104+
106105
@Override
107106
public String toString(PortalFolder folder) {
108107
return folder.getTitle();
@@ -125,34 +124,37 @@ void authenticate() {
125124
authenticationDialog.setOnCloseRequest(r -> {
126125

127126
OAuthConfiguration configuration = authenticationDialog.getResult();
128-
AuthenticationManager.addOAuthConfiguration(configuration);
129-
130-
// setup the handler that will prompt an authentication challenge to the user
131-
AuthenticationManager.setAuthenticationChallengeHandler(new OAuthChallengeHandler());
132-
133-
portal = new Portal("http://" + configuration.getPortalUrl(), true);
134-
portal.addDoneLoadingListener(() -> {
135-
if (portal.getLoadStatus() == LoadStatus.LOADED) {
136-
try {
137-
PortalUserContent portalUserContent = portal.getUser().fetchContentAsync().get();
138-
List<PortalFolder> portalFolders = portalUserContent.getFolders();
139-
folderList.getItems().addAll(portalFolders);
140-
} catch (Exception e) {
141-
e.printStackTrace();
127+
// check authentication went through
128+
if (configuration != null) {
129+
AuthenticationManager.addOAuthConfiguration(configuration);
130+
131+
// setup the handler that will prompt an authentication challenge to the user
132+
AuthenticationManager.setAuthenticationChallengeHandler(new OAuthChallengeHandler());
133+
134+
portal = new Portal("http://" + configuration.getPortalUrl(), true);
135+
portal.addDoneLoadingListener(() -> {
136+
if (portal.getLoadStatus() == LoadStatus.LOADED) {
137+
try {
138+
PortalUserContent portalUserContent = portal.getUser().fetchContentAsync().get();
139+
List<PortalFolder> portalFolders = portalUserContent.getFolders();
140+
folderList.getItems().addAll(portalFolders);
141+
} catch (Exception e) {
142+
e.printStackTrace();
143+
}
144+
145+
saveButton.setDisable(false);
146+
147+
} else if (portal.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
148+
149+
// show alert message on error
150+
showMessage("Authentication failed", portal.getLoadError().getMessage(), Alert.AlertType.ERROR);
142151
}
152+
});
143153

144-
saveButton.setDisable(false);
145-
146-
} else if (portal.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
147-
148-
// show alert message on error
149-
showMessage("Authentication failed", portal.getLoadError().getMessage(), Alert.AlertType.ERROR);
150-
}
151-
});
152-
153-
// loading the portal info of a secured resource
154-
// this will invoke the authentication challenge
155-
portal.loadAsync();
154+
// loading the portal info of a secured resource
155+
// this will invoke the authentication challenge
156+
portal.loadAsync();
157+
}
156158
});
157159

158160
}
@@ -161,6 +163,7 @@ void authenticate() {
161163
* Shows a Basemap title in a ListView.
162164
*/
163165
private class BasemapCell extends ListCell<Basemap> {
166+
164167
@Override
165168
protected void updateItem(Basemap basemap, boolean empty) {
166169
super.updateItem(basemap, empty);
@@ -173,6 +176,7 @@ protected void updateItem(Basemap basemap, boolean empty) {
173176
* Shows a Layer title in a ListView.
174177
*/
175178
private class LayerCell extends ListCell<Layer> {
179+
176180
@Override
177181
protected void updateItem(Layer layer, boolean empty) {
178182
super.updateItem(layer, empty);

src/main/java/com/esri/samples/portal/oauth/AuthenticationDialog.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,11 @@
3030
*/
3131
class AuthenticationDialog extends Dialog<OAuthConfiguration> {
3232

33-
@FXML
34-
private TextField portalURL;
35-
@FXML
36-
private TextField clientID;
37-
@FXML
38-
private TextField redirectURI;
39-
@FXML
40-
private ButtonType cancelButton;
41-
@FXML
42-
private ButtonType continueButton;
33+
@FXML private TextField portalURL;
34+
@FXML private TextField clientID;
35+
@FXML private TextField redirectURI;
36+
@FXML private ButtonType cancelButton;
37+
@FXML private ButtonType continueButton;
4338

4439
AuthenticationDialog() {
4540
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/oauth_auth_dialog.fxml"));
@@ -60,18 +55,19 @@ class AuthenticationDialog extends Dialog<OAuthConfiguration> {
6055
try {
6156
return new OAuthConfiguration(portalURL.getText(), clientID.getText(), redirectURI.getText());
6257
} catch (Exception e) {
63-
Alert alert = new Alert(Alert.AlertType.ERROR);
64-
alert.setContentText(e.getMessage());
65-
alert.show();
58+
showMessage(e.getMessage());
6659
}
6760
} else {
68-
Alert alert = new Alert(Alert.AlertType.ERROR);
69-
alert.setContentText("missing credentials");
70-
alert.show();
61+
showMessage("missing credentials");
7162
}
7263
}
7364
return null;
7465
});
7566
}
7667

77-
}
68+
private void showMessage(String message) {
69+
Alert alert = new Alert(Alert.AlertType.ERROR);
70+
alert.setContentText(message);
71+
alert.show();
72+
}
73+
}

src/main/java/com/esri/samples/portal/oauth/OAuthController.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ void authenticate() throws Exception {
5454
authenticationDialog.setOnCloseRequest(r -> {
5555

5656
OAuthConfiguration configuration = authenticationDialog.getResult();
57+
// check that configuration was made
58+
if(configuration != null){
5759
AuthenticationManager.addOAuthConfiguration(configuration);
5860

5961
// setup the handler that will prompt an authentication challenge to the user
@@ -80,6 +82,7 @@ void authenticate() throws Exception {
8082
// loading the portal info of a secured resource
8183
// this will invoke the authentication challenge
8284
portal.loadAsync();
85+
}
8386
});
8487
}
8588

0 commit comments

Comments
 (0)