AgentConfigImpl.java
11.1 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
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.adobe.granite.crypto.CryptoException
* com.adobe.granite.crypto.CryptoSupport
* org.apache.commons.lang.text.StrSubstitutor
* org.apache.jackrabbit.util.Text
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.api.wrappers.ValueMapDecorator
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.replication.impl;
import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.day.cq.replication.AgentConfig;
import com.day.cq.replication.AgentConfigGroup;
import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.Key;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.lang.text.StrSubstitutor;
import org.apache.jackrabbit.util.Text;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.wrappers.ValueMapDecorator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AgentConfigImpl
implements AgentConfig {
private static final Logger log = LoggerFactory.getLogger(AgentConfigImpl.class);
protected static final String DEFAULT_SERIALIZATION_TYPE = "durbo";
protected static final long DEFAULT_RETRY_DELAY = 60000;
protected static final int DEFAULT_MAX_RETRIES = -1;
protected static final String DEFAULT_LOG_LEVEL = "INFO";
static final String DISTRIBUTION_SERIALIZATION_TYPE = "distribution";
static final String DISTRIBUTION_TRANSPORT_PREFIX = "distribution://";
private final String id;
private final String configPath;
private final ValueMap properties;
private final AgentConfigGroup configGroup;
private final CryptoSupport crypto;
private final String[] uris;
private final String serializationType;
public AgentConfigImpl(String id, Map<String, Object> props, String configPath, AgentConfigGroup group, CryptoSupport crypto) {
this.id = id;
this.configPath = configPath;
this.properties = new ValueMapDecorator(props);
this.configGroup = group;
this.crypto = crypto;
String serializationType = (String)this.properties.get("serializationType", (Object)"durbo");
String transportUri = (String)this.properties.get("transportUri", String.class);
if (transportUri != null && transportUri.startsWith("distribution://")) {
serializationType = "distribution";
}
if ("distribution".equals(serializationType) && !transportUri.startsWith("distribution://")) {
transportUri = "distribution://" + this.getAgentId();
}
this.serializationType = serializationType;
this.uris = AgentConfigImpl.splitUris(transportUri);
}
@Override
public ValueMap getProperties() {
return this.properties;
}
@Override
public String getId() {
return this.id;
}
@Override
public String getAgentId() {
return Text.getName((String)this.id);
}
@Override
public String getConfigPath() {
return this.configPath;
}
@Override
public AgentConfigGroup getConfigGroup() {
return this.configGroup;
}
@Override
public String getSerializationType() {
return this.serializationType;
}
@Override
public String getName() {
return (String)this.properties.get("jcr:title", String.class);
}
@Override
public String getAgentUserId() {
return (String)this.properties.get("userId", String.class);
}
@Override
public String getLogLevel() {
return (String)this.properties.get("logLevel", (Object)"INFO");
}
@Override
public boolean isSpecific() {
return (Boolean)this.properties.get("triggerSpecific", (Object)false) != false || this.isTriggeredOnDistribute() || this.usedForReverseReplication();
}
@Override
public boolean isTriggeredOnModification() {
return (Boolean)this.properties.get("triggerModified", (Object)false);
}
@Override
public boolean isTriggeredOnOffTime() {
return (Boolean)this.properties.get("triggerOnOffTime", (Object)false);
}
@Override
public boolean isTriggeredOnDistribute() {
return (Boolean)this.properties.get("triggerDistribute", (Object)false);
}
@Override
public boolean usedForReverseReplication() {
return (Boolean)this.properties.get("reverseReplication", (Object)false);
}
@Override
public boolean isEnabled() {
return (Boolean)this.properties.get("enabled", (Object)false);
}
@Override
public boolean isTriggeredOnReceive() {
return (Boolean)this.properties.get("triggerReceive", (Object)false);
}
@Override
public boolean isBatchMode() {
return (Boolean)this.properties.get("queueBatchMode", (Object)false);
}
@Override
public long getBatchWaitTime() {
return ((Integer)this.properties.get("queueBatchWaitTime", (Object)2)).intValue();
}
@Override
public long getBatchMaxSize() {
return ((Integer)this.properties.get("queueBatchMaxSize", (Object)5242880)).intValue();
}
@Override
public int getMaxRetries() {
return (Integer)this.properties.get("maxRetries", (Object)-1);
}
@Override
public long getRetryDelay() {
long delay = (Long)this.properties.get("retryDelay", (Object)60000);
return delay < 1000 ? 1000 : delay;
}
@Override
public void checkValid() {
String[] uris = this.getAllTransportURIs();
AgentConfigImpl.checkValidUris(uris);
if (this.isBatchMode()) {
if (this.getBatchMaxSize() < 1024) {
throw new IllegalArgumentException("Maximum batch size for batch processing is too low: " + this.getBatchMaxSize());
}
if (this.getBatchWaitTime() < 1) {
throw new IllegalArgumentException("Batch wait time for batch processing is too low: " + this.getBatchWaitTime());
}
}
}
private static void checkValidUris(String[] uris) {
if (uris != null && uris.length > 0) {
for (String uri : uris) {
if (uri == null) {
throw new IllegalArgumentException("Transport URI is not specified.");
}
HashMap<String, String> props = new HashMap<String, String>();
props.put("path", "path");
props.put("action", "activate");
StrSubstitutor replace = new StrSubstitutor(props, "{", "}");
try {
new URI(replace.replace(uri));
continue;
}
catch (URISyntaxException e) {
throw new IllegalArgumentException("Transport URI is malformed: " + e.getMessage());
}
}
} else {
throw new IllegalArgumentException("Transport URI is not specified.");
}
}
@Override
public String getTransportURI() {
String[] uris = this.getAllTransportURIs();
if (uris == null || uris.length == 0) {
return null;
}
return uris[0];
}
@Override
public String[] getAllTransportURIs() {
return this.uris;
}
@Override
public String getTransportUser() {
return (String)this.properties.get("transportUser", String.class);
}
@Override
public boolean noVersions() {
return (Boolean)this.properties.get("noVersioning", (Object)false);
}
@Override
public boolean noStatusUpdate() {
return (Boolean)this.properties.get("noStatusUpdate", (Object)false);
}
@Override
public String getTransportPassword() {
String pwd = (String)this.properties.get("transportPassword", String.class);
return this.getPassword(pwd);
}
@Override
public boolean isInMaintenanceMode() {
return (Boolean)this.properties.get("maintenanceMode", (Object)false);
}
@Override
public boolean aliasUpdate() {
return (Boolean)this.properties.get("aliasUpdate", (Object)false);
}
static String legacyDecrypt(String s) {
String PREFIX = "{DES}";
int KEY_LENGTH = 8;
if (!s.startsWith("{DES}")) {
return null;
}
try {
byte[] data = new byte[(s.length() - "{DES}".length()) / 2];
int i = "{DES}".length();
int b = 0;
while (i < s.length()) {
data[b] = (byte)(Integer.parseInt(s.substring(i, i + 2), 16) & 255);
i += 2;
++b;
}
SecretKeySpec key = new SecretKeySpec(data, 0, 8, "DES");
Cipher cipher = Cipher.getInstance("DES");
ByteArrayOutputStream out = new ByteArrayOutputStream(data.length);
cipher.init(2, key);
out.write(cipher.update(data, 8, data.length - 8));
out.write(cipher.doFinal());
return out.toString("utf-8");
}
catch (Exception e) {
log.warn("Unable to decrypt data: " + e);
return null;
}
}
@Override
public String getSSLConfig() {
return (String)this.properties.get("ssl", (Object)"default");
}
@Override
public boolean allowsExpiredCertificates() {
return (Boolean)this.properties.get("protocolHTTPExpired", (Object)false);
}
@Override
public boolean isOAuthEnabled() {
return (Boolean)this.properties.get("enableOauth", (Object)false);
}
private String getPassword(String pwd) {
if (pwd != null) {
String dec = AgentConfigImpl.legacyDecrypt(pwd);
if (dec == null && this.crypto != null && this.crypto.isProtected(pwd)) {
try {
dec = this.crypto.unprotect(pwd);
}
catch (CryptoException e) {
log.error("Error while unprotecting password: {}", (Object)e.toString());
}
}
if (dec != null) {
pwd = dec;
}
}
return pwd;
}
public String getDistributionAgentName() {
String transportUri = this.getTransportURI();
if (transportUri.startsWith("distribution://")) {
return transportUri.replace("distribution://", "");
}
return null;
}
private static String[] splitUris(String urisString) {
if (urisString == null) {
return null;
}
urisString = urisString.trim();
String[] uris = urisString.split(",");
ArrayList<String> list = new ArrayList<String>();
for (String uri : uris) {
if ((uri = uri.trim()).length() <= 0) continue;
list.add(uri);
}
String[] resultUris = list.toArray(new String[list.size()]);
if (resultUris.length > 1) {
try {
AgentConfigImpl.checkValidUris(resultUris);
}
catch (IllegalArgumentException e) {
resultUris = new String[]{urisString};
}
}
return resultUris;
}
}