Make Responses HTTP specific.
This commit is contained in:
@@ -42,8 +42,9 @@ import ch.psi.daq.domain.query.operation.ResponseFormat;
|
||||
import ch.psi.daq.domain.reader.Backend;
|
||||
import ch.psi.daq.domain.request.validate.RequestProviderValidator;
|
||||
import ch.psi.daq.queryrest.query.QueryManager;
|
||||
import ch.psi.daq.queryrest.response.AbstractHTTPResponse;
|
||||
import ch.psi.daq.queryrest.response.csv.CSVResponseStreamWriter;
|
||||
import ch.psi.daq.queryrest.response.json.JSONResponse;
|
||||
import ch.psi.daq.queryrest.response.json.JSONHTTPResponse;
|
||||
import ch.psi.daq.queryrest.response.json.JSONResponseStreamWriter;
|
||||
|
||||
@RestController
|
||||
@@ -74,7 +75,7 @@ public class QueryRestController {
|
||||
@Resource
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
private Response defaultResponse = new JSONResponse();
|
||||
private Response defaultResponse = new JSONHTTPResponse();
|
||||
|
||||
@InitBinder
|
||||
protected void initBinder(WebDataBinder binder) {
|
||||
@@ -178,7 +179,7 @@ public class QueryRestController {
|
||||
try {
|
||||
LOGGER.debug("Executing queries '{}'", queries);
|
||||
|
||||
queries.getResponseOrDefault(defaultResponse).respond(appContext, queries, res);
|
||||
((AbstractHTTPResponse)queries.getResponseOrDefault(defaultResponse)).respond(appContext, queries, res);
|
||||
|
||||
} catch (Exception e) {
|
||||
LOGGER.error("Failed to execute query '{}'.", queries, e);
|
||||
|
||||
+3
-4
@@ -13,15 +13,14 @@ import ch.psi.daq.domain.query.DAQQueries;
|
||||
import ch.psi.daq.domain.query.operation.ResponseFormat;
|
||||
import ch.psi.daq.domain.query.operation.ResponseImpl;
|
||||
|
||||
public abstract class AbstractResponse extends ResponseImpl {
|
||||
public abstract class AbstractHTTPResponse extends ResponseImpl {
|
||||
|
||||
public AbstractResponse(ResponseFormat format) {
|
||||
public AbstractHTTPResponse(ResponseFormat format) {
|
||||
super(format);
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
@Override
|
||||
public abstract void respond(ApplicationContext context, DAQQueries queries, Object response) throws Exception;
|
||||
public abstract void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse httpResponse) throws Exception;
|
||||
|
||||
/**
|
||||
* Configures the output stream and headers according to whether compression is wanted or not.
|
||||
@@ -4,16 +4,16 @@ import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
import ch.psi.daq.queryrest.response.csv.CSVResponse;
|
||||
import ch.psi.daq.queryrest.response.json.JSONResponse;
|
||||
import ch.psi.daq.queryrest.response.csv.CSVHTTPResponse;
|
||||
import ch.psi.daq.queryrest.response.json.JSONHTTPResponse;
|
||||
|
||||
@JsonTypeInfo(
|
||||
use = JsonTypeInfo.Id.NAME,
|
||||
include = JsonTypeInfo.As.EXISTING_PROPERTY,
|
||||
property = "format")
|
||||
@JsonSubTypes({
|
||||
@Type(value = JSONResponse.class, name = JSONResponse.FORMAT),
|
||||
@Type(value = CSVResponse.class, name = CSVResponse.FORMAT)
|
||||
@Type(value = JSONHTTPResponse.class, name = JSONHTTPResponse.FORMAT),
|
||||
@Type(value = CSVHTTPResponse.class, name = CSVHTTPResponse.FORMAT)
|
||||
})
|
||||
// see: http://stackoverflow.com/questions/24631923/alternative-to-jackson-jsonsubtypes
|
||||
public abstract class PolymorphicResponseMixIn {
|
||||
|
||||
+7
-16
@@ -24,35 +24,26 @@ import ch.psi.daq.domain.query.operation.QueryField;
|
||||
import ch.psi.daq.domain.query.operation.ResponseFormat;
|
||||
import ch.psi.daq.query.model.impl.BackendQuery;
|
||||
import ch.psi.daq.queryrest.query.QueryManager;
|
||||
import ch.psi.daq.queryrest.response.AbstractResponse;
|
||||
import ch.psi.daq.queryrest.response.AbstractHTTPResponse;
|
||||
|
||||
public class CSVResponse extends AbstractResponse {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CSVResponse.class);
|
||||
public class CSVHTTPResponse extends AbstractHTTPResponse {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(CSVHTTPResponse.class);
|
||||
|
||||
public static final String FORMAT = "csv";
|
||||
public static final String CONTENT_TYPE = "text/csv";
|
||||
|
||||
public CSVResponse() {
|
||||
public CSVHTTPResponse() {
|
||||
super(ResponseFormat.CSV);
|
||||
}
|
||||
|
||||
public CSVResponse(Compression compression) {
|
||||
public CSVHTTPResponse(Compression compression) {
|
||||
this();
|
||||
setCompression(compression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void respond(ApplicationContext context, DAQQueries queries, Object response) throws Exception {
|
||||
OutputStream out;
|
||||
if (response instanceof HttpServletResponse) {
|
||||
out = super.handleCompressionAndResponseHeaders((HttpServletResponse) response, CONTENT_TYPE);
|
||||
} else {
|
||||
String message =
|
||||
String.format("'%s' does not support response Object of type '%s'", getFormat().getKey(), response
|
||||
.getClass().getName());
|
||||
LOGGER.error(message);
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
public void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse httpResponse) throws Exception {
|
||||
OutputStream out = handleCompressionAndResponseHeaders(httpResponse, CONTENT_TYPE);
|
||||
|
||||
// do csv specific validations
|
||||
validateQueries(queries);
|
||||
+7
-16
@@ -20,35 +20,26 @@ import ch.psi.daq.domain.query.operation.Compression;
|
||||
import ch.psi.daq.domain.query.operation.ResponseFormat;
|
||||
import ch.psi.daq.query.model.impl.BackendQuery;
|
||||
import ch.psi.daq.queryrest.query.QueryManager;
|
||||
import ch.psi.daq.queryrest.response.AbstractResponse;
|
||||
import ch.psi.daq.queryrest.response.AbstractHTTPResponse;
|
||||
|
||||
public class JSONResponse extends AbstractResponse {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JSONResponse.class);
|
||||
public class JSONHTTPResponse extends AbstractHTTPResponse {
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(JSONHTTPResponse.class);
|
||||
|
||||
public static final String FORMAT = "json";
|
||||
public static final String CONTENT_TYPE = MediaType.APPLICATION_JSON_VALUE;
|
||||
|
||||
public JSONResponse() {
|
||||
public JSONHTTPResponse() {
|
||||
super(ResponseFormat.JSON);
|
||||
}
|
||||
|
||||
public JSONResponse(Compression compression) {
|
||||
public JSONHTTPResponse(Compression compression) {
|
||||
this();
|
||||
setCompression(compression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void respond(ApplicationContext context, DAQQueries queries, Object response) throws Exception {
|
||||
OutputStream out;
|
||||
if (response instanceof HttpServletResponse) {
|
||||
out = super.handleCompressionAndResponseHeaders((HttpServletResponse) response, CONTENT_TYPE);
|
||||
} else {
|
||||
String message =
|
||||
String.format("'%s' does not support response Object of type '%s'", getFormat().getKey(), response.getClass()
|
||||
.getName());
|
||||
LOGGER.error(message);
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
public void respond(ApplicationContext context, DAQQueries queries, HttpServletResponse response) throws Exception {
|
||||
OutputStream out = handleCompressionAndResponseHeaders(response, CONTENT_TYPE);
|
||||
|
||||
try {
|
||||
LOGGER.debug("Executing query '{}'", queries);
|
||||
Reference in New Issue
Block a user