From 36f66d1cb1b28046284861bb517524581cd4c693 Mon Sep 17 00:00:00 2001 From: Alexis Delhaie Date: Fri, 18 Sep 2020 19:42:50 +0200 Subject: [PATCH] Configuration window, allowdowngrade --- src/META-INF/MANIFEST.MF | 3 - src/ovh/alexisdelhaie/endpoint/Main.java | 1 + .../alexisdelhaie/endpoint/configuration.fxml | 20 --- .../ConfigurationProperties.java | 127 ++++++++++++++++++ .../controllers/ConfigurationController.java | 73 ++++++++++ .../{ => controllers}/Controller.java | 68 ++++++++-- .../endpoint/{ => controllers}/about.fxml | 0 .../endpoint/{ => controllers}/banner.png | Bin .../endpoint/controllers/configuration.fxml | 20 +++ .../endpoint/controllers/icon.png | Bin 0 -> 22975 bytes .../{ => controllers}/responsetab.fxml | 17 +++ .../endpoint/http/HttpClient.java | 36 ++++- .../endpoint/http/HttpStatus.java | 3 +- .../alexisdelhaie/endpoint/http/Request.java | 6 +- .../alexisdelhaie/endpoint/http/Response.java | 8 +- .../alexisdelhaie/endpoint/mainwindow.fxml | 4 +- .../endpoint/url/SpecialChar.java | 44 ++++++ .../endpoint/{ => url}/URLGenerator.java | 43 ++---- 18 files changed, 390 insertions(+), 83 deletions(-) delete mode 100644 src/ovh/alexisdelhaie/endpoint/configuration.fxml create mode 100644 src/ovh/alexisdelhaie/endpoint/configuration/ConfigurationProperties.java create mode 100644 src/ovh/alexisdelhaie/endpoint/controllers/ConfigurationController.java rename src/ovh/alexisdelhaie/endpoint/{ => controllers}/Controller.java (84%) rename src/ovh/alexisdelhaie/endpoint/{ => controllers}/about.fxml (100%) rename src/ovh/alexisdelhaie/endpoint/{ => controllers}/banner.png (100%) create mode 100644 src/ovh/alexisdelhaie/endpoint/controllers/configuration.fxml create mode 100644 src/ovh/alexisdelhaie/endpoint/controllers/icon.png rename src/ovh/alexisdelhaie/endpoint/{ => controllers}/responsetab.fxml (77%) create mode 100644 src/ovh/alexisdelhaie/endpoint/url/SpecialChar.java rename src/ovh/alexisdelhaie/endpoint/{ => url}/URLGenerator.java (55%) diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF index f0d972d..091c70a 100644 --- a/src/META-INF/MANIFEST.MF +++ b/src/META-INF/MANIFEST.MF @@ -1,6 +1,3 @@ Manifest-Version: 1.0 Main-Class: ovh.alexisdelhaie.endpoint.Main -Class-Path: src.zip javafx-swt.jar javafx.web.jar javafx.base.jar javafx - .fxml.jar javafx.media.jar javafx.swing.jar javafx.controls.jar javafx. - graphics.jar diff --git a/src/ovh/alexisdelhaie/endpoint/Main.java b/src/ovh/alexisdelhaie/endpoint/Main.java index cc8634a..00cd5e6 100644 --- a/src/ovh/alexisdelhaie/endpoint/Main.java +++ b/src/ovh/alexisdelhaie/endpoint/Main.java @@ -6,6 +6,7 @@ import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.stage.Stage; +import ovh.alexisdelhaie.endpoint.controllers.Controller; public class Main extends Application { diff --git a/src/ovh/alexisdelhaie/endpoint/configuration.fxml b/src/ovh/alexisdelhaie/endpoint/configuration.fxml deleted file mode 100644 index 1065c18..0000000 --- a/src/ovh/alexisdelhaie/endpoint/configuration.fxml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - diff --git a/src/ovh/alexisdelhaie/endpoint/configuration/ConfigurationProperties.java b/src/ovh/alexisdelhaie/endpoint/configuration/ConfigurationProperties.java new file mode 100644 index 0000000..a2a5b21 --- /dev/null +++ b/src/ovh/alexisdelhaie/endpoint/configuration/ConfigurationProperties.java @@ -0,0 +1,127 @@ +package ovh.alexisdelhaie.endpoint.configuration; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import javafx.scene.control.Alert; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; + +public class ConfigurationProperties { + + private Map properties; + private String osname; + private String filepath; + private ObjectMapper mapper; + + @SuppressWarnings("unchecked") + public ConfigurationProperties() { + osname = System.getProperty("os.name").toUpperCase(); + properties = new HashMap<>(); + mapper = new ObjectMapper(); + filepath = new StringBuilder(getAppData()) + .append("EndPoint") + .append(getSeparator()) + .append("settings.json") + .toString(); + createAppFolder(); + load(); + } + + public void setProperty(String key, String value) { + properties.put(key, value); + save(); + } + + public String getStringProperty(String key, String defaultS) { + if (properties.containsKey(key)) { + return properties.get(key); + } + return defaultS; + } + + public boolean getBooleanProperty(String key, boolean defaultB) { + if (properties.containsKey(key)) { + return Boolean.parseBoolean(properties.get(key)); + } + return defaultB; + } + + private void save() { + try { + mapper.writeValue(new File(filepath), properties); + } catch (Exception e) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Cannot save settings"); + alert.setHeaderText("There was an error while saving settings file"); + alert.setContentText(e.getMessage()); + alert.showAndWait(); + } + } + + private void load() { + File f = new File(filepath); + try { + if (f.exists()) { + properties = mapper.readValue(f, new TypeReference>() { }); + } + } catch (IOException e) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Cannot initialize settings"); + alert.setHeaderText("There was an error while initializing settings file"); + alert.setContentText(e.getMessage()); + alert.showAndWait(); + } + } + + private void createAppFolder() { + try { + Path path = Paths.get(new StringBuilder(getAppData()) + .append("EndPoint") + .append(getSeparator()).toString()); + if (!Files.exists(path)) { + Files.createDirectories(path); + } + } catch (IOException e) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Cannot create app folder"); + alert.setHeaderText("There was an error while creating appdata folder"); + alert.setContentText(e.getMessage()); + alert.showAndWait(); + } + } + + private String getAppData() { + String path = ""; + if (osname.contains("WIN")) { + path = System.getenv("APPDATA"); + path = (path.endsWith("\\") ? path : path + "\\"); + } + else if (osname.contains("MAC")) { + path = System.getProperty("user.home") + "/Library/"; + } + else if (osname.contains("NUX")) { + path = System.getProperty("user.home"); + path = (path.endsWith("/") ? path : path + "/"); + } + else { + path = System.getProperty("user.dir"); + path = (path.endsWith("/") ? path : path + "/"); + } + + return path; + } + + private String getSeparator() { + if (osname.contains("WIN")) { + return "\\"; + } + return "/"; + } + +} diff --git a/src/ovh/alexisdelhaie/endpoint/controllers/ConfigurationController.java b/src/ovh/alexisdelhaie/endpoint/controllers/ConfigurationController.java new file mode 100644 index 0000000..cbc4e07 --- /dev/null +++ b/src/ovh/alexisdelhaie/endpoint/controllers/ConfigurationController.java @@ -0,0 +1,73 @@ +package ovh.alexisdelhaie.endpoint.controllers; + +import javafx.fxml.FXML; +import javafx.fxml.FXMLLoader; +import javafx.fxml.Initializable; +import javafx.scene.Parent; +import javafx.scene.Scene; +import javafx.scene.control.Alert; +import javafx.scene.control.CheckBox; +import javafx.scene.image.Image; +import javafx.scene.input.MouseEvent; +import javafx.stage.Modality; +import javafx.stage.Stage; +import ovh.alexisdelhaie.endpoint.configuration.ConfigurationProperties; + +import java.io.IOException; +import java.net.URL; +import java.util.ResourceBundle; + +public class ConfigurationController +{ + + private ConfigurationProperties configurationProperties; + private Stage primaryStage; + + @FXML + private CheckBox allowInvalidSsl; + @FXML + private CheckBox allowDowngrade; + + public void setStageAndSetupListeners(Stage s) { + primaryStage = s; + } + + public void setConfigurationProperties(ConfigurationProperties properties) { + configurationProperties = properties; + allowInvalidSsl.setSelected(properties.getBooleanProperty("allowInvalidSsl", false)); + allowDowngrade.setSelected(properties.getBooleanProperty("allowDowngrade", true)); + } + + @FXML + private void onBooleanValueChanged(MouseEvent event) { + CheckBox c = (CheckBox) event.getSource(); + configurationProperties.setProperty(c.getId(), String.valueOf(c.isSelected())); + } + + @FXML + private void showAboutDialog() { + try { + Stage dialog = new Stage(); + Parent xml = FXMLLoader.load(getClass().getResource("about.fxml")); + dialog.initOwner(primaryStage); + dialog.setScene(new Scene(xml, 677, 365)); + dialog.setMaxHeight(365); + dialog.setMinHeight(365); + dialog.setMaxWidth(707); + dialog.setMinWidth(707); + dialog.setResizable(false); + dialog.setTitle("About EndPoint"); + dialog.getIcons().add( new Image( + Controller.class.getResourceAsStream( "icon.png" ))); + dialog.initModality(Modality.APPLICATION_MODAL); + dialog.showAndWait(); + } catch (IOException e) { + Alert alert = new Alert(Alert.AlertType.ERROR); + alert.setTitle("Cannot initialize About"); + alert.setHeaderText("There was an error while initializing this dialog"); + alert.setContentText(e.getMessage()); + alert.showAndWait(); + } + } + +} diff --git a/src/ovh/alexisdelhaie/endpoint/Controller.java b/src/ovh/alexisdelhaie/endpoint/controllers/Controller.java similarity index 84% rename from src/ovh/alexisdelhaie/endpoint/Controller.java rename to src/ovh/alexisdelhaie/endpoint/controllers/Controller.java index 289c57a..e546f88 100644 --- a/src/ovh/alexisdelhaie/endpoint/Controller.java +++ b/src/ovh/alexisdelhaie/endpoint/controllers/Controller.java @@ -1,4 +1,4 @@ -package ovh.alexisdelhaie.endpoint; +package ovh.alexisdelhaie.endpoint.controllers; import javafx.application.Platform; import javafx.beans.property.StringProperty; @@ -13,18 +13,23 @@ import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.control.cell.PropertyValueFactory; import javafx.scene.image.Image; +import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.stage.Modality; import javafx.stage.Stage; +import org.apache.commons.validator.routines.UrlValidator; +import ovh.alexisdelhaie.endpoint.configuration.ConfigurationProperties; import ovh.alexisdelhaie.endpoint.http.HttpClient; import ovh.alexisdelhaie.endpoint.http.Request; import ovh.alexisdelhaie.endpoint.http.RequestBuilder; import ovh.alexisdelhaie.endpoint.http.Response; import ovh.alexisdelhaie.endpoint.impl.EditCell; import ovh.alexisdelhaie.endpoint.model.Param; +import ovh.alexisdelhaie.endpoint.url.URLGenerator; import java.io.IOException; +import java.net.MalformedURLException; import java.net.URL; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; @@ -80,8 +85,11 @@ public class Controller implements Initializable { private Stage primaryStage; private HashMap requests; + private ConfigurationProperties properties; + @Override public void initialize(URL location, ResourceBundle resources) { + properties = new ConfigurationProperties(); requests = new HashMap<>(); String[] method = { "GET", "POST", "HEAD", "PUT", "DELETE" }; httpMethod.setItems(FXCollections.observableArrayList(method)); @@ -184,6 +192,13 @@ public class Controller implements Initializable { @FXML private void start() { + if (requestInput.getText().isBlank()) { + Alert alert = new Alert(Alert.AlertType.WARNING); + alert.setTitle("URL field empty"); + alert.setContentText("Enter your URL in the field at the top of the window."); + alert.showAndWait(); + return; + } runningIndicatorPane.setVisible(true); new Thread(() -> { final String method = httpMethod.getValue(); @@ -194,7 +209,10 @@ public class Controller implements Initializable { Request r = new RequestBuilder(requestInput.getText()) .setCustomHeaders(getCustomHeaders()) .build(); - HttpClient hc = new HttpClient(); + HttpClient hc = new HttpClient( + properties.getBooleanProperty("allowInvalidSsl", false), + properties.getBooleanProperty("allowDowngrade", true) + ); switch (method) { case "GET" -> response = hc.get(r); case "POST" -> response = hc.post(r, getBody()); @@ -213,7 +231,6 @@ public class Controller implements Initializable { } } catch (IOException | NoSuchAlgorithmException | KeyManagementException e) { System.err.println(e.getMessage()); - e.printStackTrace(); textArea.ifPresent(area -> Platform.runLater(() -> { resetResponseTab(); area.setStyle("-fx-text-fill: red"); @@ -255,6 +272,8 @@ public class Controller implements Initializable { raw.setText(res.getRawResponse()); TextArea request = (TextArea) responseTab.get().getContent().lookup("#request"); request.setText(res.getRequest().getRawRequest()); + GridPane downgradedIndicator = (GridPane) responseTab.get().getContent().lookup("#downgraded_indicator"); + downgradedIndicator.setVisible(res.isDowngraded()); TableView headers = (TableView) responseTab.get().getContent().lookup("#headers"); headers.getItems().clear(); for (Map.Entry entry : res.getHeaders().entrySet()) { @@ -279,6 +298,10 @@ public class Controller implements Initializable { time.setText("... ms"); TextArea raw = (TextArea) responseTab.get().getContent().lookup("#raw"); raw.setText(""); + TextArea request = (TextArea) responseTab.get().getContent().lookup("#request"); + request.setText(""); + GridPane downgradedIndicator = (GridPane) responseTab.get().getContent().lookup("#downgraded_indicator"); + downgradedIndicator.setVisible(false); TableView headers = (TableView) responseTab.get().getContent().lookup("#headers"); headers.getItems().clear(); } @@ -384,25 +407,29 @@ public class Controller implements Initializable { } @FXML - private void showAboutDialog() { + private void showConfigurationDialog() { try { Stage dialog = new Stage(); - Parent xml = FXMLLoader.load(getClass().getResource("about.fxml")); + FXMLLoader loader = new FXMLLoader(getClass().getResource("configuration.fxml")); + Parent xml = loader.load(); + ConfigurationController controller = loader.getController(); + controller.setStageAndSetupListeners(primaryStage); + controller.setConfigurationProperties(properties); dialog.initOwner(primaryStage); - dialog.setScene(new Scene(xml, 677, 365)); - dialog.setMaxHeight(365); - dialog.setMinHeight(365); - dialog.setMaxWidth(707); - dialog.setMinWidth(707); + dialog.setScene(new Scene(xml, 412, 556)); + dialog.setMaxHeight(556); + dialog.setMinHeight(556); + dialog.setMaxWidth(412); + dialog.setMinWidth(412); dialog.setResizable(false); - dialog.setTitle("About EndPoint"); + dialog.setTitle("Settings"); dialog.getIcons().add( new Image( Controller.class.getResourceAsStream( "icon.png" ))); dialog.initModality(Modality.APPLICATION_MODAL); dialog.showAndWait(); } catch (IOException e) { Alert alert = new Alert(Alert.AlertType.ERROR); - alert.setTitle("Cannot initialize About"); + alert.setTitle("Cannot initialize Settings"); alert.setHeaderText("There was an error while initializing this dialog"); alert.setContentText(e.getMessage()); alert.showAndWait(); @@ -412,7 +439,22 @@ public class Controller implements Initializable { @FXML private void requestInputOnKeyPressed() { Tab tab = tabs.getSelectionModel().getSelectedItem(); - requests.put(tab.hashCode(), requestInput.getText()); + String url = requestInput.getText(); + requests.put(tab.hashCode(), url); + if (url.isBlank()) { + tab.setText("Untitled"); + } else { + url = URLGenerator.addSchemaToUrl(url); + UrlValidator urlValidator = new UrlValidator(); + if (urlValidator.isValid(url)) { + try { + URL u = new URL(url); + tab.setText(u.getHost()); + } catch (MalformedURLException e) { + System.err.println(e.getMessage()); + } + } + } } } diff --git a/src/ovh/alexisdelhaie/endpoint/about.fxml b/src/ovh/alexisdelhaie/endpoint/controllers/about.fxml similarity index 100% rename from src/ovh/alexisdelhaie/endpoint/about.fxml rename to src/ovh/alexisdelhaie/endpoint/controllers/about.fxml diff --git a/src/ovh/alexisdelhaie/endpoint/banner.png b/src/ovh/alexisdelhaie/endpoint/controllers/banner.png similarity index 100% rename from src/ovh/alexisdelhaie/endpoint/banner.png rename to src/ovh/alexisdelhaie/endpoint/controllers/banner.png diff --git a/src/ovh/alexisdelhaie/endpoint/controllers/configuration.fxml b/src/ovh/alexisdelhaie/endpoint/controllers/configuration.fxml new file mode 100644 index 0000000..b367d4a --- /dev/null +++ b/src/ovh/alexisdelhaie/endpoint/controllers/configuration.fxml @@ -0,0 +1,20 @@ + + + + + + + + + +