Added RestaurantRequest, GETRequest classes

This commit is contained in:
Rohit Awate 2018-01-27 22:24:13 +05:30
parent b2b1301892
commit f286c6853d
6 changed files with 91 additions and 8 deletions

8
.gitignore vendored
View file

@ -1,2 +1,6 @@
nb-configuration.xml
/target/
/target/
/RESTaurant.iml
.idea/
RESTaurant.iml
classes/
src/main/java/META-INF/

View file

@ -16,6 +16,7 @@
package com.rohitawate.restaurant.dashboard;
import com.jfoenix.controls.JFXSnackbar;
import com.rohitawate.restaurant.models.GETRequest;
import com.rohitawate.restaurant.models.RestaurantResponse;
import com.rohitawate.restaurant.requests.RequestManager;
import javafx.fxml.FXML;
@ -73,10 +74,10 @@ public class DashboardController implements Initializable {
return;
}
RestaurantResponse response;
URL url = new URL(address);
switch (httpMethodBox.getValue()) {
case "GET":
response = requestManager.get(url);
GETRequest GETRequest = new GETRequest(addressField.getText());
response = requestManager.get(GETRequest);
break;
default:
response = new RestaurantResponse();

View file

@ -0,0 +1,30 @@
/*
* 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 java.net.MalformedURLException;
import java.net.URL;
public class GETRequest extends RestaurantRequest {
public GETRequest(URL target) {
super(target);
}
public GETRequest(String target) throws MalformedURLException {
super(target);
}
}

View file

@ -0,0 +1,46 @@
/*
* 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 java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
abstract class RestaurantRequest {
private URL target;
private HashMap<String, String> headers;
RestaurantRequest(String target) throws MalformedURLException {
this.target = new URL(target);
}
RestaurantRequest(URL target) {
this.target = target;
}
public URL getTarget() {
return target;
}
public void addHeader(String key, String value) {
headers.put(key, value);
}
public HashMap<String, String> getHeaders() {
return this.headers;
}
}

View file

@ -18,6 +18,7 @@ package com.rohitawate.restaurant.requests;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.rohitawate.restaurant.models.GETRequest;
import com.rohitawate.restaurant.models.RestaurantResponse;
import javax.ws.rs.client.Client;
@ -25,7 +26,6 @@ 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;
public class RequestManager {
@ -35,9 +35,9 @@ public class RequestManager {
client = ClientBuilder.newClient();
}
public RestaurantResponse get(URL url) throws IOException {
public RestaurantResponse get(GETRequest GETRequest) throws IOException {
RestaurantResponse response = new RestaurantResponse();
WebTarget target = client.target(url.toString());
WebTarget target = client.target(GETRequest.getTarget().toString());
long initialTime = System.currentTimeMillis();
Response serverResponse = target.request().get();
@ -46,6 +46,7 @@ public class RequestManager {
if (serverResponse == null)
throw new IOException();
System.out.println(serverResponse);
String type = (String) serverResponse.getHeaders().getFirst("Content-type");
System.out.println(type);
String responseBody = serverResponse.readEntity(String.class);
@ -54,6 +55,7 @@ public class RequestManager {
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));

View file

@ -52,7 +52,7 @@
}
.jfx-snackbar-content {
-fx-background-color: darkslategray;
-fx-background-color: #282828;
}
.jfx-snackbar-toast {