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.link.type;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.lang3.StringUtils;
26  import org.apache.sling.api.SlingHttpServletRequest;
27  import org.apache.sling.api.resource.Resource;
28  import org.apache.sling.api.resource.ResourceResolver;
29  import org.apache.sling.models.annotations.Model;
30  import org.apache.sling.models.annotations.injectorspecific.Self;
31  import org.jetbrains.annotations.NotNull;
32  import org.jetbrains.annotations.Nullable;
33  import org.osgi.annotation.versioning.ProviderType;
34  
35  import io.wcm.handler.link.Link;
36  import io.wcm.handler.link.LinkNameConstants;
37  import io.wcm.handler.link.LinkRequest;
38  import io.wcm.handler.link.SyntheticLinkResource;
39  import io.wcm.handler.link.spi.LinkType;
40  import io.wcm.handler.link.type.helpers.InternalLinkResolver;
41  import io.wcm.handler.link.type.helpers.InternalLinkResolverOptions;
42  import io.wcm.handler.url.UrlHandler;
43  
44  /**
45   * Default implementation of {@link io.wcm.handler.link.spi.LinkType} for internal links.
46   * Internal links are links to content pages inside the CMS.
47   *
48   * <p>
49   * This link type ensures all links target only pages inside the same inner-most configuration scope, which is usually
50   * the same site/language. All link paths referencing pages outside this content subtree are rewritten via
51   * {@link UrlHandler#rewritePathToContext(Resource)} with the root path of the inner-most configuration scope/site and
52   * then resolved.
53   * </p>
54   */
55  @Model(adaptables = {
56      SlingHttpServletRequest.class, Resource.class
57  })
58  @ProviderType
59  public final class InternalLinkType extends LinkType {
60  
61    /**
62     * Link type ID
63     */
64    public static final @NotNull String ID = "internal";
65  
66    private final @NotNull InternalLinkResolverOptions resolverOptions = new InternalLinkResolverOptions()
67        .primaryLinkRefProperty(getPrimaryLinkRefProperty())
68        .rewritePathToContext(true)
69        .useTargetContext(false);
70  
71    @Self
72    private InternalLinkResolver internalLinkResolver;
73  
74    /**
75     * @return Link type ID (is stored as identifier in repository)
76     */
77    @Override
78    public @NotNull String getId() {
79      return ID;
80    }
81  
82    @Override
83    public @NotNull String getLabel() {
84      return "Internal (same site)";
85    }
86  
87    @Override
88    public String getPrimaryLinkRefProperty() {
89      return LinkNameConstants.PN_LINK_CONTENT_REF;
90    }
91  
92    @Override
93    public @Nullable String getEditComponentResourceType() {
94      return "wcm-io/handler/link/components/granite/form/linktype/internal";
95    }
96  
97    @Override
98    public boolean hasRichTextPlugin() {
99      return true;
100   }
101 
102   @Override
103   public boolean accepts(@NotNull String linkRef) {
104     // accept as internal link if the ref starts with "/content/"
105     return StringUtils.startsWith(linkRef, "/content/")
106         && !MediaLinkType.isDefaultMediaContentPath(linkRef);
107   }
108 
109   @Override
110   public boolean accepts(@NotNull LinkRequest linkRequest) {
111     if (internalLinkResolver.acceptPage(linkRequest.getPage(), resolverOptions)) {
112       // support direct links to pages
113       return true;
114     }
115     // check for matching link type ID in link resource
116     return super.accepts(linkRequest);
117   }
118 
119   @Override
120   public @NotNull Link resolveLink(@NotNull Link link) {
121     return internalLinkResolver.resolveLink(link, resolverOptions);
122   }
123 
124   /**
125    * Get synthetic link resource for this link type.
126    * @param resourceResolver Resource resolver
127    * @param path Resource path. Can be a non-existing path, but the path should be located somewhere within the
128    *          applications content paths to make sure the handler configuration looked up via context-aware services
129    *          is the expected one.
130    * @param pageRef Path to target page
131    * @return Synthetic link resource
132    */
133   public static @NotNull Resource getSyntheticLinkResource(@NotNull ResourceResolver resourceResolver,
134       @NotNull String path, @NotNull String pageRef) {
135     Map<String, Object> map = new HashMap<>();
136     map.put(LinkNameConstants.PN_LINK_TYPE, ID);
137     map.put(LinkNameConstants.PN_LINK_CONTENT_REF, pageRef);
138     return new SyntheticLinkResource(resourceResolver, path, map);
139   }
140 
141   @Override
142   public String toString() {
143     return ID;
144   }
145 
146 }