001/*
002 * ====================================================================
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 * ====================================================================
020 *
021 * This software consists of voluntary contributions made by many
022 * individuals on behalf of the Apache Software Foundation.  For more
023 * information on the Apache Software Foundation, please see
024 * <http://www.apache.org/>.
025 *
026 */
027
028package org.apache.http.client;
029
030import org.apache.http.HttpRequest;
031import org.apache.http.HttpResponse;
032import org.apache.http.ProtocolException;
033import org.apache.http.client.methods.HttpUriRequest;
034import org.apache.http.protocol.HttpContext;
035
036/**
037 * A strategy for determining if an HTTP request should be redirected to
038 * a new location in response to an HTTP response received from the target
039 * server.
040 * <p>
041 * Implementations of this interface must be thread-safe. Access to shared
042 * data must be synchronized as methods of this interface may be executed
043 * from multiple threads.
044 *
045 * @since 4.1
046 */
047public interface RedirectStrategy {
048
049    /**
050     * Determines if a request should be redirected to a new location
051     * given the response from the target server.
052     *
053     * @param request the executed request
054     * @param response the response received from the target server
055     * @param context the context for the request execution
056     *
057     * @return {@code true} if the request should be redirected, {@code false}
058     * otherwise
059     */
060    boolean isRedirected(
061            HttpRequest request,
062            HttpResponse response,
063            HttpContext context) throws ProtocolException;
064
065    /**
066     * Determines the redirect location given the response from the target
067     * server and the current request execution context and generates a new
068     * request to be sent to the location.
069     *
070     * @param request the executed request
071     * @param response the response received from the target server
072     * @param context the context for the request execution
073     *
074     * @return redirected request
075     */
076    HttpUriRequest getRedirect(
077            HttpRequest request,
078            HttpResponse response,
079            HttpContext context) throws ProtocolException;
080
081}