-
Notifications
You must be signed in to change notification settings - Fork 116
Extension Parameters
Extension methods marked with an @ExtensionDefinition
annotation contain the logic to be executed by the Rexster. To do anything useful an extension method must have access to the resources that Rexster has to from the context of the request, the general graph configuration and other such parameters. Extension methods access these resources through method parameters that are marked with the @RexsterContext
or @ExtensionRequestParameter
annotations. Rexster will read these marked parameters and try to inject an object based on its type.
Parameters marked with one of these annotation that is not one of an expected type (described more below) will be set to null. Furthermore, parameters that are not marked by annotation at all will be set to null.
The @RexsterContext
annotation tells Rexster to inject resources internal to Rexster to the extension method and can be applied to parameters of the following types:
type | description |
---|---|
Graph | The requested blueprints graph. |
RexsterApplicationGraph | The requested blueprints graph. |
ExtensionMethod | Provides access to the reflected Method as well as the ExtensionDefinition and ExtensionDescriptor . It also provides the getExtensionApiAsJson helper method for getting API information to return as JSON. |
UriInfo | The requested URI. |
HttpServletRequest | The actual servlet request made. |
RexsterResourceContext | A container for the UriInfo , HttpServletRequest , ExtensionMethod , RexsterApplicationGraph and two request objects: one that is mapped to JSON and one that just contains the raw values passed to Rexster |
Edge | The requested edge given a GRAPH or EDGE @ExtensionPoint . |
Vertex | The requested edge given a GRAPH or EDGE @ExtensionPoint . |
Consider the following example taken from the SimpleRootExtension
extension in the Sample Kibbles project:
public ExtensionResponse doWorkOnGraph(@RexsterContext Graph graph) {
In this example, Rexster will inject the Graph
object from the request into the doWorkOnGraph
method.
The @ExtensionRequestParameter
annotation tells Rexster to inject parameters from the request object into a parameter. This annotation takes several parameters: name
, an optional description
and parseToJson
. The name refers to the key in the root of the request object. Rexster will try to coerce values from the request object into the specified type of the parameter. The following types are supported:
- String
- Integer
- Float
- Double
- Long
- Boolean
- JSONObject
- JSONArray
The parseToJson
parameter is defaulted to true
and when set as such, tells Rexster to inject the value of the parameter as mapped from JSON. When set to false
, Rexster will inject the raw value of the parameter without the mapping. This is especially useful when dealing with parameters that contain characters that incorrectly parse to JSON when a string value is really what is needed.
Consider this snippet from the PingExtension
in the Sample Kibbles project:
@ExtensionDefinition(extensionPoint = ExtensionPoint.GRAPH)
@ExtensionDescriptor(description = "Ping me.")
public ExtensionResponse evaluatePing(@RexsterContext RexsterResourceContext context,
@RexsterContext Graph graph,
@ExtensionRequestParameter(name="reply", description="a value to reply with") String reply) {
Map<String, String> map = new HashMap<String, String>();
map.put("ping", reply);
return ExtensionResponse.ok(map);
}
When accessing this URI:
http://localhost:8182/mygraph/ex/ping?reply=ping-a-ling
The value of the reply
URI query string parameter, “ping-a-ling” is injected into the reply
method parameter on the extension.