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

Support http client proxy for GlobalHttpConfiguration #426

Merged
merged 1 commit into from
Aug 7, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.commonjava.maven.galley.spi.transport.PublishJob;
import org.commonjava.maven.galley.spi.transport.Transport;
import org.commonjava.maven.galley.transport.htcli.conf.GlobalHttpConfiguration;
import org.commonjava.maven.galley.transport.htcli.conf.HttpJobType;
import org.commonjava.maven.galley.transport.htcli.internal.HttpDownload;
import org.commonjava.maven.galley.transport.htcli.internal.HttpExistence;
import org.commonjava.maven.galley.transport.htcli.internal.HttpListing;
Expand All @@ -49,6 +50,10 @@
import java.net.URL;
import java.util.Map;

import static org.commonjava.maven.galley.transport.htcli.conf.HttpJobType.download;
import static org.commonjava.maven.galley.transport.htcli.conf.HttpJobType.existence;
import static org.commonjava.maven.galley.transport.htcli.conf.HttpJobType.listing;
import static org.commonjava.maven.galley.transport.htcli.conf.HttpJobType.publish;
import static org.commonjava.maven.galley.util.UrlUtils.buildUrl;

@ApplicationScoped
Expand Down Expand Up @@ -111,16 +116,17 @@ public DownloadJob createDownloadJob( final ConcreteResource resource, final Tra
final EventMetadata eventMetadata )
throws TransferException
{
return new HttpDownload( getUrl( resource ), getHttpLocation( resource.getLocation() ), target, transferSizes, eventMetadata,
http, mapper, metricRegistry, metricConfig );
return new HttpDownload( getUrl( resource ), getHttpLocation( resource.getLocation(), download ), target,
transferSizes, eventMetadata, http, mapper, metricRegistry, metricConfig );
}

@Override
public PublishJob createPublishJob( final ConcreteResource resource, final InputStream stream, final long length, final String contentType,
final int timeoutSeconds )
throws TransferException
{
return new HttpPublish( getUrl( resource ), getHttpLocation( resource.getLocation() ), stream, length, contentType, http );
return new HttpPublish( getUrl( resource ), getHttpLocation( resource.getLocation(), publish ), stream, length,
contentType, http );
}

@Override
Expand Down Expand Up @@ -148,24 +154,26 @@ public boolean handles( final Location location )
}

@Override
public ListingJob createListingJob( final ConcreteResource resource, final Transfer target, final int timeoutSeconds )
throws TransferException
public ListingJob createListingJob( final ConcreteResource resource, final Transfer target,
final int timeoutSeconds )
throws TransferException
{
return new HttpListing( getUrl( resource ),
new ConcreteResource( getHttpLocation( resource.getLocation() ), resource.getPath() ),
http );
new ConcreteResource( getHttpLocation( resource.getLocation(), listing ),
resource.getPath() ), http );
}

private HttpLocation getHttpLocation( final Location repository )
throws TransferException
private HttpLocation getHttpLocation( final Location repository, HttpJobType httpJobType )
throws TransferException
{
try
{
return ( repository instanceof HttpLocation ) ? (HttpLocation) repository : new WrapperHttpLocation( repository, globalConfig );
return new WrapperHttpLocation( repository, globalConfig, httpJobType );
}
catch ( final MalformedURLException e )
{
throw new TransferLocationException( repository, "Failed to parse base-URL for: {}", e, repository.getUri() );
throw new TransferLocationException( repository, "Failed to parse base-URL for: {}", e,
repository.getUri() );
}
}

Expand All @@ -174,7 +182,8 @@ public ExistenceJob createExistenceJob( final ConcreteResource resource, final T
final int timeoutSeconds )
throws TransferException
{
return new HttpExistence( getUrl( resource ), getHttpLocation( resource.getLocation() ), target, http, mapper );
return new HttpExistence( getUrl( resource ), getHttpLocation( resource.getLocation(), existence ), target,
http, mapper );
}

private String getUrl( final ConcreteResource resource )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@
*/
package org.commonjava.maven.galley.transport.htcli.conf;

import java.net.URL;


