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

Confluence Download Image Resource URL

Steve Kling July 21, 2016

I've scoured the mostly JIRA-centric entries about download resources, specifically trying to get URLs for images, get images to show, etc.

Figured it out how to do this with css a while back

#myicon{
        background: url('images/my-icon.png');
    vertical-align: middle;
            height: 16px;
             width: 16px;
           display:inline-block
}

but in this case I'm using the NotificaitionBuilder and I'm forced to find a way to get a relative/FQ path to the actual image.  When I look at the paths generated by the above css-based images, they're always in bundled resources which makes hardcoding that path untenable. 

 

This is what I think should work but does not:

String iconURL = webResourceUrlProvider.getResourceUrl("com.my.plugins.my-cnfl-plugin", "image.jpg"); <--generated a url but it's always broken link

Notification notification = notificationService.createOrUpdate(recipient, new NotificationBuilder()
            .application("com.my.plugins.my-cnfl-plugin") 
            .title(title)
            .itemIconUrl(iconURL)
            .itemTitle(message)
            .itemUrl(itemURL) 
            .action(envelopeId)
            .actionIconUrl(iconURL) //<--has to be static.  can't css it. :(
            .description(message)
            .iconUrl(iconURL) //<--has to go here.  can't css it. :(
            .groupingId(myobjID)
            .createNotification()).get();

    return notification;
}

2 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
Steve Kling July 22, 2016

After much trial and error...got this to work. Thanks to @Panos Karampis for the initial direction.

atlassian-plugin.xml

<web-resource key="myicons" type="download" name="DOESN'T SEEM TO MATTER" location="images/">
    <param name="content-type" value="image/png"/>
</web-resource>

Snippet

String iconURL =
        StaticAccessor.getWebResourceUrlProvider()
                .getStaticPluginResourceUrl("com.my.plugins.my-cnfl-plugin:myicons", "images/some-icon.png", UrlMode.RELATIVE);

Atlassian JavaDoc

public abstract String getStaticPluginResourceUrl (String moduleCompleteKey, String resourceName, UrlMode urlMode)
...
This method returns a URL in either a relative or an absolute format, depending on the value of urlMode. See UrlMode for details of the different options for URL format. If a module with the given key cannot be found null is returned.
Parameters
moduleCompleteKey complete plugin module key
resourceName the name of the resource as defined in the plugin manifest

Lessons Learned:

  1. Method getStaticPluginResourceUrl's "resourceName" parameter is misleading.  Suggest: "relativePathToResource" as I can name the web-resource whatever I want (see above example) and it won't work unless I give it a relative path to an image based on the moduleCompleteKey.
  2. <resource> resources are not included in the "webResourceUrlProvider" only <web-resources> as the name implies but that's a hard/time-consuming lesson to learn. sad
Panos
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.
July 22, 2016

I am glad you made it. Documentation is a bit ugly sometimes and have to improvise smile

0 votes
Panos
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.
July 21, 2016
<web-item key="somekey" section="somesection" weight="10"><label key="text" />
		<link>somelink</link>
<param name="icon" value="a resource here (grlupid.artifactid:key)" />
	</web-item>

 

And then declare a resource as per value and parse the webitem. I cant write a descent answer with mobile... Sorry. 

I hope thats what you ask.

Panos
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.
July 21, 2016

To parse the webitem use the WebInterfaceManager

final String[] tokens = webItem.getParams().get("icon").split(":");
final String moduleKey = tokens[0];
final String resourceName = tokens[1];
final DataSource source = dataSourceFactory.forPlugin(your plugin key).get().resourceFromModuleByName(moduleKey, resourceName).get();
new DefaultDataSourceFactory.NamedDataSource(source, webItem.getWebLabel().getKey());

 

 

 

Steve Kling July 21, 2016

Thanks, Panos, but your answer doesn't appear to be entirely complete.  

Why would one need to declare a web-item simply to obtain an icon image?  Is it actually necessary to split webitem tokens and use a mail service like dataSourceFactory?  

I've tried both these approaches.  Each time the webResourceUrlProvider.getResourceUrl() returns a path (no errors) but to nothing..

&lt;resource key="images" name="images/" type="download" location="images"/&gt;
 
&lt;resource type="download" name="ds-16.jpg" location="images/ds-16.jpg"&gt;
    &lt;param name="content-type" value="image/jpg"/&gt;
&lt;/resource&gt;

There's got to be a more straightforward way to accomplish this.

Panos
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.
July 21, 2016

Hi Steve, I realize that my answer is based on my needs and is part of my production code, which makes it a bit more complicated than should. 

The webResourceUrlProvider has a bunch of methods and the one you are using does not 

"This method is not responsible for adding any static caching url prefixes." maybe the other methods give something more concrete?


Steve Kling July 22, 2016

So supposedly these should work but I've never got them to return anything but null.  Sigh.

String iconURL =
        StaticAccessor.getWebResourceUrlProvider()
                .getStaticPluginResourceUrl("com.my.plugins.my-cnfl-plugin:images", "ds-16.jpg", UrlMode.RELATIVE);
String iconURL2 =
        StaticAccessor.getWebResourceUrlProvider()
                .getStaticPluginResourceUrl("com.my.plugins.my-cnfl-plugin:icon", "icon16", UrlMode.RELATIVE);

correlates to these to atlassian-plugin.xml resource declarations:

//iconURL1 - the standard image folder xml entry
&lt;resource key="images" name="images/" type="download" location="images"/&gt;
 
//iconURL2
&lt;resource key="icon" type="download" name="icon16" location="images/icon-16.jpg"&gt;
    &lt;param name="content-type" value="image/jpg"/&gt;
&lt;/resource&gt;

I noticed that "moduleCompleteKey" is the first argument of "getStaticPluginResoucesUrl".  Does the resource have to belong to a module to work?  I'll try it.

TAGS
AUG Leaders

Atlassian Community Events