Fix issues related to RawBody type not being maintained during sessions and tab switches

This commit is contained in:
Rohit Awate 2018-07-30 20:54:00 +05:30
parent e943cf8ca9
commit 3dcb57070c
6 changed files with 82 additions and 43 deletions

View file

@ -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"));
}
}

View file

@ -99,7 +99,7 @@ public class HistoryItemController implements Initializable, Searchable<Composer
}
if (state.httpMethod.equals("POST") || state.httpMethod.equals("PUT")) {
switch (state.contentType) {
switch (state.rawBodyContentType) {
case MediaType.TEXT_PLAIN:
case MediaType.APPLICATION_JSON:
case MediaType.APPLICATION_XML:

View file

@ -16,24 +16,25 @@
package com.rohitawate.everest.controllers.state;
import com.rohitawate.everest.controllers.BodyTabController.BodyTab;
import java.util.ArrayList;
/**
* Convenience class to abstract the state of the application.
*/
public class ComposerState {
public BodyTab visibleBodyTab;
public String target;
public String httpMethod;
public ArrayList<FieldState> params;
public ArrayList<FieldState> 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<FieldState> urlStringTuples;

View file

@ -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:

View file

@ -74,6 +74,7 @@ public abstract class RequestManager extends Service<EverestResponse> {
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<EverestResponse> {
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"));
}

View file

@ -20,9 +20,9 @@
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<TabPane fx:id="bodyTabPane" prefHeight="100.0" prefWidth="1280.0" stylesheets="@../../css/Adreana.css"
tabClosingPolicy="UNAVAILABLE" tabMinWidth="150.0" xmlns="http://javafx.com/javafx/8.0.141"
xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.rohitawate.everest.controllers.BodyTabController">
<TabPane fx:id="bodyTabPane" stylesheets="@../../css/Adreana.css" tabClosingPolicy="UNAVAILABLE" tabMinWidth="150.0"
xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.rohitawate.everest.controllers.BodyTabController">
<tabs>
<Tab fx:id="formTab" text="FORM"/>
<Tab fx:id="urlTab" text="URL ENCODED"/>