From c739f2133775bf29291ac0a7b8bfce84c04c14ec Mon Sep 17 00:00:00 2001 From: Rohit Awate Date: Tue, 24 Jul 2018 15:52:31 +0530 Subject: [PATCH] Add LoadingLayer maintenance while switching tabs --- .../controllers/DashboardController.java | 6 +- .../controllers/state/DashboardState.java | 83 ++++++++++++++++++- .../requestmanager/RequestManager.java | 4 + .../resources/fxml/homewindow/Dashboard.fxml | 18 ++-- 4 files changed, 99 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java index 5b1a69d..96a33f8 100644 --- a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java +++ b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java @@ -188,9 +188,6 @@ public class DashboardController implements Initializable { responseArea.setMode(mode); }); - errorTitle.setText("Oops... That's embarrassing!"); - errorDetails.setText("Something went wrong. Try to make another request.\nRestart Everest if that doesn't work."); - visualizer = new Visualizer(); visualizerTab.setContent(visualizer); @@ -657,6 +654,9 @@ public class DashboardController implements Initializable { dashboardState.errorTitle = errorTitle.getText(); dashboardState.errorDetails = errorDetails.getText(); break; + case LOADING: + dashboardState.setRequestManager(requestManager); + break; } return dashboardState; diff --git a/src/main/java/com/rohitawate/everest/controllers/state/DashboardState.java b/src/main/java/com/rohitawate/everest/controllers/state/DashboardState.java index 0981c2d..8762d09 100644 --- a/src/main/java/com/rohitawate/everest/controllers/state/DashboardState.java +++ b/src/main/java/com/rohitawate/everest/controllers/state/DashboardState.java @@ -17,7 +17,20 @@ package com.rohitawate.everest.controllers.state; import com.rohitawate.everest.controllers.DashboardController.ResponseLayer; +import com.rohitawate.everest.exceptions.RedirectException; +import com.rohitawate.everest.exceptions.UnreliableResponseException; +import com.rohitawate.everest.misc.Services; +import com.rohitawate.everest.models.requests.EverestRequest; +import com.rohitawate.everest.models.responses.EverestResponse; +import com.rohitawate.everest.requestmanager.DataDispatchRequestManager; +import com.rohitawate.everest.requestmanager.RequestManager; +import javafx.concurrent.WorkerStateEvent; +import javafx.event.Event; +import javax.ws.rs.ProcessingException; +import java.io.FileNotFoundException; +import java.net.MalformedURLException; +import java.time.LocalDateTime; import java.util.HashMap; public class DashboardState { @@ -30,12 +43,40 @@ public class DashboardState { public String responseBody; public int responseTime; public int responseSize; + public HashMap responseHeaders; // ErrorLayer parameters public String errorTitle; public String errorDetails; - public HashMap responseHeaders; + // ResponseLayer parameters + private RequestManager requestManager; + + public void setRequestManager(RequestManager manager) { + this.requestManager = manager; + requestManager.removeEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, requestManager.getOnSucceeded()); + requestManager.removeEventHandler(WorkerStateEvent.WORKER_STATE_FAILED, requestManager.getOnFailed()); + + requestManager.setOnFailed(this::onRequestFailed); + requestManager.setOnSucceeded(this::onRequestSucceeded); + } + + private void onRequestSucceeded(Event e) { + this.visibleLayer = ResponseLayer.RESPONSE; + EverestResponse response = requestManager.getValue(); + responseCode = response.getStatusCode(); + responseType = response.getMediaType().toString(); + responseTime = (int) response.getTime(); + responseSize = response.getSize(); + responseBody = response.getBody(); + + if (responseHeaders == null) + responseHeaders = new HashMap<>(); + else + responseHeaders.clear(); + + response.getHeaders().forEach((key, value) -> responseHeaders.put(key, value.get(0))); + } public DashboardState() { } @@ -43,4 +84,44 @@ public class DashboardState { public DashboardState(ComposerState composer) { this.composer = composer; } + + private void onRequestFailed(Event e) { + this.visibleLayer = ResponseLayer.ERROR; + Throwable throwable = requestManager.getException(); + Exception exception = (Exception) throwable; + Services.loggingService.logWarning(this.composer.httpMethod + " request could not be processed.", exception, LocalDateTime.now()); + + if (throwable.getClass() == UnreliableResponseException.class) { + UnreliableResponseException URE = (UnreliableResponseException) throwable; + errorTitle = URE.getExceptionTitle(); + errorDetails = URE.getExceptionDetails(); + } else if (throwable.getClass() == ProcessingException.class) { + errorTitle = "Everest couldn't connect."; + errorDetails = "Either you are not connected to the Internet or the server is offline."; + } else if (throwable.getClass() == RedirectException.class) { + RedirectException redirect = (RedirectException) throwable; + this.composer.target = redirect.getNewLocation(); + EverestRequest request = requestManager.getRequest(); + try { + request.setTarget(redirect.getNewLocation()); + requestManager.restart(); + } catch (MalformedURLException MURLE) { + Services.loggingService.logInfo("Invalid URL: " + this.composer.target, LocalDateTime.now()); + } + + return; + } + + if (requestManager.getClass() == DataDispatchRequestManager.class) { + if (throwable.getCause() != null && throwable.getCause().getClass() == IllegalArgumentException.class) { + errorTitle = "Did you forget something?"; + errorDetails = "Please specify a body for your " + this.composer.httpMethod + " request."; + } else if (throwable.getClass() == FileNotFoundException.class) { + errorTitle = "File(s) not found:"; + errorDetails = throwable.getMessage(); + } + } + + requestManager.reset(); + } } diff --git a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java index 569ed23..b1d4431 100644 --- a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java +++ b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java @@ -68,6 +68,10 @@ public abstract class RequestManager extends Service { appendHeaders(); } + public EverestRequest getRequest() { + return this.request; + } + private void appendHeaders() { HashMap headers = request.getHeaders(); Map.Entry mapEntry; diff --git a/src/main/resources/fxml/homewindow/Dashboard.fxml b/src/main/resources/fxml/homewindow/Dashboard.fxml index aef9b25..59b8341 100644 --- a/src/main/resources/fxml/homewindow/Dashboard.fxml +++ b/src/main/resources/fxml/homewindow/Dashboard.fxml @@ -145,12 +145,12 @@ - + - + @@ -236,7 +236,7 @@ - + - +