View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.cache;
28  
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import org.apache.hc.client5.http.cache.RequestCacheControl;
33  import org.apache.hc.core5.annotation.Contract;
34  import org.apache.hc.core5.annotation.Internal;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.http.Header;
37  import org.apache.hc.core5.http.HttpHeaders;
38  import org.apache.hc.core5.http.HttpMessage;
39  import org.apache.hc.core5.http.NameValuePair;
40  import org.apache.hc.core5.http.message.BasicHeader;
41  import org.apache.hc.core5.http.message.BasicHeaderValueFormatter;
42  import org.apache.hc.core5.http.message.BufferedHeader;
43  import org.apache.hc.core5.util.Args;
44  import org.apache.hc.core5.util.CharArrayBuffer;
45  
46  @Internal
47  @Contract(threading = ThreadingBehavior.IMMUTABLE)
48  class CacheControlHeaderGenerator {
49  
50      public static final CacheControlHeaderGenerator INSTANCE = new CacheControlHeaderGenerator();
51  
52      public List<NameValuePair> convert(final RequestCacheControl cacheControl) {
53          Args.notNull(cacheControl, "Cache control");
54          final List<NameValuePair> params = new ArrayList<>(10);
55          if (cacheControl.getMaxAge() >= 0) {
56              params.add(new BasicHeader("max-age", cacheControl.getMaxAge()));
57          }
58          if (cacheControl.getMaxStale() >= 0) {
59              params.add(new BasicHeader("max-stale", cacheControl.getMaxStale()));
60          }
61          if (cacheControl.getMinFresh() >= 0) {
62              params.add(new BasicHeader("min-fresh", cacheControl.getMinFresh()));
63          }
64          if (cacheControl.isNoCache()) {
65              params.add(new BasicHeader("no-cache", null));
66          }
67          if (cacheControl.isNoStore()) {
68              params.add(new BasicHeader("no-store", null));
69          }
70          if (cacheControl.isOnlyIfCached()) {
71              params.add(new BasicHeader("only-if-cached", null));
72          }
73          if (cacheControl.getStaleIfError() >= 0) {
74              params.add(new BasicHeader("stale-if-error", cacheControl.getStaleIfError()));
75          }
76          return params;
77      }
78  
79      public Header generate(final RequestCacheControl cacheControl) {
80          final List<NameValuePair> params = convert(cacheControl);
81          if (!params.isEmpty()) {
82              final CharArrayBuffer buf = new CharArrayBuffer(1024);
83              buf.append(HttpHeaders.CACHE_CONTROL);
84              buf.append(": ");
85              for (int i = 0; i < params.size(); i++) {
86                  if (i > 0) {
87                      buf.append(", ");
88                  }
89                  BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(buf, params.get(i), false);
90              }
91              return BufferedHeader.create(buf);
92          } else {
93              return null;
94          }
95      }
96  
97      public void generate(final RequestCacheControl cacheControl, final HttpMessage message) {
98          final Header h = generate(cacheControl);
99          if (h != null) {
100             message.addHeader(h);
101         }
102     }
103 
104 }
105 
106