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

Way to pass current User and/or credentials from context when creating ApplicationLinkRequest in Java?

Jason Nguyen May 14, 2014

I have a custom REST service that I developed and within one of the methods within the service I am trying to create an ApplicationLinkRequest in Java.

I'm quite confused about the whole CredentialsRequiredException part and don't know too much about URI redirecting and authentication.

But basically I'm trying to do a rest call from Confluence -> JIRA (where an application link exists between the two).

The response I get from JIRA seems to just give me data back as if I did an anonymous request. (i.e. it'll only list a few of the JIRA projects, perhaps the publically viewable ones but I'd like a list of ALL projects or somehow get the current Confluence user and ask JIRA which projects the current Confluence user can see). Any advice would be nice.

My code for the ApplicationLinkRequest is below:

if(applicationLink.getType().getI18nKey().equals("applinks.jira")){
              ApplicationLinkRequestFactory factory = applicationLink.createAuthenticatedRequestFactory();
                //http://atlas-j.cirrus.com/rest/api/2/serverInfo

                try {
                    ApplicationLinkRequest applicationLinkRequest = factory.createRequest(Request.MethodType.GET, "/rest/api/2/project");
                    String responseBody = applicationLinkRequest.execute(new ApplicationLinkResponseHandler<String>()
                    {
                        public String credentialsRequired(final com.atlassian.sal.api.net.Response response) throws ResponseException
                        {
                            return response.getResponseBodyAsString();
                        }

                        public String handle(final com.atlassian.sal.api.net.Response response) throws ResponseException
                        {
                            return response.getResponseBodyAsString();
                        }

                    });
                    System.out.println("JSON RESPONSE = " + responseBody);
                    JSONParser jsonParser = new JSONParser();

                    try {
                        JSONArray array = (JSONArray)jsonParser.parse(responseBody);
                        //JSONArray array = (JSONArray)jsonObject;
                        //String self = (String) jsonObject.get("self");
                        System.out.println("selfie = "+array);
                        for(int i = 0 ; i < array.size(); i++){
                            JSONObject obj = (JSONObject)array.get(i);
                            System.out.println("obj self = "+obj.get("key"));
                            bjp.addJiraProjectKey(obj.get("key").toString());
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }


                } catch (ResponseException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                } catch (CredentialsRequiredException e) {
                    System.out.println("AUTHORIZATION URI = "+ e.getAuthorisationURI());
                    e.printStackTrace();
                }
            }

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
Andreas Ebert
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.
May 14, 2014

I can only write about doing this in JIRA, not in Confluence. But in JIRA the created ApplicationLinkRequestFactory automatically uses the currently logged-in user. So, what I did in JIRA was setting the logged-in user before create the request factory. In JIRA this is done like this:

JiraAuthenticationContext authContext;
authContext.setLoggedInUser(myCurrentUser);
ApplicationLinkRequestFactory factory = applicationLink.createAuthenticatedRequestFactory();
...

Maybe there is something comparable in Confluence.

Mike Friedrich
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.
October 10, 2014

Any idea how to do this in Stash? The StashAuthenticationContext has no setLoggedInUser method.

TAGS
AUG Leaders

Atlassian Community Events