All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.customware.license.jira.AbstractLicenseAction Maven / Gradle / Ivy

The newest version!
package net.customware.license.jira;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import net.customware.license.support.util.LicenseUtils;
import webwork.action.ServletActionContext;
import webwork.multipart.MultiPartRequestWrapper;

import com.atlassian.jira.security.Permissions;
import com.atlassian.jira.web.action.JiraWebActionSupport;

import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;

/**
 * This class provides the basline for allowing plugins to install their own
 * license key.
 *
 * @author David Peterson
 */
public abstract class AbstractLicenseAction extends JiraWebActionSupport {
   
	private static final long serialVersionUID = 1L;

	private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;

    private LicenseManager licenseManager = null;

    private MultiPartRequestWrapper multiPartRequest;

    private String upload;

    private String uninstall;

    private DateFormat dateFormatter;

    protected final MultiPartRequestWrapper getMultiPartRequest() {
         if (multiPartRequest == null && ServletActionContext.getRequest() instanceof MultiPartRequestWrapper) {
            multiPartRequest = (MultiPartRequestWrapper) ServletActionContext.getRequest();
        }
        return multiPartRequest;
    }

    @Override
    public final String execute() throws Exception {
        super.execute();
        // place security check in here as the webwork1 plugin in JIRA doesnt
        // support "required-roles"!
        if (!this.getPermissionManager().hasPermission(Permissions.ADMINISTER, getLoggedInUser())) {
            return PERMISSION_VIOLATION_RESULT;
        }
        if (uninstall != null) {
            return doUninstall();
        }
        if (upload != null) {
            return doInstall();
        }

        return INPUT;
    }

    private String doUninstall() {
        try {
            getLicenseManager().uninstall();
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            addErrorMessage(getText(e.getLocalizedMessage()));
            return ERROR;
        }
    }

    private String doInstall() {
        File licenseFile = getMultiPartRequest().getFile("licenseFile");
    	 
        if (licenseFile == null) {
            addErrorMessage(getText("license.error.uploadFileRequired"));
            return ERROR;
        }

        try {
            getLicenseManager().install(licenseFile);
        } catch (Exception e) {
            e.printStackTrace();
            addErrorMessage(getText(e.getLocalizedMessage()));
            return ERROR;
        }

        return SUCCESS;
    }

    public final LicenseContent getLicenseContent() {
        try {
            return getLicenseManager().verify();
        } catch (Exception e) {
            return null;
        }
    }


    public String getLicenseHolder() {
        LicenseContent content = getLicenseContent();
        if (content != null) {
            return LicenseUtils.formatX500Principal(content.getHolder());
        }
        return null;
    }

    public String getLicenseIssuer() {
        LicenseContent content = getLicenseContent();
        if (content != null) {
            return LicenseUtils.formatX500Principal(content.getIssuer());
        }
        return null;
    }

    public int getLicenseExpiring() {
        LicenseContent lc = getLicenseContent();
        if (lc != null && lc.getNotAfter() != null) {
            long now = System.currentTimeMillis();
            long expires = lc.getNotAfter().getTime();

            return (int) ((expires - now) / MILLIS_PER_DAY);
        }
        return Integer.MAX_VALUE;
    }

    public final String getUpload() {
        return upload;
    }

    public final void setUpload(final String upload) {
        this.upload = upload;
    }

    protected final LicenseManager getLicenseManager() {
        if (licenseManager == null) {
            licenseManager = createLicenseManager();
        }

        return licenseManager;
    }

    public final String getUninstall() {
        return uninstall;
    }

    public final void setUninstall(final String uninstall) {
        this.uninstall = uninstall;
    }

    public final DateFormat getDateFormatter() {
        if (dateFormatter == null) {
            dateFormatter = new SimpleDateFormat(getDateFormat());
        }
        return dateFormatter;
    }

    public final String getAbsoluteEulaPath() {
        String eulaPath = getEulaPath();
        if (eulaPath != null && eulaPath.startsWith("/")) {
            eulaPath = ServletActionContext.getRequest().getContextPath() + eulaPath;
        }
        return eulaPath;
    }

    /**
     * Called to create the license manager for this plugin.
     *
     * @return The license manager implementation.
     */
    protected abstract LicenseManager createLicenseManager();

    /**
     * Returns the name of the plugin, for use in the user interface.
     * Implementors may wish to use the {@link #getText(String)} method(s) to
     * internationalise the title.
     *
     * @return The plugin name.
     */
    public abstract String getPluginName();

    /**
     * Returns the Confluence-relative path to the EULA text.
     *
     * @return The EULA path.
     */
    public abstract String getEulaPath();
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy