# RestFilters for ES 5.x

**URL:** <https://discuss.elastic.co/t/restfilters-for-es-5-x/134123>\
**Category:** Elasticsearch\
**Created:** [June 1, 2018, 1:52am UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123 "2018-06-01T01:52:09Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vinh\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/v/3ab097/32.png) [@Vinh\_Nguyen](https://discuss.elastic.co/u/Vinh_Nguyen)\
**Post date:** [June 1, 2018, 1:52am UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/1 "2018-06-01T01:52:09Z")

</div>

Goal: log all REST requests made to ElasticSearch 5.6.4 cluster.

\*Note: in ES 2.x, this can done via RestFilter and RestFilterChain but I am struggle to find the equivalent for ES 5.x .

Implementation 1:

- I wrote a plugin (RestlogPlugin) which extends Plugin and implements ActionPlugin.
- My plugin overrides getRestHandlerWrapper() and returns a RestHandler (RestLogHandler).
- My rest handler overrides:  
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client)

Question:

- within the rest handler once I finish my custom bizlogic, how do I create RestChannelConsumer to continue processing the request through tis normal flow? Specifically, what is the 5.x equivalent of Es 2.x RestFilterChain.continueProcessing()?

Implementation 2:

- I wrote a plugin (RestlogPlugin) which extends Plugin and implements ActionPlugin.
- My plugin overrides:

getActionFilters() and returns an ActionFilter (RestLogActionFilter).

- My action filter overrides:

