Added header processing for GETRequestManager
This commit is contained in:
parent
d4f14f4056
commit
5d129b1c9f
6 changed files with 53 additions and 14 deletions
|
@ -61,6 +61,7 @@ public class DashboardController implements Initializable {
|
|||
private JFXSnackbar snackBar;
|
||||
private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"};
|
||||
private RequestManager requestManager;
|
||||
private HeaderTabController headerTabController;
|
||||
|
||||
@Override
|
||||
public void initialize(URL url, ResourceBundle rb) {
|
||||
|
@ -68,7 +69,10 @@ public class DashboardController implements Initializable {
|
|||
Task<Parent> parentLoader = new Task<Parent>() {
|
||||
@Override
|
||||
protected Parent call() throws Exception {
|
||||
return FXMLLoader.load(getClass().getResource("/fxml/dashboard/HeaderTab.fxml"));
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/dashboard/HeaderTab.fxml"));
|
||||
Parent parent = loader.load();
|
||||
headerTabController = loader.getController();
|
||||
return parent;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -108,6 +112,7 @@ public class DashboardController implements Initializable {
|
|||
}
|
||||
|
||||
GETRequest getRequest = new GETRequest(addressField.getText());
|
||||
getRequest.addHeaders(headerTabController.getHeaders());
|
||||
requestManager.setRequest(getRequest);
|
||||
cancelButton.setOnAction(e -> requestManager.cancel());
|
||||
requestManager.setOnRunning(e -> {
|
||||
|
|
|
@ -24,24 +24,42 @@ import javafx.scene.layout.VBox;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class HeaderTabController implements Initializable {
|
||||
@FXML
|
||||
private VBox headersBox;
|
||||
|
||||
private List<HeaderFieldController> controllers;
|
||||
|
||||
@Override
|
||||
public void initialize(URL location, ResourceBundle resources) {
|
||||
controllers = new ArrayList<>();
|
||||
addHeader();
|
||||
}
|
||||
|
||||
@FXML
|
||||
private void addHeader() {
|
||||
try {
|
||||
Parent headerField = FXMLLoader.load(getClass().getResource("/fxml/dashboard/HeaderField.fxml"));
|
||||
FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/dashboard/HeaderField.fxml"));
|
||||
Parent headerField = loader.load();
|
||||
HeaderFieldController controller = loader.getController();
|
||||
controllers.add(controller);
|
||||
headersBox.getChildren().add(headerField);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, String> getHeaders() {
|
||||
HashMap<String, String> headers = new HashMap<>();
|
||||
for (HeaderFieldController controller : controllers) {
|
||||
if (controller.isChecked())
|
||||
headers.put(controller.getHeader().getKey(), controller.getHeader().getValue());
|
||||
}
|
||||
return headers;
|
||||
}
|
||||
}
|
|
@ -40,6 +40,10 @@ public abstract class RestaurantRequest {
|
|||
headers.put(key, value);
|
||||
}
|
||||
|
||||
public void addHeaders(HashMap<String, String> headers) {
|
||||
this.headers = headers;
|
||||
}
|
||||
|
||||
public HashMap<String, String> getHeaders() {
|
||||
return this.headers;
|
||||
}
|
||||
|
|
|
@ -22,12 +22,14 @@ import com.fasterxml.jackson.databind.SerializationFeature;
|
|||
import com.rohitawate.restaurant.models.responses.RestaurantResponse;
|
||||
import javafx.concurrent.Task;
|
||||
|
||||
import javax.ws.rs.client.Invocation.Builder;
|
||||
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 GETRequestManager extends RequestManager {
|
||||
|
||||
@Override
|
||||
protected Task<RestaurantResponse> createTask() {
|
||||
return new Task<RestaurantResponse>() {
|
||||
|
@ -36,8 +38,18 @@ public class GETRequestManager extends RequestManager {
|
|||
RestaurantResponse response = new RestaurantResponse();
|
||||
WebTarget target = client.target(request.getTarget().toString());
|
||||
|
||||
Builder requestBuilder = target.request();
|
||||
|
||||
HashMap<String, String> headers = request.getHeaders();
|
||||
Map.Entry<String, String> mapEntry;
|
||||
|
||||
for (Map.Entry entry : headers.entrySet()) {
|
||||
mapEntry = (Map.Entry) entry;
|
||||
requestBuilder.header(mapEntry.getKey(), mapEntry.getValue());
|
||||
}
|
||||
|
||||
long initialTime = System.currentTimeMillis();
|
||||
Response serverResponse = target.request().get();
|
||||
Response serverResponse = requestBuilder.get();
|
||||
response.setTime(initialTime, System.currentTimeMillis());
|
||||
|
||||
if (serverResponse == null)
|
||||
|
@ -79,6 +91,8 @@ public class GETRequestManager extends RequestManager {
|
|||
case "text/html; charset=utf-8":
|
||||
response.setBody(responseBody);
|
||||
break;
|
||||
default:
|
||||
response.setBody(responseBody);
|
||||
}
|
||||
|
||||
response.setMediaType(serverResponse.getMediaType());
|
||||
|
|
|
@ -104,8 +104,8 @@
|
|||
}
|
||||
|
||||
#keyField, #valueField {
|
||||
-fx-prompt-text-fill: #2b2b2b;
|
||||
-fx-background-color: #7a7a7a;
|
||||
-fx-prompt-text-fill: #919191;
|
||||
-fx-background-color: #393939;
|
||||
-fx-text-fill: white;
|
||||
}
|
||||
|
||||
|
|
|
@ -29,9 +29,10 @@
|
|||
<center>
|
||||
<VBox prefHeight="200.0" prefWidth="100.0" BorderPane.alignment="CENTER">
|
||||
<children>
|
||||
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
|
||||
<HBox alignment="CENTER" maxHeight="100.0" VBox.vgrow="ALWAYS">
|
||||
<children>
|
||||
<ImageView fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true">
|
||||
<ImageView fitHeight="80.0" fitWidth="80.0" pickOnBounds="true" preserveRatio="true"
|
||||
HBox.hgrow="ALWAYS">
|
||||
<image>
|
||||
<Image url="@../../assets/LogoWithoutText.png"/>
|
||||
</image>
|
||||
|
@ -47,7 +48,7 @@
|
|||
<Font size="18.0" />
|
||||
</font>
|
||||
</TextField>
|
||||
<ComboBox fx:id="httpMethodBox" prefHeight="39.0" prefWidth="150.0">
|
||||
<ComboBox fx:id="httpMethodBox" prefHeight="39.0" prefWidth="150.0" HBox.hgrow="ALWAYS">
|
||||
<HBox.margin>
|
||||
<Insets right="20.0" />
|
||||
</HBox.margin>
|
||||
|
@ -64,12 +65,9 @@
|
|||
</HBox.margin>
|
||||
</JFXButton>
|
||||
</children>
|
||||
<padding>
|
||||
<Insets bottom="20.0" top="20.0" />
|
||||
</padding>
|
||||
</HBox>
|
||||
<TabPane fx:id="requestTabs" maxHeight="200.0" tabClosingPolicy="UNAVAILABLE" tabMinWidth="150.0"
|
||||
VBox.vgrow="ALWAYS">
|
||||
<TabPane fx:id="requestTabs" maxHeight="200.0" minHeight="200.0" tabClosingPolicy="UNAVAILABLE"
|
||||
tabMinWidth="150.0" VBox.vgrow="ALWAYS">
|
||||
<tabs>
|
||||
<Tab fx:id="authTab" text="AUTHORIZATION"/>
|
||||
<Tab fx:id="headersTab" text="HEADERS"/>
|
||||
|
|
Loading…
Reference in a new issue