Skip to content

Commit 02372e6

Browse files
authored
Merge pull request #116 from Esri/john0005/UpdatingOAuthSample
Added check that authentication went through
2 parents 98bd0cd + 9eec588 commit 02372e6

File tree

4 files changed

+109
-110
lines changed

4 files changed

+109
-110
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: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,16 @@
1818

1919
import java.text.SimpleDateFormat;
2020

21-
import com.esri.arcgisruntime.loadable.LoadStatus;
22-
import com.esri.arcgisruntime.portal.Portal;
23-
import com.esri.arcgisruntime.security.AuthenticationManager;
24-
import com.esri.arcgisruntime.security.OAuthConfiguration;
25-
2621
import javafx.fxml.FXML;
2722
import javafx.scene.control.Alert;
28-
import javafx.scene.control.Dialog;
2923
import javafx.scene.control.Label;
3024
import javafx.scene.control.TextField;
3125

26+
import com.esri.arcgisruntime.loadable.LoadStatus;
27+
import com.esri.arcgisruntime.portal.Portal;
28+
import com.esri.arcgisruntime.security.AuthenticationManager;
29+
import com.esri.arcgisruntime.security.OAuthConfiguration;
30+
3231
public class OAuthController {
3332

3433
@FXML private TextField portalURL;
@@ -54,32 +53,35 @@ void authenticate() throws Exception {
5453
authenticationDialog.setOnCloseRequest(r -> {
5554

5655
OAuthConfiguration configuration = authenticationDialog.getResult();
57-
AuthenticationManager.addOAuthConfiguration(configuration);
58-
59-
// setup the handler that will prompt an authentication challenge to the user
60-
AuthenticationManager.setAuthenticationChallengeHandler(new OAuthChallengeHandler());
61-
62-
Portal portal = new Portal("http://" + configuration.getPortalUrl(), true);
63-
portal.addDoneLoadingListener(() -> {
64-
if (portal.getLoadStatus() == LoadStatus.LOADED) {
65-
66-
// display portal user info
67-
fullName.setText(portal.getUser().getFullName());
68-
username.setText(portal.getUser().getUsername());
69-
email.setText(portal.getUser().getEmail());
70-
memberSince.setText(formatter.format(portal.getUser().getCreated().getTime()));
71-
role.setText(portal.getUser().getRole() != null ? portal.getUser().getRole().name() : "");
72-
73-
} else if (portal.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
74-
75-
// show alert message on error
76-
showMessage("Authentication failed", portal.getLoadError().getMessage(), Alert.AlertType.ERROR);
77-
}
78-
});
79-
80-
// loading the portal info of a secured resource
81-
// this will invoke the authentication challenge
82-
portal.loadAsync();
56+
// check that configuration was made
57+
if (configuration != null) {
58+
AuthenticationManager.addOAuthConfiguration(configuration);
59+
60+
// setup the handler that will prompt an authentication challenge to the user
61+
AuthenticationManager.setAuthenticationChallengeHandler(new OAuthChallengeHandler());
62+
63+
Portal portal = new Portal("http://" + configuration.getPortalUrl(), true);
64+
portal.addDoneLoadingListener(() -> {
65+
if (portal.getLoadStatus() == LoadStatus.LOADED) {
66+
67+
// display portal user info
68+
fullName.setText(portal.getUser().getFullName());
69+
username.setText(portal.getUser().getUsername());
70+
email.setText(portal.getUser().getEmail());
71+
memberSince.setText(formatter.format(portal.getUser().getCreated().getTime()));
72+
role.setText(portal.getUser().getRole() != null ? portal.getUser().getRole().name() : "");
73+
74+
} else if (portal.getLoadStatus() == LoadStatus.FAILED_TO_LOAD) {
75+
76+
// show alert message on error
77+
showMessage("Authentication failed", portal.getLoadError().getMessage(), Alert.AlertType.ERROR);
78+
}
79+
});
80+
81+
// loading the portal info of a secured resource
82+
// this will invoke the authentication challenge
83+
portal.loadAsync();
84+
}
8385
});
8486
}
8587

0 commit comments

Comments
 (0)