View Javadoc
1   package org.apache.maven.wagon.shared.http;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.http.HttpResponse;
23  import org.apache.http.HttpStatus;
24  import org.apache.http.annotation.Contract;
25  import org.apache.http.annotation.ThreadingBehavior;
26  import org.apache.http.client.ServiceUnavailableRetryStrategy;
27  import org.apache.http.protocol.HttpContext;
28  import org.apache.http.util.Args;
29  
30  /**
31   * An implementation of the {@link ServiceUnavailableRetryStrategy} interface.
32   * that retries {@code 408} (Request Timeout), {@code 429} (Too Many Requests),
33   * and {@code 500} (Server side error) responses for a fixed number of times at a fixed interval.
34   */
35  @Contract( threading = ThreadingBehavior.IMMUTABLE )
36  public class StandardServiceUnavailableRetryStrategy implements ServiceUnavailableRetryStrategy
37  {
38      /**
39       * Maximum number of allowed retries if the server responds with a HTTP code
40       * in our retry code list.
41       */
42      private final int maxRetries;
43  
44      /**
45       * Retry interval between subsequent requests, in milliseconds.
46       */
47      private final long retryInterval;
48  
49      public StandardServiceUnavailableRetryStrategy( final int maxRetries, final int retryInterval )
50      {
51          super();
52          Args.positive( maxRetries, "Max retries" );
53          Args.positive( retryInterval, "Retry interval" );
54          this.maxRetries = maxRetries;
55          this.retryInterval = retryInterval;
56      }
57  
58      @Override
59      public boolean retryRequest( final HttpResponse response, final int executionCount, final HttpContext context )
60      {
61          int statusCode = response.getStatusLine().getStatusCode();
62          boolean retryableStatusCode = statusCode == HttpStatus.SC_REQUEST_TIMEOUT
63                  // Too Many Requests ("standard" rate-limiting)
64                  || statusCode == AbstractHttpClientWagon.SC_TOO_MANY_REQUESTS
65                  // Assume server errors are momentary hiccups
66                  || statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR
67                  || statusCode == HttpStatus.SC_BAD_GATEWAY
68                  || statusCode == HttpStatus.SC_SERVICE_UNAVAILABLE
69                  || statusCode == HttpStatus.SC_GATEWAY_TIMEOUT;
70          return executionCount <= maxRetries && retryableStatusCode;
71      }
72  
73      @Override
74      public long getRetryInterval()
75      {
76          return retryInterval;
77      }
78  
79  }