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.time.Instant;
30  
31  import org.apache.hc.client5.http.utils.DateUtils;
32  import org.apache.hc.core5.annotation.Internal;
33  import org.apache.hc.core5.http.Header;
34  import org.apache.hc.core5.http.MessageHeaders;
35  
36  /**
37   * HTTP cache date support utilities.
38   *
39   * @since 5.2
40   */
41  @Internal
42  public final class DateSupport {
43  
44      /**
45       * Tests if the first message is after (newer) than second one
46       * using the given message header for comparison.
47       *
48       * @param message1 the first message
49       * @param message2 the second message
50       * @param headerName header name
51       *
52       * @return {@code true} if both messages contain a header with the given name
53       *  and the value of the header from the first message is newer that of
54       *  the second message.
55       *
56       * @since 5.0
57       */
58      public static boolean isAfter(
59              final MessageHeaders message1,
60              final MessageHeaders message2,
61              final String headerName) {
62          if (message1 != null && message2 != null) {
63              final Header dateHeader1 = message1.getFirstHeader(headerName);
64              if (dateHeader1 != null) {
65                  final Header dateHeader2 = message2.getFirstHeader(headerName);
66                  if (dateHeader2 != null) {
67                      final Instant date1 = DateUtils.parseStandardDate(dateHeader1.getValue());
68                      if (date1 != null) {
69                          final Instant date2 = DateUtils.parseStandardDate(dateHeader2.getValue());
70                          if (date2 != null) {
71                              return date1.isAfter(date2);
72                          }
73                      }
74                  }
75              }
76          }
77          return false;
78      }
79  
80      /**
81       * Tests if the first message is before (older) than the second one
82       * using the given message header for comparison.
83       *
84       * @param message1 the first message
85       * @param message2 the second message
86       * @param headerName header name
87       *
88       * @return {@code true} if both messages contain a header with the given name
89       *  and the value of the header from the first message is older that of
90       *  the second message.
91       *
92       * @since 5.0
93       */
94      public static boolean isBefore(
95              final MessageHeaders message1,
96              final MessageHeaders message2,
97              final String headerName) {
98          if (message1 != null && message2 != null) {
99              final Header dateHeader1 = message1.getFirstHeader(headerName);
100             if (dateHeader1 != null) {
101                 final Header dateHeader2 = message2.getFirstHeader(headerName);
102                 if (dateHeader2 != null) {
103                     final Instant date1 = DateUtils.parseStandardDate(dateHeader1.getValue());
104                     if (date1 != null) {
105                         final Instant date2 = DateUtils.parseStandardDate(dateHeader2.getValue());
106                         if (date2 != null) {
107                             return date1.isBefore(date2);
108                         }
109                     }
110                 }
111             }
112         }
113         return false;
114     }
115 
116 }