JobConsumerManagerConfigurationServlet.java
5.93 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.servlet.ServletException
* org.apache.felix.scr.annotations.Properties
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.sling.SlingServlet
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.SlingHttpServletResponse
* org.apache.sling.api.servlets.SlingAllMethodsServlet
* org.apache.sling.discovery.DiscoveryService
* org.apache.sling.discovery.InstanceDescription
* org.apache.sling.discovery.TopologyView
* org.osgi.service.cm.Configuration
* org.osgi.service.cm.ConfigurationAdmin
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.offloading.impl.servlets;
import com.adobe.granite.offloading.api.InstanceNetworkInfoService;
import com.adobe.granite.offloading.impl.DiscoveryConfigurationCache;
import java.io.IOException;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Set;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.sling.SlingServlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.discovery.DiscoveryService;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.TopologyView;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@SlingServlet(methods={"POST"}, paths={"/libs/granite/offloading/config/jobconsumer"})
@Properties(value={@Property(name="sling.auth.requirements", value={"-/libs/granite/offloading/config/jobconsumer"})})
public class JobConsumerManagerConfigurationServlet
extends SlingAllMethodsServlet {
private static final Logger LOG = LoggerFactory.getLogger(JobConsumerManagerConfigurationServlet.class);
@Reference
private ConfigurationAdmin configurationAdmin;
@Reference
private DiscoveryService discoveryService;
@Reference
private DiscoveryConfigurationCache discoveryConfigurationCache;
@Reference
private InstanceNetworkInfoService iins;
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Set<String> allowedHosts = this.discoveryConfigurationCache.getAllowedHosts();
InstanceDescription localInstance = this.discoveryService.getTopology().getLocalInstance();
allowedHosts.addAll(this.iins.getListeningAddresses(localInstance));
if (allowedHosts.contains(request.getRemoteAddr()) || allowedHosts.contains(request.getRemoteHost())) {
Configuration jobConsumerManagerConfig = this.configurationAdmin.getConfiguration("org.apache.sling.event.impl.jobs.JobConsumerManager");
if (jobConsumerManagerConfig != null) {
Hashtable<String, String[]> properties;
String[] whitelist = request.getParameterValues("job.consumermanager.whitelist");
String[] blacklist = request.getParameterValues("job.consumermanager.blacklist");
Boolean persistConfig = Boolean.valueOf(request.getParameter("org.apache.sling.installer.configuration.persist"));
if (whitelist == null && blacklist == null) {
response.sendError(400, "Expected job.consumermanager.blacklist or job.consumermanager.whitelist as a request parameter");
}
if ((properties = jobConsumerManagerConfig.getProperties()) == null) {
properties = new Hashtable<String, String[]>();
}
if (whitelist != null) {
properties.put("job.consumermanager.whitelist", whitelist);
}
if (blacklist != null) {
properties.put("job.consumermanager.blacklist", blacklist);
}
properties.put("org.apache.sling.installer.configuration.persist", (String[])persistConfig);
jobConsumerManagerConfig.update(properties);
}
} else {
response.sendError(404);
LOG.error("Request made by unauthorized host: (" + request.getRemoteAddr() + ":" + request.getRemoteHost() + ")");
}
}
protected void bindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
this.configurationAdmin = configurationAdmin;
}
protected void unbindConfigurationAdmin(ConfigurationAdmin configurationAdmin) {
if (this.configurationAdmin == configurationAdmin) {
this.configurationAdmin = null;
}
}
protected void bindDiscoveryService(DiscoveryService discoveryService) {
this.discoveryService = discoveryService;
}
protected void unbindDiscoveryService(DiscoveryService discoveryService) {
if (this.discoveryService == discoveryService) {
this.discoveryService = null;
}
}
protected void bindDiscoveryConfigurationCache(DiscoveryConfigurationCache discoveryConfigurationCache) {
this.discoveryConfigurationCache = discoveryConfigurationCache;
}
protected void unbindDiscoveryConfigurationCache(DiscoveryConfigurationCache discoveryConfigurationCache) {
if (this.discoveryConfigurationCache == discoveryConfigurationCache) {
this.discoveryConfigurationCache = null;
}
}
protected void bindIins(InstanceNetworkInfoService instanceNetworkInfoService) {
this.iins = instanceNetworkInfoService;
}
protected void unbindIins(InstanceNetworkInfoService instanceNetworkInfoService) {
if (this.iins == instanceNetworkInfoService) {
this.iins = null;
}
}
}