diff --git a/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java b/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java index 1b6f89f..23cdd9e 100644 --- a/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java +++ b/src/main/java/com/rohitawate/everest/controllers/BodyTabController.java @@ -20,7 +20,6 @@ import com.rohitawate.everest.controllers.codearea.EverestCodeArea; import com.rohitawate.everest.controllers.codearea.highlighters.HighlighterFactory; import com.rohitawate.everest.controllers.state.ComposerState; import com.rohitawate.everest.controllers.state.FieldState; -import com.rohitawate.everest.format.FormatterFactory; import com.rohitawate.everest.misc.Services; import com.rohitawate.everest.misc.ThemeManager; import javafx.fxml.FXML; @@ -29,6 +28,7 @@ import javafx.fxml.Initializable; import javafx.scene.Parent; import javafx.scene.control.ComboBox; import javafx.scene.control.Tab; +import javafx.scene.control.TabPane; import javafx.scene.control.TextField; import javafx.scene.layout.VBox; import javafx.stage.FileChooser; @@ -54,12 +54,18 @@ public class BodyTabController implements Initializable { @FXML TextField filePathField; @FXML + private TabPane bodyTabPane; + @FXML private VBox rawVBox; EverestCodeArea rawInputArea; FormDataTabController formDataTabController; URLTabController urlTabController; + public enum BodyTab { + FORM, URL, RAW, BINARY + } + @Override public void initialize(URL location, ResourceBundle resources) { rawInputTypeBox.getItems().addAll("PLAIN TEXT", "JSON", "XML", "HTML"); @@ -74,14 +80,7 @@ public class BodyTabController implements Initializable { String type = rawInputTypeBox.getValue(); if (type.equals("JSON")) { - try { - rawInputArea.setText(rawInputArea.getText(), - FormatterFactory.getHighlighter(type), - HighlighterFactory.getHighlighter(type)); - } catch (IOException e) { - Services.loggingService.logWarning("Response could not be parsed.", e, LocalDateTime.now()); - } - + rawInputArea.setHighlighter(HighlighterFactory.getHighlighter(type)); return; } @@ -122,33 +121,34 @@ public class BodyTabController implements Initializable { public ComposerState getState() { ComposerState state = new ComposerState(); - state.rawBodyType = rawInputTypeBox.getValue(); - state.rawBody = rawInputArea.getText(); state.urlStringTuples = urlTabController.getFieldStates(); state.formStringTuples = formDataTabController.getStringFieldStates(); state.formFileTuples = formDataTabController.getFileFieldStates(); state.binaryFilePath = filePathField.getText(); + state.rawBody = rawInputArea.getText(); + switch (rawInputTypeBox.getValue()) { + case "JSON": + state.rawBodyContentType = MediaType.APPLICATION_JSON; + break; + case "XML": + state.rawBodyContentType = MediaType.APPLICATION_XML; + break; + case "HTML": + state.rawBodyContentType = MediaType.TEXT_HTML; + break; + default: + state.rawBodyContentType = MediaType.TEXT_PLAIN; + } + if (rawTab.isSelected()) { - switch (rawInputTypeBox.getValue()) { - case "JSON": - state.contentType = MediaType.APPLICATION_JSON; - break; - case "XML": - state.contentType = MediaType.APPLICATION_XML; - break; - case "HTML": - state.contentType = MediaType.TEXT_HTML; - break; - default: - state.contentType = MediaType.TEXT_PLAIN; - } + state.visibleBodyTab = BodyTab.RAW; } else if (formTab.isSelected()) { - state.contentType = MediaType.MULTIPART_FORM_DATA; + state.visibleBodyTab = BodyTab.FORM; } else if (urlTab.isSelected()) { - state.contentType = MediaType.APPLICATION_FORM_URLENCODED; + state.visibleBodyTab = BodyTab.URL; } else { - state.contentType = MediaType.APPLICATION_OCTET_STREAM; + state.visibleBodyTab = BodyTab.BINARY; } return state; @@ -174,6 +174,26 @@ public class BodyTabController implements Initializable { setRawTab(state); filePathField.setText(state.binaryFilePath); + + int tab; + if (state.visibleBodyTab != null) { + switch (state.visibleBodyTab) { + case BINARY: + tab = 3; + break; + case URL: + tab = 1; + break; + case RAW: + tab = 2; + break; + default: + tab = 0; + } + } else { + tab = 0; + } + bodyTabPane.getSelectionModel().select(tab); } void reset() { @@ -185,9 +205,26 @@ public class BodyTabController implements Initializable { } private void setRawTab(ComposerState state) { - if (state.rawBodyType != null && state.rawBody != null) { - rawInputArea.setText(state.rawBody, HighlighterFactory.getHighlighter(state.rawBodyType)); + if (state.rawBodyContentType != null && state.rawBody != null) { + // TODO: Remove this conversion + String simplifiedContentType; + switch (state.rawBodyContentType) { + case MediaType.APPLICATION_JSON: + simplifiedContentType = "JSON"; + break; + case MediaType.APPLICATION_XML: + simplifiedContentType = "XML"; + break; + case MediaType.TEXT_HTML: + simplifiedContentType = "HTML"; + break; + default: + simplifiedContentType = "PLAIN TEXT"; + } + rawInputTypeBox.setValue(simplifiedContentType); + rawInputArea.setText(state.rawBody, HighlighterFactory.getHighlighter(simplifiedContentType)); } else { + rawInputTypeBox.setValue("PLAIN TEXT"); rawInputArea.setHighlighter(HighlighterFactory.getHighlighter("PLAIN TEXT")); } } diff --git a/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java b/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java index 219ac53..d5c737b 100644 --- a/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java +++ b/src/main/java/com/rohitawate/everest/controllers/HistoryItemController.java @@ -99,7 +99,7 @@ public class HistoryItemController implements Initializable, Searchable params; public ArrayList headers; - // Determined from the active tab within the Body tab - public String contentType; - // Body and content-type of requests with raw bodies public String rawBody; - public String rawBodyType; + public String rawBodyContentType; // Tuples of URL-encoded requests public ArrayList urlStringTuples; diff --git a/src/main/java/com/rohitawate/everest/history/HistoryManager.java b/src/main/java/com/rohitawate/everest/history/HistoryManager.java index 6358c90..6c0a610 100644 --- a/src/main/java/com/rohitawate/everest/history/HistoryManager.java +++ b/src/main/java/com/rohitawate/everest/history/HistoryManager.java @@ -142,7 +142,7 @@ public class HistoryManager { if (RS.next()) contentType = RS.getString("ContentType"); - state.contentType = contentType; + state.rawBodyContentType = contentType; // Retrieves body from corresponding table switch (contentType) { @@ -282,10 +282,10 @@ public class HistoryManager { if (RS.next()) previousContentType = RS.getString("ContentType"); - if (!newState.contentType.equals(previousContentType)) + if (!newState.rawBodyContentType.equals(previousContentType)) return false; - switch (newState.contentType) { + switch (newState.rawBodyContentType) { case MediaType.TEXT_PLAIN: case MediaType.APPLICATION_JSON: case MediaType.APPLICATION_XML: @@ -391,12 +391,12 @@ public class HistoryManager { // Maps the request to its ContentType for faster retrieval statement = conn.prepareStatement(EverestUtilities.trimString(queries.get("saveRequestContentPair").toString())); statement.setInt(1, requestID); - statement.setString(2, state.contentType); + statement.setString(2, state.rawBodyContentType); statement.executeUpdate(); // Determines where to fetch the body from, based on the ContentType - switch (state.contentType) { + switch (state.rawBodyContentType) { case MediaType.TEXT_PLAIN: case MediaType.APPLICATION_JSON: case MediaType.APPLICATION_XML: diff --git a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java index 70cab29..1abf333 100644 --- a/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java +++ b/src/main/java/com/rohitawate/everest/requestmanager/RequestManager.java @@ -74,6 +74,7 @@ public abstract class RequestManager extends Service { private void appendHeaders() { request.getHeaders().forEach((key, value) -> requestBuilder.header(key, value)); + requestBuilder.header("User-Agent", "Everest"); } void processServerResponse(Response serverResponse) @@ -81,7 +82,7 @@ public abstract class RequestManager extends Service { if (serverResponse == null) { throw new UnreliableResponseException("The server did not respond.", "Like that crush from high school.."); - } else if (serverResponse.getStatus() == 301) { + } else if (serverResponse.getStatus() == 301 || serverResponse.getStatus() == 302) { throw new RedirectException( serverResponse.getHeaderString("location")); } diff --git a/src/main/resources/fxml/homewindow/BodyTab.fxml b/src/main/resources/fxml/homewindow/BodyTab.fxml index 862229c..1ac209c 100644 --- a/src/main/resources/fxml/homewindow/BodyTab.fxml +++ b/src/main/resources/fxml/homewindow/BodyTab.fxml @@ -20,9 +20,9 @@ - +