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