001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client;
014
015import java.io.*;
016
017import org.apache.http.impl.client.*;
018import org.apache.http.protocol.*;
019
020/**
021 * An extension of {@link StandardHttpRequestRetryHandler} that adds support for a retry interval.
022 *
023 * <h5 class='section'>See Also:</h5><ul>
024 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-client">juneau-rest-client</a>
025 * </ul>
026 */
027public class BasicHttpRequestRetryHandler extends StandardHttpRequestRetryHandler {
028
029   private final int retryInterval;
030
031   /**
032    * Create the request retry handler.
033    *
034    * Uses the following list of non-retryable IOException classes:
035    * <ul>
036    * <li>InterruptedIOException</li>
037    * <li>UnknownHostException</li>
038    * <li>ConnectException</li>
039    * <li>SSLException</li>
040    * </ul>
041    *
042    * @param retryCount How many times to retry. <code>0</code> means no retries.
043    * @param retryInterval The time in milliseconds to sleep before trying again, or <code>&lt;=0</code> for no interval.
044    * @param requestSentRetryEnabled Specify <jk>true</jk> if it's OK to retry non-idempotent requests that have been sent.
045    */
046   public BasicHttpRequestRetryHandler(int retryCount, int retryInterval, boolean requestSentRetryEnabled) {
047      super(retryCount, requestSentRetryEnabled);
048      this.retryInterval = retryInterval;
049   }
050
051   @Override
052   public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
053      if (retryInterval > 0) {
054         try {
055            Thread.sleep(retryInterval);
056         } catch (InterruptedException e) {
057            e.printStackTrace();
058         }
059      }
060      return super.retryRequest(exception, executionCount, context);
061   }
062}