-
Notifications
You must be signed in to change notification settings - Fork 43
/
SidebarLinkPlugin.java
151 lines (137 loc) · 5.49 KB
/
SidebarLinkPlugin.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
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Alan Harder
*
* 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.plugins.sidebar_link;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.kohsuke.stapler.interceptor.RequirePOST;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Hudson;
import hudson.util.FormValidation;
import jenkins.model.GlobalConfiguration;
import jenkins.model.Jenkins;
/**
* Add links in the main page sidepanel.
*
* @author Alan Harder
*/
@Extension
@Symbol("sidebarGlobalLink")
public class SidebarLinkPlugin extends GlobalConfiguration {
@SuppressWarnings("unused")
private static final Logger LOGGER = Logger.getLogger(SidebarLinkPlugin.class.getName());
private List<LinkAction> links = new ArrayList<>();
public SidebarLinkPlugin() {
// When Jenkins is restarted, load any saved configuration from disk.
load();
Jenkins.get().getActions().addAll(links);
}
/** @return the currently configured links, if any */
public List<LinkAction> getLinks() {
return links;
}
/**
* Together with {@link #getLinks}, binds to entry in {@code config.jelly}.
* @param links the new value of this field
*/
@DataBoundSetter
public void setLinks(List<LinkAction> links) {
/* remove all links of this type */
Jenkins.get().getActions().removeAll(this.links);
this.links.clear();
this.links.addAll(links);
Jenkins.get().getActions().addAll(this.links);
save();
}
/**
* Receive file upload from startUpload.jelly.
* File is placed in $JENKINS_HOME/userContent directory.
*/
@RequirePOST
@Restricted(NoExternalUse.class)
public void doUploadLinkImage(StaplerRequest req, StaplerResponse rsp)
throws IOException, ServletException, InterruptedException {
Jenkins jenkins = Jenkins.get();
jenkins.checkPermission(Hudson.ADMINISTER);
FileItem file = req.getFileItem("linkimage.file");
String error = null;
String filename = null;
if (file == null || file.getName().isEmpty())
error = Messages.NoFile();
else {
filename = "userContent/"
// Sanitize given filename:
+ file.getName().replaceFirst(".*/", "").replaceAll("[^\\w.,;:()#@!=+-]", "_");
FilePath imageFile = jenkins.getRootPath().child(filename);
if (imageFile.exists())
error = Messages.DupName();
else {
imageFile.copyFrom(file.getInputStream());
imageFile.chmod(0644);
}
}
rsp.setContentType("text/html");
rsp.getWriter().println(
(error != null ? error : Messages.Uploaded("<tt>/" + filename + "</tt>"))
+ " <a href=\"javascript:history.back()\">" + Messages.Back() + "</a>");
}
@Restricted(NoExternalUse.class)
public FormValidation doCheckLinkUrl(@QueryParameter String value) {
return LinkProtection.verifyUrl(value);
}
@Restricted(NoExternalUse.class)
public FormValidation doCheckLinkText(@QueryParameter String value) {
if (StringUtils.isBlank(value)) {
return FormValidation.error("The provided text is blank or empty");
}
return FormValidation.ok();
}
@Restricted(NoExternalUse.class)
public FormValidation doCheckLinkIcon(@QueryParameter String value) {
if (StringUtils.isBlank(value)) {
return FormValidation.warning("The provided icon is blank or empty. Default will be used.");
}
FilePath imageFile = Jenkins.get().getRootPath().child(value);
try {
if (!imageFile.exists()) {
return FormValidation.error("Image does not exist: " + imageFile);
}
} catch (Exception e) {
return FormValidation.error(e, "Problem with link icon: " + value);
}
return FormValidation.ok();
}
}