Class file for Condition not found

Hello everyone,

I’m writing a simple plug-in for checking the connection with JIRA/JIRA API.

I have made a web item that displays only to admins but my condition on the web item is failing.
These are the errors on building the project with intelliJ :

java: cannot access com.atlassian.plugin.web.Condition class file for comn.atlassian.plugin.web.Condition not found
java: method does not override or implement a method from a supertype

Here’s my code for the condition, atlassian-plugin.xml and servlet I’ve made :

UserCondition.java

package se.zendev.checkconnection.conditions;
import com.atlassian.jira.plugin.webfragment.conditions.AbstractWebCondition;
import com.atlassian.jira.plugin.webfragment.model.JiraHelper;
import com.atlassian.jira.user.ApplicationUser;

public class UserCondition extends AbstractWebCondition{

     @Override
     public boolean shouldDisplay(ApplicationUser user, JiraHelper helper){
              return user != null && user.getName().equals("admin");
    }
}

atlassian-plugin.xml

<atlassian-plugin key="${atlassian.plugin.key}" name="${project.name}" plugins-version="2">
    <plugin-info>
        <description>${project.description}</description>
        <version>${project.version}</version>
        <vendor name="${project.organization.name}" url="${project.organization.url}" />
        <param name="plugin-icon">images/pluginIcon.png</param>
        <param name="plugin-logo">images/pluginLogo.png</param>
    </plugin-info>

    <!-- add our i18n resource -->
    <resource type="i18n" name="i18n" location="check-connection"/>
    
    <!-- add our web resources -->
    <web-resource key="check-connection-resources" name="check-connection Web Resources">
        <dependency>com.atlassian.auiplugin:ajs</dependency>
        
        <resource type="download" name="check-connection.css" location="/css/check-connection..css"/>
        <resource type="download" name="check-connection.js" location="/js/check-connection..js"/>
        <resource type="download" name="images/" location="/images"/>

        <context>jira.admin</context>
    </web-resource>
    <servlet name="Admin Servlet" i18n-name-key="admin-servlet.name" key="admin-servlet" class"se.zendev.checkconnection.adminui.AdminServlet">
         <description key="admin-servlet.description">Connection plugin</description>
         <url-pattern>/adminservlet</url-pattern>
     </servlet>
    <web-item name="Check Connection" i18n-name-key="check-connection.name" key="check-connection" section="admin/globalsettings" weight="100">
          <description key="check-connection.description">Check connection plugin</description>
          <label key="check-connection.label">
          </label>
          <link linkId="check-connection-link">/plugins/servlet/adminservlet</link>
                <condition class="se.zendev.checkconnection.conditions.UserCondition"/>
      </web-item>
</atlassian-plugin>
    

And AdminServlet.java

package se.zendev.checkconnection.adminui;
 
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.rest.json.beans.JiraBaseUrls;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.util.InjectableComponent;
import com.atlassian.plugin.spring.scanner.annotation.component.Scanned;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.atlassian.sal.api.auth.LoginUriProvider;
 
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import com.atlassian.sal.api.user.UserManager;
import javax.inject.Inject;
import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.MalformedInputException;
 
 
public class AdminServlet extends HttpServlet{
 
 
    private JiraAuthenticationContext authenticationContext;
    private LoginUriProvider loginUriProvider;
    private JiraBaseUrls baseUrls;
 
    @Override
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        authenticationContext = ComponentAccessor.getJiraAuthenticationContext();
        baseUrls = ComponentAccessor.getComponent(JiraBaseUrls.class);
        loginUriProvider = ComponentAccessor.getComponent(LoginUriProvider.class);
    }
 
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
 
        out.println("Checking if the user is admin ...");
        ApplicationUser user = authenticationContext.getLoggedInUser();
        String username = user.getUsername();
        if (username == null || username != "admin")
        {
            out.println("User not admin, redirecting to login page ...");
            redirectToLogin(request, response);
            return;
        }
        else{
            out.println("Welcome admin, checking for connection issues ...");
            int statusCode = checkConnectionIssues(baseUrls.baseUrl(), 15);
            if(statusCode>=200 && statusCode<=399){
                out.println("URL Reachable");
            }
            else{
                out.println("URL not reachable");
            }
 
        }
 
    }
 
    private static int checkConnectionIssues(String baseUrls, int timeout) throws IOException {
        baseUrls = baseUrls.replaceFirst("^https", "http");
 
        try{
            HttpURLConnection connection = (HttpURLConnection) new URL(baseUrls).openConnection();
            connection.setConnectTimeout(timeout);
            connection.setReadTimeout(timeout);
            connection.setRequestMethod("HEAD");
            return connection.getResponseCode();
        }catch (IOException exception){
            throw new IOException("Error message : " + exception.getMessage());
        }
    }
 
    private void redirectToLogin(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        response.sendRedirect(loginUriProvider.getLoginUri(getUri(request)).toASCIIString());
    }
 
    private URI getUri(HttpServletRequest request)
    {
        StringBuffer builder = request.getRequestURL();
        if (request.getQueryString() != null)
        {
            builder.append("?");
            builder.append(request.getQueryString());
        }
        return URI.create(builder.toString());
    }
 
 
}

The “comn.atlassian…” is probably a typo, right?

I can’t see any problem with your Condition implementation, but you might just as well use the built-in com.atlassian.jira.plugin.webfragment.conditions.IsAdminCondition, which checks for global admin permission rather than a particular user name.

Hi, yes that was a typo, I’ve tried with IsAdminCondition, there are no more errors while building the project, but I can’t access the add-on from jira now. Getting error when going to manage add-ons, enable :

failed to enable plugin

Anything in the log?

I’ve set debugging level to debug for my package and all I get in the audit log is :

Failed to enable add-on check-connection (se.zendev.checkconnection.check-connection)

I can post a screenshot if you want.