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 org.apache.http.*;
016import org.apache.http.client.*;
017
018/**
019 * Used to determine whether a request should be retried based on the HTTP response code.
020 *
021 * <p>
022 * Subclasses should override either the {@link #onCode(int)} method (if you only care about the HTTP status code)
023 * or {@link #onResponse(HttpResponse)} (if you want full access to the HTTP response object.
024 *
025 * @deprecated Use {@link org.apache.juneau.rest.client2.RestClientBuilder#retryHandler(HttpRequestRetryHandler)}.
026 */
027@Deprecated
028public abstract class RetryOn {
029
030   /**
031    * Default RetryOn that returns <jk>true</jk> of any HTTP response &gt;= 400 is received.
032    */
033   public static final RetryOn DEFAULT = new RetryOn() {
034      @Override /* RetryOn */
035      public boolean onCode(int httpResponseCode) {
036         return httpResponseCode <= 0 || httpResponseCode >= 400;
037      }
038   };
039
040   /**
041    * Default RetryOn that returns <jk>true</jk> if the HTTP connection could not be made.
042    */
043   public static final RetryOn CONNECTION_LOST = new RetryOn() {
044      @Override /* RetryOn */
045      public boolean onCode(int httpResponseCode) {
046         return httpResponseCode <= 0;
047      }
048   };
049
050   /**
051    * Subclasses should override this method to determine whether the HTTP response is retryable.
052    *
053    * @param response The HTTP response object.  May be <jk>null</jk> if a connection could not be made.
054    * @return <jk>true</jk> if the specified response code is retryable.
055    */
056   protected boolean onResponse(HttpResponse response) {
057      return onCode(response == null || response.getStatusLine() == null ? -1 : response.getStatusLine().getStatusCode());
058   }
059
060   /**
061    * Subclasses should override this method to determine whether the HTTP response is retryable.
062    *
063    * @param httpResponseCode The HTTP response code.  <c>-1</c> if a connection could not be made.
064    * @return <jk>true</jk> if the specified response code is retryable.
065    */
066   protected boolean onCode(int httpResponseCode) {
067      return false;
068   }
069}