Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/HTTP-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ The url must start with a protocol (e.g. http://).

### Basic Authentication

**Preemptive basic authentication:** If true, basic authentication will be performed in preemptive mode,
without additional negotiation request.

**Username:** Username for basic authentication.

**Password:** Password for basic authentication.
Expand Down
3 changes: 3 additions & 0 deletions docs/HTTP-batchsource.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ can be omitted as long as the field is present in schema.

### Basic Authentication

**Preemptive basic authentication:** If true, basic authentication will be performed in preemptive mode,
without additional negotiation request.

**Username:** Username for basic authentication.

**Password:** Password for basic authentication.
Expand Down
3 changes: 3 additions & 0 deletions docs/HTTP-streamingsource.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ can be omitted as long as the field is present in schema.

### Basic Authentication

**Preemptive basic authentication:** If true, basic authentication will be performed in preemptive mode,
without additional negotiation request.

**Username:** Username for basic authentication.

**Password:** Password for basic authentication.
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/cdap/plugin/http/action/HttpActionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ public class HttpActionConfig extends PluginConfig implements IHttpConfig {
@Macro
protected String requestBody;

@Name(HttpConstants.PROPERTY_PREEMPTIVE_BASIC_AUTH)
@Description("If true, basic authentication will be performed in preemptive mode, " +
"without additional negotiation request.")
@Macro
protected String preemptiveBasicAuth;

@Nullable
@Name(HttpConstants.PROPERTY_USERNAME)
@Description("Username for basic authentication.")
Expand Down Expand Up @@ -255,6 +261,11 @@ public String getRequestBody() {
return requestBody;
}

@Override
public Boolean getPreemptiveBasicAuth() {
return Boolean.parseBoolean(preemptiveBasicAuth);
}

@Override
@Nullable
public String getUsername() {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/cdap/plugin/http/common/BaseHttpSourceConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ public abstract class BaseHttpSourceConfig extends ReferencePluginConfig impleme
@Macro
protected String csvSkipFirstRow;

@Name(HttpConstants.PROPERTY_PREEMPTIVE_BASIC_AUTH)
@Description("If true, basic authentication will be performed in preemptive mode, " +
"without additional negotiation request.")
@Macro
protected String preemptiveBasicAuth;

@Nullable
@Name(HttpConstants.PROPERTY_USERNAME)
@Description("Username for basic authentication.")
Expand Down Expand Up @@ -403,6 +409,11 @@ public Boolean getCsvSkipFirstRow() {
return Boolean.parseBoolean(csvSkipFirstRow);
}

@Override
public Boolean getPreemptiveBasicAuth() {
return Boolean.parseBoolean(preemptiveBasicAuth);
}

@Nullable
public String getUsername() {
return username;
Expand Down
23 changes: 20 additions & 3 deletions src/main/java/io/cdap/plugin/http/common/http/HttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
Expand All @@ -36,6 +40,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand All @@ -48,6 +53,7 @@ public class HttpClient implements Closeable {
private final IHttpConfig config;
private final StringEntity requestBody;
private CloseableHttpClient httpClient;
private HttpClientContext localContext;

public HttpClient(IHttpConfig config) {
this.config = config;
Expand Down Expand Up @@ -81,7 +87,7 @@ public CloseableHttpResponse executeHTTP(String uri) throws IOException {
request.setEntity(requestBody);
}

return httpClient.execute(request);
return localContext == null ? httpClient.execute(request) : httpClient.execute(request, localContext);
}

@Override
Expand All @@ -106,11 +112,22 @@ private CloseableHttpClient createHttpClient() throws IOException {

// basic auth
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
localContext = HttpClientContext.create();

if (!Strings.isNullOrEmpty(config.getUsername()) && !Strings.isNullOrEmpty(config.getPassword())) {
URL targetURL = new URL(config.getUrl());
HttpHost targetHost = new HttpHost(targetURL.getHost(), targetURL.getPort(), targetURL.getProtocol());

credentialsProvider.setCredentials(
new AuthScope(HttpHost.create(config.getUrl())),
new UsernamePasswordCredentials(config.getUsername(), config.getPassword())
new AuthScope(targetHost),
new UsernamePasswordCredentials(config.getUsername(), config.getPassword())
);
if (config.getPreemptiveBasicAuth()) {
AuthCache authCache = new BasicAuthCache();
authCache.put(targetHost, new BasicScheme());

localContext.setAuthCache(authCache);
}
}

// proxy and proxy auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ private HttpConstants() {
public static final String PROPERTY_HEADERS = "headers";
public static final String PROPERTY_REQUEST_BODY = "requestBody";
public static final String PROPERTY_USERNAME = "username";
public static final String PROPERTY_PREEMPTIVE_BASIC_AUTH = "preemptiveBasicAuth";
public static final String PROPERTY_PASSWORD = "password";
public static final String PROPERTY_PROXY_URL = "proxyUrl";
public static final String PROPERTY_PROXY_USERNAME = "proxyUsername";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public interface IHttpConfig {
@Nullable
String getRequestBody();

Boolean getPreemptiveBasicAuth();

@Nullable
String getUsername();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ protected Map<String, String> getProperties(Map<String, String> sourceProperties
return new ImmutableMap.Builder<String, String>()
.put("referenceName", testName.getMethodName())
.put(HttpConstants.PROPERTY_HTTP_METHOD, "GET")
.put(HttpConstants.PROPERTY_PREEMPTIVE_BASIC_AUTH, "false")
.put(HttpConstants.PROPERTY_OAUTH2_ENABLED, "false")
.put(HttpConstants.PROPERTY_HTTP_ERROR_HANDLING, "2..:Success,.*:Fail")
.put(HttpConstants.PROPERTY_ERROR_HANDLING, "stopOnError")
Expand Down
16 changes: 16 additions & 0 deletions widgets/HTTP-action.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@
{
"label": "Basic Authentication",
"properties": [
{
"widget-type": "toggle",
"label": "Preemptive basic authentication",
"name": "preemptiveBasicAuth",
"widget-attributes": {
"default": "false",
"on": {
"label": "True",
"value": "true"
},
"off": {
"label": "False",
"value": "false"
}
}
},
{
"widget-type": "textbox",
"label": "Username",
Expand Down
16 changes: 16 additions & 0 deletions widgets/HTTP-batchsource.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@
{
"label": "Basic Authentication",
"properties": [
{
"widget-type": "toggle",
"label": "Preemptive basic authentication",
"name": "preemptiveBasicAuth",
"widget-attributes": {
"default": "false",
"on": {
"label": "True",
"value": "true"
},
"off": {
"label": "False",
"value": "false"
}
}
},
{
"widget-type": "textbox",
"label": "Username",
Expand Down
16 changes: 16 additions & 0 deletions widgets/HTTP-streamingsource.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,22 @@
{
"label": "Basic Authentication",
"properties": [
{
"widget-type": "toggle",
"label": "Preemptive basic authentication",
"name": "preemptiveBasicAuth",
"widget-attributes": {
"default": "false",
"on": {
"label": "True",
"value": "true"
},
"off": {
"label": "False",
"value": "false"
}
}
},
{
"widget-type": "textbox",
"label": "Username",
Expand Down