View Javadoc
1   /*
2    * Copyright (c) 2002-2026 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;
16  
17  import java.io.IOException;
18  import java.net.URL;
19  
20  /**
21   * This refresh handler performs an immediate refresh if the refresh delay is
22   * less or equal to the configured time and otherwise ignores totally the refresh instruction.
23   *
24   * @author Marc Guillemot
25   * @author Ronald Brill
26   */
27  public class NiceRefreshHandler extends ImmediateRefreshHandler {
28      private final int maxDelay_;
29  
30      /**
31       * Creates a new refresh handler that will immediately refresh if the refresh delay is no
32       * longer than {@code maxDelay}. No refresh will be perform at all for refresh values
33       * larger than {@code maxDelay}.
34       *
35       * @param maxDelay the maximum refreshValue (in seconds) that should cause a refresh
36       */
37      public NiceRefreshHandler(final int maxDelay) {
38          super();
39          if (maxDelay <= 0) {
40              throw new IllegalArgumentException("Invalid maxDelay: " + maxDelay);
41          }
42          maxDelay_ = maxDelay;
43      }
44  
45      /**
46       * Refreshes the specified page using the specified URL immediately if the {@code requestedWait}
47       * is not larger than the {@code maxDelay}. Does nothing otherwise.
48       * @param page the page that is going to be refreshed
49       * @param url the URL where the new page will be loaded
50       * @param requestedWait the number of seconds to wait before reloading the page
51       * @throws IOException if the refresh fails
52       */
53      @Override
54      public void handleRefresh(final Page page, final URL url, final int requestedWait) throws IOException {
55          if (requestedWait > maxDelay_) {
56              return;
57          }
58  
59          super.handleRefresh(page, url, requestedWait);
60      }
61  
62  }