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.jetbrains.annotations.NotNull;
28 import org.jetbrains.annotations.Nullable;
29
30 import com.day.cq.wcm.api.Page;
31
32 import io.wcm.handler.url.integrator.IntegratorHandler;
33 import io.wcm.handler.url.integrator.IntegratorPlaceholder;
34 import io.wcm.handler.url.spi.UrlHandlerConfig;
35 import io.wcm.sling.commons.adapter.AdaptTo;
36 import io.wcm.wcm.commons.util.RunMode;
37
38
39
40
41
42
43 @SuppressWarnings("java:S2160")
44 public final class FullUrlProtocolRelativeUrlMode extends AbstractUrlMode {
45
46 private final boolean forcePublish;
47
48
49
50
51 public FullUrlProtocolRelativeUrlMode(boolean forcePublish) {
52 this.forcePublish = forcePublish;
53 }
54
55 @Override
56 public @NotNull String getId() {
57 return "FULL_URL_PROTOCOLRELATIVE";
58 }
59
60 @SuppressWarnings("deprecation")
61 @Override
62 public String getLinkUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
63 @Nullable Page currentPage, @Nullable Page targetPage) {
64
65
66 IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
67 if (integratorHandler.isIntegratorTemplateMode()
68 && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
69 UrlHandlerConfig urlHandlerConfig = AdaptTo.notNull(adaptable, UrlHandlerConfig.class);
70 if (targetPage != null && urlHandlerConfig.isSecure(targetPage)) {
71 return IntegratorPlaceholder.URL_CONTENT_SECURE;
72 }
73 else {
74 return IntegratorPlaceholder.URL_CONTENT;
75 }
76 }
77
78 UrlConfig config = getUrlConfigForTarget(adaptable, targetPage);
79
80
81 if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
82 return config.getSiteUrlAuthor();
83 }
84
85
86 return convertToProtocolRelative(config.getSiteUrl());
87 }
88
89 @SuppressWarnings("deprecation")
90 @Override
91 public String getResourceUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
92 @Nullable Page currentPage, @Nullable Resource targetResource) {
93
94
95 IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
96 if (integratorHandler.isIntegratorTemplateMode()
97 && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
98 return IntegratorPlaceholder.URL_CONTENT_PROXY;
99 }
100
101 UrlConfig config = getUrlConfigForTarget(adaptable, targetResource);
102
103
104 if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
105 return config.getSiteUrlAuthor();
106 }
107
108
109 return convertToProtocolRelative(config.getSiteUrl());
110 }
111
112
113
114
115
116
117 private String convertToProtocolRelative(String pPrefix) {
118 if (StringUtils.isEmpty(pPrefix)) {
119 return null;
120 }
121 int index = StringUtils.indexOf(pPrefix, "://");
122 if (index >= 0) {
123 return pPrefix.substring(index + 1);
124 }
125 else {
126 return pPrefix;
127 }
128 }
129
130 }