001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2012-2014 ForgeRock AS.
015 */
016
017package org.forgerock.openig.util;
018
019// Apache HttpComponents
020
021import java.io.IOException;
022
023import org.apache.http.client.HttpRequestRetryHandler;
024import org.apache.http.protocol.HttpContext;
025import org.forgerock.openig.log.LogLevel;
026import org.forgerock.openig.log.Logger;
027
028/**
029 * A very simple implementation that always returns false for every exception which effectively turns off any
030 * request retries.
031 */
032public class NoRetryHttpRequestRetryHandler implements HttpRequestRetryHandler {
033
034    Logger logger = null;
035
036    /**
037     * Constructs a new <strong>{@code NoRetryHttpRequestRetryHandler}</strong>.
038     *
039     * @param logger The {@code Logger} to use when logging the exception message in {@code retryRequest}
040     */
041    public NoRetryHttpRequestRetryHandler(Logger logger) {
042        this.logger = logger;
043    }
044
045    /**
046     * Log the IOException message (when logger at {@code LogLevel.DEBUG} level) and return false for every request.
047     *
048     * @param e The IOException that triggered this retryRequest
049     * @param i The number of times this retryRequest has been called
050     * @param httpContext The HttpContext for this retryRequest
051     * @return boolean always false in this implementation
052     */
053    @Override
054    public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
055
056        if (logger != null && logger.isLoggable(LogLevel.DEBUG)) {
057            logger.debug("NoRetryHttpRequestRetryHandler.retryRequest: IOException message " + e.getMessage());
058        }
059
060        return false;
061    }
062}