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

How to access Confluence User profile picture through Java?

Jason Nguyen November 19, 2013

Hey guys I'm trying to write a macro that will need to retrieve the Confluence user's profile picture when it is given the user name as a parameter. I can't figure out how to access the profile picture of a user.

I saw a similar thread where someone mentioned:

https://answers.atlassian.com/questions/196629/access-users-profile-picture

By using the DefaultUserAccessor it's possible.

1
2
3
ProfilePictureInfo pic = defaultUserAccessor.getUserProfilePicture(accountName);
String pathToImage = pic.getDownloadPath()

My Problem is that I can't figure out how to instantiate DefaultUserAccesor

I have no idea what to pass to the constructor.

DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor( ????);

5 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Answer accepted
Joe Clark
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 19, 2013

Hi Jason,

You should not construct instances of "accessor" and "manager" classes yourself. You should expect that Confluence will handle the construction of these objects internally, and then provide access to these via a design pattern called "dependency injection" or "inversion of control".

You may find it beneficial to read up general documentation about these design patterns. Alternatively, here is a document that describes how this pattern is applied to Confluence in practice: https://developer.atlassian.com/display/CONFDEV/Spring+IoC+in+Confluence

If you just want the quick answer, then it's here: https://developer.atlassian.com/display/CONFDEV/Accessing+Confluence+Components+from+Plugin+Modules

To summarize, you need to write your code like this:

public class MyClassThatDoesStuff
{
  private UserAccessor theThingIWantToUse;

  public MyClassThatDoesStuff(UserAccessor confluenceGivesMeTheThingIWantToUse)
  {
    this.theThingIWantToUse = confluenceGivesMeTheThingIWantToUse;
  }

  public void doStuff()
  {
    // In here you can access the UserAccessor
  }
}

In order for this to work, Confluence also needs to be responsible for calling the constructor of your class. You can achieve this by making your class a "component module", which means you'll need to add something like this to your atlassian-plugin.xml:

<component key="myComponent"
           name="My Component"
           class="com.example.plugin.MyClassThatDoesStuff">
  <description>This is my component in my plugin</description>
</component>

Jason Nguyen November 20, 2013

This worked for me. Thank you, Joseph.

0 votes
Jason Nguyen November 20, 2013

Oh wow, thanks for your help. I finally got it to work. I'm new to dependency injection so this is pretty nifty.

0 votes
Jason Nguyen November 19, 2013

DefaultUserAccessor defaultUserAccessor = new DefaultUserAccessor(repositoryAccessor);

Tried this. Note that I'm using bucket.user.DefaultUserAccessor

I get 'DefaultUserAccessor' is abstract; cannot be instantiatiated.

I found another DefaultUserAccessor: com.atlassian.confluence.user.DefaultUserAccessor

This one requires a LOT more arguments though, which I'm not sure how to obtain. Hopefully there's an easier way to do this

public DefaultUserAccessor(com.atlassian.user.configuration.RepositoryAccessor repositoryAccessor, com.atlassian.confluence.security.SpacePermissionManager spacePermissionManager, com.atlassian.confluence.core.ContentEntityManager contentEntityManager, com.atlassian.confluence.mail.notification.NotificationManager notificationManager, com.atlassian.confluence.user.PersonalInformationManager personalInformationManager, com.atlassian.confluence.util.UserChecker userChecker, com.atlassian.confluence.pages.AttachmentManager attachmentManager, com.atlassian.event.EventManager eventManager, com.atlassian.confluence.core.ContentPermissionManager contentPermissionManager, com.atlassian.confluence.security.PermissionManager permissionManager, com.atlassian.confluence.userstatus.UserStatusManager userStatusManager, com.atlassian.confluence.follow.FollowManager followManager, com.atlassian.crowd.embedded.api.CrowdService crowdService, com.atlassian.confluence.user.DisabledUserManager disabledUserManager, com.atlassian.confluence.core.persistence.hibernate.HibernateSessionManager sessionManager, com.atlassian.confluence.user.persistence.dao.ConfluenceUserDao confluenceUserDao, com.atlassian.user.UserManager backingUserManager, com.atlassian.confluence.setup.settings.SettingsManager settingsManager) { /* compiled code */ }

0 votes
Jason Nguyen November 19, 2013

Yes I saw that but how do I get an instance of RepositoryAccessor?

0 votes
Erik Saline [BlackPearl PDM]
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.
November 19, 2013

Is this what you are looking for?

https://developer.atlassian.com/static/javadoc/confluence/4.2.13/reference/bucket/user/DefaultUserAccessor.html

<th align="left" colspan="2">
protected DefaultUserAccessor(com.atlassian.user.configuration.RepositoryAccessor repositoryAccessor)

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