Added RequestManager and get method

This commit is contained in:
Rohit Awate 2018-01-18 22:53:25 +05:30
parent 40b55c8c54
commit 546766b611
4 changed files with 90 additions and 11 deletions

View file

@ -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);
}
}
}

View file

@ -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;
}
}

View file

@ -9,7 +9,7 @@
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" stylesheets="@../styles/dashboard.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.rohitawate.restaurant.dashboard.DashboardController">
<BorderPane fx:id="dashboard" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" stylesheets="@../styles/dashboard.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.rohitawate.restaurant.dashboard.DashboardController">
<center>
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
<children>

View file

@ -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;
}