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;
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   */
26  public class NiceRefreshHandler extends ImmediateRefreshHandler {
27      private final int maxDelay_;
28  
29      /**
30       * Creates a new refresh handler that will immediately refresh if the refresh delay is no
31       * longer than {@code maxDelay}. No refresh will be perform at all for refresh values
32       * larger than {@code maxDelay}.
33       *
34       * @param maxDelay the maximum refreshValue (in seconds) that should cause a refresh
35       */
36      public NiceRefreshHandler(final int maxDelay) {
37          super();
38          if (maxDelay <= 0) {
39              throw new IllegalArgumentException("Invalid maxDelay: " + maxDelay);
40          }
41          maxDelay_ = maxDelay;
42      }
43  
44      /**
45       * Refreshes the specified page using the specified URL immediately if the {@code requestedWait}
46       * is not larger than the {@code maxDelay}. Does nothing otherwise.
47       * @param page the page that is going to be refreshed
48       * @param url the URL where the new page will be loaded
49       * @param requestedWait the number of seconds to wait before reloading the page
50       * @throws IOException if the refresh fails
51       */
52      @Override
53      public void handleRefresh(final Page page, final URL url, final int requestedWait) throws IOException {
54          if (requestedWait > maxDelay_) {
55              return;
56          }
57  
58          super.handleRefresh(page, url, requestedWait);
59      }
60  
61  }