Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to authenticate in a SAL to call REST ?

Valerie AQUILA June 5, 2013

In a plugin, I've :

- a custom REST api

- a SAL scheduler

The custom REST api and methods work well and I can use them with the REST API browser client.

The class constructor is like that :

public lfapi(JiraAuthenticationContext auth) {
		this.auth = auth;
		System.out.println("auth=" + auth.getLoggedInUser().getDisplayName());

	}

In the SAL schedule, I need to call the REST methods.

How can I authenticate ? How can I instanciate with what JiraAuthenticationContext ?

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Valerie AQUILA June 6, 2013

In the atlassian-plugin.xml, I've added the component-import :

<component-import key="jiraAuthenticationContext" interface="com.atlassian.jira.security.JiraAuthenticationContext" />

In the scheduler class, I've added the JiraAthenticationContext in the class constructor :

	
...
private final JiraAuthenticationContext jiraAuthenticationContext; // provided by SAL ... public ADImpl(PluginScheduler pluginScheduler, JiraAuthenticationContext jiraAuthenticationContext) { this.pluginScheduler = pluginScheduler; this.jiraAuthenticationContext = jiraAuthenticationContext; } // declared by LifecycleAware public void onStart() { reschedule(interval); } public void reschedule(long interval) { this.interval = interval; pluginScheduler.scheduleJob(JOB_NAME, // unique name of the job ADTask.class, // class of the job new HashMap<String, Object>() { { put(KEY, ADImpl.this); } }, // data that needs to be passed to the job new Date(), // the time the job is to start interval); // interval between repeats, in milliseconds System.out.println(String.format( "AD synchro task scheduled to run every %dms", interval)); } ...
 public JiraAuthenticationContext getJiraAuthenticationContext() { return jiraAuthenticationContext; }

In the task class :

final ADImpl monitor = (ADImpl) jobDataMap.get(ADImpl.KEY);
assert monitor != null;

User userAdmin = (User) UserUtils.getUser("admin"); JiraAuthenticationContext jiraAuthenticationContext = monitor .getJiraAuthenticationContext(); jiraAuthenticationContext.setLoggedInUser(userAdmin);

  lfapi resource = new lfapi(jiraAuthenticationContext);

granthbr June 21, 2013

Hi Valerie, Did you resolve this issue? I'm having a similar problem.

Valerie AQUILA June 23, 2013

Hello Granthbr,

Yes I did !

I did exactly what I explained in my own answer.

Do you need more clarification ?

granthbr June 23, 2013

I'm having dependency issues when I attempt to use JiraAuthenticationContext. Something with my pom.xml I think. Whenever I attempt to connect I get !! com.atlassian.jira.security,version=0.0.0 from -- Cannot be resolved .Is there any chance that I could see what your pom.xml looks like? Thank you!

Royce Wong
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 10, 2015

Hi Brandon, I have similar issue, having problem importing com.atlassian.jira.security.roles.actor.UserRoleActorFactory. Did you figure out which dependency to add to pom.xml to resolve the issue? Thanks.

granthbr June 10, 2015

Hi Royce, I asked this so long ago that I barely remember ever asking this question. I think I did find a resolution, i just don't remember what it is... Sorry for the bad news.

1 vote
Valerie AQUILA June 24, 2013

Hi granthbr,

Here is my pom. hope this helps. Cheers

