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");
}
}
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);
}
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());
}
}
}
}
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("");
}
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();
}
Aggregations