@@ -110,15 +110,35 @@ public MavenArtifactDownloader(MavenArtifactCache mavenArtifactCache,
110110 bodyStream = Files .newInputStream (Paths .get (URI .create (uri )));
111111 } else {
112112 HttpSender .Request .Builder request = applyAuthentication (dependency .getRepository (), httpSender .get (uri ));
113- try (HttpSender .Response response = Failsafe .with (retryPolicy ).get (() -> httpSender .send (request .build ()));
114- InputStream body = response .getBody ()) {
115- if (!response .isSuccessful () || body == null ) {
116- onError .accept (new MavenDownloadingException (String .format ("Unable to download dependency %s:%s:%s from %s. Response was %d" ,
117- dependency .getGroupId (), dependency .getArtifactId (), dependency .getVersion (), uri , response .getCode ()), null ,
113+ try {
114+ byte [] responseBytes = null ;
115+ int responseCode ;
116+ try (HttpSender .Response response = Failsafe .with (retryPolicy ).get (() -> httpSender .send (request .build ()));
117+ InputStream body = response .getBody ()) {
118+ responseCode = response .getCode ();
119+ if (response .isSuccessful () && body != null ) {
120+ responseBytes = readAllBytes (body );
121+ }
122+ }
123+ // Fall back to anonymous if authenticated request fails with a 4xx client error
124+ if (responseBytes == null && hasCredentials (dependency .getRepository ()) && responseCode >= 400 && responseCode < 500 ) {
125+ try (HttpSender .Response response = Failsafe .with (retryPolicy ).get (() -> httpSender .send (httpSender .get (uri ).build ()));
126+ InputStream body = response .getBody ()) {
127+ responseCode = response .getCode ();
128+ if (response .isSuccessful () && body != null ) {
129+ responseBytes = readAllBytes (body );
130+ }
131+ }
132+ }
133+ if (responseBytes == null ) {
134+ onError .accept (new MavenDownloadingException (String .format ("Unable to download dependency %s:%s:%s%s from %s. Response was %d" ,
135+ dependency .getGroupId (), dependency .getArtifactId (), dependency .getVersion (),
136+ dependency .getClassifier () == null ? "" : ":" + dependency .getClassifier (),
137+ uri , responseCode ), null ,
118138 dependency .getRequested ().getGav ()));
119139 return null ;
120140 }
121- bodyStream = new ByteArrayInputStream (readAllBytes ( body ) );
141+ bodyStream = new ByteArrayInputStream (responseBytes );
122142 } catch (Throwable t ) {
123143 Throwable cause = t instanceof FailsafeException && t .getCause () != null ? t .getCause () : t ;
124144 throw new MavenDownloadingException ("Unable to download dependency" , cause ,
@@ -130,14 +150,27 @@ public MavenArtifactDownloader(MavenArtifactCache mavenArtifactCache,
130150 }
131151
132152 private HttpSender .Request .Builder applyAuthentication (MavenRepository repository , HttpSender .Request .Builder request ) {
153+ MavenSettings .Server authInfo = serverIdToServer .get (repository .getId ());
154+ if (authInfo != null && authInfo .getConfiguration () != null && authInfo .getConfiguration ().getHttpHeaders () != null ) {
155+ for (MavenSettings .HttpHeader header : authInfo .getConfiguration ().getHttpHeaders ()) {
156+ request .withHeader (header .getName (), header .getValue ());
157+ }
158+ }
159+ String [] credentials = resolveCredentials (repository );
160+ if (credentials != null ) {
161+ return request .withBasicAuthentication (credentials [0 ], credentials [1 ]);
162+ }
163+ return request ;
164+ }
165+
166+ private boolean hasCredentials (MavenRepository repository ) {
167+ return resolveCredentials (repository ) != null ;
168+ }
169+
170+ private String @ Nullable [] resolveCredentials (MavenRepository repository ) {
133171 String username , password ;
134172 MavenSettings .Server authInfo = serverIdToServer .get (repository .getId ());
135173 if (authInfo != null ) {
136- if (authInfo .getConfiguration () != null && authInfo .getConfiguration ().getHttpHeaders () != null ) {
137- for (MavenSettings .HttpHeader header : authInfo .getConfiguration ().getHttpHeaders ()) {
138- request .withHeader (header .getName (), header .getValue ());
139- }
140- }
141174 username = authInfo .getUsername ();
142175 password = authInfo .getPassword ();
143176 } else {
@@ -146,8 +179,8 @@ private HttpSender.Request.Builder applyAuthentication(MavenRepository repositor
146179 }
147180 if (username != null && !username .contains ("${" ) &&
148181 password != null && !password .contains ("${" )) {
149- return request . withBasicAuthentication ( username , password ) ;
182+ return new String []{ username , password } ;
150183 }
151- return request ;
184+ return null ;
152185 }
153186}
0 commit comments