34
34
import edu .umd .cs .findbugs .annotations .NonNull ;
35
35
import hudson .Extension ;
36
36
import hudson .Functions ;
37
- import hudson .Main ;
38
37
import hudson .model .Api ;
39
38
import hudson .model .Failure ;
40
39
import hudson .model .RootAction ;
43
42
import hudson .security .Permission ;
44
43
import jakarta .servlet .ServletException ;
45
44
import jakarta .servlet .ServletOutputStream ;
45
+ import jakarta .servlet .http .HttpServletResponse ;
46
46
import java .io .File ;
47
47
import java .io .FileInputStream ;
48
48
import java .io .FileOutputStream ;
66
66
import java .util .stream .Collectors ;
67
67
import java .util .zip .ZipEntry ;
68
68
import java .util .zip .ZipOutputStream ;
69
-
70
- import jakarta .servlet .http .HttpServletResponse ;
71
69
import jenkins .model .Jenkins ;
72
70
import jenkins .util .ProgressiveRendering ;
73
71
import jenkins .util .Timer ;
@@ -110,11 +108,12 @@ public class SupportAction implements RootAction, StaplerProxy {
110
108
private final Logger logger = Logger .getLogger (SupportAction .class .getName ());
111
109
112
110
private static final String SUPPORT_BUNDLE_FILE_NAME = "support-bundle.zip" ;
113
- private static final String SUPPORT_BUNDLE_CREATION_FOLDER = Paths .get (System .getProperty ("java.io.tmpdir" )).resolve ("support-bundle" ).toString ();
111
+ private static final String SUPPORT_BUNDLE_CREATION_FOLDER = Paths .get (System .getProperty ("java.io.tmpdir" ))
112
+ .resolve ("support-bundle" )
113
+ .toString ();
114
114
115
115
private static final Map <UUID , SupportBundleAsyncGenerator > generatorMap = new ConcurrentHashMap <>();
116
116
117
-
118
117
@ Override
119
118
@ Restricted (NoExternalUse .class )
120
119
public Object getTarget () {
@@ -345,7 +344,6 @@ public void doGenerateAllBundles(StaplerRequest2 req, StaplerResponse2 rsp) thro
345
344
prepareBundle (rsp , components );
346
345
}
347
346
348
-
349
347
/**
350
348
* Generates a support bundle with selected components from the UI. in async
351
349
* @param req The stapler request
@@ -354,31 +352,32 @@ public void doGenerateAllBundles(StaplerRequest2 req, StaplerResponse2 rsp) thro
354
352
* @throws IOException If an input or output exception occurs
355
353
*/
356
354
@ RequirePOST
357
- public HttpRedirect doGenerateAllBundlesAsync (StaplerRequest2 req , StaplerResponse2 rsp ) throws ServletException , IOException {
355
+ public HttpRedirect doGenerateAllBundlesAsync (StaplerRequest2 req , StaplerResponse2 rsp )
356
+ throws ServletException , IOException {
358
357
JSONObject json = req .getSubmittedForm ();
359
358
if (!json .has ("components" )) {
360
359
rsp .sendError (SC_BAD_REQUEST );
361
360
return new HttpRedirect ("support" );
362
361
}
363
362
final List <Component > components = getComponents (req , json );
364
363
365
- for (Component component : components ) {
366
- if (component instanceof AboutBrowser ){
364
+ for (Component component : components ) {
365
+ if (component instanceof AboutBrowser ) {
367
366
AboutBrowser aboutBrowser = (AboutBrowser ) component ;
368
367
aboutBrowser .setScreenResolution (Functions .getScreenResolution ());
369
368
aboutBrowser .setCurrentRequest (Stapler .getCurrentRequest2 ());
370
369
aboutBrowser .setGeneratedAsync (true );
371
370
}
372
371
373
- if (component instanceof ReverseProxy ){
372
+ if (component instanceof ReverseProxy ) {
374
373
ReverseProxy reverseProxy = (ReverseProxy ) component ;
375
374
reverseProxy .setCurrentRequest (Stapler .getCurrentRequest2 ());
376
375
}
377
376
}
378
377
379
378
UUID taskId = UUID .randomUUID ();
380
379
SupportBundleAsyncGenerator supportBundleAsyncGenerator = new SupportBundleAsyncGenerator ();
381
- supportBundleAsyncGenerator .init (taskId ,components );
380
+ supportBundleAsyncGenerator .init (taskId , components );
382
381
generatorMap .put (taskId , supportBundleAsyncGenerator );
383
382
384
383
return new HttpRedirect ("progressPage?taskId=" + taskId );
@@ -511,31 +510,34 @@ public ProgressiveRendering getGenerateSupportBundle(@QueryParameter String task
511
510
return generatorMap .get (UUID .fromString (taskId ));
512
511
}
513
512
514
- public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
513
+ public static class SupportBundleAsyncGenerator extends ProgressiveRendering {
515
514
private final Logger logger = Logger .getLogger (SupportBundleAsyncGenerator .class .getName ());
516
515
private UUID taskId ;
517
516
private boolean isCompleted ;
518
517
private String pathToBundle ;
519
518
private List <Component > components ;
520
519
private boolean supportBundleGenerationInProgress = false ;
521
520
522
- public SupportBundleAsyncGenerator init ( UUID taskId ,List <Component > components ) {
521
+ public SupportBundleAsyncGenerator init (UUID taskId , List <Component > components ) {
523
522
this .taskId = taskId ;
524
523
this .components = components ;
525
524
return this ;
526
525
}
527
526
528
527
@ Override
529
528
protected void compute () throws Exception {
530
- if (!supportBundleGenerationInProgress ) {
529
+ if (!supportBundleGenerationInProgress ) {
531
530
this .supportBundleGenerationInProgress = true ;
532
- logger .info ("Generating support bundle... task id " + taskId );
531
+ logger .info ("Generating support bundle... task id " + taskId );
533
532
File outputDir = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId );
534
533
if (!outputDir .exists ()) {
535
- outputDir .mkdirs ();
534
+ if (!outputDir .mkdirs ()) {
535
+ throw new IOException ("Failed to create directory: " + outputDir .getAbsolutePath ());
536
+ }
536
537
}
537
538
538
- try (FileOutputStream fileOutputStream = new FileOutputStream (new File (outputDir , SUPPORT_BUNDLE_FILE_NAME ))) {
539
+ try (FileOutputStream fileOutputStream =
540
+ new FileOutputStream (new File (outputDir , SUPPORT_BUNDLE_FILE_NAME ))) {
539
541
SupportPlugin .setRequesterAuthentication (Jenkins .getAuthentication2 ());
540
542
try {
541
543
try (ACLContext ignored = ACL .as2 (ACL .SYSTEM2 )) {
@@ -568,25 +570,25 @@ protected JSON data() {
568
570
}
569
571
}
570
572
571
-
572
573
public void doDownloadBundle (@ QueryParameter ("taskId" ) String taskId , StaplerResponse2 rsp ) throws IOException {
573
- File bundleFile = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + SUPPORT_BUNDLE_FILE_NAME );
574
+ File bundleFile = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId + "/" + SUPPORT_BUNDLE_FILE_NAME );
574
575
if (!bundleFile .exists ()) {
575
576
rsp .sendError (HttpServletResponse .SC_NOT_FOUND , "Support bundle file not found" );
576
577
return ;
577
578
}
578
579
579
580
rsp .setContentType ("application/zip" );
580
- rsp .addHeader ("Content-Disposition" , "attachment; filename=" +SUPPORT_BUNDLE_FILE_NAME );
581
- try (ServletOutputStream outputStream = rsp .getOutputStream (); FileInputStream inputStream = new FileInputStream (bundleFile )) {
581
+ rsp .addHeader ("Content-Disposition" , "attachment; filename=" + SUPPORT_BUNDLE_FILE_NAME );
582
+ try (ServletOutputStream outputStream = rsp .getOutputStream ();
583
+ FileInputStream inputStream = new FileInputStream (bundleFile )) {
582
584
IOUtils .copy (inputStream , outputStream );
583
585
}
584
586
585
587
// Clean up temporary files after assembling the full bundle
586
588
Timer .get ()
587
589
.schedule (
588
590
() -> {
589
- File outputDir = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId );
591
+ File outputDir = new File (SUPPORT_BUNDLE_CREATION_FOLDER + "/" + taskId );
590
592
591
593
try {
592
594
FileUtils .deleteDirectory (outputDir );
0 commit comments