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