View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2014 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.modes;
21  
22  import java.util.Set;
23  
24  import org.apache.commons.lang3.StringUtils;
25  import org.apache.sling.api.adapter.Adaptable;
26  import org.apache.sling.api.resource.Resource;
27  import org.apache.sling.api.resource.ResourceResolver;
28  import org.jetbrains.annotations.NotNull;
29  import org.jetbrains.annotations.Nullable;
30  
31  import com.day.cq.wcm.api.Page;
32  
33  import io.wcm.handler.url.UrlModes;
34  import io.wcm.handler.url.integrator.IntegratorHandler;
35  import io.wcm.handler.url.spi.UrlHandlerConfig;
36  import io.wcm.sling.commons.adapter.AdaptTo;
37  import io.wcm.wcm.commons.util.Path;
38  
39  /**
40   * Default mode: Does generate a full externalized URL only if both siteUrl and siteUrlSecure parameter
41   * are set in context-specific configuration. If not set, only URLs without hostname are generated.
42   * If the target is an internal content page, siteUrl or siteUrlSecure is chosen automatically depending on the secure
43   * state of the page.
44   */
45  public final class DefaultUrlMode extends AbstractUrlMode {
46  
47    @Override
48    public @NotNull String getId() {
49      return "DEFAULT";
50    }
51  
52    @Override
53    public String getLinkUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
54        @Nullable Page currentPage, @Nullable Page targetPage) {
55      // default to full url for content URLs
56      return UrlModes.FULL_URL.getLinkUrlPrefix(adaptable, runModes, currentPage, targetPage);
57    }
58  
59    @Override
60    public String getResourceUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
61        @Nullable Page currentPage, @Nullable Resource targetResource) {
62      // in integrator template mode or if resource is from another site default to full URL mode, otherwise to no-hostname mode
63      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
64      if (integratorHandler.isIntegratorTemplateMode()
65          || linksToOtherDomain(adaptable, currentPage, targetResource)) {
66        return UrlModes.FULL_URL.getResourceUrlPrefix(adaptable, runModes, currentPage, targetResource);
67      }
68      return UrlModes.NO_HOSTNAME.getResourceUrlPrefix(adaptable, runModes, currentPage, targetResource);
69    }
70  
71    /**
72     * Checks if the target resource is located outsite the current site, and if for this other
73     * resource context a valid url configuration with a specific hostname exists.
74     * @param adaptable Adaptable
75     * @param currentPage Current page (may be null)
76     * @param targetResource Target resource (may be null)
77     * @return true if the target resources is located in another site/context with separate url configuration
78     */
79    private boolean linksToOtherDomain(Adaptable adaptable, Page currentPage, Resource targetResource) {
80      if (currentPage == null || targetResource == null) {
81        return false;
82      }
83  
84      UrlHandlerConfig urlHandlerConfig = AdaptTo.notNull(adaptable, UrlHandlerConfig.class);
85      Resource currentResource = AdaptTo.notNull(currentPage, Resource.class);
86      ResourceResolver resourceResolver = currentResource.getResourceResolver();
87      String currentSiteRoot = getRootPath(currentPage.getPath(), urlHandlerConfig.getSiteRootLevel(currentResource), resourceResolver);
88      String pathSiteRoot = getRootPath(targetResource.getPath(), urlHandlerConfig.getSiteRootLevel(targetResource), resourceResolver);
89      boolean notInCurrentSite = !StringUtils.equals(currentSiteRoot, pathSiteRoot);
90  
91      if (notInCurrentSite) {
92        UrlConfig targetUrlConfig = new UrlConfig(targetResource);
93        return targetUrlConfig.isValid();
94      }
95      else {
96        return false;
97      }
98    }
99  
100   /**
101    * Gets site root level path of a site.
102    * @param path Path of page within the site
103    * @param rootLevel Level of root page
104    * @param resourceResolver Resource resolver
105    * @return Site root path for the site. The path is not checked for validness.
106    */
107   private String getRootPath(String path, int rootLevel, ResourceResolver resourceResolver) {
108     String rootPath = Path.getAbsoluteParent(path, rootLevel, resourceResolver);
109 
110     // strip off everything after first "." - root path may be passed with selectors/extension which is not relevant
111     if (StringUtils.contains(rootPath, ".")) {
112       rootPath = StringUtils.substringBefore(rootPath, ".");
113     }
114 
115     return rootPath;
116   }
117 
118 }