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      /**
51       * Default instance of {@link CacheControlHeaderGenerator}.
52       */
53      public static final CacheControlHeaderGenerator INSTANCE = new CacheControlHeaderGenerator();
54  
55      public List<NameValuePair> convert(final RequestCacheControl cacheControl) {
56          Args.notNull(cacheControl, "Cache control");
57          final List<NameValuePair> params = new ArrayList<>(10);
58          if (cacheControl.getMaxAge() >= 0) {
59              params.add(new BasicHeader("max-age", cacheControl.getMaxAge()));
60          }
61          if (cacheControl.getMaxStale() >= 0) {
62              params.add(new BasicHeader("max-stale", cacheControl.getMaxStale()));
63          }
64          if (cacheControl.getMinFresh() >= 0) {
65              params.add(new BasicHeader("min-fresh", cacheControl.getMinFresh()));
66          }
67          if (cacheControl.isNoCache()) {
68              params.add(new BasicHeader("no-cache", null));
69          }
70          if (cacheControl.isNoStore()) {
71              params.add(new BasicHeader("no-store", null));
72          }
73          if (cacheControl.isOnlyIfCached()) {
74              params.add(new BasicHeader("only-if-cached", null));
75          }
76          if (cacheControl.getStaleIfError() >= 0) {
77              params.add(new BasicHeader("stale-if-error", cacheControl.getStaleIfError()));
78          }
79          return params;
80      }
81  
82      public Header generate(final RequestCacheControl cacheControl) {
83          final List<NameValuePair> params = convert(cacheControl);
84          if (!params.isEmpty()) {
85              final CharArrayBuffer buf = new CharArrayBuffer(1024);
86              buf.append(HttpHeaders.CACHE_CONTROL);
87              buf.append(": ");
88              for (int i = 0; i < params.size(); i++) {
89                  if (i > 0) {
90                      buf.append(", ");
91                  }
92                  BasicHeaderValueFormatter.INSTANCE.formatNameValuePair(buf, params.get(i), false);
93              }
94              return BufferedHeader.create(buf);
95          } else {
96              return null;
97          }
98      }
99  
100     public void generate(final RequestCacheControl cacheControl, final HttpMessage message) {
101         final Header h = generate(cacheControl);
102         if (h != null) {
103             message.addHeader(h);
104         }
105     }
106 
107 }
108 
109