Added support for PUTRequest processing
This commit is contained in:
parent
027f98ee50
commit
fb484b3ecf
4 changed files with 66 additions and 30 deletions
|
@ -16,7 +16,8 @@
|
|||
|
||||
package com.rohitawate.restaurant.dashboard;
|
||||
|
||||
import com.rohitawate.restaurant.models.requests.POSTRequest;
|
||||
import com.rohitawate.restaurant.models.requests.DataDispatchRequest;
|
||||
import com.rohitawate.restaurant.models.requests.RestaurantRequest;
|
||||
import com.rohitawate.restaurant.util.ThemeManager;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
|
@ -75,8 +76,9 @@ public class BodyTabController implements Initializable {
|
|||
/**
|
||||
* Returns a RestaurantRequest object initialized with the request body.
|
||||
*/
|
||||
public POSTRequest getBasicRequest() {
|
||||
POSTRequest request = new POSTRequest();
|
||||
public RestaurantRequest getBasicRequest(String requestType) {
|
||||
DataDispatchRequest request = new DataDispatchRequest(requestType);
|
||||
|
||||
if (rawTab.isSelected()) {
|
||||
String contentType;
|
||||
switch (rawInputTypeBox.getValue()) {
|
||||
|
|
|
@ -17,11 +17,11 @@ package com.rohitawate.restaurant.dashboard;
|
|||
|
||||
import com.jfoenix.controls.JFXButton;
|
||||
import com.jfoenix.controls.JFXSnackbar;
|
||||
import com.rohitawate.restaurant.models.requests.DataDispatchRequest;
|
||||
import com.rohitawate.restaurant.models.requests.GETRequest;
|
||||
import com.rohitawate.restaurant.models.requests.POSTRequest;
|
||||
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
|
||||
import com.rohitawate.restaurant.requestsmanager.DataDispatchRequestManager;
|
||||
import com.rohitawate.restaurant.requestsmanager.GETRequestManager;
|
||||
import com.rohitawate.restaurant.requestsmanager.POSTRequestManager;
|
||||
import com.rohitawate.restaurant.requestsmanager.RequestManager;
|
||||
import com.rohitawate.restaurant.util.Settings;
|
||||
import com.rohitawate.restaurant.util.ThemeManager;
|
||||
|
@ -175,19 +175,22 @@ public class DashboardController implements Initializable {
|
|||
});
|
||||
requestManager.start();
|
||||
break;
|
||||
// DataDispatchRequestManager will generate appropriate request based on the type.
|
||||
case "POST":
|
||||
if (requestManager == null || requestManager.getClass() != POSTRequestManager.class)
|
||||
requestManager = new POSTRequestManager();
|
||||
case "PUT":
|
||||
if (requestManager == null || requestManager.getClass() != DataDispatchRequestManager.class)
|
||||
requestManager = new DataDispatchRequestManager();
|
||||
else if (requestManager.isRunning()) {
|
||||
snackBar.show("Please wait while the current request is processed.", 3000);
|
||||
return;
|
||||
}
|
||||
|
||||
POSTRequest postRequest = bodyTabController.getBasicRequest();
|
||||
postRequest.setTarget(addressField.getText());
|
||||
postRequest.addHeaders(headerTabController.getHeaders());
|
||||
DataDispatchRequest dataDispatchRequest =
|
||||
(DataDispatchRequest) bodyTabController.getBasicRequest(httpMethodBox.getValue());
|
||||
dataDispatchRequest.setTarget(addressField.getText());
|
||||
dataDispatchRequest.addHeaders(headerTabController.getHeaders());
|
||||
|
||||
requestManager.setRequest(postRequest);
|
||||
requestManager.setRequest(dataDispatchRequest);
|
||||
cancelButton.setOnAction(e -> requestManager.cancel());
|
||||
requestManager.setOnRunning(e -> {
|
||||
responseArea.clear();
|
||||
|
@ -223,6 +226,7 @@ public class DashboardController implements Initializable {
|
|||
} catch (MalformedURLException MURLE) {
|
||||
snackBar.show("Invalid address. Please verify and try again.", 3000);
|
||||
} catch (Exception E) {
|
||||
E.printStackTrace();
|
||||
errorLayer.setVisible(true);
|
||||
errorTitle.setText("Oops... That's embarrassing!");
|
||||
errorDetails.setText("Something went wrong. Try to make another request.\nRestart RESTaurant if that doesn't work.");
|
||||
|
|
|
@ -20,21 +20,28 @@ import java.net.MalformedURLException;
|
|||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class POSTRequest extends RestaurantRequest {
|
||||
/**
|
||||
* Represents HTTP requests which contain data viz. POST and PUT.
|
||||
*/
|
||||
public class DataDispatchRequest extends RestaurantRequest {
|
||||
private String requestType;
|
||||
private String body;
|
||||
private String contentType;
|
||||
private HashMap<String, String> stringTuples;
|
||||
private HashMap<String, String> fileTuples;
|
||||
|
||||
public POSTRequest() {
|
||||
public DataDispatchRequest(String requestType) {
|
||||
this.requestType = requestType;
|
||||
}
|
||||
|
||||
public POSTRequest(URL target) {
|
||||
public DataDispatchRequest(URL target, String requestType) {
|
||||
super(target);
|
||||
this.requestType = requestType;
|
||||
}
|
||||
|
||||
public POSTRequest(String target) throws MalformedURLException {
|
||||
public DataDispatchRequest(String target, String requestType) throws MalformedURLException {
|
||||
super(target);
|
||||
this.requestType = requestType;
|
||||
}
|
||||
|
||||
public void setTarget(String target) throws MalformedURLException {
|
||||
|
@ -72,4 +79,8 @@ public class POSTRequest extends RestaurantRequest {
|
|||
public void setFileTuples(HashMap<String, String> fileTuples) {
|
||||
this.fileTuples = fileTuples;
|
||||
}
|
||||
|
||||
public String getRequestType() {
|
||||
return requestType;
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@ package com.rohitawate.restaurant.requestsmanager;
|
|||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.rohitawate.restaurant.models.requests.POSTRequest;
|
||||
import com.rohitawate.restaurant.models.requests.DataDispatchRequest;
|
||||
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
|
||||
import javafx.concurrent.Task;
|
||||
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
||||
|
@ -36,21 +36,27 @@ import java.io.*;
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class POSTRequestManager extends RequestManager {
|
||||
/**
|
||||
* Processes DataDispatchRequest by automatically determining whether it
|
||||
* is a POST or a PUT request.
|
||||
*/
|
||||
public class DataDispatchRequestManager extends RequestManager {
|
||||
@Override
|
||||
protected Task<RestaurantResponse> createTask() {
|
||||
return new Task<RestaurantResponse>() {
|
||||
@Override
|
||||
protected RestaurantResponse call() throws Exception {
|
||||
POSTRequest postRequest = (POSTRequest) request;
|
||||
DataDispatchRequest dataDispatchRequest = (DataDispatchRequest) request;
|
||||
String requestType = dataDispatchRequest.getRequestType();
|
||||
|
||||
RestaurantResponse response = new RestaurantResponse();
|
||||
WebTarget target = client.target(request.getTarget().toString());
|
||||
WebTarget target = client.target(dataDispatchRequest.getTarget().toString());
|
||||
Map.Entry<String, String> mapEntry;
|
||||
|
||||
Builder requestBuilder = target.request();
|
||||
|
||||
// Add the headers to the request.
|
||||
HashMap<String, String> headers = request.getHeaders();
|
||||
HashMap<String, String> headers = dataDispatchRequest.getHeaders();
|
||||
for (Map.Entry entry : headers.entrySet()) {
|
||||
mapEntry = (Map.Entry) entry;
|
||||
requestBuilder.header(mapEntry.getKey(), mapEntry.getValue());
|
||||
|
@ -58,11 +64,11 @@ public class POSTRequestManager extends RequestManager {
|
|||
|
||||
// Adds the request body based on the content type and generates an invocation.
|
||||
Invocation invocation;
|
||||
switch (postRequest.getContentType()) {
|
||||
switch (dataDispatchRequest.getContentType()) {
|
||||
case MediaType.MULTIPART_FORM_DATA:
|
||||
FormDataMultiPart formData = new FormDataMultiPart();
|
||||
|
||||
HashMap<String, String> pairs = postRequest.getStringTuples();
|
||||
HashMap<String, String> pairs = dataDispatchRequest.getStringTuples();
|
||||
for (Map.Entry entry : pairs.entrySet()) {
|
||||
mapEntry = (Map.Entry) entry;
|
||||
formData.field(mapEntry.getKey(), mapEntry.getValue());
|
||||
|
@ -71,7 +77,7 @@ public class POSTRequestManager extends RequestManager {
|
|||
String filePath;
|
||||
File file;
|
||||
InputStream stream;
|
||||
pairs = postRequest.getFileTuples();
|
||||
pairs = dataDispatchRequest.getFileTuples();
|
||||
for (Map.Entry entry : pairs.entrySet()) {
|
||||
mapEntry = (Map.Entry) entry;
|
||||
filePath = mapEntry.getValue();
|
||||
|
@ -86,26 +92,39 @@ public class POSTRequestManager extends RequestManager {
|
|||
|
||||
formData.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
|
||||
|
||||
invocation = requestBuilder.buildPost(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE));
|
||||
if (requestType.equals("POST"))
|
||||
invocation = requestBuilder.buildPost(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE));
|
||||
else
|
||||
invocation = requestBuilder.buildPut(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE));
|
||||
break;
|
||||
case MediaType.APPLICATION_OCTET_STREAM:
|
||||
stream = new FileInputStream(postRequest.getBody());
|
||||
invocation = requestBuilder.buildPost(Entity.entity(stream, MediaType.APPLICATION_OCTET_STREAM_TYPE));
|
||||
stream = new FileInputStream(dataDispatchRequest.getBody());
|
||||
if (requestType.equals("POST"))
|
||||
invocation = requestBuilder.buildPost(Entity.entity(stream, MediaType.APPLICATION_OCTET_STREAM_TYPE));
|
||||
else
|
||||
invocation = requestBuilder.buildPut(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()) {
|
||||
for (Map.Entry entry : dataDispatchRequest.getStringTuples().entrySet()) {
|
||||
mapEntry = (Map.Entry) entry;
|
||||
form.param(mapEntry.getKey(), mapEntry.getValue());
|
||||
}
|
||||
|
||||
invocation = requestBuilder.buildPost(Entity.form(form));
|
||||
if (requestType.equals("POST"))
|
||||
invocation = requestBuilder.buildPost(Entity.form(form));
|
||||
else
|
||||
invocation = requestBuilder.buildPut(Entity.form(form));
|
||||
break;
|
||||
default:
|
||||
// Handles raw data types (JSON, Plain text, XML, HTML)
|
||||
invocation = requestBuilder
|
||||
.buildPost(Entity.entity(postRequest.getBody(), postRequest.getContentType()));
|
||||
if (requestType.equals("POST"))
|
||||
invocation = requestBuilder
|
||||
.buildPost(Entity.entity(dataDispatchRequest.getBody(), dataDispatchRequest.getContentType()));
|
||||
else
|
||||
invocation = requestBuilder
|
||||
.buildPut(Entity.entity(dataDispatchRequest.getBody(), dataDispatchRequest.getContentType()));
|
||||
}
|
||||
|
||||
long initialTime = System.currentTimeMillis();
|
Loading…
Reference in a new issue