Search in sources :

Example 1 with InvalidRequestException

use of de.fraunhofer.iosb.ilt.faaast.service.endpoint.http.exception.InvalidRequestException 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)

Aggregations

ServiceContext (de.fraunhofer.iosb.ilt.faaast.service.ServiceContext)1 SerializationException (de.fraunhofer.iosb.ilt.faaast.service.dataformat.SerializationException)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 OutputModifier (de.fraunhofer.iosb.ilt.faaast.service.model.api.modifier.OutputModifier)1 RequestWithModifier (de.fraunhofer.iosb.ilt.faaast.service.model.request.RequestWithModifier)1 ServletException (jakarta.servlet.ServletException)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)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