From 546766b611447498d0bfb58607aac15156d0b3c9 Mon Sep 17 00:00:00 2001 From: Rohit Awate Date: Thu, 18 Jan 2018 22:53:25 +0530 Subject: [PATCH] Added RequestManager and get method --- .../dashboard/DashboardController.java | 43 +++++++++++++---- .../restaurant/requests/RequestManager.java | 47 +++++++++++++++++++ src/main/resources/fxml/Dashboard.fxml | 2 +- src/main/resources/styles/dashboard.css | 9 ++++ 4 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/rohitawate/restaurant/requests/RequestManager.java diff --git a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java index 4943bde..d4cc0ef 100644 --- a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java +++ b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java @@ -15,24 +15,20 @@ */ package com.rohitawate.restaurant.dashboard; -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; +import com.jfoenix.controls.JFXSnackbar; +import com.rohitawate.restaurant.requests.RequestManager; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; +import java.net.MalformedURLException; import java.net.URL; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.ComboBox; import javafx.scene.control.TextArea; import javafx.scene.control.TextField; +import javafx.scene.layout.BorderPane; /** * FXML Controller class @@ -40,7 +36,8 @@ import javafx.scene.control.TextField; * @author Rohit Awate */ public class DashboardController implements Initializable { - + @FXML + private BorderPane dashboard; @FXML private TextField addressField; @FXML @@ -48,17 +45,43 @@ public class DashboardController implements Initializable { @FXML private TextArea responseArea; + private JFXSnackbar snackBar; private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"}; - + private RequestManager requestManager; + @Override public void initialize(URL url, ResourceBundle rb) { httpMethodBox.getItems().addAll(httpMethods); httpMethodBox.setValue("GET"); responseArea.wrapTextProperty().set(true); + + requestManager = new RequestManager(); + snackBar = new JFXSnackbar(dashboard); } @FXML private void sendAction() { + try { + String address = addressField.getText(); + if (address.equals("")) { + snackBar.show("Please enter a valid address", 7000); + return; + } + String response = ""; + URL url = new URL(address); + switch (httpMethodBox.getValue()) { + case "GET": + response = requestManager.get(url); + break; + } + responseArea.setText(response); + } catch (MalformedURLException ex) { + Logger.getLogger(DashboardController.class.getName()).log(Level.SEVERE, null, ex); + snackBar.show("Server did not respond", 7000); + } catch (IOException ex) { + Logger.getLogger(DashboardController.class.getName()).log(Level.SEVERE, null, ex); + snackBar.show("Server did not respond", 7000); + } } } diff --git a/src/main/java/com/rohitawate/restaurant/requests/RequestManager.java b/src/main/java/com/rohitawate/restaurant/requests/RequestManager.java new file mode 100644 index 0000000..b05fe9c --- /dev/null +++ b/src/main/java/com/rohitawate/restaurant/requests/RequestManager.java @@ -0,0 +1,47 @@ +/* + * Copyright 2018 Rohit Awate. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.rohitawate.restaurant.requests; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * + * @author Rohit Awate + */ +public class RequestManager { + public String get(URL target) throws MalformedURLException, IOException { + String response = ""; + + HttpURLConnection conn = (HttpURLConnection) target.openConnection(); + conn.setRequestMethod("GET"); + + InputStream responseStream = conn.getInputStream(); + BufferedReader responseReader = + new BufferedReader(new InputStreamReader(responseStream)); + + String line; + while ((line = responseReader.readLine()) != null) + response += line + "\n"; + + return response; + } +} diff --git a/src/main/resources/fxml/Dashboard.fxml b/src/main/resources/fxml/Dashboard.fxml index a916c02..bc0465d 100644 --- a/src/main/resources/fxml/Dashboard.fxml +++ b/src/main/resources/fxml/Dashboard.fxml @@ -9,7 +9,7 @@ - +
diff --git a/src/main/resources/styles/dashboard.css b/src/main/resources/styles/dashboard.css index 3b558d8..03b2690 100644 --- a/src/main/resources/styles/dashboard.css +++ b/src/main/resources/styles/dashboard.css @@ -4,4 +4,13 @@ #responseArea { -fx-font-family: 'Liberation Mono'; +} + +.jfx-snackbar-content { + -fx-background-color: darkslategray; +} + +.jfx-snackbar-toast { + -fx-text-fill: lightgray; + -fx-alignment: center; } \ No newline at end of file