-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathAbstractItem.java
948 lines (852 loc) · 35.1 KB
/
AbstractItem.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Kohsuke Kawaguchi,
* Daniel Dyer, Tom Huybrechts, Yahoo!, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.model;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import com.infradna.tool.bridge_method_injector.WithBridgeMethods;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.AbortException;
import hudson.BulkChange;
import hudson.Functions;
import hudson.Util;
import hudson.XmlFile;
import hudson.cli.declarative.CLIResolver;
import hudson.model.listeners.ItemListener;
import hudson.model.listeners.SaveableListener;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.AccessControlled;
import hudson.util.AlternativeUiTextProvider;
import hudson.util.AlternativeUiTextProvider.Message;
import hudson.util.AtomicFileWriter;
import hudson.util.FormValidation;
import hudson.util.IOUtils;
import hudson.util.Secret;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import jenkins.model.DirectlyModifiableTopLevelItemGroup;
import jenkins.model.Jenkins;
import jenkins.model.Loadable;
import jenkins.model.queue.ItemDeletion;
import jenkins.security.NotReallyRoleSensitiveCallable;
import jenkins.util.SystemProperties;
import jenkins.util.xml.XMLUtils;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Copy;
import org.apache.tools.ant.types.FileSet;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.HttpDeletable;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.HttpResponses;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerProxy;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.WebMethod;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.interceptor.RequirePOST;
import org.springframework.security.access.AccessDeniedException;
import org.xml.sax.SAXException;
/**
* Partial default implementation of {@link Item}.
*
* @author Kohsuke Kawaguchi
*/
// Item doesn't necessarily have to be Actionable, but
// Java doesn't let multiple inheritance.
@ExportedBean
public abstract class AbstractItem extends Actionable implements Loadable, Item, HttpDeletable, AccessControlled, DescriptorByNameOwner, StaplerProxy {
private static final Logger LOGGER = Logger.getLogger(AbstractItem.class.getName());
/**
* Project name.
*/
protected /*final*/ transient String name;
/**
* Project description. Can be HTML.
*/
protected volatile String description;
private transient ItemGroup parent;
protected String displayName;
protected AbstractItem(ItemGroup parent, String name) {
this.parent = parent;
doSetName(name);
}
@Override
@Exported(visibility = 999)
public String getName() {
return name;
}
/**
* Get the term used in the UI to represent this kind of
* {@link Item}. Must start with a capital letter.
*/
public String getPronoun() {
return AlternativeUiTextProvider.get(PRONOUN, this, Messages.AbstractItem_Pronoun());
}
/**
* Gets the term used in the UI to represent the kind of {@link Queue.Task} associated with this kind of
* {@link Item}. Must start with a capital letter. Defaults to "Build".
* @since 2.50
*/
public String getTaskNoun() {
return AlternativeUiTextProvider.get(TASK_NOUN, this, Messages.AbstractItem_TaskNoun());
}
/**
* @return The display name of this object, or if it is not set, the name
* of the object.
*/
@Override
@Exported
public String getDisplayName() {
if (null != displayName) {
return displayName;
}
// if the displayName is not set, then return the name as we use to do
return getName();
}
/**
* This is intended to be used by the Job configuration pages where
* we want to return null if the display name is not set.
* @return The display name of this object or null if the display name is not
* set
*/
@Exported
public String getDisplayNameOrNull() {
return displayName;
}
/**
* This method exists so that the Job configuration pages can use
* getDisplayNameOrNull so that nothing is shown in the display name text
* box if the display name is not set.
*/
public void setDisplayNameOrNull(String displayName) throws IOException {
setDisplayName(displayName);
}
public void setDisplayName(String displayName) throws IOException {
this.displayName = Util.fixEmptyAndTrim(displayName);
save();
}
@Override
public File getRootDir() {
return getParent().getRootDirFor(this);
}
/**
* This bridge method is to maintain binary compatibility with {@link TopLevelItem#getParent()}.
*/
@WithBridgeMethods(value = Jenkins.class, castRequired = true)
@Override public @NonNull ItemGroup getParent() {
if (parent == null) {
throw new IllegalStateException("no parent set on " + getClass().getName() + "[" + name + "]");
}
return parent;
}
/**
* Gets the project description HTML.
*/
@Exported
public String getDescription() {
return description;
}
/**
* Sets the project description HTML.
*/
public void setDescription(String description) throws IOException {
this.description = description;
save();
ItemListener.fireOnUpdated(this);
}
/**
* Just update {@link #name} without performing the rename operation,
* which would involve copying files and etc.
*/
protected void doSetName(String name) {
this.name = name;
}
/**
* Controls whether the default rename action is available for this item.
*
* @return whether {@link #name} can be modified by a user
* @see #checkRename
* @see #renameTo
* @since 2.110
*/
public boolean isNameEditable() {
return false;
}
/**
* Renames this item
*/
@RequirePOST
@Restricted(NoExternalUse.class)
public HttpResponse doConfirmRename(@QueryParameter String newName) throws IOException {
newName = newName == null ? null : newName.trim();
FormValidation validationError = doCheckNewName(newName);
if (validationError.kind != FormValidation.Kind.OK) {
throw new Failure(validationError.getMessage());
}
renameTo(newName);
// send to the new job page
// note we can't use getUrl() because that would pick up old name in the
// Ancestor.getUrl()
return HttpResponses.redirectTo("../" + Functions.encode(newName));
}
/**
* Called by {@link #doConfirmRename} and {@code rename.jelly} to validate renames.
* @return {@link FormValidation#ok} if this item can be renamed as specified, otherwise
* {@link FormValidation#error} with a message explaining the problem.
*/
@Restricted(NoExternalUse.class)
public @NonNull FormValidation doCheckNewName(@QueryParameter String newName) {
if (!isNameEditable()) {
return FormValidation.error("Trying to rename an item that does not support this operation.");
}
// TODO: Create an Item.RENAME permission to use here, see JENKINS-18649.
if (!hasPermission(Item.CONFIGURE)) {
if (parent instanceof AccessControlled) {
((AccessControlled) parent).checkPermission(Item.CREATE);
}
checkPermission(Item.DELETE);
}
newName = newName == null ? null : newName.trim();
try {
Jenkins.checkGoodName(newName);
assert newName != null; // Would have thrown Failure
if (newName.equals(name)) {
return FormValidation.warning(Messages.AbstractItem_NewNameUnchanged());
}
Jenkins.get().getProjectNamingStrategy().checkName(getParent().getFullName(), newName);
checkIfNameIsUsed(newName);
checkRename(newName);
} catch (Failure e) {
return FormValidation.error(e.getMessage());
}
return FormValidation.ok();
}
/**
* Check new name for job
* @param newName - New name for job.
*/
private void checkIfNameIsUsed(@NonNull String newName) throws Failure {
try {
Item item = getParent().getItem(newName);
if (item != null) {
throw new Failure(Messages.AbstractItem_NewNameInUse(newName));
}
try (ACLContext ctx = ACL.as2(ACL.SYSTEM2)) {
item = getParent().getItem(newName);
if (item != null) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Unable to rename the job {0}: name {1} is already in use. " +
"User {2} has no {3} permission for existing job with the same name",
new Object[] {this.getFullName(), newName, ctx.getPreviousContext2().getAuthentication().getName(), Item.DISCOVER.name});
}
// Don't explicitly mention that there is another item with the same name.
throw new Failure(Messages.Jenkins_NotAllowedName(newName));
}
}
} catch (AccessDeniedException ex) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Unable to rename the job {0}: name {1} is already in use. " +
"User {2} has {3} permission, but no {4} for existing job with the same name",
new Object[] {this.getFullName(), newName, User.current(), Item.DISCOVER.name, Item.READ.name});
}
throw new Failure(Messages.AbstractItem_NewNameInUse(newName));
}
}
/**
* Allows subclasses to block renames for domain-specific reasons. Generic validation of the new name
* (e.g., null checking, checking for illegal characters, and checking that the name is not in use)
* always happens prior to calling this method.
*
* @param newName the new name for the item
* @throws Failure if the rename should be blocked
* @since 2.110
* @see Job#checkRename
*/
protected void checkRename(@NonNull String newName) throws Failure {
}
/**
* Renames this item.
* Not all the Items need to support this operation, but if you decide to do so,
* you can use this method.
*/
@SuppressFBWarnings(value = "SWL_SLEEP_WITH_LOCK_HELD", justification = "no big deal")
protected void renameTo(final String newName) throws IOException {
if (!isNameEditable()) {
throw new IOException("Trying to rename an item that does not support this operation.");
}
// always synchronize from bigger objects first
final ItemGroup parent = getParent();
String oldName = this.name;
String oldFullName = getFullName();
synchronized (parent) {
synchronized (this) {
// sanity check
if (newName == null)
throw new IllegalArgumentException("New name is not given");
// noop?
if (this.name.equals(newName))
return;
// the lookup is case insensitive, so we should not fail if this item was the “existing” one
// to allow people to rename "Foo" to "foo", for example.
// see http://www.nabble.com/error-on-renaming-project-tt18061629.html
Items.verifyItemDoesNotAlreadyExist(parent, newName, this);
File oldRoot = this.getRootDir();
doSetName(newName);
File newRoot = this.getRootDir();
boolean success = false;
try { // rename data files
boolean interrupted = false;
boolean renamed = false;
// try to rename the job directory.
// this may fail on Windows due to some other processes
// accessing a file.
// so retry few times before we fall back to copy.
for (int retry = 0; retry < 5; retry++) {
if (oldRoot.renameTo(newRoot)) {
renamed = true;
break; // succeeded
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// process the interruption later
interrupted = true;
}
}
if (interrupted)
Thread.currentThread().interrupt();
if (!renamed) {
// failed to rename. it must be that some lengthy
// process is going on
// to prevent a rename operation. So do a copy. Ideally
// we'd like to
// later delete the old copy, but we can't reliably do
// so, as before the VM
// shuts down there might be a new job created under the
// old name.
Copy cp = new Copy();
cp.setProject(new Project());
cp.setTodir(newRoot);
FileSet src = new FileSet();
src.setDir(oldRoot);
cp.addFileset(src);
cp.setOverwrite(true);
cp.setPreserveLastModified(true);
cp.setFailOnError(false); // keep going even if
// there's an error
cp.execute();
// try to delete as much as possible
try {
Util.deleteRecursive(oldRoot);
} catch (IOException e) {
// but ignore the error, since we expect that
e.printStackTrace();
}
}
success = true;
} finally {
// if failed, back out the rename.
if (!success)
doSetName(oldName);
}
parent.onRenamed(this, oldName, newName);
}
}
ItemListener.fireLocationChange(this, oldFullName);
}
/**
* Notify this item it's been moved to another location, replaced by newItem (might be the same object, but not guaranteed).
* This method is executed <em>after</em> the item root directory has been moved to it's new location.
* <p>
* Derived classes can override this method to add some specific behavior on move, but have to call parent method
* so the item is actually setup within it's new parent.
*
* @see hudson.model.Items#move(AbstractItem, jenkins.model.DirectlyModifiableTopLevelItemGroup)
*/
public void movedTo(DirectlyModifiableTopLevelItemGroup destination, AbstractItem newItem, File destDir) throws IOException {
newItem.onLoad(destination, name);
}
/**
* Gets all the jobs that this {@link Item} contains as descendants.
*/
@Override
public abstract Collection<? extends Job> getAllJobs();
@Override
@Exported
public final String getFullName() {
String n = getParent().getFullName();
if (n.isEmpty()) return getName();
else return n + '/' + getName();
}
@Override
@Exported
public final String getFullDisplayName() {
String n = getParent().getFullDisplayName();
if (n.isEmpty()) return getDisplayName();
else return n + " » " + getDisplayName();
}
/**
* Gets the display name of the current item relative to the given group.
*
* @since 1.515
* @param p the ItemGroup used as point of reference for the item
* @return
* String like "foo » bar"
*/
public String getRelativeDisplayNameFrom(ItemGroup p) {
return Functions.getRelativeDisplayNameFrom(this, p);
}
/**
* This method only exists to disambiguate {@link #getRelativeNameFrom(ItemGroup)} and {@link #getRelativeNameFrom(Item)}
* @since 1.512
* @see #getRelativeNameFrom(ItemGroup)
*/
public String getRelativeNameFromGroup(ItemGroup p) {
return getRelativeNameFrom(p);
}
/**
* Called right after when a {@link Item} is loaded from disk.
* This is an opportunity to do a post load processing.
*/
@Override
public void onLoad(ItemGroup<? extends Item> parent, String name) throws IOException {
this.parent = parent;
doSetName(name);
}
/**
* When a {@link Item} is copied from existing one,
* the files are first copied on the file system,
* then it will be loaded, then this method will be invoked
* to perform any implementation-specific work.
*
* @param src
* Item from which it's copied from. The same type as {@code this}. Never null.
*/
@Override
public void onCopiedFrom(Item src) {
}
@Override
public final String getUrl() {
// try to stick to the current view if possible
StaplerRequest req = Stapler.getCurrentRequest();
String shortUrl = getShortUrl();
String uri = req == null ? null : req.getRequestURI();
if (req != null) {
String seed = Functions.getNearestAncestorUrl(req, this);
LOGGER.log(Level.FINER, "seed={0} for {1} from {2}", new Object[] {seed, this, uri});
if (seed != null) {
// trim off the context path portion and leading '/', but add trailing '/'
return seed.substring(req.getContextPath().length() + 1) + '/';
}
List<Ancestor> ancestors = req.getAncestors();
if (!ancestors.isEmpty()) {
Ancestor last = ancestors.get(ancestors.size() - 1);
if (last.getObject() instanceof View view) {
if (view.getOwner().getItemGroup() == getParent() && !view.isDefault()) {
// Showing something inside a view, so should use that as the base URL.
String prefix = req.getContextPath() + "/";
String url = last.getUrl();
if (url.startsWith(prefix)) {
String base = url.substring(prefix.length()) + '/';
LOGGER.log(Level.FINER, "using {0}{1} for {2} from {3} given {4}", new Object[] {base, shortUrl, this, uri, prefix});
return base + shortUrl;
} else {
LOGGER.finer(() -> url + " does not start with " + prefix + " as expected");
}
} else {
LOGGER.log(Level.FINER, "irrelevant {0} for {1} from {2}", new Object[] {view.getViewName(), this, uri});
}
} else {
LOGGER.log(Level.FINER, "inapplicable {0} for {1} from {2}", new Object[] {last.getObject(), this, uri});
}
} else {
LOGGER.log(Level.FINER, "no ancestors for {0} from {1}", new Object[] {this, uri});
}
} else {
LOGGER.log(Level.FINER, "no current request for {0}", this);
}
// otherwise compute the path normally
String base = getParent().getUrl();
LOGGER.log(Level.FINER, "falling back to {0}{1} for {2} from {3}", new Object[] {base, shortUrl, this, uri});
return base + shortUrl;
}
@Override
public String getShortUrl() {
String prefix = getParent().getUrlChildPrefix();
String subdir = Util.rawEncode(getName());
return prefix.equals(".") ? subdir + '/' : prefix + '/' + subdir + '/';
}
@Override
public String getSearchUrl() {
return getShortUrl();
}
@Override
@Exported(visibility = 999, name = "url")
public final String getAbsoluteUrl() {
return Item.super.getAbsoluteUrl();
}
/**
* Remote API access.
*/
public final Api getApi() {
return new Api(this);
}
/**
* Returns the {@link ACL} for this object.
*/
@NonNull
@Override
public ACL getACL() {
return Jenkins.get().getAuthorizationStrategy().getACL(this);
}
/**
* Save the settings to a file.
*/
@Override
public synchronized void save() throws IOException {
if (BulkChange.contains(this)) return;
getConfigFile().write(this);
SaveableListener.fireOnChange(this, getConfigFile());
}
public final XmlFile getConfigFile() {
return Items.getConfigFile(this);
}
protected Object writeReplace() {
return XmlFile.replaceIfNotAtTopLevel(this, () -> new Replacer(this));
}
private static class Replacer {
private final String fullName;
Replacer(AbstractItem i) {
fullName = i.getFullName();
}
private Object readResolve() {
Jenkins j = Jenkins.getInstanceOrNull();
if (j == null) {
return null;
}
// Will generally only work if called after job loading:
return j.getItemByFullName(fullName);
}
}
/**
* Accepts the new description.
*/
@RequirePOST
public synchronized void doSubmitDescription(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
checkPermission(CONFIGURE);
setDescription(req.getParameter("description"));
rsp.sendRedirect("."); // go to the top page
}
/**
* Deletes this item.
* Note on the funny name: for reasons of historical compatibility, this URL is {@code /doDelete}
* since it predates {@code <l:confirmationLink>}. {@code /delete} goes to a Jelly page
* which should now be unused by core but is left in case plugins are still using it.
*/
@RequirePOST
public void doDoDelete(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException, InterruptedException {
delete();
if (req == null || rsp == null) { // CLI
return;
}
List<Ancestor> ancestors = req.getAncestors();
ListIterator<Ancestor> it = ancestors.listIterator(ancestors.size());
String url = getParent().getUrl(); // fallback but we ought to get to Jenkins.instance at the root
while (it.hasPrevious()) {
Object a = it.previous().getObject();
if (a instanceof View) {
url = ((View) a).getUrl();
break;
} else if (a instanceof ViewGroup && a != this) {
url = ((ViewGroup) a).getUrl();
break;
}
}
rsp.sendRedirect2(req.getContextPath() + '/' + url);
}
@Override
public void delete(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
try {
delete();
rsp.setStatus(204);
} catch (InterruptedException e) {
// TODO: allow this in Stapler
throw new ServletException(e);
}
}
/**
* Deletes this item.
*
* <p>
* Any exception indicates the deletion has failed, but {@link AbortException} would prevent the caller
* from showing the stack trace.
* @see ItemDeletion
*/
@Override
public void delete() throws IOException, InterruptedException {
checkPermission(DELETE);
ItemListener.checkBeforeDelete(this);
boolean responsibleForAbortingBuilds = !ItemDeletion.contains(this);
boolean ownsRegistration = ItemDeletion.register(this);
if (!ownsRegistration && ItemDeletion.isRegistered(this)) {
// we are not the owning thread and somebody else is concurrently deleting this exact item
throw new Failure(Messages.AbstractItem_BeingDeleted(getPronoun()));
}
try {
// if a build is in progress. Cancel it.
if (responsibleForAbortingBuilds || ownsRegistration) {
ItemDeletion.cancelBuildsInProgress(this);
}
if (this instanceof ItemGroup) {
// delete individual items first
// (disregard whether they would be deletable in isolation)
// JENKINS-34939: do not hold the monitor on this folder while deleting them
// (thus we cannot do this inside performDelete)
try (ACLContext oldContext = ACL.as2(ACL.SYSTEM2)) {
for (Item i : ((ItemGroup<?>) this).getItems(TopLevelItem.class::isInstance)) {
try {
i.delete();
} catch (AbortException e) {
throw (AbortException) new AbortException(
"Failed to delete " + i.getFullDisplayName() + " : " + e.getMessage()).initCause(e);
} catch (IOException e) {
throw new IOException("Failed to delete " + i.getFullDisplayName(), e);
}
}
}
}
synchronized (this) { // could just make performDelete synchronized but overriders might not honor that
performDelete();
} // JENKINS-19446: leave synch block, but JENKINS-22001: still notify synchronously
} finally {
if (ownsRegistration) {
ItemDeletion.deregister(this);
}
}
getParent().onDeleted(AbstractItem.this);
Jenkins.get().rebuildDependencyGraphAsync();
}
/**
* Does the real job of deleting the item.
*/
protected void performDelete() throws IOException, InterruptedException {
getConfigFile().delete();
Util.deleteRecursive(getRootDir());
}
/**
* Accepts {@code config.xml} submission, as well as serve it.
*/
@WebMethod(name = "config.xml")
public void doConfigDotXml(StaplerRequest req, StaplerResponse rsp)
throws IOException {
if (req.getMethod().equals("GET")) {
// read
rsp.setContentType("application/xml");
writeConfigDotXml(rsp.getOutputStream());
return;
}
if (req.getMethod().equals("POST")) {
// submission
updateByXml((Source) new StreamSource(req.getReader()));
return;
}
// huh?
rsp.sendError(SC_BAD_REQUEST);
}
static final Pattern SECRET_PATTERN = Pattern.compile(">(" + Secret.ENCRYPTED_VALUE_PATTERN + ")<");
/**
* Writes {@code config.xml} to the specified output stream.
* The user must have at least {@link #EXTENDED_READ}.
* If he lacks {@link #CONFIGURE}, then any {@link Secret}s detected will be masked out.
*/
@Restricted(NoExternalUse.class)
public void writeConfigDotXml(OutputStream os) throws IOException {
checkPermission(EXTENDED_READ);
XmlFile configFile = getConfigFile();
if (hasPermission(CONFIGURE)) {
IOUtils.copy(configFile.getFile(), os);
} else {
String encoding = configFile.sniffEncoding();
String xml = Files.readString(Util.fileToPath(configFile.getFile()), Charset.forName(encoding));
Matcher matcher = SECRET_PATTERN.matcher(xml);
StringBuilder cleanXml = new StringBuilder();
while (matcher.find()) {
if (Secret.decrypt(matcher.group(1)) != null) {
matcher.appendReplacement(cleanXml, ">********<");
}
}
matcher.appendTail(cleanXml);
org.apache.commons.io.IOUtils.write(cleanXml.toString(), os, encoding);
}
}
/**
* @deprecated as of 1.473
* Use {@link #updateByXml(Source)}
*/
@Deprecated
public void updateByXml(StreamSource source) throws IOException {
updateByXml((Source) source);
}
/**
* Updates an Item by its XML definition.
* @param source source of the Item's new definition.
* The source should be either a {@link StreamSource} or a {@link SAXSource}, other
* sources may not be handled.
* @since 1.473
*/
public void updateByXml(Source source) throws IOException {
checkPermission(CONFIGURE);
XmlFile configXmlFile = getConfigFile();
final AtomicFileWriter out = new AtomicFileWriter(configXmlFile.getFile());
try {
try {
XMLUtils.safeTransform(source, new StreamResult(out));
out.close();
} catch (TransformerException | SAXException e) {
throw new IOException("Failed to persist config.xml", e);
}
// try to reflect the changes by reloading
Object o = new XmlFile(Items.XSTREAM, out.getTemporaryPath().toFile()).unmarshalNullingOut(this);
if (o != this) {
// ensure that we've got the same job type. extending this code to support updating
// to different job type requires destroying & creating a new job type
throw new IOException("Expecting " + this.getClass() + " but got " + o.getClass() + " instead");
}
Items.whileUpdatingByXml(new NotReallyRoleSensitiveCallable<Void, IOException>() {
@Override public Void call() throws IOException {
onLoad(getParent(), getRootDir().getName());
return null;
}
});
Jenkins.get().rebuildDependencyGraphAsync();
// if everything went well, commit this new version
out.commit();
SaveableListener.fireOnChange(this, getConfigFile());
ItemListener.fireOnUpdated(this);
} finally {
out.abort(); // don't leave anything behind
}
}
/**
* Reloads this job from the disk.
*
* Exposed through CLI as well.
*
* TODO: think about exposing this to UI
*
* @since 1.556
*/
@RequirePOST
public void doReload() throws IOException {
load();
}
@Override
public void load() throws IOException {
checkPermission(CONFIGURE);
// try to reflect the changes by reloading
getConfigFile().unmarshal(this);
Items.whileUpdatingByXml(new NotReallyRoleSensitiveCallable<Void, IOException>() {
@Override
public Void call() throws IOException {
onLoad(getParent(), getParent().getItemName(getRootDir(), AbstractItem.this));
return null;
}
});
Jenkins.get().rebuildDependencyGraphAsync();
}
@Override
public String getSearchName() {
// the search name of abstract items should be the name and not display name.
// this will make suggestions use the names and not the display name
// so that the links will 302 directly to the thing the user was finding
return getName();
}
@Override public String toString() {
return super.toString() + '[' + (parent != null ? getFullName() : "?/" + name) + ']';
}
@Override
@Restricted(NoExternalUse.class)
public Object getTarget() {
if (!SKIP_PERMISSION_CHECK) {
if (!hasPermission(Item.DISCOVER)) {
return null;
}
checkPermission(Item.READ);
}
return this;
}
/**
* Escape hatch for StaplerProxy-based access control
*/
@Restricted(NoExternalUse.class)
@SuppressFBWarnings(value = "MS_SHOULD_BE_FINAL", justification = "for script console")
public static /* Script Console modifiable */ boolean SKIP_PERMISSION_CHECK = SystemProperties.getBoolean(AbstractItem.class.getName() + ".skipPermissionCheck");
/**
* Used for CLI binding.
*/
@CLIResolver
public static AbstractItem resolveForCLI(
@Argument(required = true, metaVar = "NAME", usage = "Item name") String name) throws CmdLineException {
// TODO can this (and its pseudo-override in AbstractProject) share code with GenericItemOptionHandler, used for explicit CLICommand’s rather than CLIMethod’s?
AbstractItem item = Jenkins.get().getItemByFullName(name, AbstractItem.class);
if (item == null) {
AbstractItem project = Items.findNearest(AbstractItem.class, name, Jenkins.get());
throw new CmdLineException(null, project == null ? Messages.AbstractItem_NoSuchJobExistsWithoutSuggestion(name)
: Messages.AbstractItem_NoSuchJobExists(name, project.getFullName()));
}
return item;
}
/**
* Replaceable pronoun of that points to a job. Defaults to "Job"/"Project" depending on the context.
*/
public static final Message<AbstractItem> PRONOUN = new Message<>();
/**
* Replaceable noun for describing the kind of task that this item represents. Defaults to "Build".
*/
public static final Message<AbstractItem> TASK_NOUN = new Message<>();
}