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