View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.openehealth.ipf.boot.fhir;
18  
19  import ca.uhn.fhir.context.FhirContext;
20  import ca.uhn.fhir.parser.IParser;
21  import ca.uhn.fhir.rest.api.server.IBundleProvider;
22  import ca.uhn.fhir.rest.server.IPagingProvider;
23  import ca.uhn.fhir.rest.server.SimpleBundleProvider;
24  import lombok.Getter;
25  import lombok.Setter;
26  import org.springframework.cache.Cache;
27  import org.springframework.cache.CacheManager;
28  
29  import java.util.List;
30  import java.util.UUID;
31  import java.util.stream.Collectors;
32  
33  /**
34   * Paging provider that uses a Spring cache abstraction to store {@link IBundleProvider} instances.
35   * The PagingProvider is used whenever there are more results than the client has requested.
36   * <p>
37   * Note that {@link IBundleProvider} is not serializable, so if {@link #isDistributed()} returns true,
38   * only the bundles are stored. When the result list is {@link #retrieveResultList(String) retrieved}
39   * a new instance of {@link SimpleBundleProvider} is created and returned instead. This does
40   * not work with {@link org.openehealth.ipf.commons.ihe.fhir.LazyBundleProvider}
41   * as this class relies on a completely initialized result list.
42   * </p>
43   * <p>
44   * Eviction of paging results is done by appropriately configuring the fhirPagingCache cache
45   * </p>
46   *
47   * @author Christian Ohr
48   * @since 3.2
49   */
50  public class CachingPagingProvider implements IPagingProvider {
51  
52      private static final String PAGING_CACHE = "fhirPagingCache";
53      private Cache cache;
54  
55      @Getter
56      @Setter
57      private int defaultPageSize = 50;
58  
59      @Getter
60      @Setter
61      private int maximumPageSize = 100;
62  
63      @Getter
64      @Setter
65      private boolean distributed;
66  
67  
68      private FhirContext fhirContext;
69  
70      public CachingPagingProvider(CacheManager cacheManager, FhirContext fhirContext) {
71          this.cache = cacheManager.getCache(PAGING_CACHE);
72          this.fhirContext = fhirContext;
73      }
74  
75      @Override
76      public String storeResultList(IBundleProvider bundleProvider) {
77          String key = UUID.randomUUID().toString();
78          cache.put(key, distributed ?
79                  serialize(bundleProvider) :
80                  bundleProvider);
81          return key;
82      }
83  
84      @Override
85      public IBundleProvider retrieveResultList(String id) {
86          return distributed ?
87                  deserialize(cache.get(id, List.class)) :
88                  cache.get(id, IBundleProvider.class);
89      }
90  
91  
92      private List<String> serialize(IBundleProvider bundleProvider) {
93          IParser parser = fhirContext.newJsonParser();
94          return bundleProvider.getResources(0, Integer.MAX_VALUE).stream()
95                  .map(parser::encodeResourceToString)
96                  .collect(Collectors.toList());
97      }
98  
99      private IBundleProvider deserialize(List<String> list) {
100         IParser parser = fhirContext.newJsonParser();
101         return new SimpleBundleProvider(list.stream()
102                 .map(parser::parseResource)
103                 .collect(Collectors.toList()));
104     }
105 
106 }