I want to create an interceptor that takes data from the incoming request and issues a custom query to the underlying db, returning the result of that query to the user. I cannot figure out how to return the results of the custom query to the user.
Here is the example I tried, but the response document I create and set in the plugin is not being returned to the user.
Is it possible to do this using a MongoInterceptor?
@RegisterPlugin(name = "exampleInterceptor",
description = "example interceptor",
interceptPoint = InterceptPoint.REQUEST_AFTER_AUTH,
priority = 100)
public class ExampleInterceptor implements MongoInterceptor {
@Override
public void handle(MongoRequest request, MongoResponse response)
throws Exception
{
String dbName = request.getDBName();
String collName = request.getCollectionName();
String[] pathInfo = request.getMappedRequestUri().split("/");
if (pathInfo.length == 3) {
String id = pathInfo[2];
BsonDocument doc = new BsonDocument();
doc.put("db", new BsonString(dbName));
doc.put("collection", new BsonString(collName));
doc.put("id", new BsonString(id));
response.setContent(doc);
}
}
@Override
public boolean resolve(MongoRequest request, MongoResponse response) {
return true;
}
}
In order to modify the response, you need
interceptPoint = InterceptPoint.RESPONSE
otherwise the response content will be overwritten by the MongoServiceAlso note that the response content can be null (for write requests), an object (for GET /coll/docid) or an array (for GET /coll), so you need to deal with different cases
Try the following: