From 8082f002fe50df2cfc08bad321e35216a01dee91 Mon Sep 17 00:00:00 2001 From: Rohit Awate Date: Wed, 15 Aug 2018 19:11:04 +0530 Subject: [PATCH] Add ability to make DataRequests with empty bodies --- .../controllers/DashboardController.java | 5 +---- .../requestmanager/RequestManager.java | 22 +++++++++++-------- .../everest/state/ComposerState.java | 13 +++++------ .../everest/sync/SQLiteManager.java | 14 ++++++------ 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java index 407b03f..ccf36d3 100644 --- a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java +++ b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java @@ -346,10 +346,7 @@ public class DashboardController implements Initializable { } if (requestManager.getRequest().getClass().equals(DataRequest.class)) { - if (throwable.getCause() != null && throwable.getCause().getClass() == IllegalArgumentException.class) { - errorTitle.setText("Did you forget something?"); - errorDetails.setText("Please specify a body for your " + httpMethodBox.getValue() + " request."); - } else if (throwable.getClass() == FileNotFoundException.class) { + if (throwable.getClass() == FileNotFoundException.class) { errorTitle.setText("File(s) not found:"); errorDetails.setText(throwable.getMessage()); } diff --git a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java index d1560b7..0ba1d12 100644 --- a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java +++ b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java @@ -189,18 +189,22 @@ public class RequestManager extends Service { */ String overriddenContentType = request.getHeaders().get("Content-Type"); Invocation invocation = null; - Map.Entry mapEntry; String requestType = dataRequest.getRequestType(); switch (dataRequest.getContentType()) { case MediaType.MULTIPART_FORM_DATA: + // This enables Everest to make requests with an empty body. + if (dataRequest.getStringTuples().size() == 0 && dataRequest.getFileTuples().size() == 0) { + invocation = getInvocation(MediaType.MULTIPART_FORM_DATA, requestType, null, requestBuilder); + break; + } + FormDataMultiPart formData = new FormDataMultiPart(); // Adding the string tuples to the request HashMap pairs = dataRequest.getStringTuples(); for (Map.Entry entry : pairs.entrySet()) { - mapEntry = entry; - formData.field(mapEntry.getKey(), mapEntry.getValue()); + formData.field(entry.getKey(), entry.getValue()); } String filePath; @@ -211,12 +215,11 @@ public class RequestManager extends Service { // Adding the file tuples to the request for (Map.Entry entry : pairs.entrySet()) { - mapEntry = entry; - filePath = mapEntry.getValue(); + filePath = entry.getValue(); file = new File(filePath); if (file.exists()) - formData.bodyPart(new FileDataBodyPart(mapEntry.getKey(), + formData.bodyPart(new FileDataBodyPart(entry.getKey(), file, MediaType.APPLICATION_OCTET_STREAM_TYPE)); else { fileException = true; @@ -238,8 +241,10 @@ public class RequestManager extends Service { overriddenContentType = MediaType.APPLICATION_OCTET_STREAM; filePath = dataRequest.getBody(); + // This enables Everest to make requests with empty bodies. if (filePath.equals("")) { - throw new FileNotFoundException("No file selected"); + invocation = getInvocation(overriddenContentType, requestType, null, requestBuilder); + break; } File check = new File(filePath); @@ -259,8 +264,7 @@ public class RequestManager extends Service { Form form = new Form(); for (Map.Entry entry : dataRequest.getStringTuples().entrySet()) { - mapEntry = entry; - form.param(mapEntry.getKey(), mapEntry.getValue()); + form.param(entry.getKey(), entry.getValue()); } invocation = getInvocation(overriddenContentType, requestType, form, requestBuilder); diff --git a/src/main/java/com/rohitawate/everest/state/ComposerState.java b/src/main/java/com/rohitawate/everest/state/ComposerState.java index d099d1d..4c1e651 100644 --- a/src/main/java/com/rohitawate/everest/state/ComposerState.java +++ b/src/main/java/com/rohitawate/everest/state/ComposerState.java @@ -18,8 +18,7 @@ package com.rohitawate.everest.state; import com.rohitawate.everest.models.requests.HTTPConstants; -import java.util.ArrayList; -import java.util.Objects; +import java.util.List; /** * Represents the state of the Composer. @@ -28,8 +27,8 @@ public class ComposerState { public String target; public String httpMethod; - public ArrayList params; - public ArrayList headers; + public List params; + public List headers; public String contentType; // Body and content-type of requests with raw bodies @@ -37,11 +36,11 @@ public class ComposerState { public String rawBodyBoxValue; // Tuples of URL-encoded requests - public ArrayList urlStringTuples; + public List urlStringTuples; // String and file tuples of multipart-form requests - public ArrayList formStringTuples; - public ArrayList formFileTuples; + public List formStringTuples; + public List formFileTuples; // File path of application/octet-stream requests public String binaryFilePath; diff --git a/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java b/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java index 2eb024b..e4d0c6b 100644 --- a/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java +++ b/src/main/java/com/rohitawate/everest/sync/SQLiteManager.java @@ -208,15 +208,15 @@ class SQLiteManager implements DataManager { /** * @param requestID Database ID of the request whose tuples are needed. - * @param type Type of tuples needed ('String', 'File' or 'Param') - * @return tuples - Map of tuples of corresponding type + * @param type Type of tuples needed ('URLString', 'FormString', 'File', 'Header' or 'Param') + * @return fieldStates - List of FieldStates for the tuples */ - private ArrayList getTuples(int requestID, String type) throws SQLException { + private List getTuples(int requestID, String type) throws SQLException { if (!(type.equals("FormString") || type.equals("URLString") || type.equals("File") || type.equals("Param") || type.equals("Header"))) return null; - ArrayList tuples = new ArrayList<>(); + ArrayList fieldStates = new ArrayList<>(); PreparedStatement statement = conn.prepareStatement(Queries.selectTuplesByType); statement.setInt(1, requestID); @@ -230,10 +230,10 @@ class SQLiteManager implements DataManager { key = RS.getString("Key"); value = RS.getString("Value"); checked = RS.getBoolean("Checked"); - tuples.add(new FieldState(key, value, checked)); + fieldStates.add(new FieldState(key, value, checked)); } - return tuples; + return fieldStates; } private ComposerState getLastRequest() { @@ -297,7 +297,7 @@ class SQLiteManager implements DataManager { return null; } - private void saveTuple(ArrayList tuples, String tupleType, int requestID) { + private void saveTuple(List tuples, String tupleType, int requestID) { if (tuples.size() > 0) { try { for (FieldState fieldState : tuples) {