Rewrite DashboardState to save all tabs in BodyTab and not just the active ones

This commit is contained in:
Rohit Awate 2018-06-29 11:08:26 +05:30
parent 1efaf40a3c
commit 5c7b7b2a43
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
10 changed files with 237 additions and 227 deletions

View file

@ -20,7 +20,6 @@ import com.rohitawate.everest.controllers.codearea.EverestCodeArea;
import com.rohitawate.everest.controllers.codearea.EverestCodeArea.HighlightMode;
import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.misc.ThemeManager;
import com.rohitawate.everest.models.DashboardState;
import com.rohitawate.everest.models.requests.DataDispatchRequest;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
@ -28,7 +27,6 @@ import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
@ -39,7 +37,7 @@ import javax.ws.rs.core.MediaType;
import java.io.IOException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ResourceBundle;
/*
@ -48,8 +46,6 @@ import java.util.ResourceBundle;
URL encoded and Form tabs have special FXMLs.
*/
public class BodyTabController implements Initializable {
@FXML
private TabPane bodyTabPane;
@FXML
ComboBox<String> rawInputTypeBox;
@FXML
@ -108,7 +104,7 @@ public class BodyTabController implements Initializable {
}
/**
* Returns a EverestRequest object initialized with the request body.
* Returns a EverestRequest object initialized with the request rawBody.
*/
public DataDispatchRequest getBasicRequest(String requestType) {
DataDispatchRequest request = new DataDispatchRequest(requestType);
@ -136,14 +132,14 @@ public class BodyTabController implements Initializable {
request.setContentType(contentType);
request.setBody(rawInputArea.getText());
} else if (formTab.isSelected()) {
request.setStringTuples(formDataTabController.getStringTuples());
request.setFileTuples(formDataTabController.getFileTuples());
request.setStringTuples(formDataTabController.getStringTuples(true));
request.setFileTuples(formDataTabController.getFileTuples(true));
request.setContentType(MediaType.MULTIPART_FORM_DATA);
} else if (binaryTab.isSelected()) {
request.setBody(filePathField.getText());
request.setContentType(MediaType.APPLICATION_OCTET_STREAM);
} else if (urlTab.isSelected()) {
request.setStringTuples(urlTabController.getStringTuples());
request.setStringTuples(urlTabController.getStringTuples(true));
request.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
}
return request;
@ -155,73 +151,87 @@ public class BodyTabController implements Initializable {
fileChooser.setTitle("Choose a binary file to add to request...");
Window dashboardWindow = filePathField.getScene().getWindow();
String filePath;
try {
filePath = fileChooser.showOpenDialog(dashboardWindow).getAbsolutePath();
} catch (NullPointerException NPE) {
filePath = "";
}
filePathField.setText(filePath);
}
public void setState(DashboardState dashboardState) {
try {
switch (dashboardState.getContentType()) {
case MediaType.TEXT_PLAIN:
setRawTab(dashboardState, "PLAIN TEXT");
break;
case MediaType.APPLICATION_JSON:
setRawTab(dashboardState, "JSON");
break;
case MediaType.APPLICATION_XML:
setRawTab(dashboardState, "XML");
break;
case MediaType.TEXT_HTML:
setRawTab(dashboardState, "HTML");
break;
case MediaType.MULTIPART_FORM_DATA:
// For file tuples
for (Map.Entry entry : dashboardState.getFileTuples().entrySet())
formDataTabController.addFileField(entry.getKey().toString(), entry.getValue().toString());
public DashboardState getState() {
DashboardState state = new DashboardState();
// For string tuples
for (Map.Entry entry : dashboardState.getStringTuples().entrySet())
formDataTabController.addStringField(entry.getKey().toString(), entry.getValue().toString());
bodyTabPane.getSelectionModel().select(formTab);
state.rawBodyType = rawInputTypeBox.getValue();
state.rawBody = rawInputArea.getText();
state.urlStringTuples = urlTabController.getStringTuples(false);
state.formStringTuples = formDataTabController.getStringTuples(false);
state.formFileTuples = formDataTabController.getFileTuples(false);
state.binaryFilePath = filePathField.getText();
if (rawTab.isSelected()) {
switch (rawInputTypeBox.getValue()) {
case "JSON":
state.contentType = MediaType.APPLICATION_JSON;
break;
case MediaType.APPLICATION_OCTET_STREAM:
filePathField.setText(dashboardState.getBody());
bodyTabPane.getSelectionModel().select(binaryTab);
case "XML":
state.contentType = MediaType.APPLICATION_XML;
break;
case MediaType.APPLICATION_FORM_URLENCODED:
for (Map.Entry entry : dashboardState.getStringTuples().entrySet())
urlTabController.addField(entry.getKey().toString(), entry.getValue().toString());
bodyTabPane.getSelectionModel().select(urlTab);
case "HTML":
state.contentType = MediaType.TEXT_HTML;
break;
default:
state.contentType = MediaType.TEXT_PLAIN;
}
} catch (NullPointerException NPE) {
Services.loggingService.logInfo("Dashboard loaded with blank request body.", LocalDateTime.now());
} else if (formTab.isSelected()) {
state.contentType = MediaType.MULTIPART_FORM_DATA;
} else if (urlTab.isSelected()) {
state.contentType = MediaType.APPLICATION_FORM_URLENCODED;
} else {
state.contentType = MediaType.APPLICATION_OCTET_STREAM;
}
return state;
}
private void setRawTab(DashboardState dashboardState, String contentType) {
public void setState(DashboardState state) {
// Adding Form tab's string tuples
for (Entry<String, String> entry : state.formStringTuples.entrySet())
formDataTabController.addStringField(entry.getKey(), entry.getValue());
// Adding Form tab's file tuples
for (Entry<String, String> entry : state.formFileTuples.entrySet())
formDataTabController.addFileField(entry.getKey(), entry.getValue());
// Adding URL tab's tuples
for (Entry<String, String> entry : state.urlStringTuples.entrySet())
urlTabController.addField(entry.getKey(), entry.getValue());
setRawTab(state);
filePathField.setText(state.binaryFilePath);
}
private void setRawTab(DashboardState state) {
HighlightMode mode;
switch (contentType) {
case MediaType.APPLICATION_JSON:
switch (state.rawBodyType) {
case "JSON":
mode = HighlightMode.JSON;
break;
case MediaType.APPLICATION_XML:
case "XML":
mode = HighlightMode.XML;
break;
case MediaType.TEXT_HTML:
case "HTML":
mode = HighlightMode.HTML;
break;
default:
mode = HighlightMode.PLAIN;
}
rawInputArea.setText(dashboardState.getBody(), mode);
rawInputTypeBox.getSelectionModel().select(contentType);
bodyTabPane.getSelectionModel().select(rawTab);
rawInputArea.setText(state.rawBody, mode);
rawInputTypeBox.getSelectionModel().select(state.rawBodyType);
}
}

View file

@ -26,7 +26,6 @@ import com.rohitawate.everest.exceptions.UnreliableResponseException;
import com.rohitawate.everest.misc.EverestUtilities;
import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.misc.ThemeManager;
import com.rohitawate.everest.models.DashboardState;
import com.rohitawate.everest.models.requests.DELETERequest;
import com.rohitawate.everest.models.requests.DataDispatchRequest;
import com.rohitawate.everest.models.requests.GETRequest;
@ -114,14 +113,14 @@ public class DashboardController implements Initializable {
headerTabController = headerTabLoader.getController();
headersTab.setContent(headerTabContent);
// Loading the body tab
// Loading the rawBody tab
FXMLLoader bodyTabLoader = new FXMLLoader(getClass().getResource("/fxml/homewindow/BodyTab.fxml"));
Parent bodyTabContent = bodyTabLoader.load();
ThemeManager.setTheme(bodyTabContent);
bodyTabController = bodyTabLoader.getController();
bodyTab.setContent(bodyTabContent);
} catch (IOException e) {
Services.loggingService.logSevere("Could not load headers/body tabs.", e, LocalDateTime.now());
Services.loggingService.logSevere("Could not load headers/rawBody tabs.", e, LocalDateTime.now());
}
snackbar = new JFXSnackbar(dashboard);
@ -154,7 +153,7 @@ public class DashboardController implements Initializable {
responseArea.selectAll();
responseArea.copy();
responseArea.deselect();
snackbar.show("Response body copied to clipboard.", 5000);
snackbar.show("Response rawBody copied to clipboard.", 5000);
});
responseTypeBox.getItems().addAll("JSON", "XML", "HTML", "PLAIN TEXT");
@ -233,7 +232,7 @@ public class DashboardController implements Initializable {
getRequest = new GETRequest();
getRequest.setTarget(address);
getRequest.setHeaders(headerTabController.getSelectedHeaders());
getRequest.setHeaders(headerTabController.getHeaders(true));
requestManager = Services.pool.get();
requestManager.setRequest(getRequest);
@ -250,7 +249,7 @@ public class DashboardController implements Initializable {
dataRequest.setRequestType(httpMethodBox.getValue());
dataRequest.setTarget(address);
dataRequest.setHeaders(headerTabController.getSelectedHeaders());
dataRequest.setHeaders(headerTabController.getHeaders(true));
if (bodyTabController.rawTab.isSelected()) {
String contentType;
@ -273,14 +272,14 @@ public class DashboardController implements Initializable {
dataRequest.setContentType(contentType);
dataRequest.setBody(bodyTabController.rawInputArea.getText());
} else if (bodyTabController.formTab.isSelected()) {
dataRequest.setStringTuples(bodyTabController.formDataTabController.getStringTuples());
dataRequest.setFileTuples(bodyTabController.formDataTabController.getFileTuples());
dataRequest.setStringTuples(bodyTabController.formDataTabController.getStringTuples(true));
dataRequest.setFileTuples(bodyTabController.formDataTabController.getFileTuples(true));
dataRequest.setContentType(MediaType.MULTIPART_FORM_DATA);
} else if (bodyTabController.binaryTab.isSelected()) {
dataRequest.setBody(bodyTabController.filePathField.getText());
dataRequest.setContentType(MediaType.APPLICATION_OCTET_STREAM);
} else if (bodyTabController.urlTab.isSelected()) {
dataRequest.setStringTuples(bodyTabController.urlTabController.getStringTuples());
dataRequest.setStringTuples(bodyTabController.urlTabController.getStringTuples(true));
dataRequest.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
}
@ -296,7 +295,7 @@ public class DashboardController implements Initializable {
deleteRequest = new DELETERequest();
deleteRequest.setTarget(address);
deleteRequest.setHeaders(headerTabController.getSelectedHeaders());
deleteRequest.setHeaders(headerTabController.getHeaders(true));
requestManager = Services.pool.delete();
requestManager.setRequest(deleteRequest);
@ -354,7 +353,7 @@ public class DashboardController implements Initializable {
if (requestManager.getClass() == DataDispatchRequestManager.class) {
if (throwable.getCause() != null && throwable.getCause().getClass() == IllegalArgumentException.class) {
errorTitle.setText("Did you forget something?");
errorDetails.setText("Please specify at least one body part for your " + httpMethodBox.getValue() + " request.");
errorDetails.setText("Please specify at least one rawBody part for your " + httpMethodBox.getValue() + " request.");
} else if (throwable.getClass() == FileNotFoundException.class) {
errorTitle.setText("File(s) not found:");
errorDetails.setText(throwable.getMessage());
@ -434,7 +433,7 @@ public class DashboardController implements Initializable {
}
} else {
responseTypeBox.setValue("PLAIN");
responseArea.setText("No body found in the response.", HighlightMode.PLAIN);
responseArea.setText("No rawBody found in the response.", HighlightMode.PLAIN);
}
} catch (Exception e) {
snackbar.show("Response could not be parsed.", 5000);
@ -468,14 +467,19 @@ public class DashboardController implements Initializable {
}
}
private HashMap<String, String> getParams() {
private HashMap<String, String> getParams(boolean onlyChecked) {
if (params == null)
params = new HashMap<>();
params.clear();
for (StringKeyValueFieldController controller : paramsControllers)
if (controller.isChecked())
params.put(controller.getHeader().getKey(), controller.getHeader().getValue());
for (StringKeyValueFieldController controller : paramsControllers) {
if (onlyChecked)
if (!controller.isChecked())
continue;
params.put(controller.getHeader().getKey(), controller.getHeader().getValue());
}
return params;
}
@ -540,22 +544,17 @@ public class DashboardController implements Initializable {
case "POST":
case "PUT":
case "PATCH":
dashboardState = new DashboardState(bodyTabController.getBasicRequest(httpMethodBox.getValue()));
dashboardState.setHeaders(headerTabController.getSelectedHeaders());
dashboardState = bodyTabController.getState();
break;
default:
// For GET, DELETE requests
dashboardState = new DashboardState();
}
try {
dashboardState.setTarget(addressField.getText());
} catch (MalformedURLException e) {
Services.loggingService.logInfo("Dashboard state was saved with an invalid URL.", LocalDateTime.now());
}
dashboardState.setHttpMethod(httpMethodBox.getValue());
dashboardState.setHeaders(headerTabController.getSelectedHeaders());
dashboardState.setParams(getParams());
dashboardState.target = addressField.getText();
dashboardState.httpMethod = httpMethodBox.getValue();
dashboardState.headers = headerTabController.getHeaders(false);
dashboardState.params = getParams(false);
return dashboardState;
}
@ -563,23 +562,34 @@ public class DashboardController implements Initializable {
/**
* Sets the Dashboard to the given application state.
*
* @param dashboardState - State of the dashboard
* @param state - State of the dashboard
*/
public void setState(DashboardState dashboardState) {
if (dashboardState.getTarget() != null)
addressField.setText(dashboardState.getTarget().toString());
public void setState(DashboardState state) {
boolean validMethod = false;
for (String method : httpMethods) {
if (state.httpMethod.equals(method))
validMethod = true;
}
httpMethodBox.getSelectionModel().select(dashboardState.getHttpMethod());
if (!validMethod) {
Services.loggingService.logInfo("Application state file was tampered with. State could not be recovered.", LocalDateTime.now());
return;
}
if (dashboardState.getHeaders() != null)
for (Entry entry : dashboardState.getHeaders().entrySet())
httpMethodBox.setValue(state.httpMethod);
if (state.target != null)
addressField.setText(state.target);
if (state.headers != null)
for (Entry entry : state.headers.entrySet())
headerTabController.addHeader(entry.getKey().toString(), entry.getValue().toString());
if (dashboardState.getParams() != null)
for (Entry entry : dashboardState.getParams().entrySet())
if (state.params != null)
for (Entry entry : state.params.entrySet())
addParamField(entry.getKey().toString(), entry.getValue().toString());
if (!(httpMethodBox.getValue().equals("GET") || httpMethodBox.getValue().equals("DELETE")))
bodyTabController.setState(dashboardState);
bodyTabController.setState(state);
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.everest.controllers;
import java.util.HashMap;
/**
* Convenience class to abstract the state of the application.
*/
public class DashboardState {
public String target;
public String httpMethod;
public HashMap<String, String> params;
public HashMap<String, String> headers;
public String contentType;
public String rawBody;
public String rawBodyType;
public HashMap<String, String> urlStringTuples;
public HashMap<String, String> formStringTuples;
public HashMap<String, String> formFileTuples;
public String binaryFilePath;
}

View file

@ -156,26 +156,33 @@ public class FormDataTabController implements Initializable {
}
}
public HashMap<String, String> getStringTuples() {
public HashMap<String, String> getStringTuples(boolean onlyChecked) {
if (stringMap == null)
stringMap = new HashMap<>();
stringMap.clear();
for (StringKeyValueFieldController controller : stringControllers) {
if (controller.isChecked())
stringMap.put(controller.getHeader().getKey(), controller.getHeader().getValue());
if (onlyChecked)
if (!controller.isChecked())
continue;
stringMap.put(controller.getHeader().getKey(), controller.getHeader().getValue());
}
return stringMap;
}
public HashMap<String, String> getFileTuples() {
public HashMap<String, String> getFileTuples(boolean onlyChecked) {
if (fileMap == null)
fileMap = new HashMap<>();
fileMap.clear();
for (FileKeyValueFieldController controller : fileControllers) {
if (controller.isChecked())
fileMap.put(controller.getHeader().getKey(), controller.getHeader().getValue());
if (onlyChecked)
if (!controller.isChecked())
continue;
fileMap.put(controller.getHeader().getKey(), controller.getHeader().getValue());
}
return fileMap;
}

View file

@ -103,16 +103,20 @@ public class HeaderTabController implements Initializable {
}
/**
* Returns a map of the selected headers.
* Returns a map of the checked/unchecked headers.
* @param onlyChecked
*/
public HashMap<String, String> getSelectedHeaders() {
public HashMap<String, String> getHeaders(boolean onlyChecked) {
if (headers == null)
headers = new HashMap<>();
headers.clear();
for (StringKeyValueFieldController controller : controllers) {
if (controller.isChecked())
headers.put(controller.getHeader().getKey(), controller.getHeader().getValue());
if (onlyChecked)
if (!controller.isChecked())
continue;
headers.put(controller.getHeader().getKey(), controller.getHeader().getValue());
}
return headers;
}

View file

@ -16,14 +16,16 @@
package com.rohitawate.everest.controllers;
import com.rohitawate.everest.models.DashboardState;
import com.rohitawate.everest.misc.Services;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.Tooltip;
import javax.ws.rs.core.MediaType;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.ResourceBundle;
@ -69,54 +71,60 @@ public class HistoryItemController implements Initializable {
String comparisonString;
// Checks if matches with target
comparisonString = dashboardState.getTarget().toString().toLowerCase();
comparisonString = dashboardState.target.toLowerCase();
if (comparisonString.contains(searchString))
return 10;
// Checks if matches with target's hostname
comparisonString = dashboardState.getTarget().getHost().toLowerCase();
if (comparisonString.contains(searchString))
return 10;
try {
URL url = new URL(dashboardState.target);
// Checks if matches with target's path
comparisonString = dashboardState.getTarget().getPath().toLowerCase();
if (comparisonString.contains(searchString))
return 9;
// Checks if matches with target's hostname
comparisonString = url.getHost().toLowerCase();
if (comparisonString.contains(searchString))
return 10;
// Checks if matches with target's path
comparisonString = url.getPath().toLowerCase();
if (comparisonString.contains(searchString))
return 9;
} catch (MalformedURLException e) {
Services.loggingService.logInfo("Failed to parse URL while calculating relativity index.", LocalDateTime.now());
}
// Checks if matches with HTTP method
comparisonString = dashboardState.getHttpMethod().toLowerCase();
comparisonString = dashboardState.httpMethod.toLowerCase();
if (comparisonString.contains(searchString))
return 7;
// Checks for a match in the params
for (Map.Entry param : dashboardState.getParams().entrySet()) {
for (Map.Entry param : dashboardState.params.entrySet()) {
if (param.getKey().toString().toLowerCase().contains(searchString) ||
param.getKey().toString().toLowerCase().contains(searchString))
return 5;
}
// Checks for a match in the headers
for (Map.Entry header : dashboardState.getHeaders().entrySet()) {
for (Map.Entry header : dashboardState.headers.entrySet()) {
if (header.getKey().toString().toLowerCase().contains(searchString) ||
header.getValue().toString().toLowerCase().contains(searchString))
return 6;
}
if (dashboardState.getHttpMethod().equals("POST") || dashboardState.getHttpMethod().equals("PUT")) {
switch (dashboardState.getContentType()) {
if (dashboardState.httpMethod.equals("POST") || dashboardState.httpMethod.equals("PUT")) {
switch (dashboardState.contentType) {
case MediaType.TEXT_PLAIN:
case MediaType.APPLICATION_JSON:
case MediaType.APPLICATION_XML:
case MediaType.TEXT_HTML:
case MediaType.APPLICATION_OCTET_STREAM:
// Checks for match in body of the request
comparisonString = dashboardState.getBody().toLowerCase();
// Checks for match in rawBody of the request
comparisonString = dashboardState.rawBody.toLowerCase();
if (comparisonString.contains(searchString))
return 8;
break;
case MediaType.APPLICATION_FORM_URLENCODED:
// Checks for match in string tuples
for (Map.Entry tuple : dashboardState.getStringTuples().entrySet()) {
for (Map.Entry tuple : dashboardState.urlStringTuples.entrySet()) {
if (tuple.getKey().toString().toLowerCase().contains(searchString) ||
tuple.getValue().toString().toLowerCase().contains(searchString))
return 8;
@ -124,13 +132,13 @@ public class HistoryItemController implements Initializable {
break;
case MediaType.MULTIPART_FORM_DATA:
// Checks for match in string and file tuples
for (Map.Entry tuple : dashboardState.getStringTuples().entrySet()) {
for (Map.Entry tuple : dashboardState.formStringTuples.entrySet()) {
if (tuple.getKey().toString().toLowerCase().contains(searchString) ||
tuple.getValue().toString().toLowerCase().contains(searchString))
return 8;
}
for (Map.Entry tuple : dashboardState.getFileTuples().entrySet()) {
for (Map.Entry tuple : dashboardState.formFileTuples.entrySet()) {
if (tuple.getKey().toString().toLowerCase().contains(searchString) ||
tuple.getValue().toString().toLowerCase().contains(searchString))
return 8;

View file

@ -22,7 +22,6 @@ import com.rohitawate.everest.misc.EverestUtilities;
import com.rohitawate.everest.misc.KeyMap;
import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.misc.ThemeManager;
import com.rohitawate.everest.models.DashboardState;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.StringProperty;
@ -309,9 +308,9 @@ public class HomeWindowController implements Initializable {
controller = loader.getController();
controller.setRequestType(state.getHttpMethod());
controller.setRequestType(state.httpMethod);
controller.setAddress(state.getTarget().toString());
controller.setAddress(state.target);
controller.setDashboardState(state);
if (appendToStart)

View file

@ -101,14 +101,17 @@ public class URLTabController implements Initializable {
}
}
public HashMap<String, String> getStringTuples() {
public HashMap<String, String> getStringTuples(boolean onlyChecked) {
if (tuples == null)
tuples = new HashMap<>();
tuples.clear();
for (StringKeyValueFieldController controller : controllers) {
if (controller.isChecked())
tuples.put(controller.getHeader().getKey(), controller.getHeader().getValue());
if (onlyChecked)
if (!controller.isChecked())
continue;
tuples.put(controller.getHeader().getKey(), controller.getHeader().getValue());
}
return tuples;
}

View file

@ -18,16 +18,15 @@ package com.rohitawate.everest.history;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rohitawate.everest.controllers.DashboardState;
import com.rohitawate.everest.misc.EverestUtilities;
import com.rohitawate.everest.misc.Services;
import com.rohitawate.everest.models.DashboardState;
import com.rohitawate.everest.settings.Settings;
import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.sql.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
@ -61,7 +60,7 @@ public class HistoryManager {
/**
* Creates and initializes the database with necessary tables if not already done.
*
* @throws IOException - If unable to establish a connection to the database.
* @throws IOException - If unable to establish a connection to the database.
* @throws SQLException - If invalid statement is executed on the database.
*/
private void initDatabase() throws IOException, SQLException {
@ -126,18 +125,14 @@ public class HistoryManager {
while (resultSet.next()) {
state = new DashboardState();
try {
state.setTarget(resultSet.getString("Target"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
state.target = resultSet.getString("Target");
int requestID = resultSet.getInt("ID");
state.setHeaders(getRequestHeaders(requestID));
state.setParams(getTuples(requestID, "Param"));
state.setHttpMethod(resultSet.getString("Type"));
state.headers = getRequestHeaders(requestID);
state.params = getTuples(requestID, "Param");
state.httpMethod = resultSet.getString("Type");
if (!(state.getHttpMethod().equals("GET") || state.getHttpMethod().equals("DELETE"))) {
if (!(state.httpMethod.equals("GET") || state.httpMethod.equals("DELETE"))) {
// Retrieves request body ContentType for querying corresponding table
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("selectRequestContentType").toString()));
statement.setInt(1, requestID);
@ -148,7 +143,7 @@ public class HistoryManager {
if (RS.next())
contentType = RS.getString("ContentType");
state.setContentType(contentType);
state.contentType = contentType;
// Retrieves body from corresponding table
switch (contentType) {
@ -163,14 +158,14 @@ public class HistoryManager {
RS = statement.executeQuery();
if (RS.next())
state.setBody(RS.getString("Body"));
state.rawBody = RS.getString("Body");
break;
case MediaType.APPLICATION_FORM_URLENCODED:
state.setStringTuples(getTuples(requestID, "String"));
state.urlStringTuples = getTuples(requestID, "String");
break;
case MediaType.MULTIPART_FORM_DATA:
state.setStringTuples(getTuples(requestID, "String"));
state.setFileTuples(getTuples(requestID, "File"));
state.formStringTuples = getTuples(requestID, "String");
state.formFileTuples = getTuples(requestID, "File");
break;
}
}
@ -249,8 +244,8 @@ public class HistoryManager {
int lastRequestID = -1;
if (RS.next()) {
if (!(newState.getHttpMethod().equals(RS.getString("Type"))) ||
!(newState.getTarget().toString().equals(RS.getString("Target"))) ||
if (!(newState.httpMethod.equals(RS.getString("Type"))) ||
!(newState.target.equals(RS.getString("Target"))) ||
!(LocalDate.now().equals(LocalDate.parse(RS.getString("Date")))))
return false;
else
@ -265,16 +260,16 @@ public class HistoryManager {
// Checks for new or modified headers
map = getRequestHeaders(lastRequestID);
if (!areMapsIdentical(map, newState.getHeaders()))
if (!areMapsIdentical(map, newState.headers))
return false;
// Checks for new or modified params
map = getTuples(lastRequestID, "Param");
if (!areMapsIdentical(map, newState.getParams()))
if (!areMapsIdentical(map, newState.params))
return false;
if (!(newState.getHttpMethod().equals("GET") || newState.getHttpMethod().equals("DELETE"))) {
switch (newState.getContentType()) {
if (!(newState.httpMethod.equals("GET") || newState.httpMethod.equals("DELETE"))) {
switch (newState.contentType) {
case MediaType.TEXT_PLAIN:
case MediaType.APPLICATION_JSON:
case MediaType.APPLICATION_XML:
@ -286,22 +281,22 @@ public class HistoryManager {
RS = statement.executeQuery();
if (RS.next())
if (!RS.getString("Body").equals(newState.getBody()))
if (!RS.getString("Body").equals(newState.rawBody))
return false;
break;
case MediaType.APPLICATION_FORM_URLENCODED:
// Checks for new or modified string tuples
map = getTuples(lastRequestID, "String");
return areMapsIdentical(map, newState.getStringTuples());
return areMapsIdentical(map, newState.urlStringTuples);
case MediaType.MULTIPART_FORM_DATA:
// Checks for new or modified string tuples
map = getTuples(lastRequestID, "String");
boolean stringComparison = areMapsIdentical(map, newState.getStringTuples());
boolean stringComparison = areMapsIdentical(map, newState.formStringTuples);
// Checks for new or modified file tuples
map = getTuples(lastRequestID, "File");
boolean fileComparison = areMapsIdentical(map, newState.getFileTuples());
boolean fileComparison = areMapsIdentical(map, newState.formFileTuples);
return stringComparison && fileComparison;
}
@ -343,8 +338,8 @@ public class HistoryManager {
statement =
conn.prepareStatement(EverestUtilities.trimString(queries.get("saveRequest").toString()));
statement.setString(1, state.getHttpMethod());
statement.setString(2, String.valueOf(state.getTarget()));
statement.setString(1, state.httpMethod);
statement.setString(2, String.valueOf(state.target));
statement.setString(3, LocalDate.now().toString());
statement.executeUpdate();
@ -357,10 +352,10 @@ public class HistoryManager {
if (RS.next())
requestID = RS.getInt("MaxID");
if (state.getHeaders().size() > 0) {
if (state.headers.size() > 0) {
// Saves request headers
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveHeader").toString()));
for (Entry entry : state.getHeaders().entrySet()) {
for (Entry entry : state.headers.entrySet()) {
statement.setInt(1, requestID);
statement.setString(2, entry.getKey().toString());
statement.setString(3, entry.getValue().toString());
@ -369,10 +364,10 @@ public class HistoryManager {
}
}
if (state.getParams().size() > 0) {
if (state.params.size() > 0) {
// Saves request parameters
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveTuple").toString()));
for (Entry entry : state.getParams().entrySet()) {
for (Entry entry : state.params.entrySet()) {
statement.setInt(1, requestID);
statement.setString(2, "Param");
statement.setString(3, entry.getKey().toString());
@ -382,16 +377,16 @@ public class HistoryManager {
}
}
if (!(state.getHttpMethod().equals("GET") || state.getHttpMethod().equals("DELETE"))) {
// Maps the request to its ContentType for faster recovery
if (!(state.httpMethod.equals("GET") || state.httpMethod.equals("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.getContentType());
statement.setString(2, state.contentType);
statement.executeUpdate();
// Determines where to fetch the body from, based on the ContentType
switch (state.getContentType()) {
switch (state.contentType) {
case MediaType.TEXT_PLAIN:
case MediaType.APPLICATION_JSON:
case MediaType.APPLICATION_XML:
@ -400,12 +395,12 @@ public class HistoryManager {
// 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.getBody());
statement.setString(2, state.rawBody);
statement.executeUpdate();
break;
case MediaType.APPLICATION_FORM_URLENCODED:
if (state.getStringTuples().size() > 0) {
for (Entry<String, String> entry : state.getStringTuples().entrySet()) {
if (state.urlStringTuples.size() > 0) {
for (Entry<String, String> entry : state.urlStringTuples.entrySet()) {
// Saves the string tuples
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveTuple").toString()));
statement.setInt(1, requestID);
@ -418,8 +413,8 @@ public class HistoryManager {
}
break;
case MediaType.MULTIPART_FORM_DATA:
if (state.getStringTuples().size() > 0) {
for (Entry<String, String> entry : state.getStringTuples().entrySet()) {
if (state.formStringTuples.size() > 0) {
for (Entry<String, String> entry : state.formStringTuples.entrySet()) {
// Saves the string tuples
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveTuple").toString()));
statement.setInt(1, requestID);
@ -431,8 +426,8 @@ public class HistoryManager {
}
}
if (state.getFileTuples().size() > 0) {
for (Entry<String, String> entry : state.getFileTuples().entrySet()) {
if (state.formFileTuples.size() > 0) {
for (Entry<String, String> entry : state.formFileTuples.entrySet()) {
// Saves the file tuples
statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveTuple").toString()));
statement.setInt(1, requestID);

View file

@ -1,62 +0,0 @@
/*
* Copyright 2018 Rohit Awate.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.rohitawate.everest.models;
import com.rohitawate.everest.models.requests.DataDispatchRequest;
import java.util.HashMap;
/**
* Convenience class to abstract the state of the application.
*/
public class DashboardState extends DataDispatchRequest {
private HashMap<String, String> params;
private String httpMethod;
public DashboardState() {
}
/*
Special copy constructor to instantiate DashboardState from
BodyTabController's getBasicRequest()
*/
public DashboardState(DataDispatchRequest dataDispatchRequest) {
super();
this.setHttpMethod(dataDispatchRequest.getRequestType());
this.setBody(dataDispatchRequest.getBody());
this.setContentType(dataDispatchRequest.getContentType());
this.setStringTuples(dataDispatchRequest.getStringTuples());
this.setFileTuples(dataDispatchRequest.getFileTuples());
}
public void setParams(HashMap<String, String> params) {
this.params = params;
}
public HashMap<String, String> getParams() {
return params;
}
public void setHttpMethod(String httpMethod) {
this.httpMethod = httpMethod;
}
public String getHttpMethod() {
return httpMethod;
}
}