public \<Request extends ActionRequest, Response extends ActionResponse\> void apply(Task task, String action, Request request, ActionListener listener, ActionFilterChain\<Request, Response\> chain

Question:

- within applyTask(), Request is not a RestRequest so how do I get the information http headers of the REST request?

---

<div class="post-metadata">

**Author:** ![Vinh\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/v/3ab097/32.png) [@Vinh\_Nguyen](https://discuss.elastic.co/u/Vinh_Nguyen)\
**Post date:** [June 4, 2018, 5:48pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/2 "2018-06-04T17:48:23Z")

</div>

@s1monw . Hi Simon, apologies for pinging you directly. I am a developer here at Netflix and I have hit a blocker that I hope you can provide guidance. I found your handle base on a previous discussion of the same topic ([Basic Authentication using RestFilter](https://discuss.elastic.co/t/basic-authentication-using-restfilter/97051)).

Regards

---

<div class="post-metadata">

**Author:** ![arimoham](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@arimoham](https://discuss.elastic.co/u/arimoham)\
**Post date:** [June 4, 2018, 6:53pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/3 "2018-06-04T18:53:03Z")

</div>

Hi,

No - I did not able to find a solution for Basic Autentication using RestFilter.

Thanks

-arif

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [June 4, 2018, 7:59pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/4 "2018-06-04T19:59:33Z")

</div>

I assume you use this method: [https://github.com/elastic/elasticsearch/blob/5.6/core/src/main/java/org/elasticsearch/plugins/ActionPlugin.java#L93](https://github.com/elastic/elasticsearch/blob/5.6/core/src/main/java/org/elasticsearch/plugins/ActionPlugin.java#L93)

so you have full control over all rest requests. There is no additional logic and I don't know what you mean by RestChannelConsumer to be honest. you just wrap the incoming RestHandler in your impl and execute you biz logic before delegating to the incoming RestHandler#handleRequest method ([https://github.com/elastic/elasticsearch/blob/5.6/core/src/main/java/org/elasticsearch/rest/RestHandler.java#L36](https://github.com/elastic/elasticsearch/blob/5.6/core/src/main/java/org/elasticsearch/rest/RestHandler.java#L36)). The RestChannelConsumer class doesn't exist in 5.x?

---

<div class="post-metadata">

**Author:** ![Vinh\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/v/3ab097/32.png) [@Vinh\_Nguyen](https://discuss.elastic.co/u/Vinh_Nguyen)\
**Post date:** [June 4, 2018, 9:30pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/5 "2018-06-04T21:30:38Z")

</div>

@s1monw Hi Simon, thanks for responding. Ok, getting closer, I have the following:

public class RestlogPlugin extends Plugin implements ActionPlugin {

```
@Override
public UnaryOperator<RestHandler> getRestHandlerWrapper(ThreadContext threadContext) {

         /*
        Simon, I don't see how to get the "incoming RestHandler" that you mentioned?
         */
}

```

}

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [June 5, 2018, 7:10am UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/6 "2018-06-05T07:10:00Z")

</div>

you have to return a factory like this:

```
UnaryOperator<RestHandler> getRestHandlerWrapper(ThreadContext threadContext) {
    return originalHanlder ->
        (RestHandler) (request, channel, client) -> {
            System.out.println("Log Something");
            originalHanlder.handleRequest(request, channel, client);
        };
}
```

---

<div class="post-metadata">

**Author:** ![Vinh\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/v/3ab097/32.png) [@Vinh\_Nguyen](https://discuss.elastic.co/u/Vinh_Nguyen)\
**Post date:** [June 5, 2018, 10:38pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/7 "2018-06-05T22:38:30Z")

</div>

Hi Simon,

It was not obvious to me from javadoc, discussions, or existing 5.x code samples that the original rest handler is provided to an implementation of ActionPlugin. Specifically, RestController.dispatchRequest(...) creates a RestHandler base on the request and gives it to the implementation of ActionPlugin.

Thanks for the help. I can create a PR to update the javadoc for getRestHandlerWrapper(..) if there is value.

Regards

---

<div class="post-metadata">

**Author:** ![smoitra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smoitra/32/31930_2.png) [@smoitra](https://discuss.elastic.co/u/smoitra)\
**Post date:** [June 6, 2018, 3:01am UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/8 "2018-06-06T03:01:22Z")

</div>

Hi @Vinh_Nguyen - It will be of immense help if you can put together the basics of your implementation here in this forum/topic. For eg: I need to use ES6.x for a Custom Implemengtation in the next week.I guess, the help from @s1monw will also be captured and it need not be repeated across redundant topics.

Something like,  
public class CustomRESTPlugin extends Plugin implements ActionPlugin {  
@Override  
UnaryOperator getRestHandlerWrapper(ThreadContext threadContext) {  
return originalHanlder -\>  
(RestHandler) (request, channel, client) -\> {  
System.out.println("Log Something");  
originalHanlder.handleRequest(request, channel, client);  
};  
}

//Any other methods/changes?  
}

public class CustomRESTHandler extends BaseRestHandler {  
//constructor - please add  
@Override  
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws  
IOException {  
System.out.println("CustomRESTHandler is running");  
//custom code - we have the 'request'  
}  
//any other methods ?

}

@arimoham - Did you find any other implementation ?

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [June 6, 2018, 6:31am UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/9 "2018-06-06T06:31:24Z")

</div>

> [@Vinh\_Nguyen](#):
>
> Thanks for the help. I can create a PR to update the javadoc for getRestHandlerWrapper(..) if there is value.

please do and ping me on the issue/PR. my handle on github is the same as here

---

<div class="post-metadata">

**Author:** ![Vinh\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/v/3ab097/32.png) [@Vinh\_Nguyen](https://discuss.elastic.co/u/Vinh_Nguyen)\
**Post date:** [June 6, 2018, 6:11pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/10 "2018-06-06T18:11:22Z")

</div>

@smoitra A couple of things concerning your object diagram.

1.If your plugin contains logic to handle the request, you don't need the CustomRESTHandler class.  
2.If you choose to use CustomRESTHandler:

- When your plugin instantiates your custom handler, you need to pass it the original handler. Original handler should be use within handleRequest(...) to pass the request along its normal processing.

---

<div class="post-metadata">

**Author:** ![smoitra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smoitra/32/31930_2.png) [@smoitra](https://discuss.elastic.co/u/smoitra)\
**Post date:** [June 7, 2018, 2:21am UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/11 "2018-06-07T02:21:30Z")

</div>

@Vinh_Nguyen - I need to have some logic on the header(custom security) of the request - hence wanted to have the CustomRESTHandler class.  
Can you can please put down the structure/object diagram/psuedo code in that case?

Thanks!

---

<div class="post-metadata">

**Author:** ![Vinh\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/v/3ab097/32.png) [@Vinh\_Nguyen](https://discuss.elastic.co/u/Vinh_Nguyen)\
**Post date:** [June 20, 2018, 10:03pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/12 "2018-06-20T22:03:54Z")

</div>

@smoitra here it is:

public class YourPlugin extends Plugin implements ActionPlugin {  
@Override  
public UnaryOperator getRestHandlerWrapper(ThreadContext threadContext) {  
UnaryOperator u = (originalRestHandler) -\> {  
final YourRestLogHandler yourResttLogHandler = new YourRestLogHandler(originalRestHandler);  
return yourResttLogHandler;  
}  
}  
}

public class YourRestLogHandler implements RestHandler {  
public YourRestLogHandler(RestHandler originalRestHandler) {  
this.originalRestHandler = originalRestHandler;  
}

@Override  
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) throws  
Exception {  
try {  
//your customer bizlogic  
} catch (Exception e) {  
} finally {  
this.originalRestHandler.handleRequest(request, channel, client);  
}

}  
}

---

<div class="post-metadata">

**Author:** ![smoitra](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smoitra/32/31930_2.png) [@smoitra](https://discuss.elastic.co/u/smoitra)\
**Post date:** [June 22, 2018, 2:11pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/13 "2018-06-22T14:11:38Z")

</div>

Thanks @Vinh_Nguyen

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 20, 2018, 2:11pm UTC](https://discuss.elastic.co/t/restfilters-for-es-5-x/134123/14 "2018-07-20T14:11:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
