Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  Adding support for multipart request for JDK11 HTTP client
  Adding support for multipart request for JDK11 HTTP client
  Adding support for multipart request for JDK11 HTTP client
  Solving: javax.net.ssl.SSLProtocolException with openjdk11 on Travis
  Solving: javax.net.ssl.SSLProtocolException with openjdk11 on Travis
  A JDK11 HTTP Client provider.
  A JDK11 HTTP Client provider.
  A JDK11 HTTP Client provider.
  HTTP clients configuration tests.
  • Loading branch information
maximkir committed Mar 7, 2019
2 parents 2bfafd1 + ffe981b commit bf51836
Show file tree
Hide file tree
Showing 63 changed files with 1,813 additions and 227 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: java
install: true
jdk:
- oraclejdk8
#- openjdk8
- openjdk11
before_install:
- chmod +x gradlew
stages:
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ In addition, SHCF4J enables cleaner code by declaring all possible exceptions as
## Supported Providers
* Apache HTTP Client 4.3+
* Asynchronous HTTP Client 2.6+ (AHC2)
* JDK11 HTTP Client

## Installation
Binaries are deployed on both Maven central & JCenter repositories.
Expand Down
20 changes: 8 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
mavenLocal()
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
url 'https://plugins.gradle.org/m2/'
}
}
configurations.classpath {
Expand All @@ -13,7 +13,7 @@ buildscript {
}
}
dependencies {
classpath "org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.6.3"
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.6.3'
}
}

Expand Down Expand Up @@ -83,20 +83,16 @@ configure(javaProjects()) {
}
}

wrapper {
gradleVersion = '4.10.2'
}


def publishedProjects = javaProjects()

