Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,25 @@ public abstract class ApiFilteringActionFilter<Res extends ActionResponse> imple
private final ThreadContext threadContext;
private final String actionName;
private final Class<Res> responseClass;
private final boolean filterOperatorRequests;

protected ApiFilteringActionFilter(ThreadContext threadContext, String actionName, Class<Res> responseClass) {
this(threadContext, actionName, responseClass, false);
}

protected ApiFilteringActionFilter(
ThreadContext threadContext,
String actionName,
Class<Res> responseClass,
boolean filterOperatorRequests
) {
assert threadContext != null : "threadContext cannot be null";
assert actionName != null : "actionName cannot be null";
assert responseClass != null : "responseClass cannot be null";
this.threadContext = threadContext;
this.actionName = actionName;
this.responseClass = responseClass;
this.filterOperatorRequests = filterOperatorRequests;
}

@Override
Expand All @@ -46,7 +57,7 @@ public <Request extends ActionRequest, Response extends ActionResponse> void app
ActionFilterChain<Request, Response> chain
) {
final ActionListener<Response> responseFilteringListener;
if (isOperator(threadContext) == false && actionName.equals(action)) {
if ((filterOperatorRequests || isOperator(threadContext) == false) && actionName.equals(action)) {
responseFilteringListener = listener.map(this::filter);
} else {
responseFilteringListener = listener;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testApply() {
boolean isOperator = randomBoolean();
final ThreadContext threadContext = getTestThreadContext(isOperator);
String action = "test.action";
ApiFilteringActionFilter<TestResponse> filter = new TestFilter(threadContext);
ApiFilteringActionFilter<TestResponse> filter = new TestFilter(threadContext, false);
Task task = null;
TestRequest request = new TestRequest();
AtomicBoolean listenerCalled = new AtomicBoolean(false);
Expand Down Expand Up @@ -59,6 +59,37 @@ public void onFailure(Exception e) {
assertThat(responseModified.get(), equalTo(isOperator == false));
}

public void testApplyAsOperator() {
final ThreadContext threadContext = getTestThreadContext(true);
ApiFilteringActionFilter<TestResponse> filter = new TestFilter(threadContext, true);
Task task = null;
TestRequest request = new TestRequest();
AtomicBoolean listenerCalled = new AtomicBoolean(false);
AtomicBoolean responseModified = new AtomicBoolean(false);
ActionListener<TestResponse> listener = new ActionListener<>() {
@Override
public void onResponse(TestResponse testResponse) {
listenerCalled.set(true);
responseModified.set(testResponse.modified);
}

@Override
public void onFailure(Exception e) {
fail(Strings.format("Unexpected exception: %s", e.getMessage()));
}
};
ActionFilterChain<TestRequest, TestResponse> chain = (task1, action1, request1, listener1) -> {
listener1.onResponse(new TestResponse());
};
filter.apply(task, "wrong.action", request, listener, chain);
assertThat(listenerCalled.get(), equalTo(true));
assertThat(responseModified.get(), equalTo(false));
filter.apply(task, "test.action", request, listener, chain);
assertThat(listenerCalled.get(), equalTo(true));
// The response should always be modified
assertThat(responseModified.get(), equalTo(true));
}

public void testApplyWithException() {
/*
* This test makes sure that we have correct behavior if the filter function throws an exception. In that case we expect
Expand Down Expand Up @@ -94,8 +125,8 @@ public void onFailure(Exception e) {

private static class TestFilter extends ApiFilteringActionFilter<TestResponse> {

TestFilter(ThreadContext threadContext) {
super(threadContext, "test.action", TestResponse.class);
TestFilter(ThreadContext threadContext, boolean filterOperatorRequests) {
super(threadContext, "test.action", TestResponse.class, filterOperatorRequests);
}

@Override
Expand Down