public class GlobalHttpConfiguration
{

public ProxyConfig getProxyConfig( final URL url )
ProxyConfig proxyConfig;

public GlobalHttpConfiguration()
{
}

public GlobalHttpConfiguration( ProxyConfig proxyConfig )
{
// TODO Auto-generated method stub
return null;
this.proxyConfig = proxyConfig;
}

public ProxyConfig getProxyConfig()
{
return proxyConfig;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.commonjava.maven.galley.transport.htcli.conf;

public enum HttpJobType
{
download,
publish,
existence,
listing
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.commonjava.maven.galley.transport.htcli.conf;

import java.util.List;

public interface ProxyConfig
{

Expand All @@ -24,4 +26,5 @@ public interface ProxyConfig

String getUser();

List<String> getAllowHttpJobTypes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.commonjava.maven.galley.model.Location;
import org.commonjava.maven.galley.transport.htcli.conf.GlobalHttpConfiguration;
import org.commonjava.maven.galley.transport.htcli.conf.HttpJobType;
import org.commonjava.maven.galley.transport.htcli.conf.ProxyConfig;
import org.commonjava.maven.galley.transport.htcli.model.HttpLocation;
import org.commonjava.maven.galley.transport.htcli.model.LocationTrustType;
Expand All @@ -26,7 +27,7 @@
import java.util.Map;

public class WrapperHttpLocation
implements HttpLocation
implements HttpLocation
{

private final Location delegate;
Expand All @@ -35,12 +36,16 @@ public class WrapperHttpLocation

private final GlobalHttpConfiguration globalConfig;

public WrapperHttpLocation( final Location delegate, final GlobalHttpConfiguration globalConfig )
throws MalformedURLException
private final HttpJobType httpJobType;

public WrapperHttpLocation( final Location delegate, final GlobalHttpConfiguration globalConfig,
final HttpJobType httpJobType )
throws MalformedURLException
{
this.delegate = delegate;
this.globalConfig = globalConfig;
this.url = new URL( delegate.getUri() );
this.httpJobType = httpJobType;
}

@Override
Expand All @@ -64,32 +69,33 @@ public String getUri()
@Override
public String getKeyCertPem()
{
return null;
return delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getKeyCertPem() : null;
}

@Override
public String getServerCertPem()
{
return null;
return delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getServerCertPem() : null;
}

@Override
public LocationTrustType getTrustType()
{
// FIXME: Is this something we should handle in the global config?
return null;
return delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getTrustType() : null;
}

@Override
public String getHost()
{
return url.getHost();
return delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getHost() : url.getHost();
}

@Override
public int getPort()
{
return url.getPort() < 0 ? url.getDefaultPort() : url.getPort();
int port = url.getPort() < 0 ? url.getDefaultPort() : url.getPort();
return delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getPort() : port;
}

@Override
Expand All @@ -105,34 +111,34 @@ public String getUser()
}
}

return userpass;
return delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getUser() : userpass;
}

@Override
public String getProxyHost()
{
final ProxyConfig proxy = globalConfig == null ? null : globalConfig.getProxyConfig( url );
return proxy == null ? null : proxy.getHost();
ProxyConfig proxy = getProxyConfig();
return isProxyAllowHttpJobType( proxy ) ? proxy.getHost() : null;
}

@Override
public String getProxyUser()
{
final ProxyConfig proxy = globalConfig == null ? null : globalConfig.getProxyConfig( url );
return proxy == null ? null : proxy.getUser();
ProxyConfig proxy = getProxyConfig();
return isProxyAllowHttpJobType( proxy ) ? proxy.getUser() : null;
}

@Override
public int getProxyPort()
{
final ProxyConfig proxy = globalConfig == null ? null : globalConfig.getProxyConfig( url );
return proxy == null ? 8080 : proxy.getPort();
ProxyConfig proxy = getProxyConfig();
return isProxyAllowHttpJobType( proxy ) ? proxy.getPort() : 8080;
}

@Override
public boolean isIgnoreHostnameVerification()
{
return false;
return delegate instanceof HttpLocation && ( (HttpLocation) delegate ).isIgnoreHostnameVerification();
}

@SuppressWarnings( "EqualsWhichDoesntCheckParameterClass" )
Expand Down Expand Up @@ -208,4 +214,13 @@ public String getName()
return delegate.getName();
}

private ProxyConfig getProxyConfig()
{
return globalConfig == null ? null : globalConfig.getProxyConfig();
}

private boolean isProxyAllowHttpJobType( ProxyConfig proxy )
{
return proxy != null && proxy.getAllowHttpJobTypes().contains( httpJobType.name() );
}
}
Loading