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.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   * @author Ronald Brill
34   */
35  public class WebConnectionWrapper implements WebConnection {
36      private final WebConnection wrappedWebConnection_;
37  
38      /**
39       * Constructs a WebConnection object wrapping provided WebConnection.
40       * @param webConnection the webConnection that does the real work
41       * @throws IllegalArgumentException if the connection is {@code null}
42       */
43      public WebConnectionWrapper(final WebConnection webConnection) throws IllegalArgumentException {
44          if (webConnection == null) {
45              throw new IllegalArgumentException("Wrapped connection can't be null");
46          }
47          wrappedWebConnection_ = webConnection;
48      }
49  
50      /**
51       * Constructs a WebConnection object wrapping the connection of the WebClient and places itself as
52       * connection of the WebClient.
53       * @param webClient the WebClient which WebConnection should be wrapped
54       * @throws IllegalArgumentException if the WebClient is {@code null}
55       */
56      public WebConnectionWrapper(final WebClient webClient) throws IllegalArgumentException {
57          if (webClient == null) {
58              throw new IllegalArgumentException("WebClient can't be null");
59          }
60          wrappedWebConnection_ = webClient.getWebConnection();
61          webClient.setWebConnection(this);
62      }
63  
64      /**
65       * {@inheritDoc}
66       * The default behavior of this method is to return {@link WebConnection#getResponse(WebRequest)}
67       * on the wrapped connection object.
68       */
69      @Override
70      public WebResponse getResponse(final WebRequest request) throws IOException {
71          return wrappedWebConnection_.getResponse(request);
72      }
73  
74      /**
75       * Gets the wrapped {@link WebConnection}.
76       * @return the wrapped connection
77       */
78      public WebConnection getWrappedWebConnection() {
79          return wrappedWebConnection_;
80      }
81  
82      /**
83       * {@inheritDoc}
84       * The default behavior of this method is to return {@link WebConnection#close()} on the wrapped connection object.
85       */
86      @Override
87      public void close() throws IOException {
88          wrappedWebConnection_.close();
89      }
90  }