View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit.httpclient;
16  
17  import java.util.Date;
18  
19  import org.apache.http.cookie.MalformedCookieException;
20  import org.apache.http.cookie.SetCookie;
21  import org.apache.http.impl.cookie.BasicMaxAgeHandler;
22  import org.apache.http.util.Args;
23  
24  /**
25   * Customized BasicMaxAgeHandler for HtmlUnit.
26   *
27   * @author Ronald Brill
28   */
29  final class HtmlUnitMaxAgeHandler extends BasicMaxAgeHandler {
30  
31      @Override
32      public void parse(final SetCookie cookie, final String value)
33              throws MalformedCookieException {
34          Args.notNull(cookie, "Cookie");
35          if (value == null) {
36              throw new MalformedCookieException("Missing value for 'max-age' attribute");
37          }
38          final int age;
39          try {
40              age = Integer.parseInt(value);
41          }
42          catch (final NumberFormatException e) {
43              throw new MalformedCookieException("Invalid 'max-age' attribute: " + value, e);
44          }
45          cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
46      }
47  
48  }