Add RequestManager hand-over while switching from loading tabs
This commit is contained in:
parent
d9d0ceead2
commit
197c06b227
10 changed files with 112 additions and 69 deletions
|
@ -39,6 +39,7 @@ import javafx.beans.binding.Bindings;
|
|||
import javafx.beans.property.IntegerProperty;
|
||||
import javafx.beans.property.SimpleIntegerProperty;
|
||||
import javafx.event.ActionEvent;
|
||||
import javafx.event.Event;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.fxml.FXMLLoader;
|
||||
import javafx.fxml.Initializable;
|
||||
|
@ -243,10 +244,6 @@ public class DashboardController implements Initializable {
|
|||
|
||||
requestManager = Services.pool.get();
|
||||
requestManager.setRequest(getRequest);
|
||||
|
||||
cancelButton.setOnAction(e -> requestManager.cancel());
|
||||
configureRequestManager();
|
||||
requestManager.start();
|
||||
break;
|
||||
case "POST":
|
||||
case "PUT":
|
||||
|
@ -292,10 +289,6 @@ public class DashboardController implements Initializable {
|
|||
|
||||
requestManager = Services.pool.data();
|
||||
requestManager.setRequest(dataRequest);
|
||||
|
||||
cancelButton.setOnAction(e -> requestManager.cancel());
|
||||
configureRequestManager();
|
||||
requestManager.start();
|
||||
break;
|
||||
case "DELETE":
|
||||
if (deleteRequest == null)
|
||||
|
@ -306,14 +299,13 @@ public class DashboardController implements Initializable {
|
|||
|
||||
requestManager = Services.pool.delete();
|
||||
requestManager.setRequest(deleteRequest);
|
||||
|
||||
cancelButton.setOnAction(e -> requestManager.cancel());
|
||||
configureRequestManager();
|
||||
requestManager.start();
|
||||
break;
|
||||
default:
|
||||
showLayer(ResponseLayer.PROMPT);
|
||||
}
|
||||
cancelButton.setOnAction(e -> requestManager.cancel());
|
||||
requestManager.addHandlers(this::whileRunning, this::onSucceeded, this::onFailed, this::onCancelled);
|
||||
requestManager.start();
|
||||
Services.historyManager.saveHistory(getState().composer);
|
||||
} catch (MalformedURLException MURLE) {
|
||||
showLayer(ResponseLayer.PROMPT);
|
||||
|
@ -326,15 +318,8 @@ public class DashboardController implements Initializable {
|
|||
}
|
||||
}
|
||||
|
||||
private void configureRequestManager() {
|
||||
progressBar.progressProperty().bind(requestManager.progressProperty());
|
||||
requestManager.setOnRunning(e -> whileRunning());
|
||||
requestManager.setOnSucceeded(e -> onSucceeded());
|
||||
requestManager.setOnCancelled(e -> onCancelled());
|
||||
requestManager.setOnFailed(e -> onFailed());
|
||||
}
|
||||
|
||||
private void onFailed() {
|
||||
// TODO: Clean this method
|
||||
private void onFailed(Event event) {
|
||||
showLayer(ResponseLayer.ERROR);
|
||||
Throwable throwable = requestManager.getException();
|
||||
Exception exception = (Exception) throwable;
|
||||
|
@ -369,19 +354,20 @@ public class DashboardController implements Initializable {
|
|||
requestManager.reset();
|
||||
}
|
||||
|
||||
private void onCancelled() {
|
||||
private void onCancelled(Event event) {
|
||||
showLayer(ResponseLayer.PROMPT);
|
||||
snackbar.show("Request canceled.", 2000);
|
||||
requestManager.reset();
|
||||
addressField.requestFocus();
|
||||
}
|
||||
|
||||
private void onSucceeded() {
|
||||
private void onSucceeded(Event event) {
|
||||
showResponse(requestManager.getValue());
|
||||
showLayer(ResponseLayer.RESPONSE);
|
||||
requestManager.reset();
|
||||
}
|
||||
|
||||
private void whileRunning() {
|
||||
private void whileRunning(Event event) {
|
||||
responseArea.clear();
|
||||
showLayer(ResponseLayer.LOADING);
|
||||
}
|
||||
|
@ -681,6 +667,7 @@ public class DashboardController implements Initializable {
|
|||
|
||||
dashboardState.responseBody = responseArea.getText();
|
||||
|
||||
// TODO: Get rid of similar switches
|
||||
String contentType;
|
||||
switch (responseTypeBox.getValue()) {
|
||||
case "JSON":
|
||||
|
@ -703,7 +690,7 @@ public class DashboardController implements Initializable {
|
|||
dashboardState.errorDetails = errorDetails.getText();
|
||||
break;
|
||||
case LOADING:
|
||||
dashboardState.setRequestManager(requestManager);
|
||||
dashboardState.handOverRequest(requestManager);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -748,11 +735,23 @@ public class DashboardController implements Initializable {
|
|||
errorDetails.setText(state.errorDetails);
|
||||
showLayer(ResponseLayer.ERROR);
|
||||
break;
|
||||
case LOADING:
|
||||
/*
|
||||
Accepts a RequestManager which is in the RUNNING state
|
||||
and switches its handlers.
|
||||
The handlers affect the Dashboard directly rather than the DashboardState.
|
||||
*/
|
||||
requestManager = state.getRequestManager();
|
||||
requestManager.removeHandlers();
|
||||
requestManager.addHandlers(this::whileRunning, this::onSucceeded, this::onFailed, this::onCancelled);
|
||||
showLayer(ResponseLayer.LOADING);
|
||||
break;
|
||||
default:
|
||||
showLayer(ResponseLayer.PROMPT);
|
||||
break;
|
||||
}
|
||||
if (state.visibleResponseLayer.equals(ResponseLayer.RESPONSE))
|
||||
showResponse(state);
|
||||
else
|
||||
showLayer(state.visibleResponseLayer);
|
||||
} else {
|
||||
showLayer(ResponseLayer.PROMPT);
|
||||
}
|
||||
|
||||
if (state.composer == null)
|
||||
|
|
|
@ -186,9 +186,7 @@ public class HomeWindowController implements Initializable {
|
|||
onTabSwitched(prevState, prevTab, newTab);
|
||||
|
||||
newTab.setOnCloseRequest(e -> {
|
||||
DashboardState closedState = tabStateMap.remove(newTab);
|
||||
closedState = null;
|
||||
tabPane.getTabs().remove(newTab);
|
||||
removeTab(newTab);
|
||||
|
||||
// Closes the application if the last tab is closed
|
||||
if (tabPane.getTabs().size() == 0) {
|
||||
|
@ -199,6 +197,13 @@ public class HomeWindowController implements Initializable {
|
|||
});
|
||||
}
|
||||
|
||||
private void removeTab(Tab newTab) {
|
||||
DashboardState closedState = tabStateMap.remove(newTab);
|
||||
closedState = null;
|
||||
tabPane.getTabs().remove(newTab);
|
||||
newTab = null;
|
||||
}
|
||||
|
||||
private void saveState() {
|
||||
/*
|
||||
Updating the state of the selected tab before saving it.
|
||||
|
|
|
@ -29,12 +29,12 @@ class Visualizer extends ScrollPane {
|
|||
private TreeView<HBox> visualizer;
|
||||
|
||||
Visualizer() {
|
||||
this.visualizer = new TreeView<>();
|
||||
this.visualizer.setShowRoot(false);
|
||||
this.setContent(this.visualizer);
|
||||
visualizer = new TreeView<>();
|
||||
visualizer.setShowRoot(false);
|
||||
setContent(this.visualizer);
|
||||
|
||||
this.setFitToHeight(true);
|
||||
this.setFitToWidth(true);
|
||||
setFitToHeight(true);
|
||||
setFitToWidth(true);
|
||||
}
|
||||
|
||||
void populate(JsonNode node) {
|
||||
|
|
|
@ -26,7 +26,6 @@ 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;
|
||||
|
@ -55,21 +54,39 @@ public class DashboardState {
|
|||
|
||||
// ResponseLayer parameters
|
||||
private RequestManager requestManager;
|
||||
public void setRequestManager(RequestManager manager) {
|
||||
this.requestManager = manager;
|
||||
requestManager.removeEventHandler(WorkerStateEvent.WORKER_STATE_RUNNING, requestManager.getOnRunning());
|
||||
requestManager.removeEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, requestManager.getOnSucceeded());
|
||||
requestManager.removeEventHandler(WorkerStateEvent.WORKER_STATE_FAILED, requestManager.getOnFailed());
|
||||
|
||||
requestManager.setOnFailed(this::onRequestFailed);
|
||||
requestManager.setOnSucceeded(this::onRequestSucceeded);
|
||||
/**
|
||||
* Accepts a RequestManager from the DashboardController
|
||||
* which is in the RUNNING state and switches its handlers.
|
||||
* <p>
|
||||
* The new handlers make changes to the DashboardState object
|
||||
* rather than the Dashboard.
|
||||
* <p>
|
||||
* If we switch back to the tab with DashboardState while
|
||||
* the manager is running, it is handed back over to the Dashboard.
|
||||
*/
|
||||
public void handOverRequest(RequestManager requestManager) {
|
||||
this.requestManager = requestManager;
|
||||
this.requestManager.removeHandlers();
|
||||
|
||||
this.requestManager.setOnFailed(this::onRequestFailed);
|
||||
this.requestManager.setOnSucceeded(this::onRequestSucceeded);
|
||||
this.requestManager.setOnCancelled(this::onRequestCancelled);
|
||||
}
|
||||
|
||||
private void onRequestSucceeded(Event e) {
|
||||
this.visibleResponseLayer = ResponseLayer.RESPONSE;
|
||||
private void onRequestCancelled(Event event) {
|
||||
this.visibleResponseLayer = ResponseLayer.PROMPT;
|
||||
requestManager.reset();
|
||||
}
|
||||
|
||||
private void onRequestSucceeded(Event event) {
|
||||
visibleResponseLayer = ResponseLayer.RESPONSE;
|
||||
EverestResponse response = requestManager.getValue();
|
||||
responseCode = response.getStatusCode();
|
||||
if (response.getMediaType() != null)
|
||||
responseType = response.getMediaType().toString();
|
||||
else
|
||||
responseType = "";
|
||||
responseTime = (int) response.getTime();
|
||||
responseSize = response.getSize();
|
||||
responseBody = response.getBody();
|
||||
|
@ -82,7 +99,8 @@ public class DashboardState {
|
|||
response.getHeaders().forEach((key, value) -> responseHeaders.put(key, value.get(0)));
|
||||
}
|
||||
|
||||
private void onRequestFailed(Event e) {
|
||||
// TODO: Clean this method
|
||||
private void onRequestFailed(Event event) {
|
||||
this.visibleResponseLayer = ResponseLayer.ERROR;
|
||||
Throwable throwable = requestManager.getException();
|
||||
Exception exception = (Exception) throwable;
|
||||
|
@ -93,20 +111,24 @@ public class DashboardState {
|
|||
errorTitle = URE.getExceptionTitle();
|
||||
errorDetails = URE.getExceptionDetails();
|
||||
} else if (throwable.getClass() == ProcessingException.class) {
|
||||
System.out.println(throwable.getCause().toString());
|
||||
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();
|
||||
return;
|
||||
} catch (MalformedURLException MURLE) {
|
||||
Services.loggingService.logInfo("Invalid URL: " + this.composer.target, LocalDateTime.now());
|
||||
}
|
||||
|
||||
return;
|
||||
} else {
|
||||
errorTitle = "Oops... That's embarrassing!";
|
||||
errorDetails = "Something went wrong. Try to make another request.Restart Everest if that doesn't work.";
|
||||
}
|
||||
|
||||
if (requestManager.getClass() == DataDispatchRequestManager.class) {
|
||||
|
@ -122,6 +144,10 @@ public class DashboardState {
|
|||
requestManager.reset();
|
||||
}
|
||||
|
||||
public RequestManager getRequestManager() {
|
||||
return this.requestManager;
|
||||
}
|
||||
|
||||
public DashboardState() {
|
||||
}
|
||||
|
||||
|
|
|
@ -79,8 +79,6 @@ class Logger {
|
|||
} else {
|
||||
builder.append("Stack trace unavailable.");
|
||||
}
|
||||
} else {
|
||||
builder.append("");
|
||||
}
|
||||
|
||||
logEntry = logEntry.replace("%% StackTrace %%", builder.toString());
|
||||
|
|
|
@ -19,6 +19,7 @@ package com.rohitawate.everest.requestmanager;
|
|||
import com.rohitawate.everest.models.responses.EverestResponse;
|
||||
import javafx.concurrent.Task;
|
||||
|
||||
import javax.ws.rs.ProcessingException;
|
||||
import javax.ws.rs.client.Invocation;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
|
@ -29,7 +30,7 @@ public class DELETERequestManager extends RequestManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected Task<EverestResponse> createTask() {
|
||||
protected Task<EverestResponse> createTask() throws ProcessingException {
|
||||
return new Task<EverestResponse>() {
|
||||
@Override
|
||||
protected EverestResponse call() throws Exception {
|
||||
|
|
|
@ -22,6 +22,7 @@ import javafx.concurrent.Task;
|
|||
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
||||
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
|
||||
|
||||
import javax.ws.rs.ProcessingException;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.client.Invocation;
|
||||
import javax.ws.rs.core.Form;
|
||||
|
@ -46,7 +47,7 @@ public class DataDispatchRequestManager extends RequestManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected Task<EverestResponse> createTask() {
|
||||
protected Task<EverestResponse> createTask() throws ProcessingException {
|
||||
return new Task<EverestResponse>() {
|
||||
@Override
|
||||
protected EverestResponse call() throws Exception {
|
||||
|
|
|
@ -19,6 +19,7 @@ package com.rohitawate.everest.requestmanager;
|
|||
import com.rohitawate.everest.models.responses.EverestResponse;
|
||||
import javafx.concurrent.Task;
|
||||
|
||||
import javax.ws.rs.ProcessingException;
|
||||
import javax.ws.rs.core.Response;
|
||||
|
||||
public class GETRequestManager extends RequestManager {
|
||||
|
@ -28,7 +29,7 @@ public class GETRequestManager extends RequestManager {
|
|||
}
|
||||
|
||||
@Override
|
||||
protected Task<EverestResponse> createTask() {
|
||||
protected Task<EverestResponse> createTask() throws ProcessingException {
|
||||
return new Task<EverestResponse>() {
|
||||
@Override
|
||||
protected EverestResponse call() throws Exception {
|
||||
|
|
|
@ -21,6 +21,8 @@ import com.rohitawate.everest.models.requests.EverestRequest;
|
|||
import com.rohitawate.everest.models.responses.EverestResponse;
|
||||
import com.rohitawate.everest.settings.Settings;
|
||||
import javafx.concurrent.Service;
|
||||
import javafx.concurrent.WorkerStateEvent;
|
||||
import javafx.event.EventHandler;
|
||||
import org.glassfish.jersey.client.ClientProperties;
|
||||
import org.glassfish.jersey.client.HttpUrlConnectorProvider;
|
||||
import org.glassfish.jersey.media.multipart.MultiPartFeature;
|
||||
|
@ -29,8 +31,6 @@ import javax.ws.rs.client.Client;
|
|||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.Invocation.Builder;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public abstract class RequestManager extends Service<EverestResponse> {
|
||||
private final Client client;
|
||||
|
@ -73,23 +73,17 @@ public abstract class RequestManager extends Service<EverestResponse> {
|
|||
}
|
||||
|
||||
private void appendHeaders() {
|
||||
HashMap<String, String> headers = request.getHeaders();
|
||||
Map.Entry<String, String> mapEntry;
|
||||
|
||||
for (Map.Entry entry : headers.entrySet()) {
|
||||
mapEntry = (Map.Entry) entry;
|
||||
requestBuilder.header(mapEntry.getKey(), mapEntry.getValue());
|
||||
}
|
||||
request.getHeaders().forEach((key, value) -> requestBuilder.header(key, value));
|
||||
}
|
||||
|
||||
void processServerResponse(Response serverResponse)
|
||||
throws UnreliableResponseException, RedirectException {
|
||||
if (serverResponse == null)
|
||||
if (serverResponse == null) {
|
||||
throw new UnreliableResponseException("The server did not respond.",
|
||||
"Like that crush from high school..");
|
||||
else if (serverResponse.getStatus() == 301) {
|
||||
String newLocation = serverResponse.getHeaderString("location");
|
||||
throw new RedirectException(newLocation);
|
||||
} else if (serverResponse.getStatus() == 301) {
|
||||
throw new RedirectException(
|
||||
serverResponse.getHeaderString("location"));
|
||||
}
|
||||
|
||||
String responseBody = serverResponse.readEntity(String.class);
|
||||
|
@ -102,4 +96,21 @@ public abstract class RequestManager extends Service<EverestResponse> {
|
|||
response.setStatusCode(serverResponse.getStatus());
|
||||
response.setSize(responseBody.length());
|
||||
}
|
||||
|
||||
public void addHandlers(EventHandler<WorkerStateEvent> running,
|
||||
EventHandler<WorkerStateEvent> succeeded,
|
||||
EventHandler<WorkerStateEvent> failed,
|
||||
EventHandler<WorkerStateEvent> cancelled) {
|
||||
setOnRunning(running);
|
||||
setOnSucceeded(succeeded);
|
||||
setOnFailed(failed);
|
||||
setOnCancelled(cancelled);
|
||||
}
|
||||
|
||||
public void removeHandlers() {
|
||||
removeEventHandler(WorkerStateEvent.WORKER_STATE_RUNNING, getOnRunning());
|
||||
removeEventHandler(WorkerStateEvent.WORKER_STATE_SUCCEEDED, getOnSucceeded());
|
||||
removeEventHandler(WorkerStateEvent.WORKER_STATE_FAILED, getOnFailed());
|
||||
removeEventHandler(WorkerStateEvent.WORKER_STATE_CANCELLED, getOnCancelled());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,6 +50,7 @@ public class RequestManagersPool {
|
|||
GETRequestManager newManager = new GETRequestManager();
|
||||
getManagers.add(newManager);
|
||||
|
||||
System.out.println(getManagers.size());
|
||||
return newManager;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue