View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2019 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Rewrites resource links to client libraries that are in "allowProxy" mode to /etc.clientlibs.
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") // volatile is ok here
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        // lazy initialization
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      // replace /apps or /libs with /etc.clientlibs
84      String rewrittenPath = "/etc.clientlibs" + path.substring(5);
85      log.debug("Rewrite {} to {}", path, rewrittenPath);
86      return rewrittenPath;
87    }
88  
89  }