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 static java.nio.charset.StandardCharsets.UTF_8;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.htmlunit.HttpHeader;
25  import org.htmlunit.WebClient;
26  import org.htmlunit.WebConnection;
27  import org.htmlunit.WebRequest;
28  import org.htmlunit.WebResponse;
29  import org.htmlunit.WebResponseData;
30  
31  /**
32   * Extension of {@link WebConnectionWrapper} providing facility methods to deliver something other than
33   * what the wrapped connection would deliver.
34   *
35   * @author Marc Guillemot
36   * @author Ronald Brill
37   */
38  public abstract class FalsifyingWebConnection extends WebConnectionWrapper {
39  
40      /**
41       * Constructs a WebConnection object wrapping provided WebConnection.
42       * @param webConnection the webConnection that does the real work
43       * @throws IllegalArgumentException if the connection is {@code null}
44       */
45      public FalsifyingWebConnection(final WebConnection webConnection) throws IllegalArgumentException {
46          super(webConnection);
47      }
48  
49      /**
50       * Constructs an instance and places itself as connection of the WebClient.
51       * @param webClient the WebClient which WebConnection should be wrapped
52       * @throws IllegalArgumentException if the WebClient is {@code null}
53       */
54      public FalsifyingWebConnection(final WebClient webClient) throws IllegalArgumentException {
55          super(webClient);
56      }
57  
58      /**
59       * Delivers the content for an alternate URL as if it comes from the requested URL.
60       * @param webRequest the original web request
61       * @param url the URL from which the content should be retrieved
62       * @return the response
63       * @throws IOException if a problem occurred
64       */
65      protected WebResponse deliverFromAlternateUrl(final WebRequest webRequest, final URL url)
66          throws IOException {
67          final URL originalUrl = webRequest.getUrl();
68          webRequest.setUrl(url);
69          final WebResponse resp = super.getResponse(webRequest);
70          resp.getWebRequest().setUrl(originalUrl);
71          return resp;
72      }
73  
74      /**
75       * Builds a WebResponse with new content, preserving all other information.
76       * @param wr the web response to adapt
77       * @param newContent the new content to place in the response
78       * @return a web response with the new content
79       * @throws IOException if an encoding problem occurred
80       */
81      protected WebResponse replaceContent(final WebResponse wr, final String newContent) throws IOException {
82          final byte[] body = newContent.getBytes(wr.getContentCharset());
83          final WebResponseData wrd = new WebResponseData(body, wr.getStatusCode(), wr.getStatusMessage(),
84              wr.getResponseHeaders());
85          return new WebResponse(wrd, wr.getWebRequest().getUrl(), wr.getWebRequest().getHttpMethod(),
86                  wr.getLoadTime());
87      }
88  
89      /**
90       * Creates a faked WebResponse for the request with the provided content.
91       * @param wr the web request for which a response should be created
92       * @param content the content to place in the response
93       * @param contentType the content type of the response
94       * @return a web response with the provided content
95       * @throws IOException if an encoding problem occurred
96       */
97      protected WebResponse createWebResponse(final WebRequest wr, final String content,
98              final String contentType) throws IOException {
99          return createWebResponse(wr, content, contentType, 200, "OK");
100     }
101 
102     /**
103      * Creates a faked WebResponse for the request with the provided content.
104      * @param wr the web request for which a response should be created
105      * @param content the content to place in the response
106      * @param contentType the content type of the response
107      * @param responseCode the HTTP code for the response
108      * @param responseMessage the HTTP message for the response
109      * @return a web response with the provided content
110      * @throws IOException if an encoding problem occurred
111      */
112     protected WebResponse createWebResponse(final WebRequest wr, final String content,
113             final String contentType, final int responseCode, final String responseMessage) throws IOException {
114         final List<NameValuePair> headers = new ArrayList<>();
115         headers.add(new NameValuePair(HttpHeader.CONTENT_TYPE_LC, contentType + "; charset=" + UTF_8));
116         final byte[] body = content.getBytes(UTF_8);
117         final WebResponseData wrd = new WebResponseData(body, responseCode, responseMessage, headers);
118         return new WebResponse(wrd, wr.getUrl(), wr.getHttpMethod(), 0);
119     }
120 }