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