(sorry for the format, I'm having problems with both IE and firefox with some javascripts in Atlassian Answer ???)

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.atlassian.plugins.lfoundry</groupId>
<artifactId>ADSynchro</artifactId>
<version>2.0</version>

<organization>
<name>LFoundry</name>
<url>http://www.lfoundry.com/</url>
</organization>

<name>ADSynchro</name>
<description>A SAL Scheduler that runs an Active Directory read and updates the properties set for each user</description>
<packaging>atlassian-plugin</packaging>

<dependencies>
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-api</artifactId>
<version>${jira.version}</version>
<scope>provided</scope>
</dependency>
<!-- Add dependency on jira-core if you want access to JIRA implementation classes as well as the sanctioned API. -->
<!-- This is not normally recommended, but may be required eg when migrating a plugin originally developed against JIRA 4.x -->
<!--
<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-core</artifactId>
<version>${jira.version}</version>
<scope>provided</scope>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>

<!-- WIRED TEST RUNNER DEPENDENCIES -->
<dependency>
<groupId>com.atlassian.plugins</groupId>
<artifactId>atlassian-plugins-osgi-testrunner</artifactId>
<version>${plugin.testrunner.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>jsr311-api</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2-atlassian-1</version>
</dependency>

<!-- Uncomment to use TestKit in your project. Details at https://bitbucket.org/atlassian/jira-testkit -->
<!-- You can read more about TestKit at https://developer.atlassian.com/display/JIRADEV/Plugin+Tutorial+-+Smarter+integration+testing+with+TestKit -->
<!--
<dependency>
<groupId>com.atlassian.jira.tests</groupId>
<artifactId>jira-testkit-client</artifactId>
<version>${testkit.version}</version>
<scope>test</scope>
</dependency>
-->

<dependency>
<groupId>net.homeip.yusuke</groupId>
<artifactId>twitter4j</artifactId>
<version>2.0.10</version>
</dependency>
<dependency>
<groupId>com.atlassian.sal</groupId>
<artifactId>sal-api</artifactId>
<version>2.0.0</version>
<scope>provided</scope>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>com.atlassian.maven.plugins</groupId>
<artifactId>maven-jira-plugin</artifactId>
<version>${amps.version}</version>
<extensions>true</extensions>
<configuration>
<productVersion>${jira.version}</productVersion>
<productDataVersion>${jira.version}</productDataVersion>
<!-- Uncomment to install TestKit backdoor in JIRA. -->
<!--
<pluginArtifacts>
<pluginArtifact>
<groupId>com.atlassian.jira.tests</groupId>
<artifactId>jira-testkit-plugin</artifactId>
<version>${testkit.version}</version>
</pluginArtifact>
</pluginArtifacts>
-->
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<jira.version>5.2.11</jira.version>
<amps.version>4.2.0</amps.version>
<plugin.testrunner.version>1.1.1</plugin.testrunner.version>
<!-- TestKit version 5.x for JIRA 5.x, 6.x for JIRA 6.x -->
<testkit.version>5.2.26</testkit.version>
</properties>

</project>

0 votes
Valerie AQUILA June 5, 2013

Not sure I explain well :

- I want my SAL Scheduler class to be client of the REST custom api in the same plugin.

- I also want the REST custom api beeing called by other clients (here I've no problem to authenticate by using basic authentication)

So how can I authenticate when I'm in the SAL Schedule to instanciate the REST class ?

neha March 7, 2018

Hi valerie ,

 

i think ,I am having the same problem .I am trying to develop a  substitute functionality in JIRA in which team leaders can assign their substitutes and give them permission to approve the timesheet of his team members,

It will be valid for a specified interval for that interval i am giving tempo team permission to the substitute. After the time interval is over i want to remove the team permission for the substitute,

I already have the code to remove and set permission for substitute i need to create a timer which will run every mid night to check weather team's substitute reside in the time interval for team permission or not if not then remove it .

I tried to follow this link: https://developer.atlassian.com/server/framework/atlassian-sdk/scheduling-events-via-sal-tutorial/?_ga=2.9518581.1975081085.1520238767-1080877320.1513076049

I  used following steps to create a timer.

1. I added the maven dependency.

2. Imported the SAL scheduler as defined in link.

3. Wrote a Task in which I implemented PluginJob and also pass the task  class name.I also injected the services here which I was using to delete the team permission here for substitute after checking the time interval.

// i m getting error  in this step at the point where i tried to call my authentication helper to set the httprequest which i will need to get all the tempo teams and to get the substitute for them . I am getting following error.

2:15:49,422 Caesium-1-1 ERROR ServiceRunner [c.a.scheduler.core.JobLauncher] Scheduled job with ID 'JiraPluginScheduler:com.foxconn.dta.schedular.RemoveOldSubstituteTeamPermissionsimpl:job' failed
[INFO] [talledLocalContainer] java.lang.NullPointerException
[INFO] [talledLocalContainer] at com.foxconn.dta.schedular.RemoveOldSubstitute.execute(RemoveOldSubstitute.java:31)
[INFO] [talledLocalContainer] at com.atlassian.sal.jira.scheduling.JiraPluginScheduler$JobDescriptor.runJob(JiraPluginScheduler.java:110)
[INFO] [talledLocalContainer] at com.atlassian.sal.jira.scheduling.JiraPluginScheduler.runJob(JiraPluginScheduler.java:80)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.core.JobLauncher.runJob(JobLauncher.java:153)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.core.JobLauncher.launchAndBuildResponse(JobLauncher.java:118)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.core.JobLauncher.launch(JobLauncher.java:97)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService.launchJob(CaesiumSchedulerService.java:443)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService.executeLocalJob(CaesiumSchedulerService.java:410)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService.executeQueuedJob(CaesiumSchedulerService.java:388)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService$1.consume(CaesiumSchedulerService.java:285)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.CaesiumSchedulerService$1.consume(CaesiumSchedulerService.java:282)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.SchedulerQueueWorker.executeJob(SchedulerQueueWorker.java:65)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.SchedulerQueueWorker.executeNextJob(SchedulerQueueWorker.java:59)
[INFO] [talledLocalContainer] at com.atlassian.scheduler.caesium.impl.SchedulerQueueWorker.run(SchedulerQueueWorker.java:34)
[INFO] [talledLocalContainer] at java.lang.Thread.run(Thread.java:748)

 .................................................................

public class RemoveOldSubstitute implements PluginJob {
@Inject
private SubstituteSettingsService substituteSettingService;
@Inject
private TempoRestClient tempoRestClient;
@Context
HttpServletRequest httpRequest;
@Inject
AuthenticationHelper authenticationHelper;
@Override
public void execute(Map<String, Object> jobDataMap) {
final RemoveOldSubstituteTeamPermissionsimpl monitor = (RemoveOldSubstituteTeamPermissionsimpl) jobDataMap
.get(RemoveOldSubstituteTeamPermissionsimpl.KEY);
assert monitor != null;

 

//here i m getting the error .. httprequest is null here how i can add request here .
authenticationHelper.setHttpRequest(httpRequest);

 

//getting all the teams info here 
List<TempoTeam> teams = tempoRestClient.getTeamListForUser();

if (teams != null && !teams.isEmpty()) {
for (TempoTeam team : teams) {
substituteSettingService.deleteTeamLeadSubstitute(team.getId(), team.getLead());

}
}

}

}

 

could you please help me in this .

0 votes
CelsoA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 5, 2013

Hi there,

There is a Rest Api example to authenticate using OAuth.

https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Basic+Authentication

Please give it a try.

Regards,

Valerie AQUILA June 5, 2013

Hi Celso and thanks for interresting my question,

I need to call a REST api I created in a rest module from the plugin itself, which is a SAL scheduler.

I guess I'm already connected in the scheduler plugin, but with what user ? Believe with "admin", right ?

Is it possible to call a method of the rest class that is in the same plugin without having to construct the url, like for the other rest clients ?

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events