Skip to content

Commit

Permalink
Less code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
slandelle committed Jun 4, 2015
1 parent fd1b51b commit 79238a2
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 133 deletions.
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2015 AsyncHttpClient Project. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at
* http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package org.asynchttpclient.netty.request;

import static org.asynchttpclient.util.AuthenticatorUtils.computeBasicAuthentication;
import static org.asynchttpclient.util.AuthenticatorUtils.computeDigestAuthentication;
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;

import java.io.IOException;

import org.asynchttpclient.AsyncHttpClientConfig;
import org.asynchttpclient.Realm;
import org.asynchttpclient.Request;
import org.asynchttpclient.ntlm.NtlmEngine;
import org.asynchttpclient.proxy.ProxyServer;
import org.asynchttpclient.spnego.SpnegoEngine;
import org.asynchttpclient.uri.Uri;

public abstract class NettyRequestFactoryBase {

protected final AsyncHttpClientConfig config;

public NettyRequestFactoryBase(AsyncHttpClientConfig config) {
this.config = config;
}

protected String firstRequestOnlyAuthorizationHeader(Request request, Uri uri, ProxyServer proxyServer, Realm realm) throws IOException {
String authorizationHeader = null;

if (realm != null && realm.getUsePreemptiveAuth()) {
switch (realm.getScheme()) {
case NTLM:
String msg = NtlmEngine.INSTANCE.generateType1Msg();
authorizationHeader = "NTLM " + msg;
break;
case KERBEROS:
case SPNEGO:
String host;
if (proxyServer != null)
host = proxyServer.getHost();
else if (request.getVirtualHost() != null)
host = request.getVirtualHost();
else
host = uri.getHost();

try {
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
} catch (Throwable e) {
throw new IOException(e);
}
break;
default:
break;
}
}

return authorizationHeader;
}

protected String systematicAuthorizationHeader(Request request, Uri uri, Realm realm) {

String authorizationHeader = null;

if (realm != null && realm.getUsePreemptiveAuth()) {

switch (realm.getScheme()) {
case BASIC:
authorizationHeader = computeBasicAuthentication(realm);
break;
case DIGEST:
if (isNonEmpty(realm.getNonce()))
authorizationHeader = computeDigestAuthentication(realm);
break;
case NTLM:
case KERBEROS:
case SPNEGO:
// NTLM, KERBEROS and SPNEGO are only set on the first request, see firstRequestOnlyAuthorizationHeader
case NONE:
break;
default:
throw new IllegalStateException("Invalid Authentication " + realm);
}
}

return authorizationHeader;
}
}
Expand Up @@ -46,7 +46,6 @@
import org.asynchttpclient.proxy.ProxyServer;
import org.asynchttpclient.request.body.generator.FileBodyGenerator;
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
import org.asynchttpclient.spnego.SpnegoEngine;
import org.asynchttpclient.uri.Uri;
import org.asynchttpclient.util.StringUtils;
import org.jboss.netty.buffer.ChannelBuffer;
Expand All @@ -56,14 +55,12 @@
import org.jboss.netty.handler.codec.http.HttpRequest;
import org.jboss.netty.handler.codec.http.HttpVersion;

