use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class ApiContextHandlerFactoryTest method shouldCreateContext.
@Test
public void shouldCreateContext() {
apiContextHandlerFactory = spy(apiContextHandlerFactory);
AbstractApplicationContext ctx = mock(AbstractApplicationContext.class);
when(api.isEnabled()).thenReturn(true);
when(ctx.getBean(ApiReactorHandler.class)).thenReturn(mock(ApiReactorHandler.class));
doReturn(ctx).when(apiContextHandlerFactory).createApplicationContext(api);
ReactorHandler handler = apiContextHandlerFactory.create(api);
assertNotNull(handler);
assertTrue(ApiReactorHandler.class.isAssignableFrom(handler.getClass()));
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class DefaultReactorHandlerRegistry method create.
@Override
public void create(Reactable reactable) {
logger.info("Creating a new handler for {}", reactable.item());
ReactorHandler handler = prepare(reactable);
if (handler != null) {
register(handler);
}
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class DefaultReactorHandlerRegistry method update.
@Override
public void update(Reactable reactable) {
logger.info("Updating handler for {}", reactable);
String contextPath = contextPaths.get(reactable);
if (contextPath != null) {
logger.info("Handler was previously map to {}", contextPath);
ReactorHandler newHandler = prepare(reactable);
// Do not update handler if the new is not correctly initialized
if (newHandler != null) {
ReactorHandler previousHandler = handlers.get(contextPath);
register(newHandler);
if (previousHandler != null) {
try {
logger.info("Stopping previous handler for path {}", contextPath);
previousHandler.stop();
} catch (Exception ex) {
logger.error("Unable to stop handler", ex);
}
}
}
} else {
create(reactable);
}
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class DefaultReactorHandlerRegistry method remove.
@Override
public void remove(Reactable reactable) {
String contextPath = contextPaths.remove(reactable);
if (contextPath != null) {
ReactorHandler handler = handlers.remove(contextPath);
if (handler != null) {
try {
handler.stop();
handlers.remove(handler.contextPath());
logger.info("API has been unregistered");
} catch (Exception e) {
logger.error("Unable to un-register handler", e);
}
}
}
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class DefaultReactor method route.
@Override
public void route(Request serverRequest, Response serverResponse, final Handler<Response> handler) {
LOGGER.debug("Receiving a request {} for path {}", serverRequest.id(), serverRequest.path());
// Prepare handler chain
Handler<Request> requestHandlerChain = transactionHandlerFactory.create(request -> {
ReactorHandler reactorHandler = reactorHandlerResolver.resolve(request);
if (reactorHandler != null) {
// Prepare the handler chain
Handler<Response> responseHandlerChain = new ResponseTimeHandler(request, new ReporterHandler(reporterService, request, handler));
reactorHandler.handle(request, serverResponse, responseHandlerChain);
} else {
LOGGER.debug("No handler can be found for request {}, returning NOT_FOUND (404)", request.path());
sendNotFound(serverResponse, handler);
}
}, serverResponse);
// Set gateway tenant
serverRequest.metrics().setTenant(gatewayConfiguration.tenant().orElse(null));
// And handle the request
requestHandlerChain.handle(serverRequest);
}
Aggregations