Eliminate duplicate Content-Type conversions

This commit is contained in:
Rohit Awate 2018-08-16 17:55:42 +05:30
parent 8082f002fe
commit 53a42f8de5
No known key found for this signature in database
GPG key ID: 1051D7B79CF2EE25
4 changed files with 46 additions and 69 deletions

View file

@ -134,19 +134,7 @@ public class BodyTabController implements Initializable {
state.binaryFilePath = filePathField.getText();
state.rawBody = rawInputArea.getText();
switch (rawInputTypeBox.getValue()) {
case HTTPConstants.JSON:
state.rawBodyBoxValue = MediaType.APPLICATION_JSON;
break;
case HTTPConstants.XML:
state.rawBodyBoxValue = MediaType.APPLICATION_XML;
break;
case HTTPConstants.HTML:
state.rawBodyBoxValue = MediaType.TEXT_HTML;
break;
default:
state.rawBodyBoxValue = MediaType.TEXT_PLAIN;
}
state.rawBodyBoxValue = HTTPConstants.getComplexContentType(rawInputTypeBox.getValue());
switch (bodyTabPane.getSelectionModel().getSelectedIndex()) {
case 1:
@ -219,21 +207,7 @@ public class BodyTabController implements Initializable {
private void setRawTab(ComposerState state) {
if (state.rawBodyBoxValue != null && state.rawBody != null) {
// TODO: Remove this conversion
String simplifiedContentType;
switch (state.rawBodyBoxValue) {
case MediaType.APPLICATION_JSON:
simplifiedContentType = HTTPConstants.JSON;
break;
case MediaType.APPLICATION_XML:
simplifiedContentType = HTTPConstants.XML;
break;
case MediaType.TEXT_HTML:
simplifiedContentType = HTTPConstants.HTML;
break;
default:
simplifiedContentType = HTTPConstants.PLAIN_TEXT;
}
String simplifiedContentType = HTTPConstants.getSimpleContentType(state.rawBodyBoxValue);
rawInputTypeBox.setValue(simplifiedContentType);
rawInputArea.setText(state.rawBody, HighlighterFactory.getHighlighter(simplifiedContentType));
} else {

View file

@ -260,24 +260,7 @@ public class DashboardController implements Initializable {
dataRequest.setHeaders(headerTabController.getHeaders());
if (bodyTabController.rawTab.isSelected()) {
String contentType;
switch (bodyTabController.rawInputTypeBox.getValue()) {
case HTTPConstants.PLAIN_TEXT:
contentType = MediaType.TEXT_PLAIN;
break;
case HTTPConstants.JSON:
contentType = MediaType.APPLICATION_JSON;
break;
case HTTPConstants.XML:
contentType = MediaType.APPLICATION_XML;
break;
case HTTPConstants.HTML:
contentType = MediaType.TEXT_HTML;
break;
default:
contentType = MediaType.TEXT_PLAIN;
}
dataRequest.setContentType(contentType);
dataRequest.setContentType(HTTPConstants.getComplexContentType(bodyTabController.rawInputTypeBox.getValue()));
dataRequest.setBody(bodyTabController.rawInputArea.getText());
} else if (bodyTabController.formTab.isSelected()) {
dataRequest.setStringTuples(bodyTabController.formDataTabController.getStringTuples());
@ -316,9 +299,9 @@ public class DashboardController implements Initializable {
snackbar.show("Invalid address. Please verify and try again.", 3000);
} catch (Exception E) {
LoggingService.logSevere("Request execution failed.", E, LocalDateTime.now());
showLayer(ResponseLayer.ERROR);
errorTitle.setText("Oops... That's embarrassing!");
errorDetails.setText("Something went wrong. Try to make another request.\nRestart Everest if that doesn't work.");
showLayer(ResponseLayer.ERROR);
}
}
@ -450,7 +433,10 @@ public class DashboardController implements Initializable {
try {
String simplifiedContentType;
if (contentType != null) {
// Selects only the part preceding the ';', skipping the character encoding
/*
Selects only the part preceding the ';', skipping the character encoding.
For example, "application/json; charset=utf-8" becomes "application/json"
*/
contentType = contentType.split(";")[0];
switch (contentType.toLowerCase()) {
@ -484,6 +470,9 @@ public class DashboardController implements Initializable {
simplifiedContentType = HTTPConstants.PLAIN_TEXT;
}
if (body == null || body.equals(""))
body = "No body returned in the response.";
responseArea.setText(body,
FormatterFactory.getHighlighter(simplifiedContentType),
HighlighterFactory.getHighlighter(simplifiedContentType));
@ -694,24 +683,7 @@ public class DashboardController implements Initializable {
dashboardState.responseTime = Integer.parseInt(temp);
dashboardState.responseBody = responseArea.getText();
// TODO: Get rid of similar switches
String contentType;
switch (responseTypeBox.getValue()) {
case HTTPConstants.JSON:
contentType = MediaType.APPLICATION_JSON;
break;
case HTTPConstants.XML:
contentType = MediaType.APPLICATION_XML;
break;
case HTTPConstants.HTML:
contentType = MediaType.TEXT_HTML;
break;
default:
contentType = MediaType.TEXT_PLAIN;
}
dashboardState.responseType = contentType;
dashboardState.responseType = HTTPConstants.getComplexContentType(responseTypeBox.getValue());
break;
case ERROR:
dashboardState.errorTitle = errorTitle.getText();

View file

@ -1,5 +1,7 @@
package com.rohitawate.everest.models.requests;
import javax.ws.rs.core.MediaType;
public class HTTPConstants {
public static final String GET = "GET";
public static final String POST = "POST";
@ -11,4 +13,30 @@ public class HTTPConstants {
public static final String JSON = "JSON";
public static final String XML = "XML";
public static final String HTML = "HTML";
public static String getSimpleContentType(String contentType) {
switch (contentType) {
case MediaType.APPLICATION_JSON:
return JSON;
case MediaType.APPLICATION_XML:
return XML;
case MediaType.TEXT_HTML:
return HTML;
default:
return PLAIN_TEXT;
}
}
public static String getComplexContentType(String contentType) {
switch (contentType) {
case JSON:
return MediaType.APPLICATION_JSON;
case XML:
return MediaType.APPLICATION_XML;
case HTML:
return MediaType.TEXT_HTML;
default:
return MediaType.TEXT_PLAIN;
}
}
}

View file

@ -58,9 +58,12 @@ class SQLiteManager implements DataManager {
public SQLiteManager() {
try {
File configFolder = new File("Everest/config/");
if (!configFolder.exists())
configFolder.mkdirs();
String configPath = "Everest/config/";
File configFolder = new File(configPath);
if (!configFolder.exists()) {
if (configFolder.mkdirs())
LoggingService.logSevere("Unable to create directory: " + configPath, null, LocalDateTime.now());
}
conn = DriverManager.getConnection("jdbc:sqlite:Everest/config/history.sqlite");
createDatabase();