Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,15 @@ private ResponseListener<QueryResponse> createQueryResponseListener(RestChannel
@Override
public void onResponse(QueryResponse response) {
sendResponse(channel, OK,
formatter.format(new QueryResult(response.getSchema(), response.getResults())));
formatter.format(new QueryResult(response.getSchema(), response.getResults())),
formatter.contentType());
}

@Override
public void onFailure(Exception e) {
LOG.error("Error happened during query handling", e);
logAndPublishMetrics(e);
sendResponse(channel, INTERNAL_SERVER_ERROR, formatter.format(e));
sendResponse(channel, INTERNAL_SERVER_ERROR, formatter.format(e), formatter.contentType());
}
};
}
Expand All @@ -188,8 +189,12 @@ private <T> T doPrivileged(PrivilegedExceptionAction<T> action) {
}

private void sendResponse(RestChannel channel, RestStatus status, String content) {
channel.sendResponse(new BytesRestResponse(
status, "application/json; charset=UTF-8", content));
sendResponse(channel, status, content, "application/json; charset=UTF-8");
}

private void sendResponse(RestChannel channel, RestStatus status, String content,
String contentType) {
channel.sendResponse(new BytesRestResponse(status, contentType, content));
}

private static void logAndPublishMetrics(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

@RequiredArgsConstructor
public abstract class FlatResponseFormatter implements ResponseFormatter<QueryResult> {
public static final String CONTENT_TYPE = "plain/text; charset=UTF-8";

private static String INLINE_SEPARATOR = ",";
private static final String INTERLINE_SEPARATOR = System.lineSeparator();
private static final Set<String> SENSITIVE_CHAR = ImmutableSet.of("=", "+", "-", "@");
Expand All @@ -30,6 +32,10 @@ public FlatResponseFormatter(String seperator, boolean sanitize) {
this.sanitize = sanitize;
}

public String contentType() {
return CONTENT_TYPE;
}

@Override
public String format(QueryResult response) {
FlatResult result = new FlatResult(response, sanitize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
@RequiredArgsConstructor
public abstract class JsonResponseFormatter<R> implements ResponseFormatter<R> {

public static final String CONTENT_TYPE = "application/json; charset=UTF-8";

/**
* JSON format styles: pretty format or compact format without indent and space.
*/
Expand All @@ -36,6 +38,10 @@ public enum Style {
*/
private final Style style;

public String contentType() {
return CONTENT_TYPE;
}

@Override
public String format(R response) {
return jsonify(buildJsonObject(response));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,11 @@ public interface ResponseFormatter<R> {
*/
String format(Throwable t);

/**
* The contentType of the formatter.
*
* @return string with the contentType of the formatter
*/
String contentType();

}
Loading