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.util;
16  
17  import java.io.IOException;
18  
19  import org.htmlunit.WebClient;
20  import org.htmlunit.WebConnection;
21  import org.htmlunit.WebRequest;
22  import org.htmlunit.WebResponse;
23  
24  /**
25   * <p>Provides a convenient implementation of the {@link WebConnection} interface that can be subclassed by developers
26   * wishing to adapt a particular WebConnection.</p>
27   *
28   * <p>This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped
29   * web connection object.</p>
30   *
31   * @author Marc Guillemot
32   * @author Ahmed Ashour
33   */
34  public class WebConnectionWrapper implements WebConnection {
35      private final WebConnection wrappedWebConnection_;
36  
37      /**
38       * Constructs a WebConnection object wrapping provided WebConnection.
39       * @param webConnection the webConnection that does the real work
40       * @throws IllegalArgumentException if the connection is {@code null}
41       */
42      public WebConnectionWrapper(final WebConnection webConnection) throws IllegalArgumentException {
43          if (webConnection == null) {
44              throw new IllegalArgumentException("Wrapped connection can't be null");
45          }
46          wrappedWebConnection_ = webConnection;
47      }
48  
49      /**
50       * Constructs a WebConnection object wrapping the connection of the WebClient and places itself as
51       * connection of the WebClient.
52       * @param webClient the WebClient which WebConnection should be wrapped
53       * @throws IllegalArgumentException if the WebClient is {@code null}
54       */
55      public WebConnectionWrapper(final WebClient webClient) throws IllegalArgumentException {
56          if (webClient == null) {
57              throw new IllegalArgumentException("WebClient can't be null");
58          }
59          wrappedWebConnection_ = webClient.getWebConnection();
60          webClient.setWebConnection(this);
61      }
62  
63      /**
64       * {@inheritDoc}
65       * The default behavior of this method is to return {@link WebConnection#getResponse(WebRequest)}
66       * on the wrapped connection object.
67       */
68      @Override
69      public WebResponse getResponse(final WebRequest request) throws IOException {
70          return wrappedWebConnection_.getResponse(request);
71      }
72  
73      /**
74       * Gets the wrapped {@link WebConnection}.
75       * @return the wrapped connection
76       */
77      public WebConnection getWrappedWebConnection() {
78          return wrappedWebConnection_;
79      }
80  
81      /**
82       * {@inheritDoc}
83       * The default behavior of this method is to return {@link WebConnection#close()} on the wrapped connection object.
84       */
85      @Override
86      public void close() throws IOException {
87          wrappedWebConnection_.close();
88      }
89  }