Skip to content

Commit 79238a2

Browse files
committed
Less code duplication
1 parent fd1b51b commit 79238a2

File tree

3 files changed

+108
-133
lines changed

3 files changed

+108
-133
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved.
3+
*
4+
* This program is licensed to you under the Apache License Version 2.0,
5+
* and you may not use this file except in compliance with the Apache License Version 2.0.
6+
* You may obtain a copy of the Apache License Version 2.0 at
7+
* http://www.apache.org/licenses/LICENSE-2.0.
8+
*
9+
* Unless required by applicable law or agreed to in writing,
10+
* software distributed under the Apache License Version 2.0 is distributed on an
11+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
13+
*/
14+
package org.asynchttpclient.netty.request;
15+
16+
import static org.asynchttpclient.util.AuthenticatorUtils.computeBasicAuthentication;
17+
import static org.asynchttpclient.util.AuthenticatorUtils.computeDigestAuthentication;
18+
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;
19+
20+
import java.io.IOException;
21+
22+
import org.asynchttpclient.AsyncHttpClientConfig;
23+
import org.asynchttpclient.Realm;
24+
import org.asynchttpclient.Request;
25+
import org.asynchttpclient.ntlm.NtlmEngine;
26+
import org.asynchttpclient.proxy.ProxyServer;
27+
import org.asynchttpclient.spnego.SpnegoEngine;
28+
import org.asynchttpclient.uri.Uri;
29+
30+
public abstract class NettyRequestFactoryBase {
31+
32+
protected final AsyncHttpClientConfig config;
33+
34+
public NettyRequestFactoryBase(AsyncHttpClientConfig config) {
35+
this.config = config;
36+
}
37+
38+
protected String firstRequestOnlyAuthorizationHeader(Request request, Uri uri, ProxyServer proxyServer, Realm realm) throws IOException {
39+
String authorizationHeader = null;
40+
41+
if (realm != null && realm.getUsePreemptiveAuth()) {
42+
switch (realm.getScheme()) {
43+
case NTLM:
44+
String msg = NtlmEngine.INSTANCE.generateType1Msg();
45+
authorizationHeader = "NTLM " + msg;
46+
break;
47+
case KERBEROS:
48+
case SPNEGO:
49+
String host;
50+
if (proxyServer != null)
51+
host = proxyServer.getHost();
52+
else if (request.getVirtualHost() != null)
53+
host = request.getVirtualHost();
54+
else
55+
host = uri.getHost();
56+
57+
try {
58+
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
59+
} catch (Throwable e) {
60+
throw new IOException(e);
61+
}
62+
break;
63+
default:
64+
break;
65+
}
66+
}
67+
68+
return authorizationHeader;
69+
}
70+
71+
protected String systematicAuthorizationHeader(Request request, Uri uri, Realm realm) {
72+
73+
String authorizationHeader = null;
74+
75+
if (realm != null && realm.getUsePreemptiveAuth()) {
76+
77+
switch (realm.getScheme()) {
78+
case BASIC:
79+
authorizationHeader = computeBasicAuthentication(realm);
80+
break;
81+
case DIGEST:
82+
if (isNonEmpty(realm.getNonce()))
83+
authorizationHeader = computeDigestAuthentication(realm);
84+
break;
85+
case NTLM:
86+
case KERBEROS:
87+
case SPNEGO:
88+
// NTLM, KERBEROS and SPNEGO are only set on the first request, see firstRequestOnlyAuthorizationHeader
89+
case NONE:
90+
break;
91+
default:
92+
throw new IllegalStateException("Invalid Authentication " + realm);
93+
}
94+
}
95+
96+
return authorizationHeader;
97+
}
98+
}

providers/netty3/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactory.java

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.asynchttpclient.proxy.ProxyServer;
4747
import org.asynchttpclient.request.body.generator.FileBodyGenerator;
4848
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
49-
import org.asynchttpclient.spnego.SpnegoEngine;
5049
import org.asynchttpclient.uri.Uri;
5150
import org.asynchttpclient.util.StringUtils;
5251
import org.jboss.netty.buffer.ChannelBuffer;
@@ -56,14 +55,12 @@
5655
import org.jboss.netty.handler.codec.http.HttpRequest;
5756
import org.jboss.netty.handler.codec.http.HttpVersion;
5857

