Search in sources :

Example 1 with ResponseHeaders

use of com.b2international.snowowl.core.events.util.ResponseHeaders in project snow-owl by b2ihealthcare.

the class ApiRequestHandler method handle.

@Override
public final void handle(IMessage message) {
    try {
        final Request<ServiceProvider, ?> req = message.body(Request.class, classLoader);
        final ResponseHeaders responseHeaders = new ResponseHeaders();
        final ServiceProvider executionContext = context.inject().bind(RequestHeaders.class, new RequestHeaders(message.headers())).bind(ResponseHeaders.class, responseHeaders).build();
        // monitor each request execution
        final Object body = new MonitoredRequest<>(// authorize each request execution
        new AuthorizedRequest<>(// rate limit all requests
        new RateLimitingRequest<>(// actual request
        req))).execute(executionContext);
        if (body == null) {
            LoggerFactory.getLogger(ApiRequestHandler.class).error("No response was returned from request: " + req.getClass());
        }
        message.reply(body, responseHeaders.headers());
    } catch (WrappedException e) {
        message.fail(e.getCause());
    } catch (ApiException e) {
        message.fail(e);
    } catch (Throwable e) {
        LoggerFactory.getLogger(ApiRequestHandler.class).error("Unexpected error when executing request:", e);
        message.fail(e);
    }
}
Also used : WrappedException(org.eclipse.emf.common.util.WrappedException) AuthorizedRequest(com.b2international.snowowl.core.authorization.AuthorizedRequest) ServiceProvider(com.b2international.snowowl.core.ServiceProvider) ResponseHeaders(com.b2international.snowowl.core.events.util.ResponseHeaders) RequestHeaders(com.b2international.snowowl.core.events.util.RequestHeaders) ApiException(com.b2international.commons.exceptions.ApiException)

Aggregations

ApiException (com.b2international.commons.exceptions.ApiException)1 ServiceProvider (com.b2international.snowowl.core.ServiceProvider)1 AuthorizedRequest (com.b2international.snowowl.core.authorization.AuthorizedRequest)1 RequestHeaders (com.b2international.snowowl.core.events.util.RequestHeaders)1 ResponseHeaders (com.b2international.snowowl.core.events.util.ResponseHeaders)1 WrappedException (org.eclipse.emf.common.util.WrappedException)1