Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix some strange http configuration confusion between connect timeout and read timeout #166

Merged
merged 5 commits into from Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -50,7 +50,6 @@
import org.apache.http.conn.ssl.X509HostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.apache.http.impl.conn.DefaultProxyRoutePlanner;
import org.apache.http.impl.conn.DefaultRoutePlanner;
import org.apache.http.impl.conn.DefaultSchemePortResolver;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
Expand Down Expand Up @@ -85,7 +84,6 @@
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -182,15 +180,15 @@ protected void finalize() throws Throwable
}
};

connectionManager.setDefaultMaxPerRoute(options.getMaxConnectionsPerHost());

final RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout((int) options.getConnectionTimeout())
.setConnectionRequestTimeout((int) options.getLeaseTimeout())
.setCookieSpec(options.getIgnoreCookies() ? CookieSpecs.IGNORE_COOKIES : CookieSpecs.DEFAULT)
.setSocketTimeout((int) options.getSocketTimeout())
.build();

connectionManager.setDefaultMaxPerRoute(options.getMaxConnectionsPerHost());

final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom()
.setThreadFactory(ThreadFactories.namedThreadFactory(options.getThreadPrefix() + "-io", ThreadFactories.Type.DAEMON))
.setDefaultIOReactorConfig(reactorConfig)
Expand All @@ -199,26 +197,25 @@ protected void finalize() throws Throwable
.setUserAgent(getUserAgent(options))
.setDefaultRequestConfig(requestConfig);


ProxyConfiguration proxyConfiguration = Jenkins.getInstance().proxy;
if(proxyConfiguration!=null)
{
final HttpHost proxy = new HttpHost( proxyConfiguration.name, proxyConfiguration.port );
//clientBuilder.setProxy( proxy );
if(StringUtils.isNotBlank( proxyConfiguration.getUserName()))
{
clientBuilder.setProxyAuthenticationStrategy( ProxyAuthenticationStrategy.INSTANCE );
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope( proxyConfiguration.name, proxyConfiguration.port),
new UsernamePasswordCredentials(proxyConfiguration.getUserName(),
proxyConfiguration.getPassword()) );
clientBuilder.setDefaultCredentialsProvider( credsProvider );
if(Jenkins.getInstance() != null) {
ProxyConfiguration proxyConfiguration = Jenkins.getInstance().proxy;
if ( proxyConfiguration != null ) {
final HttpHost proxy = new HttpHost( proxyConfiguration.name, proxyConfiguration.port );
//clientBuilder.setProxy( proxy );
if ( StringUtils.isNotBlank( proxyConfiguration.getUserName() ) ) {
clientBuilder.setProxyAuthenticationStrategy( ProxyAuthenticationStrategy.INSTANCE );
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials( new AuthScope( proxyConfiguration.name, proxyConfiguration.port ),
new UsernamePasswordCredentials( proxyConfiguration.getUserName(),
proxyConfiguration.getPassword() ) );
clientBuilder.setDefaultCredentialsProvider( credsProvider );
}

clientBuilder.setRoutePlanner(
new JenkinsProxyRoutePlanner( proxy, proxyConfiguration.getNoProxyHostPatterns() ) );
}

clientBuilder.setRoutePlanner(new JenkinsProxyRoutePlanner(proxy, proxyConfiguration.getNoProxyHostPatterns()));
}


/*
ProxyConfigFactory.getProxyHost(options).foreach(new Effect<HttpHost>()
{
Expand Down
Expand Up @@ -9,11 +9,11 @@
import com.google.common.annotations.VisibleForTesting;
import org.springframework.beans.factory.DisposableBean;

import javax.annotation.Nonnull;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.annotation.Nonnull;

import static com.google.common.base.Preconditions.*;
import static com.google.common.base.Preconditions.checkNotNull;

public final class DefaultHttpClientFactory implements HttpClientFactory, DisposableBean
{
Expand Down
Expand Up @@ -128,7 +128,7 @@ public DefaultRequestBuilder(final HttpClient httpClient)
{
this.httpClient = httpClient;
this.attributes = new HashMap<>();
commonBuilder = new CommonBuilder<DefaultRequest>();
commonBuilder = new CommonBuilder<>();
setAccept("*/*");
contentLength = Option.none();
}
Expand Down
Expand Up @@ -2,11 +2,9 @@