59-
public final class NettyRequestFactory {
58+
public final class NettyRequestFactory extends NettyRequestFactoryBase {
6059

6160
public static final String GZIP_DEFLATE = HttpHeaders.Values.GZIP + "," + HttpHeaders.Values.DEFLATE;
6261

63-
private final AsyncHttpClientConfig config;
64-
6562
public NettyRequestFactory(AsyncHttpClientConfig config) {
66-
this.config = config;
63+
super(config);
6764
}
6865

6966
private String requestUri(Uri uri, ProxyServer proxyServer, HttpMethod method) {
@@ -81,68 +78,7 @@ else if (proxyServer != null && !useProxyConnect(uri))
8178
return path;
8279
}
8380
}
84-
85-
public String firstRequestOnlyAuthorizationHeader(Request request, Uri uri, ProxyServer proxyServer, Realm realm) throws IOException {
86-
String authorizationHeader = null;
87-
88-
if (realm != null && realm.getUsePreemptiveAuth()) {
89-
switch (realm.getScheme()) {
90-
case NTLM:
91-
String msg = NtlmEngine.INSTANCE.generateType1Msg();
92-
authorizationHeader = "NTLM " + msg;
93-
break;
94-
case KERBEROS:
95-
case SPNEGO:
96-
String host;
97-
if (proxyServer != null)
98-
host = proxyServer.getHost();
99-
else if (request.getVirtualHost() != null)
100-
host = request.getVirtualHost();
101-
else
102-
host = uri.getHost();
103-
104-
try {
105-
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
106-
} catch (Throwable e) {
107-
throw new IOException(e);
108-
}
109-
break;
110-
default:
111-
break;
112-
}
113-
}
114-
115-
return authorizationHeader;
116-
}
11781

118-
private String systematicAuthorizationHeader(Request request, Uri uri, Realm realm) {
119-
120-
String authorizationHeader = null;
121-
122-
if (realm != null && realm.getUsePreemptiveAuth()) {
123-
124-
switch (realm.getScheme()) {
125-
case BASIC:
126-
authorizationHeader = computeBasicAuthentication(realm);
127-
break;
128-
case DIGEST:
129-
if (isNonEmpty(realm.getNonce()))
130-
authorizationHeader = computeDigestAuthentication(realm);
131-
break;
132-
case NTLM:
133-
case KERBEROS:
134-
case SPNEGO:
135-
// NTLM, KERBEROS and SPNEGO are only set on the first request, see firstRequestOnlyAuthorizationHeader
136-
case NONE:
137-
break;
138-
default:
139-
throw new IllegalStateException("Invalid Authentication " + realm);
140-
}
141-
}
142-
143-
return authorizationHeader;
144-
}
145-
14682
public String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxyServer proxyServer, HttpMethod method) throws IOException {
14783
String proxyAuthorization = null;
14884

providers/netty4/src/main/java/org/asynchttpclient/netty/request/NettyRequestFactory.java

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
package org.asynchttpclient.netty.request;
1515

1616
import static org.asynchttpclient.ntlm.NtlmUtils.getNTLM;
17-
import static org.asynchttpclient.util.AsyncHttpProviderUtils.*;
17+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.DEFAULT_CHARSET;
18+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getAuthority;
19+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
20+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.hostHeader;
21+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.keepAliveHeaderValue;
22+
import static org.asynchttpclient.util.AsyncHttpProviderUtils.urlEncodeFormParams;
1823
import static org.asynchttpclient.util.AuthenticatorUtils.computeBasicAuthentication;
1924
import static org.asynchttpclient.util.AuthenticatorUtils.computeDigestAuthentication;
2025
import static org.asynchttpclient.util.HttpUtils.isSecure;
@@ -53,18 +58,15 @@
5358
import org.asynchttpclient.proxy.ProxyServer;
5459
import org.asynchttpclient.request.body.generator.FileBodyGenerator;
5560
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
56-
import org.asynchttpclient.spnego.SpnegoEngine;
5761
import org.asynchttpclient.uri.Uri;
5862
import org.asynchttpclient.util.StringUtils;
5963

60-
public final class NettyRequestFactory {
64+
public final class NettyRequestFactory extends NettyRequestFactoryBase {
6165

6266
public static final String GZIP_DEFLATE = HttpHeaders.Values.GZIP + "," + HttpHeaders.Values.DEFLATE;
6367

64-
private final AsyncHttpClientConfig config;
65-
6668
public NettyRequestFactory(AsyncHttpClientConfig config) {
67-
this.config = config;
69+
super(config);
6870
}
6971

7072
private String requestUri(Uri uri, ProxyServer proxyServer, HttpMethod method) {
@@ -82,68 +84,7 @@ else if (proxyServer != null && !useProxyConnect(uri))
8284
return path;
8385
}
8486
}
85-
86-
public String firstRequestOnlyAuthorizationHeader(Request request, Uri uri, ProxyServer proxyServer, Realm realm) throws IOException {
87-
String authorizationHeader = null;
88-
89-
if (realm != null && realm.getUsePreemptiveAuth()) {
90-
switch (realm.getScheme()) {
91-
case NTLM:
92-
String msg = NtlmEngine.INSTANCE.generateType1Msg();
93-
authorizationHeader = "NTLM " + msg;
94-
break;
95-
case KERBEROS:
96-
case SPNEGO:
97-
String host;
98-
if (proxyServer != null)
99-
host = proxyServer.getHost();
100-
else if (request.getVirtualHost() != null)
101-
host = request.getVirtualHost();
102-
else
103-
host = uri.getHost();
104-
105-
try {
106-
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
107-
} catch (Throwable e) {
108-
throw new IOException(e);
109-
}
110-
break;
111-
default:
112-
break;
113-
}
114-
}
115-
116-
return authorizationHeader;
117-
}
11887

119-
private String systematicAuthorizationHeader(Request request, Uri uri, Realm realm) {
120-
121-
String authorizationHeader = null;
122-
123-
if (realm != null && realm.getUsePreemptiveAuth()) {
124-
125-
switch (realm.getScheme()) {
126-
case BASIC:
127-
authorizationHeader = computeBasicAuthentication(realm);
128-
break;
129-
case DIGEST:
130-
if (isNonEmpty(realm.getNonce()))
131-
authorizationHeader = computeDigestAuthentication(realm);
132-
break;
133-
case NTLM:
134-
case KERBEROS:
135-
case SPNEGO:
136-
// NTLM, KERBEROS and SPNEGO are only set on the first request, see firstRequestOnlyAuthorizationHeader
137-
case NONE:
138-
break;
139-
default:
140-
throw new IllegalStateException("Invalid Authentication " + realm);
141-
}
142-
}
143-
144-
return authorizationHeader;
145-
}
146-
14788
public String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxyServer proxyServer, HttpMethod method) throws IOException {
14889
String proxyAuthorization = null;
14990

0 commit comments

Comments
 (0)