/
DownloadSettings.java
152 lines (130 loc) · 5.27 KB
/
DownloadSettings.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
/*
* The MIT License
*
* Copyright 2014 Jesse Glick.
*
* 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 jenkins.model;
import hudson.Extension;
import hudson.Main;
import hudson.model.AdministrativeMonitor;
import hudson.model.AsyncPeriodicWork;
import hudson.model.DownloadService;
import hudson.model.DownloadService.Downloadable;
import hudson.model.TaskListener;
import hudson.model.UpdateSite;
import hudson.util.FormValidation;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.acegisecurity.AccessDeniedException;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.HttpResponse;
/**
* Lets user configure how metadata files should be downloaded.
* @see UpdateSite
* @see DownloadService
*/
@Restricted(NoExternalUse.class) // no clear reason for this to be an API
@Extension @Symbol("downloadSettings")
public final class DownloadSettings extends GlobalConfiguration {
public static DownloadSettings get() {
return Jenkins.getInstance().getInjector().getInstance(DownloadSettings.class);
}
private boolean useBrowser = false;
public DownloadSettings() {
load();
}
public boolean isUseBrowser() {
return useBrowser;
}
public void setUseBrowser(boolean useBrowser) {
this.useBrowser = useBrowser;
save();
}
@Override public GlobalConfigurationCategory getCategory() {
return GlobalConfigurationCategory.get(GlobalConfigurationCategory.Security.class);
}
public static boolean usePostBack() {
return get().isUseBrowser() && Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER);
}
public static void checkPostBackAccess() throws AccessDeniedException {
if (!get().isUseBrowser()) {
throw new AccessDeniedException("browser-based download disabled");
}
Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER);
}
@Extension @Symbol("updateCenterCheck")
public static final class DailyCheck extends AsyncPeriodicWork {
private static final Logger LOGGER = Logger.getLogger(DailyCheck.class.getName());
public DailyCheck() {
super("Download metadata");
}
@Override public long getRecurrencePeriod() {
return DAY;
}
@Override public long getInitialDelay() {
return Main.isUnitTest ? DAY : 0;
}
@Override protected void execute(TaskListener listener) throws IOException, InterruptedException {
if (get().isUseBrowser()) {
return;
}
boolean due = false;
for (UpdateSite site : Jenkins.getInstance().getUpdateCenter().getSites()) {
if (site.isDue()) {
due = true;
break;
}
}
if (!due) {
// JENKINS-32886: downloadables like the tool installer data may have never been tried if the plugin
// was installed "after a restart", so let's give them a try here.
final long now = System.currentTimeMillis();
for (Downloadable d : Downloadable.all()) {
if (d.getDue() <= now) {
try {
d.updateNow();
} catch(Exception e) {
LOGGER.log(Level.WARNING, String.format("Unable to update downloadable [%s]", d.getId()), e);
}
}
}
return;
}
// This checks updates of the update sites and downloadables.
HttpResponse rsp = Jenkins.getInstance().getPluginManager().doCheckUpdatesServer();
if (rsp instanceof FormValidation) {
listener.error(((FormValidation) rsp).renderHtml());
}
}
}
@Extension public static final class Warning extends AdministrativeMonitor {
@Override
public String getDisplayName() {
return Messages.DownloadSettings_Warning_DisplayName();
}
@Override public boolean isActivated() {
return DownloadSettings.get().isUseBrowser();
}
}
}