View Javadoc
1   package ca.uhn.fhir.rest.api;
2   
3   /*-
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 - 2018 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  import java.util.List;
27  import java.util.StringTokenizer;
28  
29  import static org.apache.commons.lang3.StringUtils.trim;
30  
31  /**
32   * Parses and stores the value(s) within HTTP Cache-Control headers
33   */
34  public class CacheControlDirective {
35  
36  	private static final String MAX_RESULTS_EQUALS = Constants.CACHE_CONTROL_MAX_RESULTS + "=";
37  	private static final Logger ourLog = LoggerFactory.getLogger(CacheControlDirective.class);
38  	private boolean myNoCache;
39  	private boolean myNoStore;
40  	private Integer myMaxResults;
41  
42  	/**
43  	 * Constructor
44  	 */
45  	public CacheControlDirective() {
46  		super();
47  	}
48  
49  	/**
50  	 * If the {@link #isNoStore() no-store} directive is set, this HAPI FHIR extention
51  	 * to the <code>Cache-Control</code> header called <code>max-results=123</code>
52  	 * specified the maximum number of results which will be fetched from the
53  	 * database before returning.
54  	 */
55  	public Integer getMaxResults() {
56  		return myMaxResults;
57  	}
58  
59  	/**
60  	 * If the {@link #isNoStore() no-store} directive is set, this HAPI FHIR extention
61  	 * to the <code>Cache-Control</code> header called <code>max-results=123</code>
62  	 * specified the maximum number of results which will be fetched from the
63  	 * database before returning.
64  	 */
65  	public CacheControlDirective setMaxResults(Integer theMaxResults) {
66  		myMaxResults = theMaxResults;
67  		return this;
68  	}
69  
70  	/**
71  	 * If <code>true<</code>, adds the <code>no-cache</code> directive to the
72  	 * request. This directive indicates that the cache should not be used to
73  	 * serve this request.
74  	 */
75  	public boolean isNoCache() {
76  		return myNoCache;
77  	}
78  
79  	/**
80  	 * If <code>true<</code>, adds the <code>no-cache</code> directive to the
81  	 * request. This directive indicates that the cache should not be used to
82  	 * serve this request.
83  	 */
84  	public CacheControlDirective setNoCache(boolean theNoCache) {
85  		myNoCache = theNoCache;
86  		return this;
87  	}
88  
89  	public boolean isNoStore() {
90  		return myNoStore;
91  	}
92  
93  	public CacheControlDirective setNoStore(boolean theNoStore) {
94  		myNoStore = theNoStore;
95  		return this;
96  	}
97  
98  	/**
99  	 * Parses a list of <code>Cache-Control</code> header values
100 	 *
101 	 * @param theValues The <code>Cache-Control</code> header values
102 	 */
103 	public CacheControlDirective parse(List<String> theValues) {
104 		if (theValues != null) {
105 			for (String nextValue : theValues) {
106 				StringTokenizer tok = new StringTokenizer(nextValue, ",");
107 				while (tok.hasMoreTokens()) {
108 					String next = trim(tok.nextToken());
109 					if (Constants.CACHE_CONTROL_NO_CACHE.equals(next)) {
110 						myNoCache = true;
111 					} else if (Constants.CACHE_CONTROL_NO_STORE.equals(next)) {
112 						myNoStore = true;
113 					} else if (next.startsWith(MAX_RESULTS_EQUALS)) {
114 						String valueString = trim(next.substring(MAX_RESULTS_EQUALS.length()));
115 						try {
116 							myMaxResults = Integer.parseInt(valueString);
117 						} catch (NumberFormatException e) {
118 							ourLog.warn("Invalid {} value: {}", Constants.CACHE_CONTROL_MAX_RESULTS, valueString);
119 						}
120 
121 					}
122 				}
123 			}
124 		}
125 
126 		return this;
127 	}
128 }