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