Add ability to make DataRequests with empty bodies

This commit is contained in:
Rohit Awate 2018-08-15 19:11:04 +05:30
parent 7b631a019b
commit 8082f002fe
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
4 changed files with 27 additions and 27 deletions

View file

@ -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());
}

View file

@ -189,18 +189,22 @@ public class RequestManager extends Service<EverestResponse> {
*/
String overriddenContentType = request.getHeaders().get("Content-Type");
Invocation invocation = null;
Map.Entry<String, String> 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<String, String> pairs = dataRequest.getStringTuples();
for (Map.Entry<String, String> 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<EverestResponse> {
// Adding the file tuples to the request
for (Map.Entry<String, String> 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<EverestResponse> {
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<EverestResponse> {
Form form = new Form();
for (Map.Entry<String, String> entry : dataRequest.getStringTuples().entrySet()) {
mapEntry = entry;
form.param(mapEntry.getKey(), mapEntry.getValue());
form.param(entry.getKey(), entry.getValue());
}
invocation = getInvocation(overriddenContentType, requestType, form, requestBuilder);

View file

@ -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<FieldState> params;
public ArrayList<FieldState> headers;
public List<FieldState> params;
public List<FieldState> 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<FieldState> urlStringTuples;
public List<FieldState> urlStringTuples;
// String and file tuples of multipart-form requests
public ArrayList<FieldState> formStringTuples;
public ArrayList<FieldState> formFileTuples;
public List<FieldState> formStringTuples;
public List<FieldState> formFileTuples;
// File path of application/octet-stream requests
public String binaryFilePath;

View file

@ -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<FieldState> getTuples(int requestID, String type) throws SQLException {
private List<FieldState> 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<FieldState> tuples = new ArrayList<>();
ArrayList<FieldState> 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<FieldState> tuples, String tupleType, int requestID) {
private void saveTuple(List<FieldState> tuples, String tupleType, int requestID) {
if (tuples.size() > 0) {
try {
for (FieldState fieldState : tuples) {