diff --git a/pom.xml b/pom.xml index e81771f..e871f41 100644 --- a/pom.xml +++ b/pom.xml @@ -1,50 +1,92 @@ - - 4.0.0 - com.rohitawate - RESTaurant - 1.0-SNAPSHOT - jar - - UTF-8 - 1.8 - 1.8 - - - - - com.jfoenix - jfoenix - 1.4.0 - - - - org.glassfish.jersey.core - jersey-client - 2.26 - - - - javax.ws.rs - javax.ws.rs-api - 2.1 - - - org.glassfish.jersey.inject - jersey-hk2 - 2.26 - - - - com.fasterxml.jackson.core - jackson-core - 2.9.3 - - - - com.fasterxml.jackson.core - jackson-databind - 2.9.3 - - + + 4.0.0 + com.rohitawate + RESTaurant + 1.0-SNAPSHOT + jar + + UTF-8 + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + com.rohitawate.restaurant.main.Main + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4 + + + package + + shade + + + true + true + + ${java.io.tmpdir}/dependency-reduced-pom.xml + + + + com.acme.coyote + hidden.coyote + + + + + + + + + + + + com.jfoenix + jfoenix + 1.4.0 + + + + org.glassfish.jersey.core + jersey-client + 2.26 + + + + javax.ws.rs + javax.ws.rs-api + 2.1 + + + org.glassfish.jersey.inject + jersey-hk2 + 2.26 + + + + com.fasterxml.jackson.core + jackson-core + 2.9.3 + + + + com.fasterxml.jackson.core + jackson-databind + 2.9.3 + + \ No newline at end of file diff --git a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java index f6b80a8..4668de8 100644 --- a/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java +++ b/src/main/java/com/rohitawate/restaurant/dashboard/DashboardController.java @@ -16,25 +16,24 @@ package com.rohitawate.restaurant.dashboard; import com.jfoenix.controls.JFXSnackbar; +import com.rohitawate.restaurant.models.RestaurantResponse; import com.rohitawate.restaurant.requests.RequestManager; +import javafx.fxml.FXML; +import javafx.fxml.Initializable; +import javafx.scene.control.ComboBox; +import javafx.scene.control.Label; +import javafx.scene.control.TextArea; +import javafx.scene.control.TextField; +import javafx.scene.layout.BorderPane; +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; -import java.util.logging.Level; -import java.util.logging.Logger; -import javafx.fxml.FXML; -import javafx.fxml.Initializable; -import javafx.scene.control.ComboBox; -import javafx.scene.control.TextArea; -import javafx.scene.control.TextField; -import javafx.scene.layout.BorderPane; -/** - * FXML Controller class - * - * @author Rohit Awate - */ public class DashboardController implements Initializable { @FXML private BorderPane dashboard; @@ -43,7 +42,13 @@ public class DashboardController implements Initializable { @FXML private ComboBox httpMethodBox; @FXML - private TextArea responseArea; + private VBox responseBox; + @FXML + private HBox responseDetails; + @FXML + private TextArea responseArea; + @FXML + private Label statusCode, statusCodeDescription, responseTime, responseSize; private JFXSnackbar snackBar; private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"}; @@ -51,7 +56,8 @@ public class DashboardController implements Initializable { @Override public void initialize(URL url, ResourceBundle rb) { - httpMethodBox.getItems().addAll(httpMethods); + responseBox.getChildren().remove(0); + httpMethodBox.getItems().addAll(httpMethods); httpMethodBox.setValue("GET"); responseArea.wrapTextProperty().set(true); @@ -67,14 +73,22 @@ public class DashboardController implements Initializable { snackBar.show("Please enter a valid address", 7000); return; } - String response = ""; + RestaurantResponse response; URL url = new URL(address); switch (httpMethodBox.getValue()) { case "GET": response = requestManager.get(url); break; + default: + response = new RestaurantResponse(); } - responseArea.setText(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) { diff --git a/src/main/java/com/rohitawate/restaurant/main/Main.java b/src/main/java/com/rohitawate/restaurant/main/Main.java index fdfb337..401dbf6 100644 --- a/src/main/java/com/rohitawate/restaurant/main/Main.java +++ b/src/main/java/com/rohitawate/restaurant/main/Main.java @@ -21,10 +21,6 @@ import javafx.scene.Parent; import javafx.scene.Scene; import javafx.stage.Stage; -/** - * - * @author Rohit Awate - */ public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { diff --git a/src/main/java/com/rohitawate/restaurant/models/RestaurantResponse.java b/src/main/java/com/rohitawate/restaurant/models/RestaurantResponse.java new file mode 100644 index 0000000..7f15312 --- /dev/null +++ b/src/main/java/com/rohitawate/restaurant/models/RestaurantResponse.java @@ -0,0 +1,67 @@ +/* + * 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; + +import javax.ws.rs.core.MediaType; + +public class RestaurantResponse { + private String body; + private int statusCode; + private long time; + private int size; + private MediaType mediaType; + + public String getBody() { + return body; + } + + public void setBody(String body) { + this.body = body; + } + + public long getTime() { + return time; + } + + public void setTime(long initialTime, long finalTime) { + this.time = finalTime - initialTime; + } + + public int getSize() { + return size; + } + + public void setSize(int size) { + this.size = size; + } + + public MediaType getMediaType() { + return mediaType; + } + + public void setMediaType(MediaType mediaType) { + this.mediaType = mediaType; + } + + public int getStatusCode() { + return statusCode; + } + + public void setStatusCode(int statusCode) { + this.statusCode = statusCode; + } +} diff --git a/src/main/java/com/rohitawate/restaurant/requests/RequestManager.java b/src/main/java/com/rohitawate/restaurant/requests/RequestManager.java index fde40f3..71a7696 100644 --- a/src/main/java/com/rohitawate/restaurant/requests/RequestManager.java +++ b/src/main/java/com/rohitawate/restaurant/requests/RequestManager.java @@ -18,48 +18,55 @@ package com.rohitawate.restaurant.requests; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; +import com.rohitawate.restaurant.models.RestaurantResponse; + 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; +import java.net.URL; -/** - * - * @author Rohit Awate - */ public class RequestManager { - private final Client client; + private final Client client; - public RequestManager() { - client = ClientBuilder.newClient(); - } + public RequestManager() { + client = ClientBuilder.newClient(); + } - public String get(URL url) throws MalformedURLException, IOException { - String responseBody; - WebTarget target = client.target(url.toString()); + public RestaurantResponse get(URL url) throws IOException { + RestaurantResponse response = new RestaurantResponse(); + WebTarget target = client.target(url.toString()); - Response response = target.request().get(); - String type = (String) response.getHeaders().getFirst("Content-type"); - System.out.println(type); - responseBody = response.readEntity(String.class); + long initialTime = System.currentTimeMillis(); + Response serverResponse = target.request().get(); + response.setTime(initialTime, System.currentTimeMillis()); - ObjectMapper mapper = new ObjectMapper(); - mapper.configure(SerializationFeature.INDENT_OUTPUT, true); - - switch (type) { - case "application/json": - JsonNode node = mapper.readTree(responseBody); - responseBody = mapper.writeValueAsString(node); - break; - case "application/xml": - responseBody = mapper.writeValueAsString(responseBody); - break; - } + if (serverResponse == null) + throw new IOException(); - return responseBody; - } + String type = (String) serverResponse.getHeaders().getFirst("Content-type"); + System.out.println(type); + String responseBody = serverResponse.readEntity(String.class); + + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationFeature.INDENT_OUTPUT, true); + + switch (type) { + case "application/json": + JsonNode node = mapper.readTree(responseBody); + response.setBody(mapper.writeValueAsString(node)); + break; + case "application/xml": + response.setBody(mapper.writeValueAsString(responseBody)); + break; + } + + response.setMediaType(serverResponse.getMediaType()); + response.setStatusCode(serverResponse.getStatus()); + response.setSize(responseBody.length()); + + return response; + } } diff --git a/src/main/resources/fxml/Dashboard.fxml b/src/main/resources/fxml/Dashboard.fxml index 028d373..398cc58 100644 --- a/src/main/resources/fxml/Dashboard.fxml +++ b/src/main/resources/fxml/Dashboard.fxml @@ -3,6 +3,7 @@ + @@ -55,7 +56,48 @@ -