Skip to content

Commit

Permalink
Support http client proxy for GlobalHttpConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
yma96 committed Aug 5, 2024
1 parent 4250d24 commit f090f35
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 34 deletions.
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() );
}
}

0 comments on commit f090f35

Please sign in to comment.