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

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

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 Community Champion Jun 21, 2018

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

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 vote
Aleksandr Zuevich Community Champion Sep 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());

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 Community Champion Jun 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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published 2 hours ago in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

13 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you