File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\ui-samples-plugin\src\main\java\jenkins\plugins\ui_samples\UISample.java.html

1 package jenkins.plugins.ui_samples;
2
3 import hudson.ExtensionList;
4 import hudson.ExtensionPoint;
5 import hudson.model.Action;
6 import hudson.model.Describable;
7 import jenkins.model.Jenkins;
8 import org.kohsuke.stapler.StaplerRequest;
9 import org.kohsuke.stapler.StaplerResponse;
10
11 import java.io.IOException;
12 import java.net.URL;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16
17 import static org.apache.commons.io.IOUtils.copy;
18
19 /**
20 * @author Kohsuke Kawaguchi
21 */
22 public abstract class UISample implements ExtensionPoint, Action, Describable<UISample> {
23 public String getIconFileName() {
24 return "gear.png";
25 }
26
27 public String getUrlName() {
28 return getClass().getSimpleName();
29 }
30
31 /**
32 * Default display name.
33 */
34 public String getDisplayName() {
35 return getClass().getSimpleName();
36 }
37
38 /**
39 * Source files associated with this sample.
40 */
41 public List<SourceFile> getSourceFiles() {
42 List<SourceFile> r = new ArrayList<SourceFile>();
43
44 r.add(new SourceFile(getClass().getSimpleName()+".java"));
45 for (String name : new String[]{"index.jelly","index.groovy"}) {
46 SourceFile s = new SourceFile(name);
47 if (s.resolve()!=null)
48 r.add(s);
49 }
50 return r;
51 }
52
53 /**
54 * Binds {@link SourceFile}s into URL.
55 */
56 public void doSourceFile(StaplerRequest req, StaplerResponse rsp) throws IOException {
57 String name = req.getRestOfPath().substring(1); // Remove leading /
58 for (SourceFile sf : getSourceFiles())
59 if (sf.name.equals(name)) {
60 sf.doIndex(rsp);
61 return;
62 }
63 rsp.sendError(rsp.SC_NOT_FOUND);
64 }
65
66 /**
67 * Returns a paragraph of natural text that describes this sample.
68 * Interpreted as HTML.
69 */
70 public abstract String getDescription();
71
72 public UISampleDescriptor getDescriptor() {
73 return (UISampleDescriptor) Jenkins.getInstance().getDescriptorOrDie(getClass());
74 }
75
76 /**
77 * Returns all the registered {@link UISample}s.
78 */
79 public static ExtensionList<UISample> all() {
80 return Jenkins.getInstance().getExtensionList(UISample.class);
81 }
82
83 /**
84 * @author Kohsuke Kawaguchi
85 */
86 public class SourceFile {
87 public final String name;
88
89 public SourceFile(String name) {
90 this.name = name;
91 }
92
93 public URL resolve() {
94 return UISample.this.getClass().getResource(
95 (name.endsWith(".jelly") || name.endsWith(".groovy")) ? UISample.this.getClass().getSimpleName()+"/"+name : name);
96 }
97
98 /**
99 * Serves this source file.
100 */
101 public void doIndex(StaplerResponse rsp) throws IOException {
102 rsp.setContentType("text/plain;charset=UTF-8");
103 copy(resolve().openStream(),rsp.getOutputStream());
104 }
105 }
106
107 }
108