Add HTTPConstants class and partial improvements to HistoryManager
This commit is contained in:
parent
0be0fbce41
commit
42bb97eb42
8 changed files with 142 additions and 122 deletions
|
@ -20,6 +20,7 @@ import com.rohitawate.everest.controllers.codearea.EverestCodeArea;
|
|||
import com.rohitawate.everest.controllers.codearea.highlighters.HighlighterFactory;
|
||||
import com.rohitawate.everest.logging.LoggingService;
|
||||
import com.rohitawate.everest.misc.ThemeManager;
|
||||
import com.rohitawate.everest.models.requests.HTTPConstants;
|
||||
import com.rohitawate.everest.state.ComposerState;
|
||||
import com.rohitawate.everest.state.FieldState;
|
||||
import javafx.fxml.FXML;
|
||||
|
@ -68,7 +69,12 @@ public class BodyTabController implements Initializable {
|
|||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
rawInputTypeBox.getItems().addAll("PLAIN TEXT", "JSON", "XML", "HTML");
|
||||
rawInputTypeBox.getItems().addAll(
|
||||
HTTPConstants.JSON,
|
||||
HTTPConstants.XML,
|
||||
HTTPConstants.HTML,
|
||||
HTTPConstants.PLAIN_TEXT
|
||||
);
|
||||
rawInputTypeBox.getSelectionModel().select(0);
|
||||
|
||||
rawInputArea = new EverestCodeArea();
|
||||
|
@ -79,7 +85,7 @@ public class BodyTabController implements Initializable {
|
|||
rawInputTypeBox.valueProperty().addListener(change -> {
|
||||
String type = rawInputTypeBox.getValue();
|
||||
|
||||
if (type.equals("JSON")) {
|
||||
if (type.equals(HTTPConstants.PLAIN_TEXT)) {
|
||||
rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(type));
|
||||
return;
|
||||
}
|
||||
|
@ -128,27 +134,31 @@ public class BodyTabController implements Initializable {
|
|||
|
||||
state.rawBody = rawInputArea.getText();
|
||||
switch (rawInputTypeBox.getValue()) {
|
||||
case "JSON":
|
||||
state.rawBodyContentType = MediaType.APPLICATION_JSON;
|
||||
case HTTPConstants.JSON:
|
||||
state.rawBodyBoxValue = MediaType.APPLICATION_JSON;
|
||||
break;
|
||||
case "XML":
|
||||
state.rawBodyContentType = MediaType.APPLICATION_XML;
|
||||
case HTTPConstants.XML:
|
||||
state.rawBodyBoxValue = MediaType.APPLICATION_XML;
|
||||
break;
|
||||
case "HTML":
|
||||
state.rawBodyContentType = MediaType.TEXT_HTML;
|
||||
case HTTPConstants.HTML:
|
||||
state.rawBodyBoxValue = MediaType.TEXT_HTML;
|
||||
break;
|
||||
default:
|
||||
state.rawBodyContentType = MediaType.TEXT_PLAIN;
|
||||
state.rawBodyBoxValue = MediaType.TEXT_PLAIN;
|
||||
}
|
||||
|
||||
if (rawTab.isSelected()) {
|
||||
state.visibleBodyTab = BodyTab.RAW;
|
||||
state.contentType = state.rawBodyBoxValue;
|
||||
} else if (formTab.isSelected()) {
|
||||
state.visibleBodyTab = BodyTab.FORM;
|
||||
state.contentType = MediaType.MULTIPART_FORM_DATA;
|
||||
} else if (urlTab.isSelected()) {
|
||||
state.visibleBodyTab = BodyTab.URL;
|
||||
state.contentType = MediaType.APPLICATION_FORM_URLENCODED;
|
||||
} else {
|
||||
state.visibleBodyTab = BodyTab.BINARY;
|
||||
state.contentType = MediaType.APPLICATION_OCTET_STREAM;
|
||||
}
|
||||
|
||||
return state;
|
||||
|
@ -200,32 +210,32 @@ public class BodyTabController implements Initializable {
|
|||
urlTabController.clear();
|
||||
formDataTabController.clear();
|
||||
rawInputArea.clear();
|
||||
rawInputTypeBox.setValue("PLAIN TEXT");
|
||||
rawInputTypeBox.setValue(HTTPConstants.PLAIN_TEXT);
|
||||
filePathField.clear();
|
||||
}
|
||||
|
||||
private void setRawTab(ComposerState state) {
|
||||
if (state.rawBodyContentType != null && state.rawBody != null) {
|
||||
if (state.rawBodyBoxValue != null && state.rawBody != null) {
|
||||
// TODO: Remove this conversion
|
||||
String simplifiedContentType;
|
||||
switch (state.rawBodyContentType) {
|
||||
switch (state.rawBodyBoxValue) {
|
||||
case MediaType.APPLICATION_JSON:
|
||||
simplifiedContentType = "JSON";
|
||||
simplifiedContentType = HTTPConstants.JSON;
|
||||
break;
|
||||
case MediaType.APPLICATION_XML:
|
||||
simplifiedContentType = "XML";
|
||||
simplifiedContentType = HTTPConstants.XML;
|
||||
break;
|
||||
case MediaType.TEXT_HTML:
|
||||
simplifiedContentType = "HTML";
|
||||
simplifiedContentType = HTTPConstants.HTML;
|
||||
break;
|
||||
default:
|
||||
simplifiedContentType = "PLAIN TEXT";
|
||||
simplifiedContentType = HTTPConstants.PLAIN_TEXT;
|
||||
}
|
||||
rawInputTypeBox.setValue(simplifiedContentType);
|
||||
rawInputArea.setText(state.rawBody, HighlighterFactory.getHighlighter(simplifiedContentType));
|
||||
} else {
|
||||
rawInputTypeBox.setValue("PLAIN TEXT");
|
||||
rawInputArea.setHighlighter(HighlighterFactory.getHighlighter("PLAIN TEXT"));
|
||||
rawInputTypeBox.setValue(HTTPConstants.PLAIN_TEXT);
|
||||
rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(HTTPConstants.PLAIN_TEXT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ import com.rohitawate.everest.misc.ThemeManager;
|
|||
import com.rohitawate.everest.models.requests.DELETERequest;
|
||||
import com.rohitawate.everest.models.requests.DataRequest;
|
||||
import com.rohitawate.everest.models.requests.GETRequest;
|
||||
import com.rohitawate.everest.models.requests.HTTPConstants;
|
||||
import com.rohitawate.everest.models.responses.EverestResponse;
|
||||
import com.rohitawate.everest.requestmanager.RequestManager;
|
||||
import com.rohitawate.everest.requestmanager.RequestManagersPool;
|
||||
|
@ -93,7 +94,6 @@ public class DashboardController implements Initializable {
|
|||
private JFXProgressBar progressBar;
|
||||
|
||||
private JFXSnackbar snackbar;
|
||||
private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"};
|
||||
private List<StringKeyValueFieldController> paramsControllers;
|
||||
private RequestManager requestManager;
|
||||
private HeaderTabController headerTabController;
|
||||
|
@ -144,10 +144,15 @@ public class DashboardController implements Initializable {
|
|||
snackbar = new JFXSnackbar(dashboard);
|
||||
|
||||
showLayer(ResponseLayer.PROMPT);
|
||||
httpMethodBox.getItems().addAll(httpMethods);
|
||||
httpMethodBox.getItems().addAll(
|
||||
HTTPConstants.GET,
|
||||
HTTPConstants.POST,
|
||||
HTTPConstants.PUT,
|
||||
HTTPConstants.PATCH,
|
||||
HTTPConstants.DELETE);
|
||||
|
||||
// Select GET by default
|
||||
httpMethodBox.getSelectionModel().select("GET");
|
||||
httpMethodBox.getSelectionModel().select(HTTPConstants.GET);
|
||||
|
||||
paramsControllers = new ArrayList<>();
|
||||
paramsCountProperty = new SimpleIntegerProperty(0);
|
||||
|
@ -155,8 +160,8 @@ public class DashboardController implements Initializable {
|
|||
addParamField(); // Adds a blank param field
|
||||
|
||||
bodyTab.disableProperty().bind(
|
||||
Bindings.or(httpMethodBox.valueProperty().isEqualTo("GET"),
|
||||
httpMethodBox.valueProperty().isEqualTo("DELETE")));
|
||||
Bindings.or(httpMethodBox.valueProperty().isEqualTo(HTTPConstants.GET),
|
||||
httpMethodBox.valueProperty().isEqualTo(HTTPConstants.DELETE)));
|
||||
|
||||
// Disabling Ctrl+Tab navigation
|
||||
requestOptionsTab.setOnKeyPressed(e -> {
|
||||
|
@ -172,7 +177,11 @@ public class DashboardController implements Initializable {
|
|||
snackbar.show("Response body copied to clipboard.", 5000);
|
||||
});
|
||||
|
||||
responseTypeBox.getItems().addAll("JSON", "XML", "HTML", "PLAIN TEXT");
|
||||
responseTypeBox.getItems().addAll(
|
||||
HTTPConstants.JSON,
|
||||
HTTPConstants.XML,
|
||||
HTTPConstants.HTML,
|
||||
HTTPConstants.PLAIN_TEXT);
|
||||
|
||||
responseTypeBox.valueProperty().addListener(change -> {
|
||||
String type = responseTypeBox.getValue();
|
||||
|
@ -231,7 +240,7 @@ public class DashboardController implements Initializable {
|
|||
addressField.setText(address);
|
||||
|
||||
switch (httpMethodBox.getValue()) {
|
||||
case "GET":
|
||||
case HTTPConstants.GET:
|
||||
if (getRequest == null)
|
||||
getRequest = new GETRequest();
|
||||
|
||||
|
@ -241,9 +250,9 @@ public class DashboardController implements Initializable {
|
|||
requestManager = RequestManagersPool.manager();
|
||||
requestManager.setRequest(getRequest);
|
||||
break;
|
||||
case "POST":
|
||||
case "PUT":
|
||||
case "PATCH":
|
||||
case HTTPConstants.POST:
|
||||
case HTTPConstants.PUT:
|
||||
case HTTPConstants.PATCH:
|
||||
if (dataRequest == null)
|
||||
dataRequest = new DataRequest();
|
||||
|
||||
|
@ -286,7 +295,7 @@ public class DashboardController implements Initializable {
|
|||
requestManager = RequestManagersPool.manager();
|
||||
requestManager.setRequest(dataRequest);
|
||||
break;
|
||||
case "DELETE":
|
||||
case HTTPConstants.DELETE:
|
||||
if (deleteRequest == null)
|
||||
deleteRequest = new DELETERequest();
|
||||
|
||||
|
@ -644,9 +653,9 @@ public class DashboardController implements Initializable {
|
|||
ComposerState composerState;
|
||||
|
||||
switch (httpMethodBox.getValue()) {
|
||||
case "POST":
|
||||
case "PUT":
|
||||
case "PATCH":
|
||||
case HTTPConstants.POST:
|
||||
case HTTPConstants.PUT:
|
||||
case HTTPConstants.PATCH:
|
||||
composerState = bodyTabController.getState();
|
||||
break;
|
||||
default:
|
||||
|
@ -781,6 +790,8 @@ public class DashboardController implements Initializable {
|
|||
This is an extreme case, but still something to be taken care of.
|
||||
*/
|
||||
boolean validMethod = false;
|
||||
String[] httpMethods =
|
||||
{HTTPConstants.GET, HTTPConstants.POST, HTTPConstants.PUT, HTTPConstants.PATCH, HTTPConstants.DELETE};
|
||||
for (String method : httpMethods) {
|
||||
if (method.equals(state.composer.httpMethod))
|
||||
validMethod = true;
|
||||
|
@ -806,12 +817,12 @@ public class DashboardController implements Initializable {
|
|||
addParamField(fieldState);
|
||||
}
|
||||
|
||||
if (!(httpMethodBox.getValue().equals("GET") || httpMethodBox.getValue().equals("DELETE")))
|
||||
if (!(state.composer.httpMethod.equals(HTTPConstants.GET) || state.composer.httpMethod.equals(HTTPConstants.DELETE)))
|
||||
bodyTabController.setState(state.composer);
|
||||
}
|
||||
|
||||
void reset() {
|
||||
httpMethodBox.setValue("GET");
|
||||
httpMethodBox.setValue(HTTPConstants.GET);
|
||||
addressField.clear();
|
||||
headerTabController.clear();
|
||||
clearParams();
|
||||
|
|
|
@ -18,6 +18,7 @@ package com.rohitawate.everest.controllers;
|
|||
|
||||
import com.rohitawate.everest.controllers.search.Searchable;
|
||||
import com.rohitawate.everest.logging.LoggingService;
|
||||
import com.rohitawate.everest.models.requests.HTTPConstants;
|
||||
import com.rohitawate.everest.state.ComposerState;
|
||||
import com.rohitawate.everest.state.FieldState;
|
||||
import javafx.fxml.FXML;
|
||||
|
@ -55,13 +56,14 @@ public class HistoryItemController implements Initializable, Searchable<Composer
|
|||
}
|
||||
|
||||
public int getRelativityIndex(String searchString) {
|
||||
int index = 0;
|
||||
searchString = searchString.toLowerCase();
|
||||
String comparisonString;
|
||||
|
||||
// Checks if matches with target
|
||||
comparisonString = state.target.toLowerCase();
|
||||
if (comparisonString.contains(searchString))
|
||||
return 10;
|
||||
index += 10;
|
||||
|
||||
try {
|
||||
URL url = new URL(state.target);
|
||||
|
@ -69,12 +71,12 @@ public class HistoryItemController implements Initializable, Searchable<Composer
|
|||
// Checks if matches with target's hostname
|
||||
comparisonString = url.getHost().toLowerCase();
|
||||
if (comparisonString.contains(searchString))
|
||||
return 10;
|
||||
index += 10;
|
||||
|
||||
// Checks if matches with target's path
|
||||
comparisonString = url.getPath().toLowerCase();
|
||||
if (comparisonString.contains(searchString))
|
||||
return 9;
|
||||
index += 9;
|
||||
} catch (MalformedURLException e) {
|
||||
LoggingService.logInfo("Failed to parse URL while calculating relativity index.", LocalDateTime.now());
|
||||
}
|
||||
|
@ -82,24 +84,24 @@ public class HistoryItemController implements Initializable, Searchable<Composer
|
|||
// Checks if matches with HTTP method
|
||||
comparisonString = state.httpMethod.toLowerCase();
|
||||
if (comparisonString.contains(searchString))
|
||||
return 7;
|
||||
index += 7;
|
||||
|
||||
// Checks for a match in the params
|
||||
for (FieldState state : state.params) {
|
||||
if (state.key.toLowerCase().contains(searchString) ||
|
||||
state.value.toLowerCase().contains(searchString))
|
||||
return 5;
|
||||
index += 5;
|
||||
}
|
||||
|
||||
// Checks for a match in the headers
|
||||
for (FieldState state : state.headers) {
|
||||
if (state.key.toLowerCase().contains(searchString) ||
|
||||
state.value.toLowerCase().contains(searchString))
|
||||
return 6;
|
||||
index += 6;
|
||||
}
|
||||
|
||||
if (state.httpMethod.equals("POST") || state.httpMethod.equals("PUT")) {
|
||||
switch (state.rawBodyContentType) {
|
||||
if (!(state.httpMethod.equals(HTTPConstants.GET) || state.httpMethod.equals(HTTPConstants.DELETE))) {
|
||||
switch (state.contentType) {
|
||||
case MediaType.TEXT_PLAIN:
|
||||
case MediaType.APPLICATION_JSON:
|
||||
case MediaType.APPLICATION_XML:
|
||||
|
@ -108,14 +110,14 @@ public class HistoryItemController implements Initializable, Searchable<Composer
|
|||
// Checks for match in rawBody of the request
|
||||
comparisonString = state.rawBody.toLowerCase();
|
||||
if (comparisonString.contains(searchString))
|
||||
return 8;
|
||||
index += 8;
|
||||
break;
|
||||
case MediaType.APPLICATION_FORM_URLENCODED:
|
||||
// Checks for match in string tuples
|
||||
for (FieldState state : state.urlStringTuples) {
|
||||
if (state.key.toLowerCase().contains(searchString) ||
|
||||
state.value.toLowerCase().contains(searchString))
|
||||
return 8;
|
||||
index += 8;
|
||||
}
|
||||
break;
|
||||
case MediaType.MULTIPART_FORM_DATA:
|
||||
|
@ -123,17 +125,17 @@ public class HistoryItemController implements Initializable, Searchable<Composer
|
|||
for (FieldState state : state.formStringTuples) {
|
||||
if (state.key.toLowerCase().contains(searchString) ||
|
||||
state.value.toLowerCase().contains(searchString))
|
||||
return 8;
|
||||
index += 8;
|
||||
}
|
||||
|
||||
for (FieldState state : state.formFileTuples) {
|
||||
if (state.key.toLowerCase().contains(searchString) ||
|
||||
state.value.toLowerCase().contains(searchString))
|
||||
return 8;
|
||||
index += 8;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.rohitawate.everest.logging.LoggingService;
|
|||
import com.rohitawate.everest.misc.EverestUtilities;
|
||||
import com.rohitawate.everest.misc.KeyMap;
|
||||
import com.rohitawate.everest.misc.ThemeManager;
|
||||
import com.rohitawate.everest.models.requests.HTTPConstants;
|
||||
import com.rohitawate.everest.state.ComposerState;
|
||||
import com.rohitawate.everest.state.DashboardState;
|
||||
import javafx.application.Platform;
|
||||
|
@ -302,7 +303,7 @@ public class HomeWindowController implements Initializable {
|
|||
dashboard.requestOptionsTab.getSelectionModel().select(dashboard.headersTab);
|
||||
} else if (KeyMap.focusBody.match(e)) {
|
||||
String httpMethod = dashboard.httpMethodBox.getValue();
|
||||
if (!httpMethod.equals("GET") && !httpMethod.equals("DELETE")) {
|
||||
if (!httpMethod.equals(HTTPConstants.GET) && !httpMethod.equals(HTTPConstants.DELETE)) {
|
||||
dashboard.requestOptionsTab.getSelectionModel().select(dashboard.bodyTab);
|
||||
}
|
||||
} else if (KeyMap.refreshTheme.match(e)) {
|
||||
|
|
|
@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.rohitawate.everest.logging.LoggingService;
|
||||
import com.rohitawate.everest.misc.EverestUtilities;
|
||||
import com.rohitawate.everest.misc.Services;
|
||||
import com.rohitawate.everest.models.requests.HTTPConstants;
|
||||
import com.rohitawate.everest.settings.Settings;
|
||||
import com.rohitawate.everest.state.ComposerState;
|
||||
import com.rohitawate.everest.state.FieldState;
|
||||
|
@ -132,7 +133,7 @@ public class HistoryManager {
|
|||
state.params = getTuples(requestID, "Param");
|
||||
state.httpMethod = resultSet.getString("Type");
|
||||
|
||||
if (!(state.httpMethod.equals("GET") || state.httpMethod.equals("DELETE"))) {
|
||||
if (!(state.httpMethod.equals(HTTPConstants.GET) || state.httpMethod.equals(HTTPConstants.DELETE))) {
|
||||
// Retrieves request body ContentType for querying corresponding table
|
||||
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestContentType").toString()));
|
||||
statement.setInt(1, requestID);
|
||||
|
@ -143,15 +144,8 @@ public class HistoryManager {
|
|||
if (RS.next())
|
||||
contentType = RS.getString("ContentType");
|
||||
|
||||
state.rawBodyContentType = contentType;
|
||||
state.contentType = contentType;
|
||||
|
||||
// Retrieves body from corresponding table
|
||||
switch (contentType) {
|
||||
case MediaType.TEXT_PLAIN:
|
||||
case MediaType.APPLICATION_JSON:
|
||||
case MediaType.APPLICATION_XML:
|
||||
case MediaType.TEXT_HTML:
|
||||
case MediaType.APPLICATION_OCTET_STREAM:
|
||||
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestBody").toString()));
|
||||
statement.setInt(1, requestID);
|
||||
|
||||
|
@ -159,15 +153,10 @@ public class HistoryManager {
|
|||
|
||||
if (RS.next())
|
||||
state.rawBody = RS.getString("Body");
|
||||
break;
|
||||
case MediaType.APPLICATION_FORM_URLENCODED:
|
||||
|
||||
state.urlStringTuples = getTuples(requestID, "String");
|
||||
break;
|
||||
case MediaType.MULTIPART_FORM_DATA:
|
||||
state.formStringTuples = getTuples(requestID, "String");
|
||||
state.formFileTuples = getTuples(requestID, "File");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
history.add(state);
|
||||
|
@ -273,7 +262,7 @@ public class HistoryManager {
|
|||
if (!areListsEqual(states, newState.params))
|
||||
return false;
|
||||
|
||||
if (!(newState.httpMethod.equals("GET") || newState.httpMethod.equals("DELETE"))) {
|
||||
if (!(newState.httpMethod.equals(HTTPConstants.GET) || newState.httpMethod.equals(HTTPConstants.DELETE))) {
|
||||
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestContentType").toString()));
|
||||
statement.setInt(1, lastRequestID);
|
||||
|
||||
|
@ -283,10 +272,10 @@ public class HistoryManager {
|
|||
if (RS.next())
|
||||
previousContentType = RS.getString("ContentType");
|
||||
|
||||
if (!newState.rawBodyContentType.equals(previousContentType))
|
||||
if (!newState.contentType.equals(previousContentType))
|
||||
return false;
|
||||
|
||||
switch (newState.rawBodyContentType) {
|
||||
switch (newState.contentType) {
|
||||
case MediaType.TEXT_PLAIN:
|
||||
case MediaType.APPLICATION_JSON:
|
||||
case MediaType.APPLICATION_XML:
|
||||
|
@ -388,35 +377,22 @@ public class HistoryManager {
|
|||
// Saves request parameters
|
||||
saveTuple(state.params, "Param", requestID);
|
||||
|
||||
if (!(state.httpMethod.equals("GET") || state.httpMethod.equals("DELETE"))) {
|
||||
if (!(state.httpMethod.equals(HTTPConstants.GET) || state.httpMethod.equals(HTTPConstants.DELETE))) {
|
||||
// Maps the request to its ContentType for faster retrieval
|
||||
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveRequestContentPair").toString()));
|
||||
statement.setInt(1, requestID);
|
||||
statement.setString(2, state.rawBodyContentType);
|
||||
statement.setString(2, state.contentType);
|
||||
|
||||
statement.executeUpdate();
|
||||
|
||||
// Determines where to fetch the body from, based on the ContentType
|
||||
switch (state.rawBodyContentType) {
|
||||
case MediaType.TEXT_PLAIN:
|
||||
case MediaType.APPLICATION_JSON:
|
||||
case MediaType.APPLICATION_XML:
|
||||
case MediaType.TEXT_HTML:
|
||||
case MediaType.APPLICATION_OCTET_STREAM:
|
||||
// Saves the body in case of raw content, or the file location in case of binary
|
||||
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveBody").toString()));
|
||||
statement.setInt(1, requestID);
|
||||
statement.setString(2, state.rawBody);
|
||||
statement.executeUpdate();
|
||||
break;
|
||||
case MediaType.APPLICATION_FORM_URLENCODED:
|
||||
|
||||
saveTuple(state.urlStringTuples, "String", requestID);
|
||||
break;
|
||||
case MediaType.MULTIPART_FORM_DATA:
|
||||
saveTuple(state.formStringTuples, "String", requestID);
|
||||
saveTuple(state.formFileTuples, "File", requestID);
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
LoggingService.logWarning("Database error.", e, LocalDateTime.now());
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.rohitawate.everest.models.requests;
|
||||
|
||||
public class HTTPConstants {
|
||||
public static final String GET = "GET";
|
||||
public static final String POST = "POST";
|
||||
public static final String PUT = "PUT";
|
||||
public static final String PATCH = "PATCH";
|
||||
public static final String DELETE = "DELETE";
|
||||
|
||||
public static final String PLAIN_TEXT = "PLAIN TEXT";
|
||||
public static final String JSON = "JSON";
|
||||
public static final String XML = "XML";
|
||||
public static final String HTML = "HTML";
|
||||
}
|
|
@ -17,10 +17,7 @@ package com.rohitawate.everest.requestmanager;
|
|||
|
||||
import com.rohitawate.everest.exceptions.NullResponseException;
|
||||
import com.rohitawate.everest.exceptions.RedirectException;
|
||||
import com.rohitawate.everest.models.requests.DELETERequest;
|
||||
import com.rohitawate.everest.models.requests.DataRequest;
|
||||
import com.rohitawate.everest.models.requests.EverestRequest;
|
||||
import com.rohitawate.everest.models.requests.GETRequest;
|
||||
import com.rohitawate.everest.models.requests.*;
|
||||
import com.rohitawate.everest.models.responses.EverestResponse;
|
||||
import com.rohitawate.everest.settings.Settings;
|
||||
import javafx.concurrent.Service;
|
||||
|
@ -52,7 +49,7 @@ import java.util.Map;
|
|||
* Manages all the requests made through Everest.
|
||||
* Converts EverestRequests into JAX-RS Invocations, which are then processed by Jersey.
|
||||
* Also parses the ServerResponse and returns an EverestResponse.
|
||||
*
|
||||
* <p>
|
||||
* Previously, Everest used separate managers for GET, Data (POST, PUT and PATCH) and DELETE requests.
|
||||
* However, RequestManager extends JavaFX's Service class, which is expensive to create objects of.
|
||||
* This made the creation of separate pools for every kind of RequestManager too expensive, memory-wise.
|
||||
|
@ -60,7 +57,7 @@ import java.util.Map;
|
|||
* Also, this enables us to re-use inactive RequestManagers for all kinds of requests.
|
||||
* For example, previously, if a GETRequestManager was requested by Everest, and all GETRequestManagers were running,
|
||||
* a new one would be created even if a DELETERequestManager was idle.
|
||||
*
|
||||
* <p>
|
||||
* TLDR: At the cost of some reduced semantic clarity, the old, separate-for-every-type-of-request RequestManagers
|
||||
* are now replaced by this single works-for-all one to save some serious amount of memory and to facilitate better re-use.
|
||||
*/
|
||||
|
@ -234,10 +231,7 @@ public class RequestManager extends Service<EverestResponse> {
|
|||
|
||||
formData.setMediaType(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));
|
||||
invocation = getInvocation(MediaType.MULTIPART_FORM_DATA, requestType, formData, requestBuilder);
|
||||
break;
|
||||
case MediaType.APPLICATION_OCTET_STREAM:
|
||||
if (overriddenContentType == null)
|
||||
|
@ -252,10 +246,7 @@ public class RequestManager extends Service<EverestResponse> {
|
|||
|
||||
FileInputStream stream = new FileInputStream(filePath);
|
||||
|
||||
if (requestType.equals("POST"))
|
||||
invocation = requestBuilder.buildPost(Entity.entity(stream, overriddenContentType));
|
||||
else
|
||||
invocation = requestBuilder.buildPut(Entity.entity(stream, overriddenContentType));
|
||||
invocation = getInvocation(overriddenContentType, requestType, stream, requestBuilder);
|
||||
break;
|
||||
case MediaType.APPLICATION_FORM_URLENCODED:
|
||||
if (overriddenContentType == null)
|
||||
|
@ -268,10 +259,7 @@ public class RequestManager extends Service<EverestResponse> {
|
|||
form.param(mapEntry.getKey(), mapEntry.getValue());
|
||||
}
|
||||
|
||||
if (requestType.equals("POST"))
|
||||
invocation = requestBuilder.buildPost(Entity.entity(form, overriddenContentType));
|
||||
else
|
||||
invocation = requestBuilder.buildPut(Entity.entity(form, overriddenContentType));
|
||||
invocation = getInvocation(overriddenContentType, requestType, form, requestBuilder);
|
||||
break;
|
||||
default:
|
||||
// Handles raw data types (JSON, Plain text, XML, HTML)
|
||||
|
@ -279,21 +267,37 @@ public class RequestManager extends Service<EverestResponse> {
|
|||
if (overriddenContentType == null)
|
||||
overriddenContentType = originalContentType;
|
||||
switch (requestType) {
|
||||
case "POST":
|
||||
case HTTPConstants.POST:
|
||||
invocation = requestBuilder
|
||||
.buildPost(Entity.entity(dataRequest.getBody(), overriddenContentType));
|
||||
break;
|
||||
case "PUT":
|
||||
case HTTPConstants.PUT:
|
||||
invocation = requestBuilder
|
||||
.buildPut(Entity.entity(dataRequest.getBody(), overriddenContentType));
|
||||
break;
|
||||
case "PATCH":
|
||||
case HTTPConstants.PATCH:
|
||||
invocation = requestBuilder
|
||||
.build("PATCH", Entity.entity(dataRequest.getBody(), overriddenContentType));
|
||||
.build(HTTPConstants.PATCH, Entity.entity(dataRequest.getBody(), overriddenContentType));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return invocation;
|
||||
}
|
||||
|
||||
private static Invocation getInvocation(String overriddenContentType, String requestType, Object entity, Builder requestBuilder) {
|
||||
Invocation invocation;
|
||||
switch (requestType) {
|
||||
case HTTPConstants.POST:
|
||||
invocation = requestBuilder.buildPost(Entity.entity(entity, overriddenContentType));
|
||||
break;
|
||||
case HTTPConstants.PUT:
|
||||
invocation = requestBuilder.buildPut(Entity.entity(entity, overriddenContentType));
|
||||
break;
|
||||
default:
|
||||
invocation = requestBuilder.build(HTTPConstants.PATCH, Entity.entity(entity, overriddenContentType));
|
||||
break;
|
||||
}
|
||||
return invocation;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package com.rohitawate.everest.state;
|
||||
|
||||
import com.rohitawate.everest.controllers.BodyTabController.BodyTab;
|
||||
import com.rohitawate.everest.models.requests.HTTPConstants;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -27,14 +28,15 @@ public class ComposerState {
|
|||
public BodyTab visibleBodyTab;
|
||||
|
||||
public String target;
|
||||
|
||||
public String httpMethod;
|
||||
|
||||
public ArrayList<FieldState> params;
|
||||
public ArrayList<FieldState> headers;
|
||||
public String contentType;
|
||||
|
||||
// Body and content-type of requests with raw bodies
|
||||
public String rawBody;
|
||||
public String rawBodyContentType;
|
||||
public String rawBodyBoxValue;
|
||||
|
||||
// Tuples of URL-encoded requests
|
||||
public ArrayList<FieldState> urlStringTuples;
|
||||
|
@ -47,6 +49,6 @@ public class ComposerState {
|
|||
public String binaryFilePath;
|
||||
|
||||
public ComposerState() {
|
||||
this.httpMethod = "GET";
|
||||
this.httpMethod = HTTPConstants.GET;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue