Skip to content

Commit

Permalink
add logout to fix preemptive add problem
Browse files Browse the repository at this point in the history
  • Loading branch information
cpesch committed Jul 3, 2024
1 parent 20a33dd commit 776a076
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,17 @@ private void putPreferencesSize() {
log.fine("Storing dialog " + getName() + " size as " + getSize());
}

private boolean disposed = false;

public void dispose() {
putPreferencesLocation();
putPreferencesSize();
super.dispose();
disposed = true;
}

public void setVisible(boolean visible) {
if(disposed) return;
super.setVisible(visible);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,13 @@
*/

public abstract class SingletonDialogAction extends FrameAction {
private SimpleDialog dialog;

protected abstract SimpleDialog createDialog();

public void run() {
if (dialog == null) {
dialog = createDialog();
dialog.showWithPreferences();
}

if (!dialog.isVisible()) {
dialog.toFront();
dialog.setVisible(true);
}
SimpleDialog dialog = createDialog();
dialog.showWithPreferences();
dialog.toFront();
dialog.setVisible(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class RouteFeedbackIT extends RouteFeedbackServiceBase {

@Before
public void setUp() {
routeFeedback = new RouteFeedback(API, new SimpleCredentials(USERNAME, PASSWORD));
routeFeedback = new RouteFeedback(API, new SimpleCredentials("routeconverter", "pmcs123$".toCharArray()));
}

@After
Expand Down
27 changes: 22 additions & 5 deletions rest/src/main/java/slash/navigation/rest/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,25 @@
*/
package slash.navigation.rest;

import org.apache.hc.client5.http.auth.AuthCache;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.CredentialsProvider;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.classic.methods.HttpUriRequestBase;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.impl.DefaultHttpRequestRetryStrategy;
import org.apache.hc.client5.http.impl.auth.BasicAuthCache;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.impl.auth.BasicScheme;
import org.apache.hc.client5.http.impl.auth.CredentialsProviderBuilder;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicClassicHttpResponse;
import org.apache.hc.core5.http.protocol.HttpContext;

import java.io.IOException;
import java.net.*;
Expand Down Expand Up @@ -133,16 +141,25 @@ public <T> T execute(HttpClientResponseHandler<T> responseHandler) throws IOExce

RequestConfig requestConfig = requestConfigBuilder.build();
clientBuilder.setDefaultRequestConfig(requestConfig);


HttpClientContext context = HttpClientContext.create();
if(credentials != null) {
UsernamePasswordCredentials preemptiveCredentials = new UsernamePasswordCredentials(credentials.getUserName(), credentials.getPassword());
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), preemptiveCredentials);
BasicScheme authScheme = new BasicScheme();
authScheme.initPreemptive(preemptiveCredentials);
context.setCredentialsProvider(credentialsProvider);

HttpHost httpHost = new HttpHost(uri.getScheme(), uri.getHost(), uri.getPort());
AuthScope authScope = new AuthScope(httpHost, null, null);
clientBuilder.setDefaultCredentialsProvider(CredentialsProviderBuilder.create()
.add(authScope, credentials.getUserName(), credentials.getPassword())
.build());
AuthCache authCache = new BasicAuthCache();
authCache.put(httpHost, authScheme);
context.setAuthCache(authCache);
}

try(CloseableHttpClient httpClient = clientBuilder.build()) {
return httpClient.execute(method, response -> {
return httpClient.execute(method, context, response -> {
HttpRequest.this.response = response;
try {
return responseHandler.handleResponse(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.List;
import java.util.*;
Expand Down Expand Up @@ -497,9 +496,22 @@ public char[] getPassword() {
};
}

public void setUserNamePreference(String userNamePreference, String passwordPreference) {
private void initializeLoginAction() {
boolean enableLogin = preferences.get(USERNAME_PREFERENCE, null) == null;
getContext().getActionManager().enable("login", enableLogin);
getContext().getActionManager().enable("logout", !enableLogin);
}

public void setLogin(String userNamePreference, String passwordPreference) {
preferences.put(USERNAME_PREFERENCE, userNamePreference);
preferences.putByteArray(PASSWORD_PREFERENCE, passwordPreference.getBytes());
initializeLoginAction();
}

public void removeLogin() {
preferences.remove(USERNAME_PREFERENCE);
preferences.remove(PASSWORD_PREFERENCE);
initializeLoginAction();
}

public File getUploadRoutePreference() {
Expand Down Expand Up @@ -1015,7 +1027,8 @@ private void addTab(final JPanel panel, final Class<? extends PanelInTab> panelI
PanelInTab panelInTab;
try {
panelInTab = panelInTabClass.getDeclaredConstructor().newInstance();
} catch (InstantiationException | InvocationTargetException | IllegalAccessException | NoSuchMethodException e) {
} catch (InstantiationException | InvocationTargetException | IllegalAccessException |
NoSuchMethodException e) {
throw new RuntimeException(e);
}
panel.add(panelInTab.getRootComponent());
Expand Down Expand Up @@ -1172,7 +1185,7 @@ protected void initializeServices() {
});
tileServerMapManager = new TileServerMapManager(getTileServersDirectory());
routingServiceFacade.addRoutingServiceFacadeListener(new RoutingServiceFacadeNotifier());
}
}

protected void initializeActions() {
ActionManager actionManager = getContext().getActionManager();
Expand All @@ -1195,6 +1208,8 @@ protected void initializeActions() {
actionManager.register("show-downloads", new ShowDownloadsAction());
actionManager.register("show-options", new ShowOptionsAction());
actionManager.register("login", new LoginAction());
actionManager.register("logout", new LogoutAction());
initializeLoginAction();
actionManager.register("complete-flight-plan", new CompleteFlightPlanAction());
actionManager.register("help-topics", new HelpTopicsAction());
actionManager.register("check-for-update", new CheckForUpdateAction(updateChecker));
Expand Down Expand Up @@ -1310,7 +1325,9 @@ protected void downloadDependencies() {
}

protected abstract void scanLocalMapsAndThemes();

protected abstract void installBackgroundMap();

protected abstract void scanRemoteMapsAndThemes();

private void scanForFilesMissingInQueue() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import slash.navigation.converter.gui.RouteConverter;
import slash.navigation.converter.gui.dialogs.LoginDialog;
import slash.navigation.converter.gui.dialogs.OptionsDialog;
import slash.navigation.gui.SimpleDialog;
import slash.navigation.gui.actions.SingletonDialogAction;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
This file is part of RouteConverter.
RouteConverter is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
RouteConverter is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RouteConverter; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Copyright (C) 2007 Christian Pesch. All Rights Reserved.
*/

package slash.navigation.converter.gui.actions;

import slash.navigation.converter.gui.RouteConverter;
import slash.navigation.gui.actions.FrameAction;

/**
* Logout from the RouteCatalog.
*
* @author Christian Pesch
*/

public class LogoutAction extends FrameAction {
public void run() throws Exception {
RouteConverter.getInstance().removeLogin();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.ResourceBundle;
Expand Down Expand Up @@ -178,7 +177,7 @@ private void login() {
routeFeedback.checkForLogin(userName, password);

successful = true;
RouteConverter.getInstance().setUserNamePreference(userName, password);
RouteConverter.getInstance().setLogin(userName, password);

showMessageDialog(frame, new JLabel(RouteConverter.getBundle().getString("login-success")),
frame.getTitle(), INFORMATION_MESSAGE);
Expand Down Expand Up @@ -246,7 +245,7 @@ private void register() {
routeFeedback.addUser(userName, password, firstName, lastName, email);

successful = true;
RouteConverter.getInstance().setUserNamePreference(userName, password);
RouteConverter.getInstance().setLogin(userName, password);

showMessageDialog(frame, new JLabel(RouteConverter.getBundle().getString("register-success")),
frame.getTitle(), INFORMATION_MESSAGE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public JMenuBar createMenuBar() {
extrasMenu.add(createItem("show-downloads"));
extrasMenu.add(createItem("show-options"));
extrasMenu.add(createItem("login"));
extrasMenu.add(createItem("logout"));

JMenu helpMenu = createMenu("help");
helpMenu.add(createItem("help-topics"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,11 @@ show-options-action-tooltip=Zeigt Optionen zu diesem Programm
show-options-action-mnemonic=O
login=Anmelden
login-action=Anmelden
login-action-tooltip=Am RouteConverter Dienst anmelden
login-action-tooltip=Anmelden am RouteConverter Dienst
login-action-mnemonic=A
logout-action=Abmelden
logout-action-tooltip=Abmelden vom RouteConverter Dienst
logout-action-mnemonic=b
help-menu=Hilfe
help-menu-mnemonic=H
help-topics-action=Hilfethemen…
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ login=Login
login-action=Login
login-action-tooltip=Login at the RouteConverter service
login-action-mnemonic=L
logout-action=Logout
logout-action-tooltip=Logout from the RouteConverter service
logout-action-mnemonic=o
help-menu=Help
help-menu-mnemonic=H
help-topics-action=Help Topics…
Expand Down

0 comments on commit 776a076

Please sign in to comment.