public final class NettyRequestFactory {
public final class NettyRequestFactory extends NettyRequestFactoryBase {

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

private final AsyncHttpClientConfig config;

public NettyRequestFactory(AsyncHttpClientConfig config) {
this.config = config;
super(config);
}

private String requestUri(Uri uri, ProxyServer proxyServer, HttpMethod method) {
Expand All @@ -81,68 +78,7 @@ else if (proxyServer != null && !useProxyConnect(uri))
return path;
}
}

public String firstRequestOnlyAuthorizationHeader(Request request, Uri uri, ProxyServer proxyServer, Realm realm) throws IOException {
String authorizationHeader = null;

if (realm != null && realm.getUsePreemptiveAuth()) {
switch (realm.getScheme()) {
case NTLM:
String msg = NtlmEngine.INSTANCE.generateType1Msg();
authorizationHeader = "NTLM " + msg;
break;
case KERBEROS:
case SPNEGO:
String host;
if (proxyServer != null)
host = proxyServer.getHost();
else if (request.getVirtualHost() != null)
host = request.getVirtualHost();
else
host = uri.getHost();

try {
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
} catch (Throwable e) {
throw new IOException(e);
}
break;
default:
break;
}
}

return authorizationHeader;
}

private String systematicAuthorizationHeader(Request request, Uri uri, Realm realm) {

String authorizationHeader = null;

if (realm != null && realm.getUsePreemptiveAuth()) {

switch (realm.getScheme()) {
case BASIC:
authorizationHeader = computeBasicAuthentication(realm);
break;
case DIGEST:
if (isNonEmpty(realm.getNonce()))
authorizationHeader = computeDigestAuthentication(realm);
break;
case NTLM:
case KERBEROS:
case SPNEGO:
// NTLM, KERBEROS and SPNEGO are only set on the first request, see firstRequestOnlyAuthorizationHeader
case NONE:
break;
default:
throw new IllegalStateException("Invalid Authentication " + realm);
}
}

return authorizationHeader;
}

public String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxyServer proxyServer, HttpMethod method) throws IOException {
String proxyAuthorization = null;

Expand Down
Expand Up @@ -14,7 +14,12 @@
package org.asynchttpclient.netty.request;

import static org.asynchttpclient.ntlm.NtlmUtils.getNTLM;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.*;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.DEFAULT_CHARSET;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getAuthority;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.hostHeader;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.keepAliveHeaderValue;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.urlEncodeFormParams;
import static org.asynchttpclient.util.AuthenticatorUtils.computeBasicAuthentication;
import static org.asynchttpclient.util.AuthenticatorUtils.computeDigestAuthentication;
import static org.asynchttpclient.util.HttpUtils.isSecure;
Expand Down Expand Up @@ -53,18 +58,15 @@
import org.asynchttpclient.proxy.ProxyServer;
import org.asynchttpclient.request.body.generator.FileBodyGenerator;
import org.asynchttpclient.request.body.generator.InputStreamBodyGenerator;
import org.asynchttpclient.spnego.SpnegoEngine;
import org.asynchttpclient.uri.Uri;
import org.asynchttpclient.util.StringUtils;

public final class NettyRequestFactory {
public final class NettyRequestFactory extends NettyRequestFactoryBase {

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

private final AsyncHttpClientConfig config;

public NettyRequestFactory(AsyncHttpClientConfig config) {
this.config = config;
super(config);
}

private String requestUri(Uri uri, ProxyServer proxyServer, HttpMethod method) {
Expand All @@ -82,68 +84,7 @@ else if (proxyServer != null && !useProxyConnect(uri))
return path;
}
}

public String firstRequestOnlyAuthorizationHeader(Request request, Uri uri, ProxyServer proxyServer, Realm realm) throws IOException {
String authorizationHeader = null;

if (realm != null && realm.getUsePreemptiveAuth()) {
switch (realm.getScheme()) {
case NTLM:
String msg = NtlmEngine.INSTANCE.generateType1Msg();
authorizationHeader = "NTLM " + msg;
break;
case KERBEROS:
case SPNEGO:
String host;
if (proxyServer != null)
host = proxyServer.getHost();
else if (request.getVirtualHost() != null)
host = request.getVirtualHost();
else
host = uri.getHost();

try {
authorizationHeader = "Negotiate " + SpnegoEngine.instance().generateToken(host);
} catch (Throwable e) {
throw new IOException(e);
}
break;
default:
break;
}
}

return authorizationHeader;
}

private String systematicAuthorizationHeader(Request request, Uri uri, Realm realm) {

String authorizationHeader = null;

if (realm != null && realm.getUsePreemptiveAuth()) {

switch (realm.getScheme()) {
case BASIC:
authorizationHeader = computeBasicAuthentication(realm);
break;
case DIGEST:
if (isNonEmpty(realm.getNonce()))
authorizationHeader = computeDigestAuthentication(realm);
break;
case NTLM:
case KERBEROS:
case SPNEGO:
// NTLM, KERBEROS and SPNEGO are only set on the first request, see firstRequestOnlyAuthorizationHeader
case NONE:
break;
default:
throw new IllegalStateException("Invalid Authentication " + realm);
}
}

return authorizationHeader;
}

public String firstRequestOnlyProxyAuthorizationHeader(Request request, ProxyServer proxyServer, HttpMethod method) throws IOException {
String proxyAuthorization = null;

Expand Down

0 comments on commit 79238a2

Please sign in to comment.