LinkType.java

  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.spi;

  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.sling.api.resource.ValueMap;
  23. import org.jetbrains.annotations.NotNull;
  24. import org.jetbrains.annotations.Nullable;
  25. import org.osgi.annotation.versioning.ConsumerType;

  26. import com.fasterxml.jackson.annotation.JsonIgnore;
  27. import com.fasterxml.jackson.annotation.JsonProperty;

  28. import io.wcm.handler.link.Link;
  29. import io.wcm.handler.link.LinkHandler;
  30. import io.wcm.handler.link.LinkNameConstants;
  31. import io.wcm.handler.link.LinkRequest;

  32. /**
  33.  * Defines a link type supported by {@link LinkHandler}.
  34.  *
  35.  * <p>
  36.  * This interface has to be implemented by a Sling Model class. The adaptables
  37.  * should be {@link org.apache.sling.api.SlingHttpServletRequest} and {@link org.apache.sling.api.resource.Resource}.
  38.  * </p>
  39.  */
  40. @ConsumerType
  41. public abstract class LinkType {

  42.   /**
  43.    * @return Link type ID (is stored as identifier in repository)
  44.    */
  45.   @JsonProperty("linkType")
  46.   public abstract @NotNull String getId();

  47.   /**
  48.    * @return Link type label (displayed in link dialogs)
  49.    */
  50.   @JsonIgnore
  51.   public @NotNull String getLabel() {
  52.     return getId();
  53.   }

  54.   /**
  55.    * @return Name of the property in which the primary link reference is stored
  56.    */
  57.   @JsonIgnore
  58.   public abstract @Nullable String getPrimaryLinkRefProperty();

  59.   /**
  60.    * Checks whether a link reference can be handled by this link type
  61.    * @param linkRequest Link reference
  62.    * @return true if this link type can handle the given link reference
  63.    */
  64.   public boolean accepts(@NotNull LinkRequest linkRequest) {
  65.     ValueMap props = linkRequest.getResourceProperties();
  66.     // check for matching link type ID in link resource
  67.     String linkTypeId = props.get(LinkNameConstants.PN_LINK_TYPE, String.class);
  68.     if (StringUtils.isNotEmpty(linkTypeId)) {
  69.       return StringUtils.equals(linkTypeId, getId());
  70.     }
  71.     // if not link type is set at all check if link ref attribute contains a valid link
  72.     // or a link reference is given with auto-detection of it's type
  73.     else {
  74.       String propertyName = getPrimaryLinkRefProperty();
  75.       String linkRef = null;
  76.       if (propertyName != null) {
  77.         linkRef = props.get(propertyName, String.class);
  78.       }
  79.       if (linkRef == null) {
  80.         linkRef = linkRequest.getReference();
  81.       }
  82.       if (linkRef != null) {
  83.         return accepts(linkRef);
  84.       }
  85.       return false;
  86.     }
  87.   }

  88.   /**
  89.    * Checks whether a link reference string can be handled by this link type
  90.    * @param linkRef Link reference string
  91.    * @return true if this link type can handle the given link reference
  92.    */
  93.   public abstract boolean accepts(@NotNull String linkRef);

  94.   /**
  95.    * Resolves a link
  96.    * @param link Link metadata
  97.    * @return Resolved link metadata. Never null.
  98.    */
  99.   public abstract @NotNull Link resolveLink(@NotNull Link link);

  100.   /**
  101.    * Granite UI component resource type to be used for editing this link type's properties in edit dialog.
  102.    * @return Granite UI component resource type or null, if none is available
  103.    */
  104.   @JsonIgnore
  105.   public @Nullable String getEditComponentResourceType() {
  106.     return null;
  107.   }

  108.   /**
  109.    * Returns true if a RTE plugin is available for this link type. If not, it is not possible to select
  110.    * this link type in the rich text editor.
  111.    * @return true if a RTE plugin is available.
  112.    */
  113.   @JsonIgnore
  114.   public boolean hasRichTextPlugin() {
  115.     return false;
  116.   }

  117. }