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  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * This refresh handler waits the specified number of seconds (or a user defined maximum)
25   * before refreshing the specified page, using the specified URL. Waiting happens
26   * on the current thread
27   * <p>
28   * If you want a refresh handler that ignores the wait time, see
29   * {@link ImmediateRefreshHandler}.
30   *
31   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
32   * @author Daniel Gredler
33   * @author Sven Strickroth
34   */
35  public class WaitingRefreshHandler implements RefreshHandler {
36  
37      /** Logging support. */
38      private static final Log LOG = LogFactory.getLog(WaitingRefreshHandler.class);
39  
40      private final int maxwait_;
41  
42      /**
43       * Creates a new refresh handler that will wait whatever time the server or content asks, unless
44       * it it longer than <code>maxwait</code>. A value of <code>maxwait</code> that is less than <code>1</code>
45       * will cause the refresh handler to always wait for whatever time the server or content requests.
46       *
47       * @param maxwait the maximum wait time before the refresh (in seconds)
48       */
49      public WaitingRefreshHandler(final int maxwait) {
50          maxwait_ = maxwait;
51      }
52  
53      /**
54       * Creates a new refresh handler that will always wait whatever time the server or content asks.
55       */
56      public WaitingRefreshHandler() {
57          maxwait_ = 0;
58      }
59  
60      /**
61       * Refreshes the specified page using the specified URL after the specified number of seconds.
62       * @param page the page that is going to be refreshed
63       * @param url the URL where the new page will be loaded
64       * @param requestedWait the number of seconds to wait before reloading the page; if this is
65       *        greater than <code>maxwait</code> then <code>maxwait</code> will be used instead
66       * @throws IOException if the refresh fails
67       */
68      @Override
69      public void handleRefresh(final Page page, final URL url, final int requestedWait) throws IOException {
70          int seconds = requestedWait;
71          if (seconds > maxwait_ && maxwait_ > 0) {
72              seconds = maxwait_;
73          }
74  
75          try {
76              Thread.sleep(seconds * 1000L);
77          }
78          catch (final InterruptedException e) {
79              /* This can happen when the refresh is happening from a navigation that started
80               * from a setTimeout or setInterval. The navigation will cause all threads to get
81               * interrupted, including the current thread in this case. It should be safe to
82               * ignore it since this is the thread now doing the navigation. Eventually we should
83               * refactor to force all navigation to happen back on the main thread.
84               */
85              LOG.debug("Waiting thread was interrupted. Ignoring interruption to continue navigation.");
86  
87              // restore interrupted status
88              Thread.currentThread().interrupt();
89          }
90  
91          final WebWindow window = page.getEnclosingWindow();
92          if (window == null) {
93              return;
94          }
95          final WebClient client = window.getWebClient();
96          client.getPage(window, new WebRequest(url));
97      }
98  
99  }