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 org.apache.commons.lang3.StringUtils;
23  
24  import io.wcm.caravan.hal.resource.HalResource;
25  import io.wcm.caravan.hal.resource.Link;
26  
27  /**
28   * Checks if the HAL resource contains link relations without title.
29   * Adds titles from the HAL documentation generated by this bundle.
30   */
31  class LinkRelationTitleAugmenter {
32  
33    private final DocsMetadata metadata;
34  
35    LinkRelationTitleAugmenter(DocsMetadata metadata) {
36      this.metadata = metadata;
37    }
38  
39    public void augment(HalResource resource) {
40      resource.getLinks().entries().stream()
41          .forEach(entry -> augmentLink(entry.getKey(), entry.getValue()));
42    }
43  
44    public void augmentLink(String rel, Link link) {
45      if (StringUtils.isBlank(link.getTitle())) {
46        link.setTitle(metadata.getLinkRelationTitle(rel));
47      }
48    }
49  
50  }