Jira plugin, using WebItemProvider, seeing 'cannot cast exception'

Damon Osgood September 25, 2017

Jira 7.2.2

I need to generate some menu items dynamically. From what I've read, it seems that the way to do this is to add a web-section in my atlassian-plugin.xml:

<web-section
key="create-document-section"
i18n-name-key="create-document-section.name"
location="opsbar-operations"
weight="100">
<label key="create-document-section.label"/>
</web-section>

And then a web-item-provider stanza:

<web-item-provider key="create-document-menu-item-factory"
name="Create Document Menu Item Factory"
section="create-document-section"
i18n-name-key="create-document-item-factory.name"
class="com.blizzard.web.DynamicLinkFactory" />

And then implement WebItemProvider:

package com.blizzard.web;

import com.atlassian.plugin.web.api.WebItem;
import com.atlassian.plugin.web.api.model.WebFragmentBuilder;
import com.atlassian.plugin.web.api.provider.WebItemProvider;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;



public class
DynamicLinkFactory implements WebItemProvider {

@Override
public Iterable<WebItem> getItems(Map<String, Object> context) {

final List<WebItem> links = new ArrayList<WebItem>();

links.add(new WebFragmentBuilder(10).
id("create_document_item").
label("test").
title("temp").
webItem("create-document-section").
url("/browse/").
build());

return links;
}
}

This compiles and loads into Jira, but when a page is rendered, I get:

...DynamicLinkFactory cannot be cast to 
com.atlassian.plugin.web.api.provider.WebItemProvider'.
Web-items provided by this provider will be ignored.

What am I doing wrong?

 

Update:

I have a theory that JIRA and my plugin are both loading DynamicLinkFactory, and the casting problem is a result of the ambiguity between the two. I have jira-api listed as a "provided" dependency:

<dependency>
<groupId>com.atlassian.jira</groupId>
<artifactId>jira-api</artifactId>
<version>${jira.version}</version>
<scope>provided</scope>
</dependency>

 And I have tried with and without atlassian-plugins-webfragment-api listed:

<dependency>
<groupId>com.atlassian.plugins</groupId>
<artifactId>atlassian-plugins-webfragment-api</artifactId>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>

 But there has been no effect.

 

Can anyone tell me if I'm on the right track? And if so, maybe how to resolve the issue?

2 answers

1 accepted

0 votes
Answer accepted
Damon Osgood June 21, 2018

I have solved my problem. I iterated on this many times, and in the course of iteration I mistakenly left a duplicate copy of the webfragment-api dependency in my pom, but without the <scope>provided</scope>, like this:

<dependency>
<groupId>com.atlassian.plugins</groupId>
<artifactId>atlassian-plugins-webfragment-api</artifactId>
<version>4.1.0</version>
</dependency>

So the webfragment-api classes were actually being packaged into my jar, causing the ambiguity I described above. Ensuring there was only one reference to this dependency, with <scope>provided</scope>, resolved my issue.

To be completely clear, it needed to look like this:

<dependency>
<groupId>com.atlassian.plugins</groupId>
<artifactId>atlassian-plugins-webfragment-api</artifactId>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>
Aleksandr Zuevich
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 21, 2018

I have only one dependency in pom: jira-api. And it's enough to add web-item-provider with your DynamicLinkFactory.

Damon Osgood June 22, 2018

Ok, I see now that the code does actually compile and run without the additional dependency (via atlas-mvn). However, I do need  the dependency on webfragment-api for my IDE to highlight the code and do code completion, which is one reason I had such a hard time with this problem.

0 votes
Aleksandr Zuevich
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.
September 26, 2017

Hi Damon,

I've tried your code and got NullPointerException
at com.atlassian.jira.issue.util.IssueOperationsBarUtil$1.apply(IssueOperationsBarUtil.java:196)
at com.atlassian.jira.issue.util.IssueOperationsBarUtil$1.apply(IssueOperationsBarUtil.java:193)

The reason is you don't have styleClass for your webItem so just add empty styleClass string:

links.add(new WebFragmentBuilder(10).
styleClass("").
id("create_document_item").
label("test").
title("temp").
webItem("create-document-section").
url("/browse/").
build());
Damon Osgood June 18, 2018

Thank you Alexandr for your response so long ago. I had to drop the project at the time, but now I am back to it and hitting the same problem.

Unfortunately I am not even getting far enough to hit the problem you are describing. In my case getItems() is never called.

I've updated my question a bit.

Aleksandr Zuevich
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 20, 2018

Hi Damon,

Glad to hear you after so long time again! Let's try to solve the issue together.

I've added your web-section and web-item-provider to atlassian-plugin.xml and added the same DynamicLinkFactory class (except with my package). And got NullPointerException as I wrote earlier. It is really fixed by adding styleClass.

Capture.PNG

Damon Osgood June 20, 2018

Thanks for replying, but as I said I am not getting that far. I did add the styleClass() as you suggested, but that code never executes at all. The DynamicLinkFactory is constructed, but in the log I see:

...DynamicLinkFactory cannot be cast to 
com.atlassian.plugin.web.api.provider.WebItemProvider'.
Web-items provided by this provider will be ignored.

Can you show me your pom file? I'd like to know how you handle the dependency in order to import the classes WebItem and WebItemProvider.

Suggest an answer

Log in or Sign up to answer