diff --git a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java index aa74f83..6edb677 100644 --- a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java +++ b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java @@ -17,9 +17,11 @@ package com.rohitawate.restaurant.dashboard; import com.jfoenix.controls.JFXButton; import com.jfoenix.controls.JFXSnackbar; +import com.rohitawate.restaurant.models.requests.DELETERequest; import com.rohitawate.restaurant.models.requests.DataDispatchRequest; import com.rohitawate.restaurant.models.requests.GETRequest; import com.rohitawate.restaurant.models.responses.RestaurantResponse; +import com.rohitawate.restaurant.requestsmanager.DELETERequestManager; import com.rohitawate.restaurant.requestsmanager.DataDispatchRequestManager; import com.rohitawate.restaurant.requestsmanager.GETRequestManager; import com.rohitawate.restaurant.requestsmanager.RequestManager; @@ -108,7 +110,7 @@ public class DashboardController implements Initializable { paramsControllers = new ArrayList<>(); appendedParams = new ArrayList<>(); - addParam(); + addParam(); // Adds a blank param field snackBar = new JFXSnackbar(dashboard); bodyTab.disableProperty().bind(Bindings.and(httpMethodBox.valueProperty().isNotEqualTo("POST"), @@ -220,6 +222,48 @@ public class DashboardController implements Initializable { }); requestManager.start(); break; + case "DELETE": + if (requestManager == null || requestManager.getClass() != DELETERequestManager.class) + requestManager = new DELETERequestManager(); + else if (requestManager.isRunning()) { + snackBar.show("Please wait while the current request is processed.", 3000); + return; + } + + DELETERequest deleteRequest = new DELETERequest(addressField.getText()); + deleteRequest.addHeaders(headerTabController.getHeaders()); + requestManager.setRequest(deleteRequest); + cancelButton.setOnAction(e -> requestManager.cancel()); + requestManager.setOnRunning(e -> { + responseArea.clear(); + errorLayer.setVisible(false); + loadingLayer.setVisible(true); + }); + requestManager.setOnSucceeded(e -> { + updateDashboard(requestManager.getValue()); + errorLayer.setVisible(false); + loadingLayer.setVisible(false); + requestManager.reset(); + }); + requestManager.setOnCancelled(e -> { + loadingLayer.setVisible(false); + promptLayer.setVisible(true); + snackBar.show("Request canceled.", 2000); + requestManager.reset(); + }); + requestManager.setOnFailed(e -> { + loadingLayer.setVisible(false); + errorLayer.setVisible(true); + Throwable exception = requestManager.getException().getCause(); + + if (exception.getClass() == UnknownHostException.class) { + errorTitle.setText("No Internet Connection"); + errorDetails.setText("Could not connect to the server. Please check your connection."); + } + requestManager.reset(); + }); + requestManager.start(); + break; default: loadingLayer.setVisible(false); } diff --git a/src/main/java/com/rohitawate/restaurant/models/requests/DELETERequest.java b/src/main/java/com/rohitawate/restaurant/models/requests/DELETERequest.java new file mode 100644 index 0000000..d23c0d9 --- /dev/null +++ b/src/main/java/com/rohitawate/restaurant/models/requests/DELETERequest.java @@ -0,0 +1,33 @@ +/* + * 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.restaurant.models.requests; + +import java.net.MalformedURLException; +import java.net.URL; + +public class DELETERequest extends RestaurantRequest { + public DELETERequest() { + } + + public DELETERequest(String target) throws MalformedURLException { + super(target); + } + + public DELETERequest(URL target) { + super(target); + } +} diff --git a/src/main/java/com/rohitawate/restaurant/requestsmanager/DELETERequestManager.java b/src/main/java/com/rohitawate/restaurant/requestsmanager/DELETERequestManager.java new file mode 100644 index 0000000..a724e07 --- /dev/null +++ b/src/main/java/com/rohitawate/restaurant/requestsmanager/DELETERequestManager.java @@ -0,0 +1,111 @@ +/* + * 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.restaurant.requestsmanager; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.rohitawate.restaurant.models.requests.DELETERequest; +import com.rohitawate.restaurant.models.responses.RestaurantResponse; +import javafx.concurrent.Task; + +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +public class DELETERequestManager extends RequestManager { + @Override + protected Task createTask() { + return new Task() { + @Override + protected RestaurantResponse call() throws Exception { + DELETERequest deleteRequest = (DELETERequest) request; + + RestaurantResponse response = new RestaurantResponse(); + WebTarget target = client.target(deleteRequest.getTarget().toString()); + Map.Entry mapEntry; + + Invocation.Builder requestBuilder = target.request(); + + // Add the headers to the request. + HashMap headers = deleteRequest.getHeaders(); + for (Map.Entry entry : headers.entrySet()) { + mapEntry = (Map.Entry) entry; + requestBuilder.header(mapEntry.getKey(), mapEntry.getValue()); + } + + Invocation invocation = requestBuilder.buildDelete(); + + long initialTime = System.currentTimeMillis(); + Response serverResponse = invocation.invoke(); + response.setTime(initialTime, System.currentTimeMillis()); + + if (serverResponse == null) + throw new IOException(); + else if (serverResponse.getStatus() == 301) { + response.setStatusCode(301); + String newLocation = serverResponse.getHeaderString("location"); + String responseHelpText; + + if (newLocation == null) + responseHelpText = "The resource has been permanently moved to another location.\n\n" + + "Here's what you can do:\n" + + "- Find the new URL from the API documentation.\n" + + "- Try using https instead of http if you're not already."; + else + responseHelpText = "The resource has been permanently moved to: " + newLocation; + + response.setBody(responseHelpText); + return response; + } + + String type = (String) serverResponse.getHeaders().getFirst("Content-type"); + String responseBody = serverResponse.readEntity(String.class); + + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationFeature.INDENT_OUTPUT, true); + + switch (type.toLowerCase()) { + case "application/json; charset=utf-8": + case "application/json": + JsonNode node = mapper.readTree(responseBody); + response.setBody(mapper.writeValueAsString(node)); + break; + case "application/xml; charset=utf-8": + case "application/xml": + response.setBody(mapper.writeValueAsString(responseBody)); + break; + case "text/html": + case "text/html; charset=utf-8": + response.setBody(responseBody); + break; + default: + response.setBody(responseBody); + } + + response.setMediaType(serverResponse.getMediaType()); + response.setStatusCode(serverResponse.getStatus()); + response.setSize(responseBody.length()); + + return response; + } + }; + } +}