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.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
41
42
43
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
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
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
73
74
75
76
77
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
102
103
104
105
106
107 private String getRootPath(String path, int rootLevel, ResourceResolver resourceResolver) {
108 String rootPath = Path.getAbsoluteParent(path, rootLevel, resourceResolver);
109
110
111 if (StringUtils.contains(rootPath, ".")) {
112 rootPath = StringUtils.substringBefore(rootPath, ".");
113 }
114
115 return rootPath;
116 }
117
118 }