Friday, 29 June 2012

Implementation of Rest service in Jira platform

REpresentational State Transfer (REST) is a style of software architecture for distributed systems such as web applications. REST has increasingly replaced other design models such as SOAP and WSDL due to its simpler style. Jira platform also gave a hook to create Rest service in it's architecture. Let's look, how to create a Rest service in Jira platform.

Problem Statement: Populate the list of priority of jira application using rest service.

1. First and the foremost point is, rest services can be created/implemented only in osgi plug-in (type-2) of jira.

Please add following xml code snippet in atlassian-plugin.xml file of type-2 plugin.

<!--             Application Rest Service             -->
    <rest key="app-rest" name="Application REST API" path="/application"    version="1.0">
        <description>Provides REST end points used in Application.</description>
    </rest>

2. Add a data source which would do the population of required object.


package com.application.company.gadget;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@Path("/fields")
@Produces(MediaType.APPLICATION_JSON)
public class CustomFieldDataResource extends AbstractGadgetResource {

    @GET
    @Path("/priority")
    public Keys getPriority() {
        List<String> priorities = this.getPriorities();
        Collection<Key> items = new ArrayList<Key>();
        for (final String priority : priorities) {
            items.add(new Key(priority, priority));
        }
        return new Keys(items);
    }
    
    private List<String> getPriorities() {
        Collection<Priority> systemPriorities = ComponentManager.getInstance().getConstantsManager()
                .getPriorityObjects();
        List<String> priorities = new ArrayList<String>();
        for (Priority priority : systemPriorities) {
            String priorityName = priority.getName();
            priorities.add(priorityName);
        }
        return priorities;
    }

   
    @XmlRootElement
    public class Keys {
        @SuppressWarnings("unused")
        @XmlElement
        private Collection<Key> keys;

        public Keys(Collection<Key> keys) {
            this.keys = keys;
        }
    }

    @XmlRootElement
    public class Key {
        @SuppressWarnings("unused")
        @XmlElement
        private String label;

        @SuppressWarnings("unused")
        @XmlElement
        private String value;

        public Key() {
        }

        Key(String label, String value) {
            this.label = label;
            this.value = value;
        }
    }

}


3. Now, build your type-2 plugin in jira platform and try to validate rest service.

Way to validate the rest service in generic form: 
http://<machine name>:<port-number>/<context>/rest/<rest-tag name>/1.0/<data-source-name>/<method/<behaviour name>

In our case, http://localhost:8887/jira/rest/application/1.0/fields/priority

You would see below response: 
{
  • "keys": [
    • {
      • "label": "P1",
      • "value": "P1"
      },
    • {
      • "label": "P2",
      • "value": "P2"
      },
    • {
      • "label": "P3",
      • "value": "P3"
      },
    • {
      • "label": "P4",
      • "value": "P4"
      },
    • {
      • "label": "P5",
      • "value": "P5"
      },
    • {
      • "label": "P6",
      • "value": "P6"
      },
    • {
      • "label": "Not Specified",
      • "value": "Not Specified"
      }
    ]
}


No comments:

Post a Comment