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.sling.api.adapter.Adaptable;
25  import org.apache.sling.api.resource.Resource;
26  import org.jetbrains.annotations.NotNull;
27  import org.jetbrains.annotations.Nullable;
28  
29  import com.day.cq.wcm.api.Page;
30  
31  import io.wcm.handler.url.integrator.IntegratorHandler;
32  import io.wcm.handler.url.integrator.IntegratorPlaceholder;
33  import io.wcm.sling.commons.adapter.AdaptTo;
34  import io.wcm.wcm.commons.util.RunMode;
35  
36  /**
37   * Enforce the generation of a full URL with protocol and hostname and secure mode.
38   * If siteUrlSecure is not set, siteUrl is used.
39   */
40  @SuppressWarnings("java:S2160") // equals is implemented via AbstractUrlMode
41  public final class FullUrlForceSecureUrlMode extends AbstractUrlMode {
42  
43    private final boolean forcePublish;
44  
45    /**
46     * @param forcePublish Force to select publish URLs even on author instance
47     */
48    public FullUrlForceSecureUrlMode(boolean forcePublish) {
49      this.forcePublish = forcePublish;
50    }
51  
52    @Override
53    public @NotNull String getId() {
54      return "FULL_URL_FORCESECURE";
55    }
56  
57    @SuppressWarnings("deprecation")
58    @Override
59    public String getLinkUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
60        @Nullable Page currentPage, @Nullable Page targetPage) {
61  
62      // if integrator template mode with placeholders is active return link url placeholder
63      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
64      if (integratorHandler.isIntegratorTemplateMode()
65          && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
66        return IntegratorPlaceholder.URL_CONTENT_SECURE;
67      }
68  
69      UrlConfig config = getUrlConfigForTarget(adaptable, targetPage);
70  
71      // in author mode return author site url
72      if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
73        return config.getSiteUrlAuthor();
74      }
75  
76      // return secure site url
77      return config.getSiteUrlSecure();
78    }
79  
80    @SuppressWarnings("deprecation")
81    @Override
82    public String getResourceUrlPrefix(@NotNull Adaptable adaptable, @NotNull Set<String> runModes,
83        @Nullable Page currentPage, @Nullable Resource targetResource) {
84  
85      // if integrator template mode with placeholders is active return resource url placeholder
86      IntegratorHandler integratorHandler = AdaptTo.notNull(adaptable, IntegratorHandler.class);
87      if (integratorHandler.isIntegratorTemplateMode()
88          && integratorHandler.getIntegratorMode().isUseUrlPlaceholders()) {
89        return IntegratorPlaceholder.URL_CONTENT_PROXY;
90      }
91  
92      UrlConfig config = getUrlConfigForTarget(adaptable, targetResource);
93  
94      // in author mode return author site url
95      if (!forcePublish && RunMode.isAuthor(runModes) && config.hasSiteUrlAuthor()) {
96        return config.getSiteUrlAuthor();
97      }
98  
99      // return secure site url
100     return config.getSiteUrlSecure();
101   }
102 
103 }