From 2d0cf380a7b5a32c420431f038466c2793461aa4 Mon Sep 17 00:00:00 2001 From: Rohit Awate Date: Wed, 15 Aug 2018 16:41:22 +0530 Subject: [PATCH] Rid HistoryManager of isDuplicate() and areListsEqual() and add binary file path and raw body type as search priorities --- .../controllers/DashboardController.java | 6 +- .../controllers/FormDataTabController.java | 12 +- .../controllers/HeaderTabController.java | 6 +- .../controllers/HistoryItemController.java | 68 ++++---- .../everest/controllers/URLTabController.java | 6 +- .../everest/history/HistoryManager.java | 156 ++---------------- 6 files changed, 66 insertions(+), 188 deletions(-) diff --git a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java index 7f02a6a..c2d6e05 100644 --- a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java +++ b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java @@ -556,8 +556,10 @@ public class DashboardController implements Initializable { public ArrayList getParamFieldStates() { ArrayList states = new ArrayList<>(); - for (StringKeyValueFieldController controller : paramsControllers) - states.add(controller.getState()); + for (StringKeyValueFieldController controller : paramsControllers) { + if (!controller.isKeyFieldEmpty() && !controller.isValueFieldEmpty()) + states.add(controller.getState()); + } return states; } diff --git a/src/main/java/com/rohitawate/everest/controllers/FormDataTabController.java b/src/main/java/com/rohitawate/everest/controllers/FormDataTabController.java index bb27ff3..385d1b1 100644 --- a/src/main/java/com/rohitawate/everest/controllers/FormDataTabController.java +++ b/src/main/java/com/rohitawate/everest/controllers/FormDataTabController.java @@ -211,8 +211,10 @@ public class FormDataTabController implements Initializable { public ArrayList getStringFieldStates() { ArrayList states = new ArrayList<>(); - for (StringKeyValueFieldController controller : stringControllers) - states.add(controller.getState()); + for (StringKeyValueFieldController controller : stringControllers) { + if (!controller.isKeyFieldEmpty() && !controller.isValueFieldEmpty()) + states.add(controller.getState()); + } return states; } @@ -224,8 +226,10 @@ public class FormDataTabController implements Initializable { public ArrayList getFileFieldStates() { ArrayList states = new ArrayList<>(); - for (FileKeyValueFieldController controller : fileControllers) - states.add(controller.getState()); + for (FileKeyValueFieldController controller : fileControllers) { + if (!controller.isFileKeyFieldEmpty() && !controller.isFileValueFieldEmpty()) + states.add(controller.getState()); + } return states; } diff --git a/src/main/java/com/rohitawate/everest/controllers/HeaderTabController.java b/src/main/java/com/rohitawate/everest/controllers/HeaderTabController.java index 54b5f17..bc97f1f 100644 --- a/src/main/java/com/rohitawate/everest/controllers/HeaderTabController.java +++ b/src/main/java/com/rohitawate/everest/controllers/HeaderTabController.java @@ -131,8 +131,10 @@ public class HeaderTabController implements Initializable { public ArrayList getFieldStates() { ArrayList states = new ArrayList<>(); - for (StringKeyValueFieldController controller : controllers) - states.add(controller.getState()); + for (StringKeyValueFieldController controller : controllers) { + if (!controller.isKeyFieldEmpty() && !controller.isValueFieldEmpty()) + states.add(controller.getState()); + } return states; } diff --git a/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java b/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java index edbe3e7..7056f8e 100644 --- a/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java +++ b/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java @@ -26,7 +26,6 @@ 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; @@ -113,52 +112,51 @@ public class HistoryItemController implements Initializable, Searchable getFieldStates() { ArrayList states = new ArrayList<>(); - for (StringKeyValueFieldController controller : controllers) - states.add(controller.getState()); + for (StringKeyValueFieldController controller : controllers) { + if (!controller.isKeyFieldEmpty() && !controller.isValueFieldEmpty()) + states.add(controller.getState()); + } return states; } diff --git a/src/main/java/com/rohitawate/everest/history/HistoryManager.java b/src/main/java/com/rohitawate/everest/history/HistoryManager.java index 04f1699..15eb318 100644 --- a/src/main/java/com/rohitawate/everest/history/HistoryManager.java +++ b/src/main/java/com/rohitawate/everest/history/HistoryManager.java @@ -24,7 +24,6 @@ import com.rohitawate.everest.state.ComposerState; import com.rohitawate.everest.state.FieldState; import javafx.util.Pair; -import javax.ws.rs.core.MediaType; import java.io.File; import java.sql.*; import java.time.LocalDate; @@ -91,9 +90,6 @@ public class HistoryManager { * @param newState - The state of the Dashboard while making the request. */ public synchronized void saveHistory(ComposerState newState) { -// if (isDuplicate(state)) -// return; - ComposerState lastState = getLastRequest(); if (newState.equals(lastState)) return; @@ -133,23 +129,14 @@ public class HistoryManager { // Retrieves request body ContentType for querying corresponding table state.contentType = getRequestContentType(requestID); - statement = conn.prepareStatement(Queries.selectRequestBody); - statement.setInt(1, requestID); + Pair rawBodyAndType = getRequestBody(requestID); - ResultSet RS = statement.executeQuery(); - - if (RS.next()) { - state.rawBody = RS.getString("Body"); - state.rawBodyBoxValue = RS.getString("Type"); + if (rawBodyAndType != null) { + state.rawBody = rawBodyAndType.getKey(); + state.rawBodyBoxValue = rawBodyAndType.getValue(); } - statement = conn.prepareStatement(Queries.selectFilePath); - statement.setInt(1, requestID); - - RS = statement.executeQuery(); - - if (RS.next()) - state.binaryFilePath = RS.getString("Path"); + state.binaryFilePath = getFilePath(requestID); state.urlStringTuples = getTuples(requestID, "URLString"); state.formStringTuples = getTuples(requestID, "FormString"); @@ -191,8 +178,7 @@ public class HistoryManager { ArrayList tuples = new ArrayList<>(); - PreparedStatement statement = - conn.prepareStatement(Queries.selectTuplesByType); + PreparedStatement statement = conn.prepareStatement(Queries.selectTuplesByType); statement.setInt(1, requestID); statement.setString(2, type); @@ -271,120 +257,6 @@ public class HistoryManager { return null; } - /** - * Performs a comprehensive comparison of the new request with the one added last to the database. - * - * @param newState The new request. - * @return true, if request is same as the last one in the database. false, otherwise. - */ - private boolean isDuplicate(ComposerState newState) { - try { - statement = conn.prepareStatement(Queries.selectMostRecentRequest); - ResultSet RS = statement.executeQuery(); - - int lastRequestID = -1; - if (RS.next()) { - if (!(newState.httpMethod.equals(RS.getString("Type"))) || - !(newState.target.equals(RS.getString("Target"))) || - !(LocalDate.now().equals(LocalDate.parse(RS.getString("Date"))))) - return false; - else - lastRequestID = RS.getInt("ID"); - } - - // This condition is observed when the database is empty - if (lastRequestID == -1) - return false; - - ArrayList states; - - // Checks for new or modified headers - states = getTuples(lastRequestID, "Header"); - if (!areListsEqual(states, newState.headers)) - return false; - - // Checks for new or modified params - states = getTuples(lastRequestID, "Param"); - if (!areListsEqual(states, newState.params)) - return false; - - if (!(newState.httpMethod.equals(HTTPConstants.GET) || newState.httpMethod.equals(HTTPConstants.DELETE))) { - statement = conn.prepareStatement(Queries.selectRequestContentType); - statement.setInt(1, lastRequestID); - - RS = statement.executeQuery(); - - String previousContentType = ""; - if (RS.next()) - previousContentType = RS.getString("ContentType"); - - if (!newState.contentType.equals(previousContentType)) - return false; - - switch (newState.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(Queries.selectRequestBody); - statement.setInt(1, lastRequestID); - - RS = statement.executeQuery(); - - while (RS.next()) { - if (!(RS.getString("Type").equals("Raw") && - RS.getString("Body").equals(newState.rawBody))) - return false; - else if (!(RS.getString("Type").equals("FilePath") && - RS.getString("Body").equals(newState.binaryFilePath))) - return false; - } - - break; - case MediaType.APPLICATION_FORM_URLENCODED: - // Checks for new or modified string tuples - states = getTuples(lastRequestID, "URLString"); - return areListsEqual(states, newState.urlStringTuples); - case MediaType.MULTIPART_FORM_DATA: - // Checks for new or modified string tuples - states = getTuples(lastRequestID, "FormString"); - boolean stringComparison = areListsEqual(states, newState.formStringTuples); - - // Checks for new or modified file tuples - states = getTuples(lastRequestID, "File"); - boolean fileComparison = areListsEqual(states, newState.formFileTuples); - - return stringComparison && fileComparison; - } - } - } catch (SQLException e) { - LoggingService.logWarning("Database error.", e, LocalDateTime.now()); - } catch (NullPointerException NPE) { - /* - NPE is thrown by containsKey indicating that the key is not present in the database thereby - classifying it as a non-duplicate request. - */ - return false; - } - return true; - } - - private static boolean areListsEqual(ArrayList firstList, ArrayList secondList) { - if (firstList == null && secondList == null) return true; - - if ((firstList == null && secondList != null) || (firstList != null && secondList == null)) return false; - - if (firstList.size() != secondList.size()) return false; - - for (FieldState state : secondList) { - if (!state.isEmpty() && state.checked && !firstList.contains(state)) - return false; - } - - return true; - } - private class HistorySaver implements Runnable { private ComposerState state; @@ -441,15 +313,13 @@ public class HistoryManager { if (tuples.size() > 0) { try { for (FieldState fieldState : tuples) { - if (!fieldState.isEmpty() && fieldState.checked) { - statement = conn.prepareStatement(Queries.saveTuple); - statement.setInt(1, requestID); - statement.setString(2, tupleType); - statement.setString(3, fieldState.key); - statement.setString(4, fieldState.value); - statement.setInt(5, fieldState.checked ? 1 : 0); - statement.addBatch(); - } + statement = conn.prepareStatement(Queries.saveTuple); + statement.setInt(1, requestID); + statement.setString(2, tupleType); + statement.setString(3, fieldState.key); + statement.setString(4, fieldState.value); + statement.setInt(5, fieldState.checked ? 1 : 0); + statement.addBatch(); } statement.executeBatch(); } catch (SQLException e) {