From b675bd35fd464b07f6bfcd3083223f0eacfea376 Mon Sep 17 00:00:00 2001 From: Rohit Awate Date: Wed, 31 Jan 2018 12:44:27 +0530 Subject: [PATCH] Improved POST request generation and processing and minor UI tweaks --- .../dashboard/BodyTabController.java | 56 +++++++++++++++++-- .../dashboard/DashboardController.java | 28 ++-------- .../models/requests/POSTRequest.java | 37 +++++++----- .../models/requests/RestaurantRequest.java | 2 +- .../requestsmanager/POSTRequestManager.java | 23 ++++++-- src/main/resources/css/Default.css | 16 ++++-- .../resources/fxml/dashboard/BodyTab.fxml | 24 ++++++-- .../resources/fxml/dashboard/Dashboard.fxml | 2 +- 8 files changed, 128 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/rohitawate/restaurant/dashboard/BodyTabController.java b/src/main/java/com/rohitawate/restaurant/dashboard/BodyTabController.java index 5f8e408..63d5d1e 100644 --- a/src/main/java/com/rohitawate/restaurant/dashboard/BodyTabController.java +++ b/src/main/java/com/rohitawate/restaurant/dashboard/BodyTabController.java @@ -16,11 +16,17 @@ package com.rohitawate.restaurant.dashboard; +import com.rohitawate.restaurant.models.requests.POSTRequest; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.ComboBox; +import javafx.scene.control.Tab; import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import javafx.stage.FileChooser; +import javafx.stage.Window; +import javax.ws.rs.core.MediaType; import java.net.URL; import java.util.ResourceBundle; @@ -29,6 +35,12 @@ public class BodyTabController implements Initializable { private ComboBox rawInputTypeBox; @FXML private TextArea rawInputArea; + @FXML + private Tab rawTab, binaryTab, urlTab; + @FXML + private TextField filePathField; + + private HeaderTabController headerTabController; @Override public void initialize(URL location, ResourceBundle resources) { @@ -36,12 +48,44 @@ public class BodyTabController implements Initializable { rawInputTypeBox.getSelectionModel().select(0); } - // Returns the request body (index 0) and media type (index 1) - public String[] getBody() { - String[] requestBody = new String[2]; - requestBody[0] = rawInputArea.getText(); - requestBody[1] = rawInputTypeBox.getValue(); + // Returns a RestaurantRequest with only the body data. + public POSTRequest getBasicRequest() { + POSTRequest request = new POSTRequest(); + if (rawTab.isSelected()) { + String contentType; + switch (rawInputTypeBox.getValue()) { + case "PLAIN TEXT": + contentType = MediaType.TEXT_PLAIN; + break; + case "JSON": + contentType = MediaType.APPLICATION_JSON; + break; + case "XML": + contentType = MediaType.APPLICATION_XML; + break; + case "HTML": + contentType = MediaType.TEXT_HTML; + break; + default: + contentType = MediaType.TEXT_PLAIN; + } + request.setContentType(contentType); + request.setBody(rawInputArea.getText()); + } + return request; + } - return requestBody; + @FXML + private void browseFile() { + FileChooser fileChooser = new FileChooser(); + fileChooser.setTitle("Choose a binary file to add to request..."); + Window dashboardWindow = filePathField.getScene().getWindow(); + String filePath; + try { + filePath = fileChooser.showOpenDialog(dashboardWindow).getAbsolutePath(); + } catch (NullPointerException NPE) { + filePath = ""; + } + filePathField.setText(filePath); } } diff --git a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java index 6f11eba..6f1c4b9 100644 --- a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java +++ b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java @@ -34,7 +34,6 @@ import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; -import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.IOException; import java.net.MalformedURLException; @@ -89,7 +88,7 @@ public class DashboardController implements Initializable { IOE.printStackTrace(); } - addressField.setText("https://api.chucknorris.io/jokes/random"); + addressField.setText("https://anapioficeandfire.com/api/characters/583"); responseBox.getChildren().remove(0); promptLayer.setVisible(true); httpMethodBox.getItems().addAll(httpMethods); @@ -141,6 +140,7 @@ public class DashboardController implements Initializable { }); requestManager.setOnCancelled(e -> { loadingLayer.setVisible(false); + promptLayer.setVisible(true); snackBar.show("Request canceled.", 2000); requestManager.reset(); }); @@ -154,27 +154,10 @@ public class DashboardController implements Initializable { return; } - String[] requestBody = bodyTabController.getBody(); - POSTRequest postRequest = new POSTRequest(addressField.getText()); - postRequest.setRequestBody(requestBody[0]); - - MediaType requestMediaType = MediaType.WILDCARD_TYPE; - switch (requestBody[1]) { - case "PLAIN TEXT": - requestMediaType = MediaType.TEXT_PLAIN_TYPE; - break; - case "JSON": - requestMediaType = MediaType.APPLICATION_JSON_TYPE; - break; - case "XML": - requestMediaType = MediaType.APPLICATION_XML_TYPE; - break; - case "HTML": - requestMediaType = MediaType.TEXT_HTML_TYPE; - break; - } - postRequest.setRequestBodyMediaType(requestMediaType); + POSTRequest postRequest = bodyTabController.getBasicRequest(); + postRequest.setTarget(addressField.getText()); postRequest.addHeaders(headerTabController.getHeaders()); + requestManager.setRequest(postRequest); cancelButton.setOnAction(e -> requestManager.cancel()); requestManager.setOnRunning(e -> { @@ -188,6 +171,7 @@ public class DashboardController implements Initializable { }); requestManager.setOnCancelled(e -> { loadingLayer.setVisible(false); + promptLayer.setVisible(true); snackBar.show("Request canceled.", 2000); requestManager.reset(); }); diff --git a/src/main/java/com/rohitawate/restaurant/models/requests/POSTRequest.java b/src/main/java/com/rohitawate/restaurant/models/requests/POSTRequest.java index 86b188f..20292a6 100644 --- a/src/main/java/com/rohitawate/restaurant/models/requests/POSTRequest.java +++ b/src/main/java/com/rohitawate/restaurant/models/requests/POSTRequest.java @@ -16,15 +16,14 @@ package com.rohitawate.restaurant.models.requests; -import javax.ws.rs.core.MediaType; -import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; public class POSTRequest extends RestaurantRequest { - private String requestBody; - private MediaType requestBodyMediaType; - private File binaryBody; + private String body; + private String contentType; + private HashMap URLTextTuples; public POSTRequest() { } @@ -37,19 +36,31 @@ public class POSTRequest extends RestaurantRequest { super(target); } - public String getRequestBody() { - return requestBody; + public void setTarget(String target) throws MalformedURLException { + this.target = new URL(target); } - public void setRequestBody(String requestBody) { - this.requestBody = requestBody; + public String getBody() { + return body; } - public MediaType getRequestBodyMediaType() { - return requestBodyMediaType; + public void setBody(String body) { + this.body = body; } - public void setRequestBodyMediaType(MediaType requestBodyMediaType) { - this.requestBodyMediaType = requestBodyMediaType; + public String getContentType() { + return contentType; + } + + public void setContentType(String contentType) { + this.contentType = contentType; + } + + public void setURLTextTuples(HashMap URLTextTuples) { + this.URLTextTuples = URLTextTuples; + } + + public HashMap getURLTextTuples() { + return URLTextTuples; } } diff --git a/src/main/java/com/rohitawate/restaurant/models/requests/RestaurantRequest.java b/src/main/java/com/rohitawate/restaurant/models/requests/RestaurantRequest.java index 0541e76..af64860 100644 --- a/src/main/java/com/rohitawate/restaurant/models/requests/RestaurantRequest.java +++ b/src/main/java/com/rohitawate/restaurant/models/requests/RestaurantRequest.java @@ -21,7 +21,7 @@ import java.net.URL; import java.util.HashMap; public abstract class RestaurantRequest { - private URL target; + URL target; private HashMap headers; RestaurantRequest() { diff --git a/src/main/java/com/rohitawate/restaurant/requestsmanager/POSTRequestManager.java b/src/main/java/com/rohitawate/restaurant/requestsmanager/POSTRequestManager.java index 847bc55..2ca75e4 100644 --- a/src/main/java/com/rohitawate/restaurant/requestsmanager/POSTRequestManager.java +++ b/src/main/java/com/rohitawate/restaurant/requestsmanager/POSTRequestManager.java @@ -25,6 +25,7 @@ import javafx.concurrent.Task; import javax.ws.rs.client.Entity; import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.Invocation.Builder; import javax.ws.rs.client.WebTarget; import javax.ws.rs.core.Response; import java.io.IOException; @@ -37,22 +38,32 @@ public class POSTRequestManager extends RequestManager { return new Task() { @Override protected RestaurantResponse call() throws Exception { + POSTRequest postRequest = (POSTRequest) request; RestaurantResponse response = new RestaurantResponse(); WebTarget target = client.target(request.getTarget().toString()); - - Invocation.Builder requestBuilder = target.request(); - - HashMap headers = request.getHeaders(); Map.Entry mapEntry; + Builder requestBuilder = target.request(); + + // Add the headers to the request. + HashMap headers = request.getHeaders(); for (Map.Entry entry : headers.entrySet()) { mapEntry = (Map.Entry) entry; requestBuilder.header(mapEntry.getKey(), mapEntry.getValue()); } + // Adds the request body based on the content type and generates an invocation. + Invocation invocation; + switch (postRequest.getContentType()) { + default: + // Handles raw data types (JSON, Plain text, XML, HTML) + invocation = requestBuilder + .buildPost(Entity.entity(postRequest.getBody(), postRequest.getContentType())); + } + + long initialTime = System.currentTimeMillis(); - Response serverResponse = requestBuilder.post(Entity.entity(((POSTRequest) request).getRequestBody(), - ((POSTRequest) request).getRequestBodyMediaType())); + Response serverResponse = invocation.invoke(); response.setTime(initialTime, System.currentTimeMillis()); if (serverResponse == null) diff --git a/src/main/resources/css/Default.css b/src/main/resources/css/Default.css index 1e03607..2a9c4ab 100644 --- a/src/main/resources/css/Default.css +++ b/src/main/resources/css/Default.css @@ -105,17 +105,19 @@ } /* Tab titles */ -.tab-pane:top .tab-header-area .headers-region .tab:top .tab-container .tab-label .text { - -fx-fill: #b8b8b8; -} -.tab-pane:top .tab-header-area .headers-region .tab:selected:top .tab-container .tab-label .text { +.tab-pane:top .tab-header-area .headers-region .tab:top .tab-container .tab-label .text { -fx-fill: white; } /* Tabs in the request 'Body' option */ #bodyTabPane:top .tab-header-area .tab-header-background, #bodyTabPane:top .tab-header-area .headers-region .tab:top { + -fx-background-color: #545454; +} + +#bodyTabPane .tab-content-area, +#bodyTabPane .scroll-pane .viewport { -fx-background-color: #3d3d3d; } @@ -132,7 +134,7 @@ -fx-background-color: #505050; } -#keyField, #valueField { +#keyField, #valueField, #filePathField { -fx-prompt-text-fill: #919191; -fx-background-color: #303030; -fx-text-fill: white; @@ -142,6 +144,10 @@ -fx-background-color: orangered; } +#browseButton { + -fx-background-color: #a3c492; +} + .jfx-snackbar-content { -fx-background-color: black; } diff --git a/src/main/resources/fxml/dashboard/BodyTab.fxml b/src/main/resources/fxml/dashboard/BodyTab.fxml index 6c415ed..2222ef8 100644 --- a/src/main/resources/fxml/dashboard/BodyTab.fxml +++ b/src/main/resources/fxml/dashboard/BodyTab.fxml @@ -16,16 +16,17 @@ ~ limitations under the License. --> + - + - + - + @@ -53,6 +54,17 @@ - + + + + + + + + + + + diff --git a/src/main/resources/fxml/dashboard/Dashboard.fxml b/src/main/resources/fxml/dashboard/Dashboard.fxml index 4b53d39..763fa39 100644 --- a/src/main/resources/fxml/dashboard/Dashboard.fxml +++ b/src/main/resources/fxml/dashboard/Dashboard.fxml @@ -185,7 +185,7 @@ -