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