View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2015 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.caravan.hal.docs.impl.augmenter;
21  
22  import static io.wcm.caravan.hal.docs.impl.augmenter.CurieUtil.LINK_RELATION_CURIES;
23  
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Set;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  
30  import org.apache.commons.lang3.StringUtils;
31  
32  import com.google.common.collect.Sets;
33  
34  import io.wcm.caravan.hal.resource.HalResource;
35  import io.wcm.caravan.hal.resource.Link;
36  
37  /**
38   * Checks if the HAL resource contains link relations with curies without documentaiton links.
39   * Add documentation links referencing the HAL documentation generated by this bundle.
40   */
41  class CurieAugmenter {
42  
43    private final DocsMetadata metadata;
44  
45    CurieAugmenter(DocsMetadata metadata) {
46      this.metadata = metadata;
47    }
48  
49    public void augment(HalResource resource) {
50      Set<String> existingCurieNames = getExistingCurieNames(resource);
51      Set<Link> curieLinks = getCurieLinks(resource, existingCurieNames);
52      resource.addLinks(LINK_RELATION_CURIES, curieLinks);
53    }
54  
55    private Set<String> getExistingCurieNames(HalResource hal) {
56      if (!hal.hasLink(LINK_RELATION_CURIES)) {
57        return Collections.emptySet();
58      }
59      return Stream.of(hal.getLink(LINK_RELATION_CURIES))
60          .map(link -> link.getName())
61          .collect(Collectors.toSet());
62    }
63  
64    private Set<Link> getCurieLinks(HalResource resource, Set<String> existingCurieNames) {
65      Set<Link> curiLinks = Sets.newLinkedHashSet();
66      curiLinks.addAll(getCurieLinksForCurrentHalResource(resource, existingCurieNames));
67      curiLinks.addAll(getCurieLinksForEmbeddedResources(resource, existingCurieNames));
68      return curiLinks;
69    }
70  
71    private List<Link> getCurieLinksForCurrentHalResource(HalResource resource, Set<String> existingCurieNames) {
72      return resource.getLinks().keySet().stream()
73          // get CURI name for relation
74          .map(CurieUtil::getCurieName)
75          // filter CURIE being empty or exist in HAL resource
76          .filter(curieName -> StringUtils.isNotEmpty(curieName) && !existingCurieNames.contains(curieName))
77          // get link for CURI name
78          .map(this::toCurieLink)
79          // filter non existing links
80          .filter(link -> link != null)
81          .collect(Collectors.toList());
82    }
83  
84    private List<Link> getCurieLinksForEmbeddedResources(HalResource resource, Set<String> existingCurieNames) {
85      return resource.getEmbedded().values().stream()
86          .flatMap(embeddedResource -> getCurieLinks(embeddedResource, existingCurieNames).stream())
87          .collect(Collectors.toList());
88    }
89  
90    private Link toCurieLink(String curieName) {
91      String docLink = metadata.getCurieLink(curieName);
92      if (docLink == null) {
93        return null;
94      }
95      else {
96        return new Link(docLink).setName(curieName).setTitle("Documentation link");
97      }
98    }
99  
100 }