REST Fragment Plugin Module
Introduction
A REST fragment module defines a Java component that can extend the functionality of a REST endpoint. The REST fragment will be executed in conjunction with any other REST fragments registered to the same location.
Only REST endpoints that are configured to execute REST fragments can be extended. A list of locations that a REST fragment module can extend, and the context that they provide, is included below.
Configuration
The root element for the REST fragment plugin module is <rest-fragment/>
. It allows the following four attributes for configuration:
Attributes
Name | Required | Description | Default |
---|---|---|---|
key | The unique identifier of the plugin module. This key must be unique within the plugin where it is defined. | N/A | |
location | The REST endpoint location to register the REST fragment to. | N/A | |
class | The fully qualified Java class name of REST fragment. This class must implement RestFragment . |
N/A | |
weight | Determines the order in which the REST fragments execute. The 'lightest' weight fragment executes first, and the 'heaviest' executes last. | 100 |
Locations
Location | Description | REST URI | Context | Since |
---|---|---|---|---|
bitbucket.repository.settings.pullRequests | Pull request settings for a repository | /rest/api/1.0/projects/{projectKey}/repos/{repositorySlug}/settings/pull-requests | repository | 4.7 |
Example
Here is an example atlassian-plugin.xml
file containing a single REST fragment:
x1<atlassian-plugin name="My Rest Fragment" key="example.plugin.fragment" plugins-version="2">
2<plugin-info>
3<description>A basic component import module test</description>
4<vendor name="My Company" url="http://www.mycompany.com"/>
5<version>1.0</version>
6</plugin-info>
7
8<rest-fragment key="myRestFragment" location="bitbucket.repository.settings.pullRequests" class="com.mycompany.example.plugin.fragment.MyRestFragment"/>
9</atlassian-plugin>
and the corresponding implementation of the REST fragment.
521public class MyRestFragment implements RestFragment {
2
3
4
5public Map<String, Object> validate( RestFragmentContext fragmentContext,
6Map<String, Object> requestContext) {
7if (fragmentContext.getMethod().equals("POST")) {
8// Validate request body for this fragment. If any fragment fails validation then no fragments will execute.
9// A request is still valid if it doesn't contain any information about this fragment, as it may be just
10// updating other fragments.
11
12// If the request was not valid, return the errors.
13// return new RestErrorMessage("myRestFragment", "Invalid request");
14}
15
16return new HashMap<String, Object>();
17}
18
19
20
21public Map<String, Object> execute( RestFragmentContext fragmentContext,
22Map<String, Object> requestContext) {
23// The "bitbucket.repository.settings.pullRequests" location provides the context repository from the request.
24Repository repository = (Repository) requestContext.get("repository");
25
26// What the fragment does depends on the HTTP method of the request.
27String httpMethod = fragmentContext.getMethod();
28if (httpMethod.equals("GET")) {
29return doGet(repository);
30} else if (httpMethod.equals("POST")) {
31return doPost(fragmentContext, repository);
32}
33
34// Return a message indicating that the HTTP method is unsupported by this fragment. This is not an error,
35// because other fragments registered to the same endpoint may use this HTTP method. It is possible to return an
36// empty Map here, but specifically noting that the request had no effect on this fragment can be useful to
37// consumers of the endpoint.
38Map<String, Object> map = new HashMap<String, Object>();
39map.put("WARNING-myInternalProjectTrackingDescription", "myRestFragment does not support the HTTP method " + httpMethod);
40return map;
41}
42
43private Map<String, Object> doGet(Repository repository) {
44// retrieve settings for this plugin
45return new HashMap<String, Object>();
46}
47
48private Map<String, Object> doPost(RestFragmentContext fragmentContext, Repository repository) {
49// update settings for this plugin
50return doGet(repository);
51}
52}
See also the Auto Unapprove Pull Request plugin for an example of how to implement a REST fragment.