diff --git a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/Http.java b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/Http.java index f4b909811..aba67bad6 100644 --- a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/Http.java +++ b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/Http.java @@ -28,6 +28,9 @@ public interface Http extends Closeable { + CloseableHttpClient createClient( HttpLocation location, boolean isProxy ) + throws GalleyException; + CloseableHttpClient createClient( HttpLocation location ) throws GalleyException; diff --git a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/HttpImpl.java b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/HttpImpl.java index 74f176a7b..6f6e2a53f 100644 --- a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/HttpImpl.java +++ b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/HttpImpl.java @@ -87,18 +87,19 @@ public CloseableHttpClient createClient() @Override public CloseableHttpClient createClient( final HttpLocation location ) throws GalleyException + { + // Default to not use proxy + return createClient( location, false ); + } + + @Override + public CloseableHttpClient createClient( final HttpLocation location, final boolean isProxy ) + throws GalleyException { try { if ( location != null ) { - logger.debug( "The location class: {}", location.getClass().getSimpleName() ); - if ( location instanceof WrapperHttpLocation ) - { - WrapperHttpLocation wrapper = (WrapperHttpLocation) location; - logger.debug( "WrapperHttpLocation:{}, isProxyAllowHttpJobType:{}", wrapper, - wrapper.isProxyAllowHttpJobType() ); - } locationLookup.register( location ); int maxConnections = LocationUtils.getMaxConnections( location ); @@ -106,20 +107,30 @@ public CloseableHttpClient createClient( final HttpLocation location ) configBuilder.withAttributes( location.getAttributes() ) .withKeyCertPem( location.getKeyCertPem() ) .withServerCertPem( location.getServerCertPem() ) - .withProxyHost( location.getProxyHost() ) - .withProxyPort( location.getProxyPort() ) - .withProxyUser( location.getProxyUser() ) .withRequestTimeoutSeconds( LocationUtils.getTimeoutSeconds( location ) ) .withUser( location.getUser() ) .withIgnoreHostnameVerification( location.isIgnoreHostnameVerification() ) .withMaxConnections( maxConnections ); + if ( isProxy ) + { + logger.trace( "The location class: {}", location.getClass().getSimpleName() ); + configBuilder.withProxyHost( location.getProxyHost() ) + .withProxyPort( location.getProxyPort() ) + .withProxyUser( location.getProxyUser() ); + if ( location instanceof WrapperHttpLocation ) + { + WrapperHttpLocation wrapper = (WrapperHttpLocation) location; + logger.debug( + "Proxy with the WrapperHttpLocation config: {}, isGlobalProxyAllowHttpJobType: {}", + wrapper, wrapper.isGlobalProxyAllowHttpJobType() ); + } + } if ( location.getTrustType() != null ) { configBuilder.withTrustType( SiteTrustType.getType( location.getTrustType().name() ) ); } - SiteConfig config = configBuilder.build(); return httpFactory.createClient( config ); diff --git a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/AbstractHttpJob.java b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/AbstractHttpJob.java index 4a18a46ee..8e81f3f33 100644 --- a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/AbstractHttpJob.java +++ b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/AbstractHttpJob.java @@ -101,36 +101,15 @@ public long getTransferSize() protected boolean executeHttp() throws TransferException { + boolean result = true; try { - client = http.createClient( location ); - response = client.execute( request, http.createContext( location ) ); - - final StatusLine line = response.getStatusLine(); - final int sc = line.getStatusCode(); - - logger.trace( "{} {} : {}", request.getMethod(), line, url ); - - if ( sc > 399 && sc != 404 && sc != 408 && sc != 502 && sc != 503 && sc != 504 ) - { - throw new TransferLocationException( location, - "Server misconfigured or not responding normally for url %s: '%s'", - url, line ); - } - else if ( !successStatuses.contains( sc ) ) - { - logger.trace( "Detected failure respon se: " + sc ); - success = TransferResponseUtils.handleUnsuccessfulResponse( request, response, location, url ); - logger.trace( "Returning non-error failure response for code: " + sc ); - return false; - } + result = doExecuteHttp(); } catch ( final NoHttpResponseException | ConnectTimeoutException | SocketTimeoutException e ) { - addFieldToActiveSpan( "target-error-reason", "timeout" ); - addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() ); - throw new TransferTimeoutException( location, url, "Repository remote request failed for: {}. Reason: {}", - e, url, e.getMessage() ); + // For those are not in the iad2 tenant egress rules, will throw timeout so use the configured proxy to retry + result = executeProxyHttp(); } catch ( final IOException e ) { @@ -171,6 +150,75 @@ else if ( !successStatuses.contains( sc ) ) } } + return result; + } + + protected boolean executeProxyHttp() + throws TransferException + { + try + { + return doExecuteHttp( true ); + } + catch ( final NoHttpResponseException | ConnectTimeoutException | SocketTimeoutException e ) + { + addFieldToActiveSpan( "target-error-reason", "timeout" ); + addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() ); + throw new TransferTimeoutException( location, url, "Repository remote request failed for: {}. Reason: {}", + e, url, e.getMessage() ); + } + catch ( final IOException e ) + { + addFieldToActiveSpan( "target-error-reason", "I/O" ); + addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() ); + throw new TransferLocationException( location, "Repository remote request failed for: {}. Reason: {}", e, + url, e.getMessage() ); + } + catch ( TransferLocationException e ) + { + addFieldToActiveSpan( "target-error-reason", "no transport" ); + addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() ); + throw e; + } + catch ( final GalleyException e ) + { + addFieldToActiveSpan( "target-error-reason", "unknown" ); + addFieldToActiveSpan( "target-error", e.getClass().getSimpleName() ); + throw new TransferException( "Repository remote request failed for: {}. Reason: {}", e, url, + e.getMessage() ); + } + } + + private boolean doExecuteHttp() + throws GalleyException, IOException + { + return doExecuteHttp( false ); + } + + private boolean doExecuteHttp( boolean isProxy ) + throws GalleyException, IOException + { + client = http.createClient( location, isProxy ); + response = client.execute( request, http.createContext( location ) ); + + final StatusLine line = response.getStatusLine(); + final int sc = line.getStatusCode(); + + logger.trace( "{} {} : {}", request.getMethod(), line, url ); + + if ( sc > 399 && sc != 404 && sc != 408 && sc != 502 && sc != 503 && sc != 504 ) + { + throw new TransferLocationException( location, + "Server misconfigured or not responding normally for url %s: '%s'", + url, line ); + } + else if ( !successStatuses.contains( sc ) ) + { + logger.trace( "Detected failure respon se: " + sc ); + success = TransferResponseUtils.handleUnsuccessfulResponse( request, response, location, url ); + logger.trace( "Returning non-error failure response for code: " + sc ); + return false; + } return true; } diff --git a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/model/WrapperHttpLocation.java b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/model/WrapperHttpLocation.java index 4e13240a8..96ff8ff22 100644 --- a/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/model/WrapperHttpLocation.java +++ b/transports/httpclient/src/main/java/org/commonjava/maven/galley/transport/htcli/internal/model/WrapperHttpLocation.java @@ -120,21 +120,24 @@ public String getUser() public String getProxyHost() { GlobalProxyConfig proxy = getGlobalProxyConfig(); - return isProxyAllowHttpJobType() ? proxy.getHost() : null; + String proxyHost = delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getProxyHost() : null; + return isGlobalProxyAllowHttpJobType() ? proxy.getHost() : proxyHost; } @Override public String getProxyUser() { GlobalProxyConfig proxy = getGlobalProxyConfig(); - return isProxyAllowHttpJobType() ? proxy.getUser() : null; + String proxyUser = delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getProxyUser() : null; + return isGlobalProxyAllowHttpJobType() ? proxy.getUser() : proxyUser; } @Override public int getProxyPort() { GlobalProxyConfig proxy = getGlobalProxyConfig(); - return isProxyAllowHttpJobType() ? proxy.getPort() : 8080; + int proxyPort = delegate instanceof HttpLocation ? ( (HttpLocation) delegate ).getProxyPort() : 8080; + return isGlobalProxyAllowHttpJobType() ? proxy.getPort() : proxyPort; } @Override @@ -221,7 +224,7 @@ public GlobalProxyConfig getGlobalProxyConfig() return globalProxyConfig; } - public boolean isProxyAllowHttpJobType() + public boolean isGlobalProxyAllowHttpJobType() { if ( globalProxyConfig == null ) {