Improved POST request generation and processing and minor UI tweaks
This commit is contained in:
parent
fa63018687
commit
b675bd35fd
8 changed files with 128 additions and 60 deletions
|
@ -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<String> 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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
|
|
|
@ -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<String, String> 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<String, String> URLTextTuples) {
|
||||
this.URLTextTuples = URLTextTuples;
|
||||
}
|
||||
|
||||
public HashMap<String, String> getURLTextTuples() {
|
||||
return URLTextTuples;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,7 +21,7 @@ import java.net.URL;
|
|||
import java.util.HashMap;
|
||||
|
||||
public abstract class RestaurantRequest {
|
||||
private URL target;
|
||||
URL target;
|
||||
private HashMap<String, String> headers;
|
||||
|
||||
RestaurantRequest() {
|
||||
|
|
|
@ -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<RestaurantResponse>() {
|
||||
@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<String, String> headers = request.getHeaders();
|
||||
Map.Entry<String, String> mapEntry;
|
||||
|
||||
Builder requestBuilder = target.request();
|
||||
|
||||
// Add the headers to the request.
|
||||
HashMap<String, String> 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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -16,16 +16,17 @@
|
|||
~ limitations under the License.
|
||||
-->
|
||||
|
||||
<?import com.jfoenix.controls.JFXButton?>
|
||||
<?import javafx.geometry.Insets?>
|
||||
<?import javafx.scene.control.*?>
|
||||
<?import javafx.scene.layout.*?>
|
||||
<TabPane fx:id="bodyTabPane" stylesheets="@../../css/Default.css" tabClosingPolicy="UNAVAILABLE" tabMinWidth="100.0"
|
||||
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111"
|
||||
fx:controller="com.rohitawate.restaurant.dashboard.BodyTabController">
|
||||
<TabPane fx:id="bodyTabPane" prefHeight="100.0" prefWidth="1280.0" stylesheets="@../../css/Default.css"
|
||||
tabClosingPolicy="UNAVAILABLE" tabMinWidth="100.0" xmlns="http://javafx.com/javafx/8.0.111"
|
||||
xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.rohitawate.restaurant.dashboard.BodyTabController">
|
||||
<tabs>
|
||||
<Tab text="RAW">
|
||||
<Tab fx:id="rawTab" text="RAW">
|
||||
<content>
|
||||
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER" vbarPolicy="ALWAYS">
|
||||
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER">
|
||||
<content>
|
||||
<VBox alignment="CENTER">
|
||||
<children>
|
||||
|
@ -53,6 +54,17 @@
|
|||
</ScrollPane>
|
||||
</content>
|
||||
</Tab>
|
||||
<Tab text="BINARY"/>
|
||||
<Tab fx:id="binaryTab" text="BINARY">
|
||||
<content>
|
||||
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0" spacing="20.0">
|
||||
<children>
|
||||
<TextField fx:id="filePathField" maxWidth="700.0" promptText="FILE PATH" HBox.hgrow="ALWAYS"/>
|
||||
<JFXButton fx:id="browseButton" buttonType="RAISED" onAction="#browseFile" ripplerFill="WHITE"
|
||||
text="BROWSE" HBox.hgrow="ALWAYS"/>
|
||||
</children>
|
||||
</HBox>
|
||||
</content>
|
||||
</Tab>
|
||||
<Tab fx:id="urlTab" text="X-WWW-FORM-URLENCODED"/>
|
||||
</tabs>
|
||||
</TabPane>
|
||||
|
|
|
@ -185,7 +185,7 @@
|
|||
<VBox fx:id="promptLayer" alignment="CENTER" prefHeight="200.0"
|
||||
prefWidth="100.0" visible="false">
|
||||
<children>
|
||||
<Label text="Enter an address, select a method and hit send."
|
||||
<Label text="Enter an address, select a method and hit send. 🚀"
|
||||
textFill="WHITE">
|
||||
<font>
|
||||
<Font size="32.0"/>
|
||||
|
|
Loading…
Reference in a new issue