import com.atlassian.fugue.Option;
import com.atlassian.httpclient.api.Response;
import com.google.common.base.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.io.InputStream;
import java.util.Map;

Expand Down Expand Up @@ -163,12 +161,7 @@ public Option<Long> getContentLength()
try
{
Option<Long> parsedLength = Option.some(Long.parseLong(lengthString));
return parsedLength.flatMap(
new Function<Long, Option<Long>>()
{
@Override
public Option<Long> apply(Long aLong)
{
return parsedLength.flatMap( aLong -> {
if (aLong < 0)
{
log.warn("Unable to parse content length. Received out of range value {}", aLong);
Expand All @@ -178,7 +171,6 @@ public Option<Long> apply(Long aLong)
{
return Option.some(aLong);
}
}
});
}
catch (NumberFormatException e)
Expand Down
Expand Up @@ -6,10 +6,9 @@

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.collect.Maps.newHashMap;

public class Headers
{
private final Map<String, String> headers;
Expand All @@ -35,7 +34,7 @@ public String getContentType()

public Map<String, String> getHeaders()
{
Map<String, String> headers = newHashMap(this.headers);
Map<String, String> headers = new HashMap( this.headers);
if (contentType != null)
{
headers.put(Names.CONTENT_TYPE, buildContentType());
Expand Down Expand Up @@ -69,7 +68,7 @@ private String buildContentType()

public static class Builder implements Buildable<Headers>
{
private final Map<String, String> headers = newHashMap();
private final Map<String, String> headers = new HashMap();
private String contentType;
private String contentCharset;

Expand Down
Expand Up @@ -14,7 +14,7 @@
import java.util.concurrent.Executor;
import java.util.concurrent.TimeoutException;

import static com.google.common.base.Preconditions.*;
import static com.google.common.base.Preconditions.checkNotNull;

final class SettableFuturePromiseHttpPromiseAsyncClient<C> implements PromiseHttpAsyncClient
{
Expand All @@ -38,41 +38,20 @@ public Promise<HttpResponse> execute(HttpUriRequest request, HttpContext context
@Override
void doCompleted(final HttpResponse httpResponse)
{
executor.execute(new Runnable()
{
@Override
public void run()
{
future.set(httpResponse);
}
});
executor.execute( () -> future.set(httpResponse));
}

@Override
void doFailed(final Exception ex)
{
executor.execute(new Runnable()
{
@Override
public void run()
{
future.setException(ex);
}
});
executor.execute(() -> future.setException(ex));
}

@Override
void doCancelled()
{
final TimeoutException timeoutException = new TimeoutException();
executor.execute(new Runnable()
{
@Override
public void run()
{
future.setException(timeoutException);
}
});
executor.execute(() -> future.setException(timeoutException));
}
});
return Promises.forListenableFuture(future);
Expand Down Expand Up @@ -118,40 +97,19 @@ private ThreadLocalContextAwareFutureCallback(ThreadLocalContextManager<C> threa
@Override
public final void completed(final HttpResponse response)
{
runInContext(threadLocalContextManager, threadLocalContext, contextClassLoader, new Runnable()
{
@Override
public void run()
{
doCompleted(response);
}
});
runInContext(threadLocalContextManager, threadLocalContext, contextClassLoader, () -> doCompleted(response));
}

@Override
public final void failed(final Exception ex)
{
runInContext(threadLocalContextManager, threadLocalContext, contextClassLoader, new Runnable()
{
@Override
public void run()
{
doFailed(ex);
}
});
runInContext(threadLocalContextManager, threadLocalContext, contextClassLoader, () -> doFailed(ex));
}

@Override
public final void cancelled()
{
runInContext(threadLocalContextManager, threadLocalContext, contextClassLoader, new Runnable()
{
@Override
public void run()
{
doCancelled();
}
});
runInContext(threadLocalContextManager, threadLocalContext, contextClassLoader, () -> doCancelled());
}
}

Expand Down Expand Up @@ -189,14 +147,7 @@ private static final class ThreadLocalDelegateRunnable<C> implements Runnable

public void run()
{
runInContext(manager, context, contextClassLoader, new Runnable()
{
@Override
public void run()
{
delegate.run();
}
});
runInContext(manager, context, contextClassLoader, () -> delegate.run());
}
}
}
5 changes: 2 additions & 3 deletions src/main/java/hudson/plugins/jira/JiraBuildAction.java
@@ -1,10 +1,9 @@
package hudson.plugins.jira;

import java.net.URL;
import java.util.HashSet;
import java.util.Set;

import com.google.common.base.Objects;
import com.google.common.collect.Sets;
import hudson.model.Action;
import hudson.model.Run;
import hudson.plugins.jira.model.JiraIssue;
Expand All @@ -27,7 +26,7 @@ public class JiraBuildAction implements Action {

public JiraBuildAction(@Nonnull Run<?, ?> owner, @Nonnull Set<JiraIssue> issues) {
this.owner = owner;
this.issues = Sets.newHashSet(issues);
this.issues = new HashSet( issues);
}

public String getIconFileName() {
Expand Down
33 changes: 17 additions & 16 deletions src/main/java/hudson/plugins/jira/JiraCreateIssueNotifier.java
Expand Up @@ -25,11 +25,13 @@
import org.kohsuke.stapler.StaplerRequest;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Logger;

import static hudson.plugins.jira.JiraRestService.BUG_ISSUE_TYPE_ID;
Expand Down Expand Up @@ -242,25 +244,21 @@ private void addComment(AbstractBuild<?, ?> build, BuildListener listener, Strin
* @throws InterruptedException
*/
private String getIssue(String filename) throws IOException, InterruptedException {

String issueId = "";
BufferedReader br = null;
try {
Path path = Paths.get(filename);
if(Files.notExists(path)){
return null;
}
try (BufferedReader br = Files.newBufferedReader(path)) {
String issue;
br = new BufferedReader(new FileReader(filename));

while ((issue = br.readLine()) != null) {
issueId = issue;
}
return issueId;
} catch (FileNotFoundException e) {
return null;
} finally {
if (br != null) {
br.close();
}
}

}

JiraSite getSiteForProject(AbstractProject<?, ?> project) {
Expand Down Expand Up @@ -303,14 +301,17 @@ private void deleteFile(String filename) {
/**
* write's the issue id in the file, which is stored in the Job's directory
*
* @param Filename
* @param filename
* @param issue
* @throws FileNotFoundException
*/
private void writeInFile(String Filename, Issue issue) throws FileNotFoundException {
PrintWriter writer = new PrintWriter(Filename);
writer.println(issue.getKey());
writer.close();
private void writeInFile(String filename, Issue issue) throws IOException {
// olamy really weird to write an empty file especially with null
// but backward compat and unit tests assert that.....
// can't believe such stuff has been merged......
try(BufferedWriter bw =Files.newBufferedWriter( Paths.get( filename ) )) {
bw.write(issue.getKey()==null?"null":issue.getKey());
}
}

/**
Expand Down
Expand Up @@ -4,7 +4,6 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.Job;
Expand All @@ -14,7 +13,6 @@
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.BuildWrapperDescriptor;
import jenkins.tasks.SimpleBuildWrapper;

import org.kohsuke.stapler.DataBoundConstructor;

import java.io.IOException;
Expand Down
Expand Up @@ -22,7 +22,7 @@ public class JiraFolderProperty extends AbstractFolderProperty<AbstractFolder<?>
/**
* Hold the JIRA sites configuration.
*/
private final CopyOnWriteList<JiraSite> sites = new CopyOnWriteList<JiraSite>();
private final CopyOnWriteList<JiraSite> sites = new CopyOnWriteList<>();

/**
* Constructor.
Expand Down