Add HTTPConstants class and partial improvements to HistoryManager

This commit is contained in:
Rohit Awate 2018-08-13 18:06:45 +05:30
parent 0be0fbce41
commit 42bb97eb42
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
8 changed files with 142 additions and 122 deletions

View file

@ -20,6 +20,7 @@ import com.rohitawate.everest.controllers.codearea.EverestCodeArea;
import com.rohitawate.everest.controllers.codearea.highlighters.HighlighterFactory; import com.rohitawate.everest.controllers.codearea.highlighters.HighlighterFactory;
import com.rohitawate.everest.logging.LoggingService; import com.rohitawate.everest.logging.LoggingService;
import com.rohitawate.everest.misc.ThemeManager; import com.rohitawate.everest.misc.ThemeManager;
import com.rohitawate.everest.models.requests.HTTPConstants;
import com.rohitawate.everest.state.ComposerState; import com.rohitawate.everest.state.ComposerState;
import com.rohitawate.everest.state.FieldState; import com.rohitawate.everest.state.FieldState;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -68,7 +69,12 @@ public class BodyTabController implements Initializable {
@Override @Override
public void initialize(URL location, ResourceBundle resources) { 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); rawInputTypeBox.getSelectionModel().select(0);
rawInputArea = new EverestCodeArea(); rawInputArea = new EverestCodeArea();
@ -79,7 +85,7 @@ public class BodyTabController implements Initializable {
rawInputTypeBox.valueProperty().addListener(change -> { rawInputTypeBox.valueProperty().addListener(change -> {
String type = rawInputTypeBox.getValue(); String type = rawInputTypeBox.getValue();
if (type.equals("JSON")) { if (type.equals(HTTPConstants.PLAIN_TEXT)) {
rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(type)); rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(type));
return; return;
} }
@ -128,27 +134,31 @@ public class BodyTabController implements Initializable {
state.rawBody = rawInputArea.getText(); state.rawBody = rawInputArea.getText();
switch (rawInputTypeBox.getValue()) { switch (rawInputTypeBox.getValue()) {
case "JSON": case HTTPConstants.JSON:
state.rawBodyContentType = MediaType.APPLICATION_JSON; state.rawBodyBoxValue = MediaType.APPLICATION_JSON;
break; break;
case "XML": case HTTPConstants.XML:
state.rawBodyContentType = MediaType.APPLICATION_XML; state.rawBodyBoxValue = MediaType.APPLICATION_XML;
break; break;
case "HTML": case HTTPConstants.HTML:
state.rawBodyContentType = MediaType.TEXT_HTML; state.rawBodyBoxValue = MediaType.TEXT_HTML;
break; break;
default: default:
state.rawBodyContentType = MediaType.TEXT_PLAIN; state.rawBodyBoxValue = MediaType.TEXT_PLAIN;
} }
if (rawTab.isSelected()) { if (rawTab.isSelected()) {
state.visibleBodyTab = BodyTab.RAW; state.visibleBodyTab = BodyTab.RAW;
state.contentType = state.rawBodyBoxValue;
} else if (formTab.isSelected()) { } else if (formTab.isSelected()) {
state.visibleBodyTab = BodyTab.FORM; state.visibleBodyTab = BodyTab.FORM;
state.contentType = MediaType.MULTIPART_FORM_DATA;
} else if (urlTab.isSelected()) { } else if (urlTab.isSelected()) {
state.visibleBodyTab = BodyTab.URL; state.visibleBodyTab = BodyTab.URL;
state.contentType = MediaType.APPLICATION_FORM_URLENCODED;
} else { } else {
state.visibleBodyTab = BodyTab.BINARY; state.visibleBodyTab = BodyTab.BINARY;
state.contentType = MediaType.APPLICATION_OCTET_STREAM;
} }
return state; return state;
@ -200,32 +210,32 @@ public class BodyTabController implements Initializable {
urlTabController.clear(); urlTabController.clear();
formDataTabController.clear(); formDataTabController.clear();
rawInputArea.clear(); rawInputArea.clear();
rawInputTypeBox.setValue("PLAIN TEXT"); rawInputTypeBox.setValue(HTTPConstants.PLAIN_TEXT);
filePathField.clear(); filePathField.clear();
} }
private void setRawTab(ComposerState state) { private void setRawTab(ComposerState state) {
if (state.rawBodyContentType != null && state.rawBody != null) { if (state.rawBodyBoxValue != null && state.rawBody != null) {
// TODO: Remove this conversion // TODO: Remove this conversion
String simplifiedContentType; String simplifiedContentType;
switch (state.rawBodyContentType) { switch (state.rawBodyBoxValue) {
case MediaType.APPLICATION_JSON: case MediaType.APPLICATION_JSON:
simplifiedContentType = "JSON"; simplifiedContentType = HTTPConstants.JSON;
break; break;
case MediaType.APPLICATION_XML: case MediaType.APPLICATION_XML:
simplifiedContentType = "XML"; simplifiedContentType = HTTPConstants.XML;
break; break;
case MediaType.TEXT_HTML: case MediaType.TEXT_HTML:
simplifiedContentType = "HTML"; simplifiedContentType = HTTPConstants.HTML;
break; break;
default: default:
simplifiedContentType = "PLAIN TEXT"; simplifiedContentType = HTTPConstants.PLAIN_TEXT;
} }
rawInputTypeBox.setValue(simplifiedContentType); rawInputTypeBox.setValue(simplifiedContentType);
rawInputArea.setText(state.rawBody, HighlighterFactory.getHighlighter(simplifiedContentType)); rawInputArea.setText(state.rawBody, HighlighterFactory.getHighlighter(simplifiedContentType));
} else { } else {
rawInputTypeBox.setValue("PLAIN TEXT"); rawInputTypeBox.setValue(HTTPConstants.PLAIN_TEXT);
rawInputArea.setHighlighter(HighlighterFactory.getHighlighter("PLAIN TEXT")); rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(HTTPConstants.PLAIN_TEXT));
} }
} }
} }

View file

@ -32,6 +32,7 @@ import com.rohitawate.everest.misc.ThemeManager;
import com.rohitawate.everest.models.requests.DELETERequest; import com.rohitawate.everest.models.requests.DELETERequest;
import com.rohitawate.everest.models.requests.DataRequest; import com.rohitawate.everest.models.requests.DataRequest;
import com.rohitawate.everest.models.requests.GETRequest; 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.models.responses.EverestResponse;
import com.rohitawate.everest.requestmanager.RequestManager; import com.rohitawate.everest.requestmanager.RequestManager;
import com.rohitawate.everest.requestmanager.RequestManagersPool; import com.rohitawate.everest.requestmanager.RequestManagersPool;
@ -93,7 +94,6 @@ public class DashboardController implements Initializable {
private JFXProgressBar progressBar; private JFXProgressBar progressBar;
private JFXSnackbar snackbar; private JFXSnackbar snackbar;
private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"};
private List<StringKeyValueFieldController> paramsControllers; private List<StringKeyValueFieldController> paramsControllers;
private RequestManager requestManager; private RequestManager requestManager;
private HeaderTabController headerTabController; private HeaderTabController headerTabController;
@ -144,10 +144,15 @@ public class DashboardController implements Initializable {
snackbar = new JFXSnackbar(dashboard); snackbar = new JFXSnackbar(dashboard);
showLayer(ResponseLayer.PROMPT); showLayer(ResponseLayer.PROMPT);
httpMethodBox.getItems().addAll(httpMethods); httpMethodBox.getItems().addAll(
HTTPConstants.GET,
HTTPConstants.POST,
HTTPConstants.PUT,
HTTPConstants.PATCH,
HTTPConstants.DELETE);
// Select GET by default // Select GET by default
httpMethodBox.getSelectionModel().select("GET"); httpMethodBox.getSelectionModel().select(HTTPConstants.GET);
paramsControllers = new ArrayList<>(); paramsControllers = new ArrayList<>();
paramsCountProperty = new SimpleIntegerProperty(0); paramsCountProperty = new SimpleIntegerProperty(0);
@ -155,8 +160,8 @@ public class DashboardController implements Initializable {
addParamField(); // Adds a blank param field addParamField(); // Adds a blank param field
bodyTab.disableProperty().bind( bodyTab.disableProperty().bind(
Bindings.or(httpMethodBox.valueProperty().isEqualTo("GET"), Bindings.or(httpMethodBox.valueProperty().isEqualTo(HTTPConstants.GET),
httpMethodBox.valueProperty().isEqualTo("DELETE"))); httpMethodBox.valueProperty().isEqualTo(HTTPConstants.DELETE)));
// Disabling Ctrl+Tab navigation // Disabling Ctrl+Tab navigation
requestOptionsTab.setOnKeyPressed(e -> { requestOptionsTab.setOnKeyPressed(e -> {
@ -172,7 +177,11 @@ public class DashboardController implements Initializable {
snackbar.show("Response body copied to clipboard.", 5000); 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 -> { responseTypeBox.valueProperty().addListener(change -> {
String type = responseTypeBox.getValue(); String type = responseTypeBox.getValue();
@ -231,7 +240,7 @@ public class DashboardController implements Initializable {
addressField.setText(address); addressField.setText(address);
switch (httpMethodBox.getValue()) { switch (httpMethodBox.getValue()) {
case "GET": case HTTPConstants.GET:
if (getRequest == null) if (getRequest == null)
getRequest = new GETRequest(); getRequest = new GETRequest();
@ -241,9 +250,9 @@ public class DashboardController implements Initializable {
requestManager = RequestManagersPool.manager(); requestManager = RequestManagersPool.manager();
requestManager.setRequest(getRequest); requestManager.setRequest(getRequest);
break; break;
case "POST": case HTTPConstants.POST:
case "PUT": case HTTPConstants.PUT:
case "PATCH": case HTTPConstants.PATCH:
if (dataRequest == null) if (dataRequest == null)
dataRequest = new DataRequest(); dataRequest = new DataRequest();
@ -286,7 +295,7 @@ public class DashboardController implements Initializable {
requestManager = RequestManagersPool.manager(); requestManager = RequestManagersPool.manager();
requestManager.setRequest(dataRequest); requestManager.setRequest(dataRequest);
break; break;
case "DELETE": case HTTPConstants.DELETE:
if (deleteRequest == null) if (deleteRequest == null)
deleteRequest = new DELETERequest(); deleteRequest = new DELETERequest();
@ -644,9 +653,9 @@ public class DashboardController implements Initializable {
ComposerState composerState; ComposerState composerState;
switch (httpMethodBox.getValue()) { switch (httpMethodBox.getValue()) {
case "POST": case HTTPConstants.POST:
case "PUT": case HTTPConstants.PUT:
case "PATCH": case HTTPConstants.PATCH:
composerState = bodyTabController.getState(); composerState = bodyTabController.getState();
break; break;
default: default:
@ -781,6 +790,8 @@ public class DashboardController implements Initializable {
This is an extreme case, but still something to be taken care of. This is an extreme case, but still something to be taken care of.
*/ */
boolean validMethod = false; boolean validMethod = false;
String[] httpMethods =
{HTTPConstants.GET, HTTPConstants.POST, HTTPConstants.PUT, HTTPConstants.PATCH, HTTPConstants.DELETE};
for (String method : httpMethods) { for (String method : httpMethods) {
if (method.equals(state.composer.httpMethod)) if (method.equals(state.composer.httpMethod))
validMethod = true; validMethod = true;
@ -806,12 +817,12 @@ public class DashboardController implements Initializable {
addParamField(fieldState); 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); bodyTabController.setState(state.composer);
} }
void reset() { void reset() {
httpMethodBox.setValue("GET"); httpMethodBox.setValue(HTTPConstants.GET);
addressField.clear(); addressField.clear();
headerTabController.clear(); headerTabController.clear();
clearParams(); clearParams();

View file

@ -18,6 +18,7 @@ package com.rohitawate.everest.controllers;
import com.rohitawate.everest.controllers.search.Searchable; import com.rohitawate.everest.controllers.search.Searchable;
import com.rohitawate.everest.logging.LoggingService; import com.rohitawate.everest.logging.LoggingService;
import com.rohitawate.everest.models.requests.HTTPConstants;
import com.rohitawate.everest.state.ComposerState; import com.rohitawate.everest.state.ComposerState;
import com.rohitawate.everest.state.FieldState; import com.rohitawate.everest.state.FieldState;
import javafx.fxml.FXML; import javafx.fxml.FXML;
@ -55,13 +56,14 @@ public class HistoryItemController implements Initializable, Searchable<Composer
} }
public int getRelativityIndex(String searchString) { public int getRelativityIndex(String searchString) {
int index = 0;
searchString = searchString.toLowerCase(); searchString = searchString.toLowerCase();
String comparisonString; String comparisonString;
// Checks if matches with target // Checks if matches with target
comparisonString = state.target.toLowerCase(); comparisonString = state.target.toLowerCase();
if (comparisonString.contains(searchString)) if (comparisonString.contains(searchString))
return 10; index += 10;
try { try {
URL url = new URL(state.target); URL url = new URL(state.target);
@ -69,12 +71,12 @@ public class HistoryItemController implements Initializable, Searchable<Composer
// Checks if matches with target's hostname // Checks if matches with target's hostname
comparisonString = url.getHost().toLowerCase(); comparisonString = url.getHost().toLowerCase();
if (comparisonString.contains(searchString)) if (comparisonString.contains(searchString))
return 10; index += 10;
// Checks if matches with target's path // Checks if matches with target's path
comparisonString = url.getPath().toLowerCase(); comparisonString = url.getPath().toLowerCase();
if (comparisonString.contains(searchString)) if (comparisonString.contains(searchString))
return 9; index += 9;
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
LoggingService.logInfo("Failed to parse URL while calculating relativity index.", LocalDateTime.now()); 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 // Checks if matches with HTTP method
comparisonString = state.httpMethod.toLowerCase(); comparisonString = state.httpMethod.toLowerCase();
if (comparisonString.contains(searchString)) if (comparisonString.contains(searchString))
return 7; index += 7;
// Checks for a match in the params // Checks for a match in the params
for (FieldState state : state.params) { for (FieldState state : state.params) {
if (state.key.toLowerCase().contains(searchString) || if (state.key.toLowerCase().contains(searchString) ||
state.value.toLowerCase().contains(searchString)) state.value.toLowerCase().contains(searchString))
return 5; index += 5;
} }
// Checks for a match in the headers // Checks for a match in the headers
for (FieldState state : state.headers) { for (FieldState state : state.headers) {
if (state.key.toLowerCase().contains(searchString) || if (state.key.toLowerCase().contains(searchString) ||
state.value.toLowerCase().contains(searchString)) state.value.toLowerCase().contains(searchString))
return 6; index += 6;
} }
if (state.httpMethod.equals("POST") || state.httpMethod.equals("PUT")) { if (!(state.httpMethod.equals(HTTPConstants.GET) || state.httpMethod.equals(HTTPConstants.DELETE))) {
switch (state.rawBodyContentType) { switch (state.contentType) {
case MediaType.TEXT_PLAIN: case MediaType.TEXT_PLAIN:
case MediaType.APPLICATION_JSON: case MediaType.APPLICATION_JSON:
case MediaType.APPLICATION_XML: case MediaType.APPLICATION_XML:
@ -108,14 +110,14 @@ public class HistoryItemController implements Initializable, Searchable<Composer
// Checks for match in rawBody of the request // Checks for match in rawBody of the request
comparisonString = state.rawBody.toLowerCase(); comparisonString = state.rawBody.toLowerCase();
if (comparisonString.contains(searchString)) if (comparisonString.contains(searchString))
return 8; index += 8;
break; break;
case MediaType.APPLICATION_FORM_URLENCODED: case MediaType.APPLICATION_FORM_URLENCODED:
// Checks for match in string tuples // Checks for match in string tuples
for (FieldState state : state.urlStringTuples) { for (FieldState state : state.urlStringTuples) {
if (state.key.toLowerCase().contains(searchString) || if (state.key.toLowerCase().contains(searchString) ||
state.value.toLowerCase().contains(searchString)) state.value.toLowerCase().contains(searchString))
return 8; index += 8;
} }
break; break;
case MediaType.MULTIPART_FORM_DATA: case MediaType.MULTIPART_FORM_DATA:
@ -123,17 +125,17 @@ public class HistoryItemController implements Initializable, Searchable<Composer
for (FieldState state : state.formStringTuples) { for (FieldState state : state.formStringTuples) {
if (state.key.toLowerCase().contains(searchString) || if (state.key.toLowerCase().contains(searchString) ||
state.value.toLowerCase().contains(searchString)) state.value.toLowerCase().contains(searchString))
return 8; index += 8;
} }
for (FieldState state : state.formFileTuples) { for (FieldState state : state.formFileTuples) {
if (state.key.toLowerCase().contains(searchString) || if (state.key.toLowerCase().contains(searchString) ||
state.value.toLowerCase().contains(searchString)) state.value.toLowerCase().contains(searchString))
return 8; index += 8;
} }
break; break;
} }
} }
return 0; return index;
} }
} }

View file

@ -21,6 +21,7 @@ import com.rohitawate.everest.logging.LoggingService;
import com.rohitawate.everest.misc.EverestUtilities; import com.rohitawate.everest.misc.EverestUtilities;
import com.rohitawate.everest.misc.KeyMap; import com.rohitawate.everest.misc.KeyMap;
import com.rohitawate.everest.misc.ThemeManager; import com.rohitawate.everest.misc.ThemeManager;
import com.rohitawate.everest.models.requests.HTTPConstants;
import com.rohitawate.everest.state.ComposerState; import com.rohitawate.everest.state.ComposerState;
import com.rohitawate.everest.state.DashboardState; import com.rohitawate.everest.state.DashboardState;
import javafx.application.Platform; import javafx.application.Platform;
@ -302,7 +303,7 @@ public class HomeWindowController implements Initializable {
dashboard.requestOptionsTab.getSelectionModel().select(dashboard.headersTab); dashboard.requestOptionsTab.getSelectionModel().select(dashboard.headersTab);
} else if (KeyMap.focusBody.match(e)) { } else if (KeyMap.focusBody.match(e)) {
String httpMethod = dashboard.httpMethodBox.getValue(); 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); dashboard.requestOptionsTab.getSelectionModel().select(dashboard.bodyTab);
} }
} else if (KeyMap.refreshTheme.match(e)) { } else if (KeyMap.refreshTheme.match(e)) {

View file

@ -21,6 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.rohitawate.everest.logging.LoggingService; import com.rohitawate.everest.logging.LoggingService;
import com.rohitawate.everest.misc.EverestUtilities; import com.rohitawate.everest.misc.EverestUtilities;
import com.rohitawate.everest.misc.Services; import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.models.requests.HTTPConstants;
import com.rohitawate.everest.settings.Settings; import com.rohitawate.everest.settings.Settings;
import com.rohitawate.everest.state.ComposerState; import com.rohitawate.everest.state.ComposerState;
import com.rohitawate.everest.state.FieldState; import com.rohitawate.everest.state.FieldState;
@ -132,7 +133,7 @@ public class HistoryManager {
state.params = getTuples(requestID, "Param"); state.params = getTuples(requestID, "Param");
state.httpMethod = resultSet.getString("Type"); 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 // Retrieves request body ContentType for querying corresponding table
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestContentType").toString())); statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestContentType").toString()));
statement.setInt(1, requestID); statement.setInt(1, requestID);
@ -143,31 +144,19 @@ public class HistoryManager {
if (RS.next()) if (RS.next())
contentType = RS.getString("ContentType"); contentType = RS.getString("ContentType");
state.rawBodyContentType = contentType; state.contentType = contentType;
// Retrieves body from corresponding table statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestBody").toString()));
switch (contentType) { statement.setInt(1, requestID);
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);
RS = statement.executeQuery(); RS = statement.executeQuery();
if (RS.next()) if (RS.next())
state.rawBody = RS.getString("Body"); state.rawBody = RS.getString("Body");
break;
case MediaType.APPLICATION_FORM_URLENCODED: state.urlStringTuples = getTuples(requestID, "String");
state.urlStringTuples = getTuples(requestID, "String"); state.formStringTuples = getTuples(requestID, "String");
break; state.formFileTuples = getTuples(requestID, "File");
case MediaType.MULTIPART_FORM_DATA:
state.formStringTuples = getTuples(requestID, "String");
state.formFileTuples = getTuples(requestID, "File");
break;
}
} }
history.add(state); history.add(state);
@ -273,7 +262,7 @@ public class HistoryManager {
if (!areListsEqual(states, newState.params)) if (!areListsEqual(states, newState.params))
return false; 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 = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestContentType").toString()));
statement.setInt(1, lastRequestID); statement.setInt(1, lastRequestID);
@ -283,10 +272,10 @@ public class HistoryManager {
if (RS.next()) if (RS.next())
previousContentType = RS.getString("ContentType"); previousContentType = RS.getString("ContentType");
if (!newState.rawBodyContentType.equals(previousContentType)) if (!newState.contentType.equals(previousContentType))
return false; return false;
switch (newState.rawBodyContentType) { switch (newState.contentType) {
case MediaType.TEXT_PLAIN: case MediaType.TEXT_PLAIN:
case MediaType.APPLICATION_JSON: case MediaType.APPLICATION_JSON:
case MediaType.APPLICATION_XML: case MediaType.APPLICATION_XML:
@ -388,35 +377,22 @@ public class HistoryManager {
// Saves request parameters // Saves request parameters
saveTuple(state.params, "Param", requestID); 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 // Maps the request to its ContentType for faster retrieval
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveRequestContentPair").toString())); statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveRequestContentPair").toString()));
statement.setInt(1, requestID); statement.setInt(1, requestID);
statement.setString(2, state.rawBodyContentType); statement.setString(2, state.contentType);
statement.executeUpdate(); statement.executeUpdate();
// Determines where to fetch the body from, based on the ContentType statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveBody").toString()));
switch (state.rawBodyContentType) { statement.setInt(1, requestID);
case MediaType.TEXT_PLAIN: statement.setString(2, state.rawBody);
case MediaType.APPLICATION_JSON: statement.executeUpdate();
case MediaType.APPLICATION_XML:
case MediaType.TEXT_HTML: saveTuple(state.urlStringTuples, "String", requestID);
case MediaType.APPLICATION_OCTET_STREAM: saveTuple(state.formStringTuples, "String", requestID);
// Saves the body in case of raw content, or the file location in case of binary saveTuple(state.formFileTuples, "File", requestID);
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) { } catch (SQLException e) {
LoggingService.logWarning("Database error.", e, LocalDateTime.now()); LoggingService.logWarning("Database error.", e, LocalDateTime.now());

View file

@ -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";
}

View file

@ -17,10 +17,7 @@ package com.rohitawate.everest.requestmanager;
import com.rohitawate.everest.exceptions.NullResponseException; import com.rohitawate.everest.exceptions.NullResponseException;
import com.rohitawate.everest.exceptions.RedirectException; import com.rohitawate.everest.exceptions.RedirectException;
import com.rohitawate.everest.models.requests.DELETERequest; import com.rohitawate.everest.models.requests.*;
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.responses.EverestResponse; import com.rohitawate.everest.models.responses.EverestResponse;
import com.rohitawate.everest.settings.Settings; import com.rohitawate.everest.settings.Settings;
import javafx.concurrent.Service; import javafx.concurrent.Service;
@ -52,7 +49,7 @@ import java.util.Map;
* Manages all the requests made through Everest. * Manages all the requests made through Everest.
* Converts EverestRequests into JAX-RS Invocations, which are then processed by Jersey. * Converts EverestRequests into JAX-RS Invocations, which are then processed by Jersey.
* Also parses the ServerResponse and returns an EverestResponse. * Also parses the ServerResponse and returns an EverestResponse.
* * <p>
* Previously, Everest used separate managers for GET, Data (POST, PUT and PATCH) and DELETE requests. * 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. * 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. * 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. * 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, * 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. * 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 * 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. * 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); formData.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
if (requestType.equals("POST")) invocation = getInvocation(MediaType.MULTIPART_FORM_DATA, requestType, formData, requestBuilder);
invocation = requestBuilder.buildPost(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE));
else
invocation = requestBuilder.buildPut(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE));
break; break;
case MediaType.APPLICATION_OCTET_STREAM: case MediaType.APPLICATION_OCTET_STREAM:
if (overriddenContentType == null) if (overriddenContentType == null)
@ -252,10 +246,7 @@ public class RequestManager extends Service<EverestResponse> {
FileInputStream stream = new FileInputStream(filePath); FileInputStream stream = new FileInputStream(filePath);
if (requestType.equals("POST")) invocation = getInvocation(overriddenContentType, requestType, stream, requestBuilder);
invocation = requestBuilder.buildPost(Entity.entity(stream, overriddenContentType));
else
invocation = requestBuilder.buildPut(Entity.entity(stream, overriddenContentType));
break; break;
case MediaType.APPLICATION_FORM_URLENCODED: case MediaType.APPLICATION_FORM_URLENCODED:
if (overriddenContentType == null) if (overriddenContentType == null)
@ -268,10 +259,7 @@ public class RequestManager extends Service<EverestResponse> {
form.param(mapEntry.getKey(), mapEntry.getValue()); form.param(mapEntry.getKey(), mapEntry.getValue());
} }
if (requestType.equals("POST")) invocation = getInvocation(overriddenContentType, requestType, form, requestBuilder);
invocation = requestBuilder.buildPost(Entity.entity(form, overriddenContentType));
else
invocation = requestBuilder.buildPut(Entity.entity(form, overriddenContentType));
break; break;
default: default:
// Handles raw data types (JSON, Plain text, XML, HTML) // Handles raw data types (JSON, Plain text, XML, HTML)
@ -279,21 +267,37 @@ public class RequestManager extends Service<EverestResponse> {
if (overriddenContentType == null) if (overriddenContentType == null)
overriddenContentType = originalContentType; overriddenContentType = originalContentType;
switch (requestType) { switch (requestType) {
case "POST": case HTTPConstants.POST:
invocation = requestBuilder invocation = requestBuilder
.buildPost(Entity.entity(dataRequest.getBody(), overriddenContentType)); .buildPost(Entity.entity(dataRequest.getBody(), overriddenContentType));
break; break;
case "PUT": case HTTPConstants.PUT:
invocation = requestBuilder invocation = requestBuilder
.buildPut(Entity.entity(dataRequest.getBody(), overriddenContentType)); .buildPut(Entity.entity(dataRequest.getBody(), overriddenContentType));
break; break;
case "PATCH": case HTTPConstants.PATCH:
invocation = requestBuilder invocation = requestBuilder
.build("PATCH", Entity.entity(dataRequest.getBody(), overriddenContentType)); .build(HTTPConstants.PATCH, Entity.entity(dataRequest.getBody(), overriddenContentType));
break; break;
} }
} }
return invocation; 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;
}
} }

View file

@ -17,6 +17,7 @@
package com.rohitawate.everest.state; package com.rohitawate.everest.state;
import com.rohitawate.everest.controllers.BodyTabController.BodyTab; import com.rohitawate.everest.controllers.BodyTabController.BodyTab;
import com.rohitawate.everest.models.requests.HTTPConstants;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,14 +28,15 @@ public class ComposerState {
public BodyTab visibleBodyTab; public BodyTab visibleBodyTab;
public String target; public String target;
public String httpMethod; public String httpMethod;
public ArrayList<FieldState> params; public ArrayList<FieldState> params;
public ArrayList<FieldState> headers; public ArrayList<FieldState> headers;
public String contentType;
// Body and content-type of requests with raw bodies // Body and content-type of requests with raw bodies
public String rawBody; public String rawBody;
public String rawBodyContentType; public String rawBodyBoxValue;
// Tuples of URL-encoded requests // Tuples of URL-encoded requests
public ArrayList<FieldState> urlStringTuples; public ArrayList<FieldState> urlStringTuples;
@ -47,6 +49,6 @@ public class ComposerState {
public String binaryFilePath; public String binaryFilePath;
public ComposerState() { public ComposerState() {
this.httpMethod = "GET"; this.httpMethod = HTTPConstants.GET;
} }
} }