1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.handler.url.impl.clientlib;
21
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.sling.api.resource.ResourceResolverFactory;
26 import org.jetbrains.annotations.NotNull;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.osgi.service.component.annotations.Reference;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36 @Component(service = ClientlibProxyRewriter.class, immediate = true)
37 public class ClientlibProxyRewriterImpl implements ClientlibProxyRewriter {
38
39 private static final Pattern STATIC_RESOURCE_PATH_PATTERN = Pattern.compile("^(/(apps|libs)/.*)/resources/.*$");
40
41 private static final Logger log = LoggerFactory.getLogger(ClientlibProxyRewriterImpl.class);
42
43 @Reference
44 private ResourceResolverFactory resourceResolverFactory;
45
46 @SuppressWarnings("java:S3077")
47 private volatile ClientlibPathCache clientlibPathCache;
48
49 @Deactivate
50 private void deactivate() {
51 if (clientlibPathCache != null) {
52 this.clientlibPathCache.close();
53 }
54 this.clientlibPathCache = null;
55 }
56
57 private ClientlibPathCache getClientlibPathCache() {
58 if (this.clientlibPathCache == null) {
59
60 synchronized (this) {
61 if (this.clientlibPathCache == null) {
62 this.clientlibPathCache = new ClientlibPathCache(resourceResolverFactory);
63 }
64 }
65 }
66 return this.clientlibPathCache;
67 }
68
69 @Override
70 public @NotNull String rewriteStaticResourcePath(@NotNull String path) {
71 Matcher matcher = STATIC_RESOURCE_PATH_PATTERN.matcher(path);
72 if (matcher.matches()) {
73 String clientlibPath = matcher.group(1);
74 boolean clientlibProxyMode = getClientlibPathCache().isClientlibWithAllowProxy(clientlibPath);
75 if (clientlibProxyMode) {
76 return rewriteClientlibProxyPath(path);
77 }
78 }
79 return path;
80 }
81
82 private String rewriteClientlibProxyPath(String path) {
83
84 String rewrittenPath = "/etc.clientlibs" + path.substring(5);
85 log.debug("Rewrite {} to {}", path, rewrittenPath);
86 return rewrittenPath;
87 }
88
89 }