Added background processing for requests

This commit is contained in:
Rohit Awate 2018-01-28 12:17:33 +05:30
parent 2c05263202
commit 68332f93b6
3 changed files with 113 additions and 78 deletions

View file

@ -18,6 +18,7 @@ package com.rohitawate.restaurant.dashboard;
import com.jfoenix.controls.JFXSnackbar;
import com.rohitawate.restaurant.models.requests.GETRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
import com.rohitawate.restaurant.requestsmanager.GETRequestManager;
import com.rohitawate.restaurant.requestsmanager.RequestManager;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
@ -30,7 +31,6 @@ import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ResourceBundle;
@ -61,7 +61,6 @@ public class DashboardController implements Initializable {
httpMethodBox.getItems().addAll(httpMethods);
httpMethodBox.setValue("GET");
requestManager = new RequestManager();
snackBar = new JFXSnackbar(dashboard);
}
@ -76,24 +75,29 @@ public class DashboardController implements Initializable {
RestaurantResponse response;
switch (httpMethodBox.getValue()) {
case "GET":
GETRequest request = new GETRequest(addressField.getText());
response = requestManager.get(request);
GETRequest getRequest = new GETRequest(addressField.getText());
requestManager = new GETRequestManager(getRequest);
requestManager.setOnSucceeded(e -> updateDashboard(requestManager.getValue()));
new Thread(requestManager).start();
break;
default:
response = new RestaurantResponse();
updateDashboard(response);
}
responseArea.setText(response.getBody());
if (responseBox.getChildren().size() != 2)
responseBox.getChildren().add(0, responseDetails);
statusCode.setText(Integer.toString(response.getStatusCode()));
statusCodeDescription.setText(Response.Status.fromStatusCode(response.getStatusCode()).getReasonPhrase());
responseTime.setText(Long.toString(response.getTime()) + " ms");
responseSize.setText(Integer.toString(response.getSize()) + " B");
} catch (MalformedURLException ex) {
snackBar.show("Invalid URL. Please verify and try again.", 7000);
} catch (IOException ex) {
snackBar.show("Server did not respond", 7000);
}
}
private void updateDashboard(RestaurantResponse response) {
responseArea.setText(response.getBody());
if (responseBox.getChildren().size() != 2)
responseBox.getChildren().add(0, responseDetails);
statusCode.setText(Integer.toString(response.getStatusCode()));
statusCodeDescription.setText(Response.Status.fromStatusCode(response.getStatusCode()).getReasonPhrase());
responseTime.setText(Long.toString(response.getTime()) + " ms");
responseSize.setText(Integer.toString(response.getSize()) + " B");
}
}

View file

@ -0,0 +1,92 @@
/*
* 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.GETRequest;
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
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());
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 (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;
}
response.setMediaType(serverResponse.getMediaType());
response.setStatusCode(serverResponse.getStatus());
response.setSize(responseBody.length());
return response;
}
}

View file

@ -15,79 +15,18 @@
*/
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.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.io.IOException;
public class RequestManager {
public abstract class RequestManager extends Task<RestaurantResponse> {
final Client client;
private final Client client;
public RequestManager() {
RequestManager() {
client = ClientBuilder.newClient();
}
public RestaurantResponse get(GETRequest request) throws IOException {
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());
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;
}
response.setMediaType(serverResponse.getMediaType());
response.setStatusCode(serverResponse.getStatus());
response.setSize(responseBody.length());
return response;
}
}