Search in sources :

Example 1 with SerializationException

use of de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException in project FAAAST-Service by FraunhoferIOSB.

the class RequestHandler method handle.

@Override
public void handle(String string, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8")) {

        @Override
        public void close() throws IOException {
            request.getInputStream().close();
        }
    };
    HttpRequest httpRequest = HttpRequest.builder().path(request.getRequestURI()).query(request.getQueryString()).body(reader.lines().collect(Collectors.joining(System.lineSeparator()))).method(HttpMethod.valueOf(request.getMethod())).headers(Collections.list(request.getHeaderNames()).stream().collect(Collectors.toMap(x -> x, x -> request.getHeader(x)))).build();
    de.fraunhofer.iosb.ilt.faaast.service.model.api.Request apiRequest = null;
    try {
        apiRequest = mappingManager.map(httpRequest);
    } catch (InvalidRequestException | IllegalArgumentException ex) {
        // ex.printStackTrace();
        sendResultResponse(response, HttpStatus.BAD_REQUEST_400, MessageType.Error, ex.getMessage());
        baseRequest.setHandled(true);
        return;
    }
    // TODO more differentiated error codes (must be generated in mappingManager)
    try {
        executeAndSend(response, apiRequest);
    } catch (SerializationException e) {
        e.printStackTrace();
    }
    baseRequest.setHandled(true);
}
Also used : HttpRequest(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.model.HttpRequest) PrintWriter(java.io.PrintWriter) Request(org.eclipse.jetty.server.Request) HttpMethod(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.model.HttpMethod) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) Date(java.util.Date) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpRequest(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.model.HttpRequest) OutputModifier(de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier) ServiceContext(de.fraunhofer.iosb.ilt.faaast.service.ServiceContext) IOException(java.io.IOException) HttpJsonSerializer(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.serialization.HttpJsonSerializer) ServletException(jakarta.servlet.ServletException) SerializationException(de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) de.fraunhofer.iosb.ilt.faaast.service.model.api(de.fraunhofer.iosb.ilt.faaast.service.model.api) InvalidRequestException(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.exception.InvalidRequestException) RequestMappingManager(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.request.RequestMappingManager) RequestWithModifier(de.fraunhofer.iosb.ilt.faaast.service.model.request.RequestWithModifier) HttpHelper(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.util.HttpHelper) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) BufferedReader(java.io.BufferedReader) HttpStatus(org.eclipse.jetty.http.HttpStatus) Collections(java.util.Collections) SerializationException(de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) InvalidRequestException(de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.exception.InvalidRequestException) de.fraunhofer.iosb.ilt.faaast.service.model.api(de.fraunhofer.iosb.ilt.faaast.service.model.api)

Example 2 with SerializationException

use of de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException 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)2 OutputModifier (de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier)2 RequestWithModifier (de.fraunhofer.iosb.ilt.faaast.service.model.request.RequestWithModifier)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 ServiceContext (de.fraunhofer.iosb.ilt.faaast.service.ServiceContext)1 InvalidRequestException (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.exception.InvalidRequestException)1 HttpMethod (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.model.HttpMethod)1 HttpRequest (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.model.HttpRequest)1 RequestMappingManager (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.request.RequestMappingManager)1 HttpJsonSerializer (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.serialization.HttpJsonSerializer)1 HttpHelper (de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.util.HttpHelper)1 de.fraunhofer.iosb.ilt.faaast.service.model.api (de.fraunhofer.iosb.ilt.faaast.service.model.api)1 ServletException (jakarta.servlet.ServletException)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 Collections (java.util.Collections)1 Date (java.util.Date)1