Skip to content

Commit

Permalink
[MRESOLVER-603] Expose redirect config for apache transport
Browse files Browse the repository at this point in the history
maxRedirects and followRedirects exposed.

---

https://issues.apache.org/jira/browse/MRESOLVER-603
  • Loading branch information
cstamas committed Oct 12, 2024
1 parent 2089216 commit 6d56284
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.LaxRedirectStrategy;
import org.apache.http.impl.client.StandardHttpRequestRetryHandler;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
Expand All @@ -105,10 +106,14 @@

import static java.util.Objects.requireNonNull;
import static org.eclipse.aether.spi.connector.transport.http.HttpConstants.CONTENT_RANGE_PATTERN;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_FOLLOW_REDIRECTS;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_HTTP_RETRY_HANDLER_NAME;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_MAX_REDIRECTS;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.CONFIG_PROP_USE_SYSTEM_PROPERTIES;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_FOLLOW_REDIRECTS;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_MAX_REDIRECTS;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.DEFAULT_USE_SYSTEM_PROPERTIES;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.HTTP_RETRY_HANDLER_NAME_DEFAULT;
import static org.eclipse.aether.transport.apache.ApacheTransporterConfigurationKeys.HTTP_RETRY_HANDLER_NAME_STANDARD;
Expand Down Expand Up @@ -261,6 +266,16 @@ final class ApacheTransporter extends AbstractTransporter implements HttpTranspo
DEFAULT_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED,
CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED + "." + repository.getId(),
CONFIG_PROP_HTTP_RETRY_HANDLER_REQUEST_SENT_ENABLED);
int maxRedirects = ConfigUtils.getInteger(
session,
DEFAULT_MAX_REDIRECTS,
CONFIG_PROP_MAX_REDIRECTS + "." + repository.getId(),
CONFIG_PROP_MAX_REDIRECTS);
boolean followRedirects = ConfigUtils.getBoolean(
session,
DEFAULT_FOLLOW_REDIRECTS,
CONFIG_PROP_FOLLOW_REDIRECTS + "." + repository.getId(),
CONFIG_PROP_FOLLOW_REDIRECTS);
String userAgent = ConfigUtils.getString(
session, ConfigurationProperties.DEFAULT_USER_AGENT, ConfigurationProperties.USER_AGENT);

Expand All @@ -275,6 +290,9 @@ final class ApacheTransporter extends AbstractTransporter implements HttpTranspo
SocketConfig socketConfig =
SocketConfig.custom().setSoTimeout(requestTimeout).build();
RequestConfig requestConfig = RequestConfig.custom()
.setMaxRedirects(maxRedirects)
.setRedirectsEnabled(followRedirects)
.setRelativeRedirectsAllowed(followRedirects)
.setConnectTimeout(connectTimeout)
.setConnectionRequestTimeout(connectTimeout)
.setLocalAddress(getHttpLocalAddress(session, repository))
Expand Down Expand Up @@ -306,6 +324,7 @@ final class ApacheTransporter extends AbstractTransporter implements HttpTranspo

HttpClientBuilder builder = HttpClientBuilder.create()
.setUserAgent(userAgent)
.setRedirectStrategy(LaxRedirectStrategy.INSTANCE)
.setDefaultSocketConfig(socketConfig)
.setDefaultRequestConfig(requestConfig)
.setServiceUnavailableRetryStrategy(serviceUnavailableRetryStrategy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,30 @@ private ApacheTransporterConfigurationKeys() {}
* @configurationType {@link java.lang.String}
*/
public static final String CONFIG_PROP_PROTOCOLS = CONFIG_PROPS_PREFIX + "https.protocols";

/**
* If enabled, Apache HttpClient will follow HTTP redirects.
*
* @configurationSource {@link RepositorySystemSession#getConfigProperties()}
* @configurationType {@link Boolean}
* @configurationDefaultValue {@link #DEFAULT_FOLLOW_REDIRECTS}
* @configurationRepoIdSuffix Yes
* @since 2.0.2
*/
public static final String CONFIG_PROP_FOLLOW_REDIRECTS = CONFIG_PROPS_PREFIX + "followRedirects";

public static final boolean DEFAULT_FOLLOW_REDIRECTS = true;

/**
* The max redirect count to follow.
*
* @configurationSource {@link RepositorySystemSession#getConfigProperties()}
* @configurationType {@link java.lang.Integer}
* @configurationDefaultValue {@link #DEFAULT_MAX_REDIRECTS}
* @configurationRepoIdSuffix Yes
* @since 2.0.2
*/
public static final String CONFIG_PROP_MAX_REDIRECTS = CONFIG_PROPS_PREFIX + "maxRedirects";

public static final int DEFAULT_MAX_REDIRECTS = 5;
}
Loading

0 comments on commit 6d56284

Please sign in to comment.