Added application/x-www-form-urlencoded support in POST requests

This commit is contained in:
Rohit Awate 2018-02-02 14:57:01 +05:30
parent a8baf68ba3
commit 3d9fc01aad
5 changed files with 152 additions and 6 deletions

View file

@ -32,18 +32,23 @@ import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
/*
Raw and Binary tabs are embedded in
this FXML itself.
URL encoded and Form tabs have special FXMLs.
*/
public class BodyTabController implements Initializable {
@FXML
private ComboBox<String> rawInputTypeBox;
@FXML
private TextArea rawInputArea;
@FXML
private Tab rawTab, binaryTab, formTab;
private Tab rawTab, binaryTab, formTab, urlTab;
@FXML
private TextField filePathField;
private HeaderTabController headerTabController;
private FormDataTabController urlTabController;
private FormDataTabController formDataTabController;
private URLTabController urlTabController;
@Override
public void initialize(URL location, ResourceBundle resources) {
@ -53,6 +58,10 @@ public class BodyTabController implements Initializable {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/dashboard/FormDataTab.fxml"));
formTab.setContent(loader.load());
formDataTabController = loader.getController();
loader = new FXMLLoader(getClass().getResource("/fxml/dashboard/URLTab.fxml"));
urlTab.setContent(loader.load());
urlTabController = loader.getController();
} catch (IOException e) {
e.printStackTrace();
@ -85,13 +94,16 @@ public class BodyTabController implements Initializable {
request.setContentType(contentType);
request.setBody(rawInputArea.getText());
} else if (formTab.isSelected()) {
request.setStringTuples(urlTabController.getStringTuples());
request.setFileTuples(urlTabController.getFileTuples());
request.setStringTuples(formDataTabController.getStringTuples());
request.setFileTuples(formDataTabController.getFileTuples());
request.setContentType(MediaType.MULTIPART_FORM_DATA);
} else if (binaryTab.isSelected()) {
request.setBody(filePathField.getText());
request.setContentType(MediaType.APPLICATION_OCTET_STREAM);
} else if (urlTab.isSelected()) {
request.setStringTuples(urlTabController.getStringTuples());
request.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
}
return request;
}

View file

@ -0,0 +1,65 @@
/*
* 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.dashboard;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.layout.VBox;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;
public class URLTabController implements Initializable {
@FXML
private VBox fieldsBox;
private List<StringKeyValueFieldController> controllers;
@Override
public void initialize(URL location, ResourceBundle resources) {
controllers = new ArrayList<>();
addField();
}
@FXML
private void addField() {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/dashboard/StringKeyValueField.fxml"));
Parent headerField = loader.load();
StringKeyValueFieldController controller = loader.getController();
controllers.add(controller);
fieldsBox.getChildren().add(headerField);
} catch (IOException e) {
e.printStackTrace();
}
}
public HashMap<String, String> getStringTuples() {
HashMap<String, String> headers = new HashMap<>();
for (StringKeyValueFieldController controller : controllers) {
if (controller.isChecked())
headers.put(controller.getHeader().getKey(), controller.getHeader().getValue());
}
return headers;
}
}

View file

@ -29,6 +29,7 @@ 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.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.File;
@ -89,6 +90,16 @@ public class POSTRequestManager extends RequestManager {
InputStream stream = new FileInputStream(postRequest.getBody());
invocation = requestBuilder.buildPost(Entity.entity(stream, MediaType.APPLICATION_OCTET_STREAM_TYPE));
break;
case MediaType.APPLICATION_FORM_URLENCODED:
Form form = new Form();
for (Map.Entry entry : postRequest.getStringTuples().entrySet()) {
mapEntry = (Map.Entry) entry;
form.param(mapEntry.getKey(), mapEntry.getValue());
}
invocation = requestBuilder.buildPost(Entity.form(form));
break;
default:
// Handles raw data types (JSON, Plain text, XML, HTML)
invocation = requestBuilder

View file

@ -24,6 +24,8 @@
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 fx:id="formTab" text="FORM"/>
<Tab fx:id="urlTab" text="URL ENCODED"/>
<Tab fx:id="rawTab" text="RAW">
<content>
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER">
@ -65,6 +67,5 @@
</HBox>
</content>
</Tab>
<Tab fx:id="formTab" text="FORM"/>
</tabs>
</TabPane>

View file

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<?import com.jfoenix.controls.JFXButton?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.*?>
<VBox stylesheets="@../../css/Default.css" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.rohitawate.restaurant.dashboard.URLTabController">
<children>
<HBox alignment="CENTER" VBox.vgrow="ALWAYS">
<VBox.margin>
<Insets/>
</VBox.margin>
<children>
<JFXButton fx:id="newStringKVButton" onAction="#addField" text=" NEW FIELD" textFill="WHITE"
HBox.hgrow="ALWAYS">
<graphic>
<ImageView fitHeight="15.0" fitWidth="15.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../../assets/Plus.png"/>
</image>
</ImageView>
</graphic>
<HBox.margin>
<Insets/>
</HBox.margin>
</JFXButton>
</children>
<padding>
<Insets bottom="15.0" left="15.0" right="15.0" top="15.0"/>
</padding>
</HBox>
<ScrollPane fitToHeight="true" fitToWidth="true" hbarPolicy="NEVER" VBox.vgrow="ALWAYS">
<content>
<VBox fx:id="fieldsBox" alignment="TOP_CENTER"/>
</content>
</ScrollPane>
</children>
</VBox>