Search in sources :

Example 1 with ComponentVisitor

use of com.cloudbees.jenkins.support.api.ComponentVisitor 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)

Aggregations

ComponentVisitor (com.cloudbees.jenkins.support.api.ComponentVisitor)1 Container (com.cloudbees.jenkins.support.api.Container)1 ObjectComponent (com.cloudbees.jenkins.support.api.ObjectComponent)1 ACLContext (hudson.security.ACLContext)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 JSONObject (net.sf.json.JSONObject)1 RequirePOST (org.kohsuke.stapler.interceptor.RequirePOST)1