diff --git a/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java b/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java index 0e1d8a3..0717c1e 100644 --- a/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java +++ b/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java @@ -20,6 +20,7 @@ import com.rohitawate.everest.controllers.codearea.EverestCodeArea; import com.rohitawate.everest.controllers.codearea.highlighters.HighlighterFactory; import com.rohitawate.everest.logging.LoggingService; import com.rohitawate.everest.misc.ThemeManager; +import com.rohitawate.everest.models.requests.HTTPConstants; import com.rohitawate.everest.state.ComposerState; import com.rohitawate.everest.state.FieldState; import javafx.fxml.FXML; @@ -68,7 +69,12 @@ public class BodyTabController implements Initializable { @Override public void initialize(URL location, ResourceBundle resources) { - rawInputTypeBox.getItems().addAll("PLAIN TEXT", "JSON", "XML", "HTML"); + rawInputTypeBox.getItems().addAll( + HTTPConstants.JSON, + HTTPConstants.XML, + HTTPConstants.HTML, + HTTPConstants.PLAIN_TEXT + ); rawInputTypeBox.getSelectionModel().select(0); rawInputArea = new EverestCodeArea(); @@ -79,7 +85,7 @@ public class BodyTabController implements Initializable { rawInputTypeBox.valueProperty().addListener(change -> { String type = rawInputTypeBox.getValue(); - if (type.equals("JSON")) { + if (type.equals(HTTPConstants.PLAIN_TEXT)) { rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(type)); return; } @@ -128,27 +134,31 @@ public class BodyTabController implements Initializable { state.rawBody = rawInputArea.getText(); switch (rawInputTypeBox.getValue()) { - case "JSON": - state.rawBodyContentType = MediaType.APPLICATION_JSON; + case HTTPConstants.JSON: + state.rawBodyBoxValue = MediaType.APPLICATION_JSON; break; - case "XML": - state.rawBodyContentType = MediaType.APPLICATION_XML; + case HTTPConstants.XML: + state.rawBodyBoxValue = MediaType.APPLICATION_XML; break; - case "HTML": - state.rawBodyContentType = MediaType.TEXT_HTML; + case HTTPConstants.HTML: + state.rawBodyBoxValue = MediaType.TEXT_HTML; break; default: - state.rawBodyContentType = MediaType.TEXT_PLAIN; + state.rawBodyBoxValue = MediaType.TEXT_PLAIN; } if (rawTab.isSelected()) { state.visibleBodyTab = BodyTab.RAW; + state.contentType = state.rawBodyBoxValue; } else if (formTab.isSelected()) { state.visibleBodyTab = BodyTab.FORM; + state.contentType = MediaType.MULTIPART_FORM_DATA; } else if (urlTab.isSelected()) { state.visibleBodyTab = BodyTab.URL; + state.contentType = MediaType.APPLICATION_FORM_URLENCODED; } else { state.visibleBodyTab = BodyTab.BINARY; + state.contentType = MediaType.APPLICATION_OCTET_STREAM; } return state; @@ -200,32 +210,32 @@ public class BodyTabController implements Initializable { urlTabController.clear(); formDataTabController.clear(); rawInputArea.clear(); - rawInputTypeBox.setValue("PLAIN TEXT"); + rawInputTypeBox.setValue(HTTPConstants.PLAIN_TEXT); filePathField.clear(); } private void setRawTab(ComposerState state) { - if (state.rawBodyContentType != null && state.rawBody != null) { + if (state.rawBodyBoxValue != null && state.rawBody != null) { // TODO: Remove this conversion String simplifiedContentType; - switch (state.rawBodyContentType) { + switch (state.rawBodyBoxValue) { case MediaType.APPLICATION_JSON: - simplifiedContentType = "JSON"; + simplifiedContentType = HTTPConstants.JSON; break; case MediaType.APPLICATION_XML: - simplifiedContentType = "XML"; + simplifiedContentType = HTTPConstants.XML; break; case MediaType.TEXT_HTML: - simplifiedContentType = "HTML"; + simplifiedContentType = HTTPConstants.HTML; break; default: - simplifiedContentType = "PLAIN TEXT"; + simplifiedContentType = HTTPConstants.PLAIN_TEXT; } rawInputTypeBox.setValue(simplifiedContentType); rawInputArea.setText(state.rawBody, HighlighterFactory.getHighlighter(simplifiedContentType)); } else { - rawInputTypeBox.setValue("PLAIN TEXT"); - rawInputArea.setHighlighter(HighlighterFactory.getHighlighter("PLAIN TEXT")); + rawInputTypeBox.setValue(HTTPConstants.PLAIN_TEXT); + rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(HTTPConstants.PLAIN_TEXT)); } } } diff --git a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java index ec72c4f..d4f588b 100644 --- a/src/main/java/com/rohitawate/everest/controllers/DashboardController.java +++ b/src/main/java/com/rohitawate/everest/controllers/DashboardController.java @@ -32,6 +32,7 @@ import com.rohitawate.everest.misc.ThemeManager; import com.rohitawate.everest.models.requests.DELETERequest; import com.rohitawate.everest.models.requests.DataRequest; import com.rohitawate.everest.models.requests.GETRequest; +import com.rohitawate.everest.models.requests.HTTPConstants; import com.rohitawate.everest.models.responses.EverestResponse; import com.rohitawate.everest.requestmanager.RequestManager; import com.rohitawate.everest.requestmanager.RequestManagersPool; @@ -93,7 +94,6 @@ public class DashboardController implements Initializable { private JFXProgressBar progressBar; private JFXSnackbar snackbar; - private final String[] httpMethods = {"GET", "POST", "PUT", "DELETE", "PATCH"}; private List paramsControllers; private RequestManager requestManager; private HeaderTabController headerTabController; @@ -144,10 +144,15 @@ public class DashboardController implements Initializable { snackbar = new JFXSnackbar(dashboard); showLayer(ResponseLayer.PROMPT); - httpMethodBox.getItems().addAll(httpMethods); + httpMethodBox.getItems().addAll( + HTTPConstants.GET, + HTTPConstants.POST, + HTTPConstants.PUT, + HTTPConstants.PATCH, + HTTPConstants.DELETE); // Select GET by default - httpMethodBox.getSelectionModel().select("GET"); + httpMethodBox.getSelectionModel().select(HTTPConstants.GET); paramsControllers = new ArrayList<>(); paramsCountProperty = new SimpleIntegerProperty(0); @@ -155,8 +160,8 @@ public class DashboardController implements Initializable { addParamField(); // Adds a blank param field bodyTab.disableProperty().bind( - Bindings.or(httpMethodBox.valueProperty().isEqualTo("GET"), - httpMethodBox.valueProperty().isEqualTo("DELETE"))); + Bindings.or(httpMethodBox.valueProperty().isEqualTo(HTTPConstants.GET), + httpMethodBox.valueProperty().isEqualTo(HTTPConstants.DELETE))); // Disabling Ctrl+Tab navigation requestOptionsTab.setOnKeyPressed(e -> { @@ -172,7 +177,11 @@ public class DashboardController implements Initializable { snackbar.show("Response body copied to clipboard.", 5000); }); - responseTypeBox.getItems().addAll("JSON", "XML", "HTML", "PLAIN TEXT"); + responseTypeBox.getItems().addAll( + HTTPConstants.JSON, + HTTPConstants.XML, + HTTPConstants.HTML, + HTTPConstants.PLAIN_TEXT); responseTypeBox.valueProperty().addListener(change -> { String type = responseTypeBox.getValue(); @@ -231,7 +240,7 @@ public class DashboardController implements Initializable { addressField.setText(address); switch (httpMethodBox.getValue()) { - case "GET": + case HTTPConstants.GET: if (getRequest == null) getRequest = new GETRequest(); @@ -241,9 +250,9 @@ public class DashboardController implements Initializable { requestManager = RequestManagersPool.manager(); requestManager.setRequest(getRequest); break; - case "POST": - case "PUT": - case "PATCH": + case HTTPConstants.POST: + case HTTPConstants.PUT: + case HTTPConstants.PATCH: if (dataRequest == null) dataRequest = new DataRequest(); @@ -286,7 +295,7 @@ public class DashboardController implements Initializable { requestManager = RequestManagersPool.manager(); requestManager.setRequest(dataRequest); break; - case "DELETE": + case HTTPConstants.DELETE: if (deleteRequest == null) deleteRequest = new DELETERequest(); @@ -644,9 +653,9 @@ public class DashboardController implements Initializable { ComposerState composerState; switch (httpMethodBox.getValue()) { - case "POST": - case "PUT": - case "PATCH": + case HTTPConstants.POST: + case HTTPConstants.PUT: + case HTTPConstants.PATCH: composerState = bodyTabController.getState(); break; default: @@ -781,6 +790,8 @@ public class DashboardController implements Initializable { This is an extreme case, but still something to be taken care of. */ boolean validMethod = false; + String[] httpMethods = + {HTTPConstants.GET, HTTPConstants.POST, HTTPConstants.PUT, HTTPConstants.PATCH, HTTPConstants.DELETE}; for (String method : httpMethods) { if (method.equals(state.composer.httpMethod)) validMethod = true; @@ -806,12 +817,12 @@ public class DashboardController implements Initializable { addParamField(fieldState); } - if (!(httpMethodBox.getValue().equals("GET") || httpMethodBox.getValue().equals("DELETE"))) + if (!(state.composer.httpMethod.equals(HTTPConstants.GET) || state.composer.httpMethod.equals(HTTPConstants.DELETE))) bodyTabController.setState(state.composer); } void reset() { - httpMethodBox.setValue("GET"); + httpMethodBox.setValue(HTTPConstants.GET); addressField.clear(); headerTabController.clear(); clearParams(); diff --git a/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java b/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java index 604e02e..13df130 100644 --- a/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java +++ b/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java @@ -18,6 +18,7 @@ package com.rohitawate.everest.controllers; import com.rohitawate.everest.controllers.search.Searchable; import com.rohitawate.everest.logging.LoggingService; +import com.rohitawate.everest.models.requests.HTTPConstants; import com.rohitawate.everest.state.ComposerState; import com.rohitawate.everest.state.FieldState; import javafx.fxml.FXML; @@ -55,13 +56,14 @@ public class HistoryItemController implements Initializable, Searchable * Previously, Everest used separate managers for GET, Data (POST, PUT and PATCH) and DELETE requests. * However, RequestManager extends JavaFX's Service class, which is expensive to create objects of. * This made the creation of separate pools for every kind of RequestManager too expensive, memory-wise. @@ -60,7 +57,7 @@ import java.util.Map; * Also, this enables us to re-use inactive RequestManagers for all kinds of requests. * For example, previously, if a GETRequestManager was requested by Everest, and all GETRequestManagers were running, * a new one would be created even if a DELETERequestManager was idle. - * + *

* TLDR: At the cost of some reduced semantic clarity, the old, separate-for-every-type-of-request RequestManagers * are now replaced by this single works-for-all one to save some serious amount of memory and to facilitate better re-use. */ @@ -234,10 +231,7 @@ public class RequestManager extends Service { formData.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE); - if (requestType.equals("POST")) - invocation = requestBuilder.buildPost(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE)); - else - invocation = requestBuilder.buildPut(Entity.entity(formData, MediaType.MULTIPART_FORM_DATA_TYPE)); + invocation = getInvocation(MediaType.MULTIPART_FORM_DATA, requestType, formData, requestBuilder); break; case MediaType.APPLICATION_OCTET_STREAM: if (overriddenContentType == null) @@ -252,10 +246,7 @@ public class RequestManager extends Service { FileInputStream stream = new FileInputStream(filePath); - if (requestType.equals("POST")) - invocation = requestBuilder.buildPost(Entity.entity(stream, overriddenContentType)); - else - invocation = requestBuilder.buildPut(Entity.entity(stream, overriddenContentType)); + invocation = getInvocation(overriddenContentType, requestType, stream, requestBuilder); break; case MediaType.APPLICATION_FORM_URLENCODED: if (overriddenContentType == null) @@ -268,10 +259,7 @@ public class RequestManager extends Service { form.param(mapEntry.getKey(), mapEntry.getValue()); } - if (requestType.equals("POST")) - invocation = requestBuilder.buildPost(Entity.entity(form, overriddenContentType)); - else - invocation = requestBuilder.buildPut(Entity.entity(form, overriddenContentType)); + invocation = getInvocation(overriddenContentType, requestType, form, requestBuilder); break; default: // Handles raw data types (JSON, Plain text, XML, HTML) @@ -279,21 +267,37 @@ public class RequestManager extends Service { if (overriddenContentType == null) overriddenContentType = originalContentType; switch (requestType) { - case "POST": + case HTTPConstants.POST: invocation = requestBuilder .buildPost(Entity.entity(dataRequest.getBody(), overriddenContentType)); break; - case "PUT": + case HTTPConstants.PUT: invocation = requestBuilder .buildPut(Entity.entity(dataRequest.getBody(), overriddenContentType)); break; - case "PATCH": + case HTTPConstants.PATCH: invocation = requestBuilder - .build("PATCH", Entity.entity(dataRequest.getBody(), overriddenContentType)); + .build(HTTPConstants.PATCH, Entity.entity(dataRequest.getBody(), overriddenContentType)); break; } } return invocation; } + + private static Invocation getInvocation(String overriddenContentType, String requestType, Object entity, Builder requestBuilder) { + Invocation invocation; + switch (requestType) { + case HTTPConstants.POST: + invocation = requestBuilder.buildPost(Entity.entity(entity, overriddenContentType)); + break; + case HTTPConstants.PUT: + invocation = requestBuilder.buildPut(Entity.entity(entity, overriddenContentType)); + break; + default: + invocation = requestBuilder.build(HTTPConstants.PATCH, Entity.entity(entity, overriddenContentType)); + break; + } + return invocation; + } } diff --git a/src/main/java/com/rohitawate/everest/state/ComposerState.java b/src/main/java/com/rohitawate/everest/state/ComposerState.java index 713adfe..d2b4f35 100644 --- a/src/main/java/com/rohitawate/everest/state/ComposerState.java +++ b/src/main/java/com/rohitawate/everest/state/ComposerState.java @@ -17,6 +17,7 @@ package com.rohitawate.everest.state; import com.rohitawate.everest.controllers.BodyTabController.BodyTab; +import com.rohitawate.everest.models.requests.HTTPConstants; import java.util.ArrayList; @@ -27,14 +28,15 @@ public class ComposerState { public BodyTab visibleBodyTab; public String target; - public String httpMethod; + public ArrayList params; public ArrayList headers; + public String contentType; // Body and content-type of requests with raw bodies public String rawBody; - public String rawBodyContentType; + public String rawBodyBoxValue; // Tuples of URL-encoded requests public ArrayList urlStringTuples; @@ -47,6 +49,6 @@ public class ComposerState { public String binaryFilePath; public ComposerState() { - this.httpMethod = "GET"; + this.httpMethod = HTTPConstants.GET; } }