Shifted RequestManager to Service

This commit is contained in:
Rohit Awate 2018-01-28 18:47:02 +05:30
parent 03c6b7c048
commit 8189de624d
4 changed files with 67 additions and 60 deletions

View file

@ -74,13 +74,15 @@ public class DashboardController implements Initializable {
try {
String address = addressField.getText();
if (address.equals("")) {
snackBar.show("Please enter a valid address", 7000);
snackBar.show("Please enter a valid address.", 7000);
return;
}
switch (httpMethodBox.getValue()) {
case "GET":
if (requestManager == null)
requestManager = new GETRequestManager();
GETRequest getRequest = new GETRequest(addressField.getText());
requestManager = new GETRequestManager(getRequest);
requestManager.setRequest(getRequest);
cancelButton.setOnAction(e -> requestManager.cancel());
requestManager.setOnRunning(e -> {
responseArea.clear();
@ -89,13 +91,14 @@ public class DashboardController implements Initializable {
requestManager.setOnSucceeded(e -> {
updateDashboard(requestManager.getValue());
loadingLayer.setVisible(false);
requestManager.reset();
});
requestManager.setOnCancelled(e -> {
loadingLayer.setVisible(false);
snackBar.show("Request canceled", 5000);
snackBar.show("Request canceled.", 5000);
requestManager.reset();
});
Thread taskThread = new Thread(requestManager);
taskThread.start();
requestManager.start();
break;
default:
loadingLayer.setVisible(false);

View file

@ -20,7 +20,7 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
abstract class RestaurantRequest {
public abstract class RestaurantRequest {
private URL target;
private HashMap<String, String> headers;

View file

@ -19,74 +19,74 @@ 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.GETRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import javafx.concurrent.Task;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.IOException;
public class GETRequestManager extends RequestManager {
private GETRequest request;
public GETRequestManager(GETRequest request) {
this.request = request;
}
@Override
protected RestaurantResponse call() throws Exception {
RestaurantResponse response = new RestaurantResponse();
WebTarget target = client.target(request.getTarget().toString());
protected Task<RestaurantResponse> createTask() {
return new Task<RestaurantResponse>() {
@Override
protected RestaurantResponse call() throws Exception {
RestaurantResponse response = new RestaurantResponse();
WebTarget target = client.target(request.getTarget().toString());
long initialTime = System.currentTimeMillis();
Response serverResponse = target.request().get();
response.setTime(initialTime, System.currentTimeMillis());
long initialTime = System.currentTimeMillis();
Response serverResponse = target.request().get();
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 (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;
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;
}
response.setBody(responseHelpText);
return response;
}
String type = (String) serverResponse.getHeaders().getFirst("Content-type");
String responseBody = serverResponse.readEntity(String.class);
String type = (String) serverResponse.getHeaders().getFirst("Content-type");
String responseBody = serverResponse.readEntity(String.class);
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
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;
}
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;
}
response.setMediaType(serverResponse.getMediaType());
response.setStatusCode(serverResponse.getStatus());
response.setSize(responseBody.length());
response.setMediaType(serverResponse.getMediaType());
response.setStatusCode(serverResponse.getStatus());
response.setSize(responseBody.length());
return response;
return response;
}
};
}
}

View file

@ -15,18 +15,22 @@
*/
package com.rohitawate.restaurant.requestsmanager;
import com.rohitawate.restaurant.models.requests.RestaurantRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import javafx.concurrent.Task;
import javafx.concurrent.Service;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
public abstract class RequestManager extends Task<RestaurantResponse> {
public abstract class RequestManager extends Service<RestaurantResponse> {
final Client client;
RestaurantRequest request;
RequestManager() {
client = ClientBuilder.newClient();
}
public void setRequest(RestaurantRequest request) {
this.request = request;
}
}