task jacocoRootReport(type: JacocoReport, group: 'Coverage reports') {
description = 'Generates an aggregate report from all subprojects'
description = 'Generates an aggregate report from all sub-projects'
dependsOn(publishedProjects.test)
additionalSourceDirs = files(publishedProjects.sourceSets.main.allSource.srcDirs)
sourceDirectories = files(publishedProjects.sourceSets.main.allSource.srcDirs)
classDirectories = files(publishedProjects.sourceSets.main.output)
executionData = files(publishedProjects.jacocoTestReport.executionData)

additionalSourceDirs.from(files(publishedProjects.sourceSets.main.allSource.srcDirs))
sourceDirectories.from(files(publishedProjects.sourceSets.main.allSource.srcDirs))
classDirectories.from(files(publishedProjects.sourceSets.main.output))
executionData.from(files(publishedProjects.jacocoTestReport.executionData))
doFirst {
executionData = files(executionData.findAll { it.exists() })
}
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
VERSION = 1.0.5
systemProp.jdk.tls.client.protocols="TLSv1.2"
2 changes: 1 addition & 1 deletion gradle/libraries.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// build a map of the dependency artifacts to use. Allows centralized definition of the version of artifacts to
// use. In that respect it serves a role similar to <dependencyManagement> in Maven
ext {
lombokVersion = '1.16.18'
lombokVersion = '1.18.4'
junitVersion = '4.12'
assertjVersion = '3.11.1'
wiremockVersion = '2.20.0'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.2.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
8 changes: 5 additions & 3 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ include "shcf4j-api",
"shcf4j-examples",
"shcf4j-tests"

if (JavaVersion.current().java11Compatible){
include "shcf4j-java-http-client"
}

rootProject.children.each { project ->
String fileBaseName = project.name.replaceAll("\\p{Upper}") { "-${it.toLowerCase()}" }
project.buildFileName = "${fileBaseName}.gradle"
}

enableFeaturePreview('STABLE_PUBLISHING')
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public AsyncHttpClientBuilder setThreadFactory(ThreadFactory threadFactory) {
public AsyncHttpClientBuilder setDefaultSocketConfig(IOReactorConfig ioReactorConfig) {
Objects.requireNonNull(ioReactorConfig, "ioReactorConfig");
this.configBuilder.setConnectTimeout(ioReactorConfig.getConnectTimeoutMilliseconds());
this.configBuilder.setRequestTimeout(ioReactorConfig.getSoTimeoutMilliseconds());
this.configBuilder.setSoLinger(ioReactorConfig.getSoLingerSeconds());
this.configBuilder.setSoRcvBuf(ioReactorConfig.getRcvBufSize());
this.configBuilder.setSoSndBuf(ioReactorConfig.getSndBufSize());
Expand Down Expand Up @@ -75,13 +76,16 @@ public AsyncHttpClientBuilder setSSLSessionStrategy(SSLSessionStrategy strategy)

@Override
public AsyncHttpClientBuilder setDefaultRequestConfig(RequestConfig config) {
this.configBuilder.setRequestTimeout(config.getSocketTimeoutMilliseconds());
this.configBuilder.setConnectTimeout(config.getConnectTimeoutMilliseconds());
this.configBuilder.setReadTimeout(config.getSocketTimeoutMilliseconds());

if (config.getProxy() != null) {
HttpHost proxy = config.getProxy();
setProxy(proxy);
}
handleCookieSpec(config.getCookieSpec());
this.configBuilder.setMaxRedirects(config.getMaxRedirects());
this.configBuilder.setFollowRedirect(config.isRedirectsEnabled());

return this;
}
Expand All @@ -94,12 +98,13 @@ public AsyncHttpClientBuilder setProxy(HttpHost proxy) {

@Override
public AsyncHttpClientBuilder setDefaultCredentialsProvider(CredentialsProvider cp) {
Objects.requireNonNull(cp, "cp");
this.configBuilder.setRealm(ConversionUtils.convert(cp).get(0));
return this;
}

@Override
public AsyncHttpClientBuilder addRequestInterceptor(Consumer<MutableHttpRequest> interceptor) {

this.configBuilder.addRequestFilter(new RequestFilterInterceptor(interceptor));
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@
import com.imperva.shcf4j.request.body.multipart.InputStreamPart;
import com.imperva.shcf4j.request.body.multipart.Part;
import com.imperva.shcf4j.request.body.multipart.StringPart;
import org.asynchttpclient.Realm;
import org.asynchttpclient.RequestBuilder;
import org.asynchttpclient.Response;

import java.io.IOException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;


/**
Expand Down Expand Up @@ -54,7 +52,7 @@ public CompletableFuture<HttpResponse> execute(HttpHost target, HttpRequest requ
public CompletableFuture<HttpResponse> execute(HttpHost target, HttpRequest request, ClientContext ctx) {


String fullUri = target.getSchemeName() + "://" + target.getHostname() + ":" + target.getPort() + request.getUri().toASCIIString();
String fullUri = target.toURI() + request.getUri().toASCIIString();
RequestBuilder builder = new RequestBuilder(request.getHttpMethod().name());
builder.setUri(org.asynchttpclient.uri.Uri.create(fullUri));
handleClientContext(ctx, builder);
Expand Down Expand Up @@ -116,39 +114,7 @@ private static void handleRequestConfig(RequestConfig rc, RequestBuilder builder

private static void handleCredentialsProvider(CredentialsProvider cp, RequestBuilder builder) {
if (cp != null) {
List<Realm> lRealms =
cp.getCredentials()
.entrySet()
.stream()
.map(e -> {
Realm.Builder realm = new Realm.Builder(
e.getValue().getUserPrincipal().getName(),
e.getValue().getPassword());
if (e.getKey().getScheme() != null) {
switch (e.getKey().getScheme().toUpperCase()) {
case "BASIC":
realm.setScheme(Realm.AuthScheme.BASIC);
break;
case "DIGEST":
realm.setScheme(Realm.AuthScheme.DIGEST);
break;
case "NTLM":
realm.setScheme(Realm.AuthScheme.NTLM);
// realm.setNtlmDomain()
break;
case "SPNEGO":
realm.setScheme(Realm.AuthScheme.SPNEGO);
break;
case "KERBEROS":
realm.setScheme(Realm.AuthScheme.KERBEROS);
break;
}
} else { // Default scheme
realm.setScheme(Realm.AuthScheme.BASIC);
}
return realm.build();
}).collect(Collectors.toList());
builder.setRealm(lRealms.get(0));
builder.setRealm(ConversionUtils.convert(cp).get(0));
}
}

Expand Down Expand Up @@ -202,7 +168,7 @@ private static void handleMultiPart(List<Part> parts, RequestBuilder builder) {

partBase.setDispositionType(p.getDispositionType());

for (Header h : p.getCustomHeaders()){
for (Header h : p.getCustomHeaders()) {
partBase.addCustomHeader(h.getName(), h.getValue());
}
builder.addBodyPart(partBase);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,73 @@
package com.imperva.shcf4j.ahc2.client.async;

import com.imperva.shcf4j.Header;
import com.imperva.shcf4j.ProcessingException;
import com.imperva.shcf4j.client.CredentialsProvider;
import io.netty.handler.codec.http.HttpHeaders;
import org.asynchttpclient.Realm;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

class ConversionUtils {



static List<Header> convert(HttpHeaders headers){
static List<Header> convert(HttpHeaders headers) {
return StreamSupport
.stream(headers.spliterator(), false)
.map(e -> Header.builder().name(e.getKey()).name(e.getValue()).build())
.collect(Collectors.toList());
}

static List<? extends Realm> convert(CredentialsProvider cp) {
if (cp != null) {

if (cp.getCredentials().size() > 1) {
throw new ProcessingException("AHC2 supports only single credentials entry");
}

return cp.getCredentials()
.entrySet()
.stream()
.map(e -> {
Realm.Builder realm = new Realm.Builder(
e.getValue().getUserPrincipal().getName(),
e.getValue().getPassword());
if (e.getKey().getScheme() != null) { // Auth scheme
switch (e.getKey().getScheme().toUpperCase()) {
case "BASIC":
realm.setScheme(Realm.AuthScheme.BASIC);
break;
case "DIGEST":
realm.setScheme(Realm.AuthScheme.DIGEST);
break;
case "NTLM":
realm.setScheme(Realm.AuthScheme.NTLM);
break;
case "SPNEGO":
realm.setScheme(Realm.AuthScheme.SPNEGO);
break;
case "KERBEROS":
realm.setScheme(Realm.AuthScheme.KERBEROS);
break;
}
} else { // Default scheme
realm.setScheme(Realm.AuthScheme.BASIC);
}

if (e.getKey().getRealm() != null) { // Auth realm name
realm.setRealmName(e.getKey().getRealm());
}

// Auth hostname
// Auth port

return realm.build();
}).collect(Collectors.toList());
} else {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.imperva.shcf4j.ahc2.client.config;

import com.imperva.shcf4j.AsyncClientBaseTest;
import com.imperva.shcf4j.config.HttpClientBuilderConfigTest;

public class AsyncAhcClientBuilderConfigTest extends HttpClientBuilderConfigTest implements AsyncClientBaseTest {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.imperva.shcf4j.ahc2.client;
package com.imperva.shcf4j.ahc2.client.config;

import com.imperva.shcf4j.AsyncClientBaseTest;
import com.imperva.shcf4j.HttpRequestBuilder;
Expand Down
2 changes: 2 additions & 0 deletions shcf4j-api/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.stopBubbling = true
lombok.addLombokGeneratedAnnotation = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.imperva.shcf4j;

public class HttpHeaderNames {

public static final String CONTENT_TYPE = "content-type";
public static final String CONTENT_LENGTH = "content-length";
public static final String CONTENT_ENCODING = "content-encoding";

}
17 changes: 14 additions & 3 deletions shcf4j-api/src/main/java/com/imperva/shcf4j/HttpHost.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@

import lombok.*;

import java.net.URI;

@Builder
@Value
public class HttpHost {

public static final String DEFAULT_SCHEME_NAME = "http";
static final String DEFAULT_SCHEME_NAME = "http";
static final String DEFAULT_HOSTNAME = "localhost";
static final int DEFAULT_PORT = 80;

private static final String URI_PATTERN = "%s://%s:%d";

public URI toURI(){
return URI.create(String.format(URI_PATTERN, getSchemeName(), getHostname(), getPort()));
}

@Builder.Default
private final String schemeName = DEFAULT_SCHEME_NAME;

/**
* hostname (IP or DNS name)
*/
private final String hostname;
@Builder.Default
private final String hostname = DEFAULT_HOSTNAME;

/**
* port, or <code>-1</code> if not set
*/
@Builder.Default
private final int port = -1;
private final int port = DEFAULT_PORT;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class ProcessingException extends RuntimeException {

public ProcessingException(String message) {
super(message);
}

public ProcessingException(Throwable cause) {
super(cause);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.imperva.shcf4j.auth;


import lombok.*;
import lombok.Builder;
import lombok.Value;

@Builder
@Value
Expand All @@ -13,7 +14,7 @@ public class AuthScope {
private final String realm;
private final String scheme;

public static AuthScope createAnyAuthScope(){
public static AuthScope createAnyAuthScope() {
return builder().build();
}

Expand Down

0 comments on commit bf51836

Please sign in to comment.