Search in sources :

Example 1 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project support-core-plugin by jenkinsci.

the class SupportObjectAction method doGenerateAndDownload.

@RequirePOST
// used by Stapler
@SuppressWarnings("unused")
public final void doGenerateAndDownload(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException, Descriptor.FormException {
    Jenkins.get().checkPermission(Jenkins.ADMINISTER);
    LOGGER.fine("Preparing response...");
    rsp.setContentType("application/zip");
    JSONObject json = req.getSubmittedForm();
    if (json == null || !json.has("components")) {
        rsp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    LOGGER.fine("Parsing request...");
    List<ObjectComponent<T>> components = new ArrayList<>(parseRequest(req));
    rsp.addHeader("Content-Disposition", "inline; filename=" + BundleFileName.generate(getBundleNameQualifier()) + ";");
    try {
        SupportPlugin.setRequesterAuthentication(Jenkins.getAuthentication());
        try (ACLContext old = ACL.as(ACL.SYSTEM)) {
            SupportPlugin.writeBundle(rsp.getOutputStream(), components, new ComponentVisitor() {

                @Override
                public <C extends Component> void visit(Container container, C component) {
                    ((ObjectComponent<T>) component).addContents(container, object);
                }
            });
        } catch (IOException e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
        } finally {
            SupportPlugin.clearRequesterAuthentication();
        }
    } finally {
        LOGGER.fine("Response completed");
    }
}
Also used : ComponentVisitor(com.cloudbees.jenkins.support.api.ComponentVisitor) ACLContext(hudson.security.ACLContext) Container(com.cloudbees.jenkins.support.api.Container) ObjectComponent(com.cloudbees.jenkins.support.api.ObjectComponent) JSONObject(net.sf.json.JSONObject) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) ArrayList(java.util.ArrayList) IOException(java.io.IOException) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 2 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project support-core-plugin by jenkinsci.

the class SupportAction method doGenerateAllBundles.

/**
 * Generates a support bundle with selected components from the UI.
 * @param req The stapler request
 * @param rsp The stapler response
 * @throws ServletException If an error occurred during form submission
 * @throws IOException If an input or output exception occurs
 */
@RequirePOST
public void doGenerateAllBundles(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
    JSONObject json = req.getSubmittedForm();
    if (!json.has("components")) {
        rsp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    logger.fine("Parsing request...");
    Set<String> remove = new HashSet<>();
    for (Selection s : req.bindJSONToList(Selection.class, json.get("components"))) {
        if (!s.isSelected()) {
            logger.log(Level.FINER, "Excluding ''{0}'' from list of components to include", s.getName());
            remove.add(s.getName());
            // those components to the list of unselected components for backward compatibility
            if ("Master".equals(s.getName()) || "Agents".equals(s.getName())) {
                logger.log(Level.WARNING, Messages._SupportCommand_jenkins_63722_deprecated_ids(s.getName()).toString());
                remove.add(s.getName() + "JVMProcessSystemMetricsContents");
                remove.add(s.getName() + "SystemConfiguration");
            }
        }
    }
    logger.fine("Selecting components...");
    final List<Component> components = new ArrayList<>(getComponents());
    components.removeIf(c -> remove.contains(c.getId()) || !c.isEnabled());
    final SupportPlugin supportPlugin = SupportPlugin.getInstance();
    if (supportPlugin != null) {
        supportPlugin.setExcludedComponents(remove);
    }
    prepareBundle(rsp, components);
}
Also used : JSONObject(net.sf.json.JSONObject) ArrayList(java.util.ArrayList) Component(com.cloudbees.jenkins.support.api.Component) HashSet(java.util.HashSet) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 3 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project support-core-plugin by jenkinsci.

the class SupportAction method doDownloadBundles.

@RequirePOST
public void doDownloadBundles(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
    JSONObject json = req.getSubmittedForm();
    if (!json.has("bundles")) {
        rsp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    Set<String> bundlesToDownload = getSelectedBundles(req, json);
    File fileToDownload = null;
    if (bundlesToDownload.size() > 1) {
        // more than one bundles were selected, create a zip file
        fileToDownload = createZipFile(bundlesToDownload);
    } else {
        fileToDownload = new File(SupportPlugin.getRootDirectory(), bundlesToDownload.iterator().next());
    }
    logger.fine("Trying to download file " + fileToDownload.getAbsolutePath());
    try {
        rsp.setContentType("application/zip");
        rsp.addHeader("Content-Disposition", "inline; filename=" + fileToDownload.getName() + ";");
        FileUtils.copyFile(fileToDownload, rsp.getOutputStream());
        logger.info("Bundle " + fileToDownload.getAbsolutePath() + " successfully downloaded");
    } catch (RuntimeException e) {
        logger.log(Level.SEVERE, "Unable to download file " + fileToDownload.getAbsolutePath(), e);
    } finally {
        if (bundlesToDownload.size() > 1) {
            if (fileToDownload.delete()) {
                logger.log(Level.FINE, "Temporary multiBundle file deleted: " + fileToDownload.getAbsolutePath());
            } else {
                logger.log(Level.SEVERE, "Unable to delete temporary multiBundle file: " + fileToDownload.getAbsolutePath());
            }
        }
    }
}
Also used : JSONObject(net.sf.json.JSONObject) File(java.io.File) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 4 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project support-core-plugin by jenkinsci.

the class SupportAction method doDeleteBundles.

@RequirePOST
public void doDeleteBundles(StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
    JSONObject json = req.getSubmittedForm();
    if (!json.has("bundles")) {
        rsp.sendError(HttpServletResponse.SC_BAD_REQUEST);
        return;
    }
    Set<String> bundlesToDelete = getSelectedBundles(req, json);
    File rootDirectory = SupportPlugin.getRootDirectory();
    for (String bundleToDelete : bundlesToDelete) {
        File fileToDelete = new File(rootDirectory, bundleToDelete);
        logger.fine("Trying to delete bundle file " + fileToDelete.getAbsolutePath());
        try {
            if (fileToDelete.delete()) {
                logger.info("Bundle " + fileToDelete.getAbsolutePath() + " successfully deleted.");
            } else {
                logger.log(Level.SEVERE, "Unable to delete file " + fileToDelete.getAbsolutePath());
            }
        } catch (RuntimeException e) {
            logger.log(Level.SEVERE, "Unable to delete file " + fileToDelete.getAbsolutePath(), e);
        }
    }
    rsp.sendRedirect("");
}
Also used : JSONObject(net.sf.json.JSONObject) File(java.io.File) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Example 5 with RequirePOST

use of org.kohsuke.stapler.interceptor.RequirePOST in project promoted-builds-plugin by jenkinsci.

the class PromotedBuildAction method doForcePromotion.

/**
 * Force a promotion.
 */
@RequirePOST
public HttpResponse doForcePromotion(@QueryParameter("name") String name) throws IOException {
    JobPropertyImpl pp = getProject().getProperty(JobPropertyImpl.class);
    if (pp == null)
        throw new IllegalStateException("This project doesn't have any promotion criteria set");
    PromotionProcess p = pp.getItem(name);
    if (p == null)
        throw new IllegalStateException("This project doesn't have the promotion criterion called " + name);
    ManualCondition manualCondition = (ManualCondition) p.getPromotionCondition(ManualCondition.class.getName());
    PromotionPermissionHelper.checkPermission(getProject(), manualCondition);
    p.promote(owner, new UserCause(), new ManualPromotionBadge());
    return HttpResponses.redirectToDot();
}
Also used : ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) UserCause(hudson.model.Cause.UserCause) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST)

Aggregations

RequirePOST (org.kohsuke.stapler.interceptor.RequirePOST)12 JSONObject (net.sf.json.JSONObject)7 File (java.io.File)4 Restricted (org.kohsuke.accmod.Restricted)4 ACLContext (hudson.security.ACLContext)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 YamlSource (io.jenkins.plugins.casc.yaml.YamlSource)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Component (com.cloudbees.jenkins.support.api.Component)1 ComponentVisitor (com.cloudbees.jenkins.support.api.ComponentVisitor)1 Container (com.cloudbees.jenkins.support.api.Container)1 ObjectComponent (com.cloudbees.jenkins.support.api.ObjectComponent)1 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Extension (hudson.Extension)1 Functions (hudson.Functions)1 PluginManager (hudson.PluginManager)1