Skip to content

Commit 995b0b2

Browse files
committed
handle httpclient stuff at one place (wip)
1 parent 3b6ce15 commit 995b0b2

5 files changed

Lines changed: 63 additions & 35 deletions

File tree

src/main/java/org/htmlunit/StringWebResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323

24-
import org.apache.http.HttpStatus;
24+
import org.htmlunit.httpclient.HttpClientConverter;
2525
import org.htmlunit.util.NameValuePair;
2626
import org.htmlunit.util.StringUtils;
2727

@@ -70,7 +70,7 @@ private static WebResponseData getWebResponseData(final String contentString, fi
7070
final byte[] content = StringUtils.toByteArray(contentString, charset);
7171
final List<NameValuePair> compiledHeaders = new ArrayList<>();
7272
compiledHeaders.add(new NameValuePair(HttpHeader.CONTENT_TYPE, "text/html; charset=" + charset));
73-
return new WebResponseData(content, HttpStatus.SC_OK, "OK", compiledHeaders);
73+
return new WebResponseData(content, HttpClientConverter.OK, "OK", compiledHeaders);
7474
}
7575

7676
private static WebRequest buildWebRequest(final URL originatingURL, final Charset charset) {

src/main/java/org/htmlunit/WebClient.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
import org.apache.commons.lang3.StringUtils;
6464
import org.apache.commons.logging.Log;
6565
import org.apache.commons.logging.LogFactory;
66-
import org.apache.http.HttpStatus;
6766
import org.apache.http.NoHttpResponseException;
6867
import org.apache.http.client.CredentialsProvider;
6968
import org.apache.http.cookie.MalformedCookieException;
@@ -594,7 +593,7 @@ public Page loadWebResponseInto(final WebResponse webResponse, final WebWindow w
594593
WebAssert.notNull("webResponse", webResponse);
595594
WebAssert.notNull("webWindow", webWindow);
596595

597-
if (webResponse.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
596+
if (webResponse.getStatusCode() == HttpClientConverter.NO_CONTENT) {
598597
return webWindow.getEnclosedPage();
599598
}
600599

@@ -1576,12 +1575,12 @@ else if (!proxyConfig.shouldBypassProxy(webRequest.getUrl().getHost())) {
15761575

15771576
// Continue according to the HTTP status code.
15781577
final int status = webResponse.getStatusCode();
1579-
if (status == HttpStatus.SC_USE_PROXY) {
1578+
if (status == HttpClientConverter.USE_PROXY) {
15801579
getIncorrectnessListener().notify("Ignoring HTTP status code [305] 'Use Proxy'", this);
15811580
}
1582-
else if (status >= HttpStatus.SC_MOVED_PERMANENTLY
1581+
else if (status >= HttpClientConverter.MOVED_PERMANENTLY
15831582
&& status <= 308
1584-
&& status != HttpStatus.SC_NOT_MODIFIED
1583+
&& status != HttpClientConverter.NOT_MODIFIED
15851584
&& getOptions().isRedirectEnabled()) {
15861585

15871586
URL newUrl;
@@ -1617,9 +1616,9 @@ && getOptions().isRedirectEnabled()) {
16171616
+ webResponse.getWebRequest().getUrl(), webResponse);
16181617
}
16191618

1620-
if (status == HttpStatus.SC_MOVED_PERMANENTLY
1621-
|| status == HttpStatus.SC_MOVED_TEMPORARILY
1622-
|| status == HttpStatus.SC_SEE_OTHER) {
1619+
if (status == HttpClientConverter.MOVED_PERMANENTLY
1620+
|| status == HttpClientConverter.MOVED_TEMPORARILY
1621+
|| status == HttpClientConverter.SEE_OTHER) {
16231622
final WebRequest wrs = new WebRequest(newUrl, HttpMethod.GET);
16241623
wrs.setCharset(webRequest.getCharset());
16251624

@@ -1631,8 +1630,8 @@ && getOptions().isRedirectEnabled()) {
16311630
}
16321631
return loadWebResponseFromWebConnection(wrs, allowedRedirects - 1);
16331632
}
1634-
else if (status == HttpStatus.SC_TEMPORARY_REDIRECT
1635-
|| status == 308) {
1633+
else if (status == HttpClientConverter.TEMPORARY_REDIRECT
1634+
|| status == HttpClientConverter.PERMANENT_REDIRECT) {
16361635
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307
16371636
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/308
16381637
// reuse method and body
@@ -1691,11 +1690,11 @@ private WebResponse getWebResponseOrUseCached(
16911690

16921691
final WebResponse webResponse = getWebConnection().getResponse(webRequest);
16931692

1694-
if (webResponse.getStatusCode() >= HttpStatus.SC_INTERNAL_SERVER_ERROR) {
1693+
if (webResponse.getStatusCode() >= HttpClientConverter.INTERNAL_SERVER_ERROR) {
16951694
return new WebResponseFromCache(cached, webRequest);
16961695
}
16971696

1698-
if (webResponse.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) {
1697+
if (webResponse.getStatusCode() == HttpClientConverter.NOT_MODIFIED) {
16991698
final Map<String, NameValuePair> header2NameValuePair = new LinkedHashMap<>();
17001699
for (final NameValuePair pair : cached.getResponseHeaders()) {
17011700
header2NameValuePair.put(pair.getName(), pair);

src/main/java/org/htmlunit/WebResponse.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.apache.commons.io.input.BOMInputStream;
3232
import org.apache.commons.logging.Log;
3333
import org.apache.commons.logging.LogFactory;
34-
import org.apache.http.HttpStatus;
3534
import org.htmlunit.DefaultPageCreator.PageType;
35+
import org.htmlunit.httpclient.HttpClientConverter;
3636
import org.htmlunit.util.EncodingSniffer;
3737
import org.htmlunit.util.NameValuePair;
3838

@@ -48,17 +48,6 @@
4848
*/
4949
public class WebResponse implements Serializable {
5050

51-
/** Forwarder to HttpStatus.SC_OK. */
52-
public static final int OK = HttpStatus.SC_OK;
53-
/** Forwarder to HttpStatus.SC_FORBIDDEN. */
54-
public static final int FORBIDDEN = HttpStatus.SC_FORBIDDEN;
55-
/** Forwarder to HttpStatus.SC_NOT_FOUND. */
56-
public static final int NOT_FOUND = HttpStatus.SC_NOT_FOUND;
57-
/** Forwarder to HttpStatus.SC_NO_CONTENT. */
58-
public static final int NO_CONTENT = HttpStatus.SC_NO_CONTENT;
59-
/** Forwarder to HttpStatus.SC_INTERNAL_SERVER_ERROR. */
60-
public static final int INTERNAL_SERVER_ERROR = HttpStatus.SC_INTERNAL_SERVER_ERROR;
61-
6251
private static final Log LOG = LogFactory.getLog(WebResponse.class);
6352
private static final ByteOrderMark[] BOM_HEADERS = {
6453
ByteOrderMark.UTF_8,
@@ -327,25 +316,25 @@ public void defaultCharsetUtf8() {
327316
*/
328317
public boolean isSuccess() {
329318
final int statusCode = getStatusCode();
330-
return statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES;
319+
return statusCode >= HttpClientConverter.OK && statusCode < HttpClientConverter.MULTIPLE_CHOICES;
331320
}
332321

333322
/**
334323
* @return true if the 2xx or 305
335324
*/
336325
public boolean isSuccessOrUseProxy() {
337326
final int statusCode = getStatusCode();
338-
return (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES)
339-
|| statusCode == HttpStatus.SC_USE_PROXY;
327+
return (statusCode >= HttpClientConverter.OK && statusCode < HttpClientConverter.MULTIPLE_CHOICES)
328+
|| statusCode == HttpClientConverter.USE_PROXY;
340329
}
341330

342331
/**
343332
* @return true if the 2xx or 305
344333
*/
345334
public boolean isSuccessOrUseProxyOrNotModified() {
346335
final int statusCode = getStatusCode();
347-
return (statusCode >= HttpStatus.SC_OK && statusCode < HttpStatus.SC_MULTIPLE_CHOICES)
348-
|| statusCode == HttpStatus.SC_USE_PROXY
349-
|| statusCode == HttpStatus.SC_NOT_MODIFIED;
336+
return (statusCode >= HttpClientConverter.OK && statusCode < HttpClientConverter.MULTIPLE_CHOICES)
337+
|| statusCode == HttpClientConverter.USE_PROXY
338+
|| statusCode == HttpClientConverter.NOT_MODIFIED;
350339
}
351340
}

src/main/java/org/htmlunit/html/HtmlPage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@
5555
import org.apache.commons.lang3.StringUtils;
5656
import org.apache.commons.logging.Log;
5757
import org.apache.commons.logging.LogFactory;
58-
import org.apache.http.HttpStatus;
5958
import org.htmlunit.BrowserVersion;
6059
import org.htmlunit.Cache;
6160
import org.htmlunit.ElementNotFoundException;
@@ -84,6 +83,7 @@
8483
import org.htmlunit.html.impl.SelectableTextInput;
8584
import org.htmlunit.html.impl.SimpleRange;
8685
import org.htmlunit.html.parser.HTMLParserDOMBuilder;
86+
import org.htmlunit.httpclient.HttpClientConverter;
8787
import org.htmlunit.javascript.AbstractJavaScriptEngine;
8888
import org.htmlunit.javascript.HtmlUnitContextFactory;
8989
import org.htmlunit.javascript.HtmlUnitScriptable;
@@ -1028,7 +1028,7 @@ JavaScriptLoadResult loadExternalJavaScriptFile(final String srcAttribute, final
10281028
return JavaScriptLoadResult.DOWNLOAD_ERROR;
10291029
}
10301030
catch (final FailingHttpStatusCodeException e) {
1031-
if (e.getStatusCode() == HttpStatus.SC_NO_CONTENT) {
1031+
if (e.getStatusCode() == HttpClientConverter.NO_CONTENT) {
10321032
return JavaScriptLoadResult.NO_CONTENT;
10331033
}
10341034
client.getJavaScriptErrorListener().loadScriptError(this, scriptURL, e);
@@ -1096,7 +1096,7 @@ private Object loadJavaScriptFromUrl(final URL url, final Charset scriptCharset)
10961096
client.throwFailingHttpStatusCodeExceptionIfNecessary(response);
10971097

10981098
final int statusCode = response.getStatusCode();
1099-
if (statusCode == HttpStatus.SC_NO_CONTENT) {
1099+
if (statusCode == HttpClientConverter.NO_CONTENT) {
11001100
throw new FailingHttpStatusCodeException(response);
11011101
}
11021102

src/main/java/org/htmlunit/httpclient/HttpClientConverter.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Set;
2525

26+
import org.apache.http.HttpStatus;
2627
import org.apache.http.NoHttpResponseException;
2728
import org.apache.http.client.utils.DateUtils;
2829
import org.apache.http.client.utils.URLEncodedUtils;
@@ -45,6 +46,45 @@
4546
*/
4647
public final class HttpClientConverter {
4748

49+
/** Forwarder to HttpStatus.SC_OK. */
50+
public static final int OK = HttpStatus.SC_OK;
51+
52+
/** Forwarder to HttpStatus.SC_NO_CONTENT. */
53+
public static final int NO_CONTENT = HttpStatus.SC_NO_CONTENT;
54+
55+
/** Forwarder to HttpStatus.MULTIPLE_CHOICES. */
56+
public static final int MULTIPLE_CHOICES = HttpStatus.SC_MULTIPLE_CHOICES;
57+
58+
/** Forwarder to HttpStatus.MOVED_PERMANENTLY. */
59+
public static final int MOVED_PERMANENTLY = HttpStatus.SC_MOVED_PERMANENTLY;
60+
61+
/** Forwarder to HttpStatus.MOVED_TEMPORARILY. */
62+
public static final int MOVED_TEMPORARILY = HttpStatus.SC_MOVED_TEMPORARILY;
63+
64+
/** Forwarder to HttpStatus.SEE_OTHER. */
65+
public static final int SEE_OTHER = HttpStatus.SC_SEE_OTHER;
66+
67+
/** Forwarder to HttpStatus.TEMPORARY_REDIRECT. */
68+
public static final int TEMPORARY_REDIRECT = HttpStatus.SC_TEMPORARY_REDIRECT;
69+
70+
/** 308. */
71+
public static final int PERMANENT_REDIRECT = 308;
72+
73+
/** Forwarder to HttpStatus.NOT_MODIFIED. */
74+
public static final int NOT_MODIFIED = HttpStatus.SC_NOT_MODIFIED;
75+
76+
/** Forwarder to HttpStatus.SC_USE_PROXY. */
77+
public static final int USE_PROXY = HttpStatus.SC_USE_PROXY;
78+
79+
/** Forwarder to HttpStatus.SC_FORBIDDEN. */
80+
public static final int FORBIDDEN = HttpStatus.SC_FORBIDDEN;
81+
82+
/** Forwarder to HttpStatus.SC_NOT_FOUND. */
83+
public static final int NOT_FOUND = HttpStatus.SC_NOT_FOUND;
84+
85+
/** Forwarder to HttpStatus.SC_INTERNAL_SERVER_ERROR. */
86+
public static final int INTERNAL_SERVER_ERROR = HttpStatus.SC_INTERNAL_SERVER_ERROR;
87+
4888
private HttpClientConverter() {
4989
}
5090

0 commit comments

Comments
 (0)