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);
}
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);
}
}
Aggregations