UpdateCenterAction.java 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
package io.jenkins.plugins.localization_zh_cn;

import hudson.Extension;
import hudson.model.RootAction;
import jenkins.model.Jenkins;
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;

import javax.annotation.CheckForNull;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Extension
public class UpdateCenterAction implements RootAction {
    private final String CRT = "mirror-adapter.crt";

    @RequirePOST
    public void doUse(StaplerResponse response) throws IOException {
        if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        try (InputStream input = this.getClass().getResourceAsStream("/" + CRT);
             OutputStream output = new FileOutputStream(new File(Jenkins.get().getRootDir(),
                     "/war/WEB-INF/update-center-rootCAs/" + CRT))) {
            if (input == null) {
                return;
            }

            IOUtils.copy(input, output);
        }
        response.sendRedirect(Jenkins.get().getRootUrl() + "/chinese");
    }

    @RequirePOST
    public void doRemove(StaplerResponse response) throws IOException {
        if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }

        File crtFile = new File(Jenkins.get().getRootDir(),
                "/war/WEB-INF/update-center-rootCAs/" + CRT);
        if(crtFile.isFile()) {
            crtFile.delete();
        }
        response.sendRedirect(Jenkins.get().getRootUrl() + "/chinese");
    }

    @CheckForNull
    @Override
    public String getIconFileName() {
        return null;
    }

    @CheckForNull
    @Override
    public String getDisplayName() {
        return "Use mirror of update center";
    }

    @CheckForNull
    @Override
    public String getUrlName() {
        return "/update-center-mirror";
    }
}