LanguageManagerImpl.java
18.3 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.Language
* com.day.cq.commons.LanguageUtil
* com.day.cq.wcm.api.LanguageManager
* com.day.cq.wcm.api.LanguageManager$Info
* com.day.cq.wcm.api.LanguageManager$Tree
* com.day.cq.wcm.api.LanguageManager$TreeNode
* com.day.cq.wcm.api.Page
* com.day.text.Text
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.collections.Predicate
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.api.resource.ValueMap
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.core.impl;
import com.day.cq.commons.Language;
import com.day.cq.commons.LanguageUtil;
import com.day.cq.wcm.api.LanguageManager;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.core.impl.predicate.IsSiteAdminPredicate;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.collections.Predicate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component(metatype=1, label="%langmgr.name", description="%langmgr.description")
@Service
public class LanguageManagerImpl
implements LanguageManager {
private static final Logger log = LoggerFactory.getLogger(LanguageManagerImpl.class);
private static final String LANGUAGE_MANAGER_SERVICE_USER = "language-manager-service";
@org.apache.felix.scr.annotations.Property(value={"/libs/wcm/core/resources/languages"})
private static final String LANGUAGE_LIST = "langmgr.list.path";
private static final String PN_DEFAULT_COUNTRY = "defaultCountry";
@org.apache.felix.scr.annotations.Property(cardinality=Integer.MAX_VALUE)
private static final String DEFAULT_COUNTRIES = "langmgr.country.default";
private static final Predicate PREDICATE = new IsSiteAdminPredicate();
private Map<String, String> defaultCountries = new HashMap<String, String>();
@Reference
private SlingRepository repository;
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void activate(ComponentContext context) {
String[] defaultCountries;
Dictionary props;
props = context.getProperties();
this.defaultCountries.clear();
String path = (String)props.get("langmgr.list.path");
if (path != null) {
Session session = null;
try {
session = this.repository.loginService("language-manager-service", null);
if (!session.hasPermission(path, "read")) {
log.warn("Not authorized to read the language list at location {}, please authorize service user {} accordingly", (Object)path, (Object)"language-manager-service");
} else if (session.nodeExists(path)) {
Node languages = session.getNode(path);
NodeIterator nodes = languages.getNodes();
while (nodes.hasNext()) {
Node node = nodes.nextNode();
if (!node.hasProperty("defaultCountry")) continue;
this.addDefaultCountry(node.getProperty("defaultCountry").getString());
}
}
}
catch (RepositoryException e) {
log.warn("Could not read language list at " + path, (Throwable)e);
}
finally {
if (session != null) {
session.logout();
}
}
}
if ((defaultCountries = (String[])props.get("langmgr.country.default")) != null) {
for (String lang : defaultCountries) {
this.addDefaultCountry(lang);
}
}
}
private void addDefaultCountry(String lang) {
Locale locale = LanguageUtil.getLocale((String)lang);
if (locale == null) {
log.error("Invalid default country specified in config: {}. ignore it.", (Object)lang);
} else {
this.defaultCountries.put(locale.getLanguage(), locale.getCountry());
}
}
public String getIsoCountry(Locale language) {
String country = language.getCountry();
if (country.length() == 0) {
country = this.defaultCountries.get(language.getLanguage());
}
return country == null ? "" : country;
}
public Map<Locale, LanguageManager.Info> getAdjacentInfo(ResourceResolver resolver, String path) {
Map<Language, LanguageManager.Info> infos = this.getAdjacentLanguageInfo(resolver, path);
if (infos == null) {
return null;
}
LinkedHashMap<Locale, LanguageManager.Info> ret = new LinkedHashMap<Locale, LanguageManager.Info>();
for (Map.Entry<Language, LanguageManager.Info> e : infos.entrySet()) {
ret.put(e.getKey().getLocale(), e.getValue());
}
return ret;
}
public Map<Language, LanguageManager.Info> getAdjacentLanguageInfo(ResourceResolver resolver, String path) {
String root = LanguageUtil.getLanguageRoot((String)path);
if (root == null) {
return null;
}
String parent = Text.getRelativeParent((String)root, (int)1);
String relPath = path.substring(root.length());
LinkedHashMap<Language, LanguageManager.Info> infos = new LinkedHashMap<Language, LanguageManager.Info>();
for (Language locale : this.getCqLanguages(resolver, path)) {
String pPath = parent + "/" + locale.toString() + relPath;
Resource res = resolver.getResource(pPath);
Long lastModified = null;
if (res != null) {
lastModified = -1;
Resource content = resolver.getResource(res, "jcr:content");
if (content != null) {
lastModified = (Long)((ValueMap)content.adaptTo(ValueMap.class)).get("jcr:lastModified", (Object)0);
}
}
InfoImpl info = new InfoImpl(pPath, lastModified);
infos.put(locale, info);
}
return infos;
}
public Collection<Page> getLanguageRoots(ResourceResolver resolver, String path) {
String root = LanguageUtil.getLanguageRoot((String)path);
if (root == null) {
return Collections.emptySet();
}
String parent = Text.getRelativeParent((String)root, (int)1);
Resource parentResource = resolver.getResource(parent);
if (parentResource == null) {
return Collections.emptySet();
}
LinkedList<Page> pages = new LinkedList<Page>();
Iterator iter = resolver.listChildren(parentResource);
while (iter.hasNext()) {
Page page;
Resource r = (Resource)iter.next();
Locale locale = LanguageUtil.getLocale((String)Text.getName((String)r.getPath()));
if (locale == null || (page = (Page)r.adaptTo(Page.class)) == null) continue;
pages.add(page);
}
return pages;
}
public Locale getLanguage(Resource res) {
return this.getLanguage(res, true);
}
public Language getCqLanguage(Resource res) {
return this.getCqLanguage(res, true);
}
public Locale getLanguage(Resource res, boolean respectContent) {
Language l = this.getCqLanguage(res, respectContent);
return l == null ? null : l.getLocale();
}
public Language getCqLanguage(Resource res, boolean respectContent) {
String languageRoot;
String path = res.getPath();
if (respectContent) {
int idx = path.indexOf("/jcr:content");
if (idx > 0) {
path = path.substring(0, idx);
}
try {
for (Node node = (hr = res.getResourceResolver().getResource((String)path)) == null ? null : (Node)hr.adaptTo(Node.class); node != null && node.getDepth() > 0; node = node.getParent()) {
if (!node.hasProperty("jcr:content/jcr:language")) continue;
String iso = node.getProperty("jcr:content/jcr:language").getString();
Language locale = LanguageUtil.getLanguage((String)iso);
if (locale == null) {
log.warn("Invalid iso code stored in {}: {}", (Object)node.getPath(), (Object)iso);
continue;
}
return locale;
}
}
catch (RepositoryException e) {
log.warn("Error while retrieving language property.", (Throwable)e);
}
}
if ((languageRoot = LanguageUtil.getLanguageRoot((String)path)) != null) {
return new Language(Text.getName((String)languageRoot));
}
return null;
}
public Page getLanguageRoot(Resource res) {
String root = LanguageUtil.getLanguageRoot((String)res.getPath());
if (root == null) {
return null;
}
Resource rootRes = res.getResourceResolver().getResource(root);
if (rootRes == null) {
return null;
}
return (Page)rootRes.adaptTo(Page.class);
}
public Collection<Locale> getLanguages(ResourceResolver resolver, String path) {
LinkedList<Locale> locales = new LinkedList<Locale>();
for (Language l : this.getCqLanguages(resolver, path)) {
locales.add(l.getLocale());
}
return locales;
}
public Collection<Language> getCqLanguages(ResourceResolver resolver, String path) {
String root = LanguageUtil.getLanguageRoot((String)path);
if (root == null) {
return Collections.emptySet();
}
String parent = Text.getRelativeParent((String)root, (int)1);
Resource parentResource = resolver.getResource(parent);
if (parentResource == null) {
return Collections.emptySet();
}
LinkedList<Language> languages = new LinkedList<Language>();
Iterator iter = resolver.listChildren(parentResource);
while (iter.hasNext()) {
Resource r = (Resource)iter.next();
Language locale = LanguageUtil.getLanguage((String)Text.getName((String)r.getPath()));
if (locale == null) continue;
languages.add(locale);
}
return languages;
}
public LanguageManager.Tree compareLanguageTrees(ResourceResolver resolver, String path) {
Set<Language> locales;
String parent;
String root = LanguageUtil.getLanguageRoot((String)path);
if (root == null) {
locales = LanguageManagerImpl.getChildLanguages(resolver, path);
parent = path;
if (locales.isEmpty()) {
return null;
}
path = root = parent + "/" + locales.iterator().next().toString();
} else {
parent = Text.getRelativeParent((String)root, (int)1);
locales = LanguageManagerImpl.getChildLanguages(resolver, parent);
}
TreeImpl tree = new TreeImpl(locales, parent);
if (path.equals(root)) {
for (Language locale : tree.getCqLanguages()) {
String pRoot = parent + "/" + locale.toString();
Resource res = resolver.getResource(pRoot);
LanguageManagerImpl.fillTree(tree, locale, pRoot.length(), resolver, res);
}
} else {
String relPath = path.substring(root.length());
for (Language locale : tree.getCqLanguages()) {
String pRoot = parent + "/" + locale.toString();
String pPath = pRoot + relPath;
Resource res = resolver.getResource(pPath);
LanguageManagerImpl.fillTree(tree, locale, pRoot.length(), resolver, res);
}
}
return tree;
}
private static Set<Language> getChildLanguages(ResourceResolver resolver, String path) {
HashSet<Language> locales = new HashSet<Language>();
Resource res = resolver.getResource(path);
if (res != null) {
Iterator iter = resolver.listChildren(res);
while (iter.hasNext()) {
res = (Resource)iter.next();
Language locale = LanguageUtil.getLanguage((String)Text.getName((String)res.getPath()));
if (locale == null) continue;
locales.add(locale);
}
}
return locales;
}
private static void fillTree(TreeImpl tree, Language language, int rootLength, ResourceResolver resolver, Resource res) {
if (res != null && PREDICATE.evaluate((Object)res)) {
Long lastModified = -1;
Iterator iter = resolver.listChildren(res);
while (iter.hasNext()) {
Resource child = (Resource)iter.next();
if (Text.getName((String)child.getPath()).equals("jcr:content")) {
lastModified = (Long)((ValueMap)child.adaptTo(ValueMap.class)).get("jcr:lastModified", (Object)0);
continue;
}
LanguageManagerImpl.fillTree(tree, language, rootLength, resolver, child);
}
String relPath = res.getPath();
relPath = rootLength < relPath.length() ? relPath.substring(rootLength + 1) : "";
tree.addInfo(language, relPath, lastModified);
}
}
protected void bindRepository(SlingRepository slingRepository) {
this.repository = slingRepository;
}
protected void unbindRepository(SlingRepository slingRepository) {
if (this.repository == slingRepository) {
this.repository = null;
}
}
private static final class InfoImpl
implements LanguageManager.Info {
private final String path;
private final Long lastModified;
public InfoImpl(String path, Long lastModified) {
this.path = path;
this.lastModified = lastModified;
}
public String getPath() {
return this.path;
}
public boolean exists() {
return this.lastModified != null;
}
public boolean hasContent() {
return this.lastModified != null && this.lastModified >= 0;
}
public long getLastModified() {
return this.lastModified == null ? 0 : this.lastModified;
}
}
private static class TreeImpl
implements LanguageManager.Tree {
private final Map<Language, Integer> indexes = new HashMap<Language, Integer>();
private final String root;
private final TreeMap<String, Node> tree;
public TreeImpl(Collection<Language> languages, String root) {
this.tree = new TreeMap(new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
return Text.compareHandles((String)o1, (String)o2);
}
});
this.root = root;
int i = 0;
for (Language locale : languages) {
this.indexes.put(locale, i++);
}
}
public String getRoot() {
return this.root;
}
public Collection<Locale> getLanguages() {
ArrayList<Locale> locales = new ArrayList<Locale>();
for (Language lang : this.indexes.keySet()) {
locales.add(lang.getLocale());
}
return locales;
}
public Collection<Language> getCqLanguages() {
return this.indexes.keySet();
}
public Collection<? extends LanguageManager.TreeNode> getNodes() {
return this.tree.values();
}
private void addInfo(Language locale, String relPath, long lastModified) {
Node node = this.tree.get(relPath);
if (node == null) {
node = new Node(relPath, this.indexes.size());
this.tree.put(relPath, node);
}
Node.access$200((Node)node)[this.indexes.get((Object)locale).intValue()] = lastModified;
}
private class Node
implements LanguageManager.TreeNode {
private final String relPath;
private final Long[] lastModified;
private Node(String relPath, int numLanguages) {
this.relPath = relPath;
this.lastModified = new Long[numLanguages];
}
public String getRelativePath() {
return this.relPath;
}
public LanguageManager.Info getInfo(Locale locale) {
return this.getInfo(new Language(locale));
}
public LanguageManager.Info getInfo(Language locale) {
Integer idx = (Integer)TreeImpl.this.indexes.get((Object)locale);
if (idx == null) {
return null;
}
StringBuffer iPath = new StringBuffer(TreeImpl.this.root).append('/').append(locale.toString());
if (this.relPath.length() > 0) {
iPath.append('/').append(this.relPath);
}
return new InfoImpl(iPath.toString(), this.lastModified[idx]);
}
static /* synthetic */ Long[] access$200(Node x0) {
return x0.lastModified;
}
}
}
}