use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.
the class UsageService method addRestHandler.
/**
* Add a REST handler to this service.
*
* @param handler the {@link BaseRestHandler} to add to the usage service.
*/
public void addRestHandler(BaseRestHandler handler) {
Objects.requireNonNull(handler);
if (handler.getName() == null) {
throw new IllegalArgumentException("handler of type [" + handler.getClass().getName() + "] does not have a name");
}
final BaseRestHandler maybeHandler = handlers.put(handler.getName(), handler);
/*
* Handlers will be registered multiple times, once for each route that the handler handles. This means that we will see handlers
* multiple times, so we do not have a conflict if we are seeing the same instance multiple times. So, we only reject if a handler
* with the same name was registered before, and it is not the same instance as before.
*/
if (maybeHandler != null && maybeHandler != handler) {
final String message = String.format(Locale.ROOT, "handler of type [%s] conflicts with handler of type [%s] as they both have the same name [%s]", handler.getClass().getName(), maybeHandler.getClass().getName(), handler.getName());
throw new IllegalArgumentException(message);
}
}
use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.
the class UsageServiceTests method testHandlerWithConflictingNamesButSameInstance.
/**
* Test that we can add the same instance of a {@link org.opensearch.rest.RestHandler} to the {@link UsageService} multiple times.
*/
public void testHandlerWithConflictingNamesButSameInstance() {
final UsageService service = new UsageService();
final String name = randomAlphaOfLength(8);
final BaseRestHandler first = new MockRestHandler(name);
service.addRestHandler(first);
// nothing bad ever happens to me
service.addRestHandler(first);
}
use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.
the class UsageServiceTests method testAHandlerWithNoName.
/**
* Test that we can not add an instance of a {@link org.opensearch.rest.RestHandler} with no name to the {@link UsageService}.
*/
public void testAHandlerWithNoName() {
final UsageService service = new UsageService();
final BaseRestHandler horse = new MockRestHandler(null);
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.addRestHandler(horse));
assertThat(e.getMessage(), equalTo("handler of type [org.opensearch.usage.UsageServiceTests$MockRestHandler] does not have a name"));
}
use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.
the class UsageServiceTests method testHandlersWithConflictingNamesButDifferentInstances.
/**
* Test that we can not add different instances of {@link org.opensearch.rest.RestHandler} with the same name to the
* {@link UsageService}.
*/
public void testHandlersWithConflictingNamesButDifferentInstances() {
final UsageService service = new UsageService();
final String name = randomAlphaOfLength(8);
final BaseRestHandler first = new MockRestHandler(name);
final BaseRestHandler second = new MockRestHandler(name);
service.addRestHandler(first);
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> service.addRestHandler(second));
final String expected = String.format(Locale.ROOT, "handler of type [%s] conflicts with handler of type [%1$s] as they both have the same name [%s]", "org.opensearch.usage.UsageServiceTests$MockRestHandler", name);
assertThat(e.getMessage(), equalTo(expected));
}
use of org.opensearch.rest.BaseRestHandler in project OpenSearch by opensearch-project.
the class UsageServiceTests method testRestUsage.
public void testRestUsage() throws Exception {
RestRequest restRequest = new FakeRestRequest();
BaseRestHandler handlerA = new MockRestHandler("a");
BaseRestHandler handlerB = new MockRestHandler("b");
BaseRestHandler handlerC = new MockRestHandler("c");
BaseRestHandler handlerD = new MockRestHandler("d");
BaseRestHandler handlerE = new MockRestHandler("e");
BaseRestHandler handlerF = new MockRestHandler("f");
UsageService usageService = new UsageService();
usageService.addRestHandler(handlerA);
usageService.addRestHandler(handlerB);
usageService.addRestHandler(handlerC);
usageService.addRestHandler(handlerD);
usageService.addRestHandler(handlerE);
usageService.addRestHandler(handlerF);
try (NodeClient client = new NoOpNodeClient(this.getClass().getSimpleName() + "TestClient")) {
handlerA.handleRequest(restRequest, null, client);
handlerB.handleRequest(restRequest, null, client);
handlerA.handleRequest(restRequest, null, client);
handlerA.handleRequest(restRequest, null, client);
handlerB.handleRequest(restRequest, null, client);
handlerC.handleRequest(restRequest, null, client);
handlerC.handleRequest(restRequest, null, client);
handlerD.handleRequest(restRequest, null, client);
handlerA.handleRequest(restRequest, null, client);
handlerB.handleRequest(restRequest, null, client);
handlerE.handleRequest(restRequest, null, client);
handlerF.handleRequest(restRequest, null, client);
handlerC.handleRequest(restRequest, null, client);
handlerD.handleRequest(restRequest, null, client);
}
Map<String, Long> restUsage = usageService.getRestUsageStats();
assertThat(restUsage, notNullValue());
assertThat(restUsage.size(), equalTo(6));
assertThat(restUsage.get("a"), equalTo(4L));
assertThat(restUsage.get("b"), equalTo(3L));
assertThat(restUsage.get("c"), equalTo(3L));
assertThat(restUsage.get("d"), equalTo(2L));
assertThat(restUsage.get("e"), equalTo(1L));
assertThat(restUsage.get("f"), equalTo(1L));
}
Aggregations