Search in sources :

Example 1 with RequestWithModifier

use of de.fraunhofer.iosb.ilt.faaast.service.model.request.RequestWithModifier in project FAAAST-Service by FraunhoferIOSB.

the class RequestHandler method executeAndSend.

private void executeAndSend(HttpServletResponse response, de.fraunhofer.iosb.ilt.faaast.service.model.api.Request apiRequest) throws IOException, SerializationException {
    // TODO forward output modifier to serializer
    if (apiRequest == null) {
        sendResultResponse(response, HttpStatus.BAD_REQUEST_400, MessageType.Error, "");
        return;
    }
    Response apiResponse = serviceContext.execute(apiRequest);
    if (apiResponse == null) {
        sendResultResponse(response, HttpStatus.INTERNAL_SERVER_ERROR_500, MessageType.Error, "");
        return;
    }
    int statusCode = HttpHelper.toHttpStatusCode(apiResponse.getStatusCode());
    if (!apiResponse.getResult().getSuccess() || !HttpStatus.isSuccess(statusCode)) {
        sendResultResponse(response, statusCode, MessageType.Error, HttpStatus.getMessage((statusCode)));
    } else if (BaseResponseWithPayload.class.isAssignableFrom(apiResponse.getClass())) {
        try {
            if (RequestWithModifier.class.isAssignableFrom(apiRequest.getClass())) {
                Object payload = ((BaseResponseWithPayload) apiResponse).getPayload();
                OutputModifier outputModifier = ((RequestWithModifier) apiRequest).getOutputModifier();
                sendJson(response, statusCode, serializer.write(payload, outputModifier));
            } else {
                sendJson(response, statusCode, serializer.write(((BaseResponseWithPayload) apiResponse).getPayload()));
            }
        } catch (SerializationException ex) {
            sendResultResponse(response, HttpStatus.INTERNAL_SERVER_ERROR_500, MessageType.Exception, ex.getMessage());
        }
    } else {
        send(response, statusCode);
    }
}
Also used : HttpServletResponse(jakarta.servlet.http.HttpServletResponse) SerializationException(de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException) OutputModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier) RequestWithModifier(de.fraunhofer.iosb.ilt.faaast.service.model.request.RequestWithModifier)

Aggregations

SerializationException (de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException)1 OutputModifier (de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier)1 RequestWithModifier (de.fraunhofer.iosb.ilt.faaast.service.model.request.RequestWithModifier)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)1