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);
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
@ -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);
|
@ -37,7 +37,7 @@ import ch.psi.daq.domain.request.range.RequestRangePulseId;
|
||||
import ch.psi.daq.domain.request.range.RequestRangeTime;
|
||||
import ch.psi.daq.domain.test.TestTimeUtils;
|
||||
import ch.psi.daq.queryrest.controller.QueryRestController;
|
||||
import ch.psi.daq.queryrest.response.csv.CSVResponse;
|
||||
import ch.psi.daq.queryrest.response.csv.CSVHTTPResponse;
|
||||
import ch.psi.daq.queryrest.response.csv.CSVResponseStreamWriter;
|
||||
import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
|
||||
|
||||
@ -62,7 +62,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
0,
|
||||
1),
|
||||
channels);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -155,7 +155,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
-1,
|
||||
-1),
|
||||
channels);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -245,7 +245,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
-1,
|
||||
-1),
|
||||
channels);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -332,7 +332,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
0,
|
||||
1),
|
||||
testChannel3));
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
channels = Arrays.asList(TEST_CHANNEL_01, TEST_CHANNEL_02, testChannel3);
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
@ -418,7 +418,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
0,
|
||||
1),
|
||||
channels);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -502,7 +502,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
TimeUtils.getTimeFromMillis(0, 0),
|
||||
TimeUtils.getTimeFromMillis(10, 0)),
|
||||
channels);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -587,7 +587,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
startDate,
|
||||
endDate),
|
||||
channels);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -675,7 +675,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
Ordering.asc,
|
||||
AggregationType.extrema,
|
||||
TEST_CHANNEL_NAMES[0]);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
@ -705,7 +705,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
Ordering.asc,
|
||||
AggregationType.index,
|
||||
TEST_CHANNEL_NAMES[0]);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
@ -739,7 +739,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
endDate),
|
||||
channels);
|
||||
request.setNrOfBins(2);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -839,7 +839,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
endDate),
|
||||
channels);
|
||||
request.setBinSize(100);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -934,7 +934,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
0,
|
||||
1),
|
||||
channels);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
LinkedHashSet<QueryField> queryFields = new LinkedHashSet<>();
|
||||
queryFields.add(QueryField.channel);
|
||||
@ -995,7 +995,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
10,
|
||||
11),
|
||||
TEST_CHANNEL_NAMES);
|
||||
request.setResponse(new CSVResponse(Compression.GZIP));
|
||||
request.setResponse(new CSVHTTPResponse(Compression.GZIP));
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
@ -1017,7 +1017,7 @@ public class QueryRestControllerCsvTest extends AbstractDaqRestTest {
|
||||
10,
|
||||
11),
|
||||
TEST_CHANNEL_NAMES);
|
||||
request.setResponse(new CSVResponse());
|
||||
request.setResponse(new CSVHTTPResponse());
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
|
||||
|
@ -24,7 +24,7 @@ import ch.psi.daq.domain.request.range.RequestRangePulseId;
|
||||
import ch.psi.daq.domain.request.range.RequestRangeTime;
|
||||
import ch.psi.daq.domain.test.TestTimeUtils;
|
||||
import ch.psi.daq.queryrest.controller.QueryRestController;
|
||||
import ch.psi.daq.queryrest.response.json.JSONResponse;
|
||||
import ch.psi.daq.queryrest.response.json.JSONHTTPResponse;
|
||||
import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
|
||||
|
||||
/**
|
||||
@ -665,7 +665,7 @@ public class QueryRestControllerJsonTest extends AbstractDaqRestTest {
|
||||
10,
|
||||
11),
|
||||
TEST_CHANNEL_NAMES);
|
||||
request.setResponse(new JSONResponse(Compression.GZIP));
|
||||
request.setResponse(new JSONHTTPResponse(Compression.GZIP));
|
||||
|
||||
String content = mapper.writeValueAsString(request);
|
||||
System.out.println(content);
|
||||
|
@ -19,8 +19,8 @@ import ch.psi.daq.domain.query.DAQQuery;
|
||||
import ch.psi.daq.domain.query.operation.Compression;
|
||||
import ch.psi.daq.domain.query.operation.Response;
|
||||
import ch.psi.daq.domain.request.range.RequestRangePulseId;
|
||||
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;
|
||||
import ch.psi.daq.test.queryrest.AbstractDaqRestTest;
|
||||
|
||||
public class ResponseQueryTest extends AbstractDaqRestTest{
|
||||
@ -30,7 +30,7 @@ public class ResponseQueryTest extends AbstractDaqRestTest{
|
||||
|
||||
@Test
|
||||
public void test_JSON_01() throws JsonParseException, JsonMappingException, IOException {
|
||||
Response respose = new CSVResponse();
|
||||
Response respose = new CSVHTTPResponse();
|
||||
|
||||
String value = mapper.writeValueAsString(respose);
|
||||
|
||||
@ -48,7 +48,7 @@ public class ResponseQueryTest extends AbstractDaqRestTest{
|
||||
0,
|
||||
100),
|
||||
"TestChannel_01");
|
||||
query.setResponse(new CSVResponse(Compression.GZIP));
|
||||
query.setResponse(new CSVHTTPResponse(Compression.GZIP));
|
||||
|
||||
String value = mapper.writeValueAsString(query);
|
||||
|
||||
@ -69,7 +69,7 @@ public class ResponseQueryTest extends AbstractDaqRestTest{
|
||||
0,
|
||||
100),
|
||||
"TestChannel_01");
|
||||
query.setResponse(new JSONResponse(Compression.NONE));
|
||||
query.setResponse(new JSONHTTPResponse(Compression.NONE));
|
||||
|
||||
String value = mapper.writeValueAsString(query);
|
||||
|
||||
|
Reference in New Issue
Block a user