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