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;
16
17 import static java.nio.charset.StandardCharsets.UTF_8;
18
19 import java.net.URL;
20 import java.nio.charset.Charset;
21 import java.util.ArrayList;
22 import java.util.List;
23
24 import org.htmlunit.http.HttpStatus;
25 import org.htmlunit.util.NameValuePair;
26 import org.htmlunit.util.StringUtils;
27
28 /**
29 * A simple WebResponse created from a string. Content is assumed to be of type <code>text/html</code>.
30 *
31 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
32 * @author Marc Guillemot
33 * @author Brad Clarke
34 * @author Ahmed Ashour
35 * @author Ronald Brill
36 * @author Carsten Steul
37 */
38 public class StringWebResponse extends WebResponse {
39
40 private boolean fromJavascript_;
41
42 /**
43 * Creates an instance associated with the specified originating URL.
44 * @param content the content to return
45 * @param originatingURL the URL that this should be associated with
46 */
47 public StringWebResponse(final String content, final URL originatingURL) {
48 // use UTF-8 here to be sure, all chars in the string are part of the charset
49 this(content, UTF_8, originatingURL);
50 }
51
52 /**
53 * Creates an instance associated with the specified originating URL.
54 * @param content the content to return
55 * @param charset the charset used to convert the content
56 * @param originatingURL the URL that this should be associated with
57 */
58 public StringWebResponse(final String content, final Charset charset, final URL originatingURL) {
59 super(getWebResponseData(content, charset), buildWebRequest(originatingURL, charset), 0);
60 }
61
62 /**
63 * Helper method for constructors. Converts the specified string into {@link WebResponseData}
64 * with other defaults specified.
65 *
66 * @param contentString the string to be converted to a <code>WebResponseData</code>
67 * @return a simple <code>WebResponseData</code> with defaults specified
68 */
69 private static WebResponseData getWebResponseData(final String contentString, final Charset charset) {
70 final byte[] content = StringUtils.toByteArray(contentString, charset);
71 final List<NameValuePair> compiledHeaders = new ArrayList<>();
72 compiledHeaders.add(new NameValuePair(HttpHeader.CONTENT_TYPE, "text/html; charset=" + charset));
73 return new WebResponseData(content, HttpStatus.OK_200, HttpStatus.OK_200_MSG, compiledHeaders);
74 }
75
76 private static WebRequest buildWebRequest(final URL originatingURL, final Charset charset) {
77 final WebRequest webRequest = new WebRequest(originatingURL, HttpMethod.GET);
78 webRequest.setCharset(charset);
79 return webRequest;
80 }
81
82 /**
83 * Returns the fromJavascript property. This is true, if the response was created
84 * from javascript (usually document.write).
85 * @return the from fromJavascript_
86 */
87 public boolean isFromJavascript() {
88 return fromJavascript_;
89 }
90
91 /**
92 * Sets the fromJavascript_ property. Set this to true, if the response was created
93 * from javascript (usually document.write).
94 * @param fromJavascript the new fromJavascript
95 */
96 public void setFromJavascript(final boolean fromJavascript) {
97 fromJavascript_ = fromJavascript;
98 }
99 }