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.cookie;
28  
29  import java.time.Instant;
30  import java.time.format.DateTimeFormatter;
31  import java.time.format.DateTimeFormatterBuilder;
32  
33  import org.apache.hc.client5.http.cookie.CommonCookieAttributeHandler;
34  import org.apache.hc.client5.http.cookie.Cookie;
35  import org.apache.hc.client5.http.cookie.MalformedCookieException;
36  import org.apache.hc.client5.http.cookie.SetCookie;
37  import org.apache.hc.client5.http.utils.DateUtils;
38  import org.apache.hc.core5.annotation.Contract;
39  import org.apache.hc.core5.annotation.ThreadingBehavior;
40  import org.apache.hc.core5.util.Args;
41  
42  /**
43   * Cookie {@code expires} attribute handler.
44   *
45   * @since 4.0
46   */
47  @Contract(threading = ThreadingBehavior.STATELESS)
48  public class BasicExpiresHandler extends AbstractCookieAttributeHandler implements CommonCookieAttributeHandler {
49  
50      /** Valid date patterns */
51      private final DateTimeFormatter[] datePatterns;
52  
53      /**
54       * @since 5.2
55       */
56      public BasicExpiresHandler(final DateTimeFormatter... datePatterns) {
57          this.datePatterns = datePatterns;
58      }
59  
60      /**
61       * @deprecated Use {@link #BasicExpiresHandler(DateTimeFormatter...)}
62       */
63      @Deprecated
64      public BasicExpiresHandler(final String[] datePatterns) {
65          Args.notNull(datePatterns, "Array of date patterns");
66          this.datePatterns = new DateTimeFormatter[datePatterns.length];
67          for (int i = 0; i < datePatterns.length; i++) {
68              this.datePatterns[i] = new DateTimeFormatterBuilder()
69                      .parseLenient()
70                      .parseCaseInsensitive()
71                      .appendPattern(datePatterns[i])
72                      .toFormatter();
73          }
74  
75      }
76  
77      @Override
78      public void parse(final SetCookie cookie, final String value)
79              throws MalformedCookieException {
80          Args.notNull(cookie, "Cookie");
81          if (value == null) {
82              throw new MalformedCookieException("Missing value for 'expires' attribute");
83          }
84          final Instant expiry = DateUtils.parseDate(value, this.datePatterns);
85          if (expiry == null) {
86              throw new MalformedCookieException("Invalid 'expires' attribute: "
87                      + value);
88          }
89          cookie.setExpiryDate(expiry);
90      }
91  
92      @Override
93      public String getAttributeName() {
94          return Cookie.EXPIRES_ATTR;
95      }
96  
97  }