Skip to content
Merged
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 @@ -141,8 +141,7 @@ public void testNullContentType()
(request) -> {
},
(statusCode, responseBody) -> {
Assert.assertEquals(statusCode, HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE.getCode(), responseBody);
assertStringCompare("Unsupported Content-Type:", responseBody, responseBody::contains);
Assert.assertEquals(statusCode, HttpResponseStatus.BAD_REQUEST.getCode(), responseBody);
}
);
}
Expand All @@ -157,7 +156,6 @@ public void testUnsupportedContentType()
},
(statusCode, responseBody) -> {
Assert.assertEquals(statusCode, HttpResponseStatus.UNSUPPORTED_MEDIA_TYPE.getCode(), responseBody);
assertStringCompare("Unsupported Content-Type:", responseBody, responseBody::contains);
}
);
}
Expand Down Expand Up @@ -220,6 +218,17 @@ public void testJSON()
Assert.assertEquals(responseBody, "[{\"EXPR$0\":567}]");
}
);

executeQuery(
"application/json; charset=UTF-8",
"{\"query\":\"select 567\"}",
(request) -> {
},
(statusCode, responseBody) -> {
Assert.assertEquals(statusCode, 200, responseBody);
Assert.assertEquals(responseBody, "[{\"EXPR$0\":567}]");
}
);
}

@Test
Expand Down
27 changes: 15 additions & 12 deletions sql/src/main/java/org/apache/druid/sql/http/SqlQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,31 @@ private static SqlQuery from(
ISqlQueryExtractor<String> rawQueryExtractor
) throws HttpException
{
try {
if (MediaType.APPLICATION_JSON.equals(contentType)) {
if (contentType == null) {
throw new HttpException(Response.Status.BAD_REQUEST, "Missing Content-Type header");
}

final MediaType requestMediaType;
try {
requestMediaType = MediaType.valueOf(contentType);
}
catch (IllegalArgumentException e) {
throw new HttpException(Response.Status.BAD_REQUEST, "Invalid Content-Type header: " + e.getMessage());
}
try {
if (MediaType.APPLICATION_JSON_TYPE.isCompatible(requestMediaType)) {
SqlQuery sqlQuery = jsonQueryExtractor.extract();
if (sqlQuery == null) {
throw new HttpException(Response.Status.BAD_REQUEST, "Empty query");
}
return sqlQuery;

} else if (MediaType.TEXT_PLAIN.equals(contentType)) {

} else if (MediaType.TEXT_PLAIN_TYPE.isCompatible(requestMediaType)) {
String sql = rawQueryExtractor.extract().trim();
if (sql.isEmpty()) {
throw new HttpException(Response.Status.BAD_REQUEST, "Empty query");
}

return new SqlQuery(sql, null, false, false, false, null, null);

} else if (MediaType.APPLICATION_FORM_URLENCODED.equals(contentType)) {

} else if (MediaType.APPLICATION_FORM_URLENCODED_TYPE.isCompatible(requestMediaType)) {
String sql = rawQueryExtractor.extract().trim();
if (sql.isEmpty()) {
throw new HttpException(Response.Status.BAD_REQUEST, "Empty query");
Expand All @@ -311,15 +316,13 @@ private static SqlQuery from(
"Unable to decode URL-Encoded SQL query: " + e.getMessage()
);
}

return new SqlQuery(sql, null, false, false, false, null, null);

} else {
throw new HttpException(
Response.Status.UNSUPPORTED_MEDIA_TYPE,
StringUtils.format(
"Unsupported Content-Type: %s. Only application/json, text/plain or application/x-www-form-urlencoded is supported.",
contentType
requestMediaType.toString()
)
);
}
Expand Down
28 changes: 17 additions & 11 deletions sql/src/main/java/org/apache/druid/sql/http/SqlResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import javax.annotation.Nullable;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
Expand Down Expand Up @@ -114,6 +115,22 @@ public Response getSupportedEngines(@Context final HttpServletRequest request)
return Response.ok(new SupportedEnginesResponse(engines)).build();
}

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes({
MediaType.APPLICATION_JSON,
MediaType.TEXT_PLAIN,
MediaType.APPLICATION_FORM_URLENCODED,
})
@Nullable
public Response doPost(
@Context final HttpServletRequest req,
@Context final HttpContext httpContext
)
{
return doPost(SqlQuery.from(httpContext), req);
}

/**
* API to list all running queries, for all engines that supports such listings.
*
Expand Down Expand Up @@ -147,17 +164,6 @@ public Response doGetRunningQueries(
return Response.ok().entity(new GetQueriesResponse(queries)).build();
}

@POST
@Produces(MediaType.APPLICATION_JSON)
@Nullable
public Response doPost(
@Context final HttpServletRequest req,
@Context final HttpContext httpContext
)
{
return doPost(SqlQuery.from(httpContext), req);
}

/**
* This method is defined as public so that tests can access it
*/
Expand Down
Loading
Loading