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.*;
018import org.apache.http.client.*;
019import org.apache.http.client.methods.*;
020import org.apache.http.protocol.*;
021
022/**
023 * Interface that allows you to override the handling of HTTP requests.
024 *
025 * <p>
026 * Providing this implementation is the equivalent to overriding the {@link RestClient#execute(HttpHost,HttpRequest,HttpContext)}.
027 * <br>This can also be accomplished by providing your own {@link RestClient.Builder#connectionManager(org.apache.http.conn.HttpClientConnectionManager) connection manager}
028 * or subclassing {@link RestClient}, but this provides a simpler way of handling the requests yourself.
029 *
030 * <p>
031 * The constructor on the implementation class can optionally take in any of the following parameters:
032 * <ul>
033 *    <li>{@link RestClient} - The client using this handler.
034 * </ul>
035 *
036 * <p>
037 * The {@link BasicRestCallHandler} shows an example of a simple pass-through handler.  Note that you must handle
038 * the case where {@link HttpHost} is null.
039 *
040 * <p class='bjava'>
041 *    <jk>public class</jk> BasicRestCallHandler <jk>implements</jk> RestCallHandler {
042 *
043 *       <jk>private final</jk> RestClient <jf>client</jf>;
044 *
045 *       <jk>public</jk> BasicRestCallHandler(RestClient <jv>client</jv>) {
046 *          <jk>this</jk>.<jf>client</jf> = <jv>client</jv>;
047 *       }
048 *
049 *       <ja>@Override</ja>
050 *       <jk>public</jk> HttpResponse run(HttpHost <jv>target</jv>, HttpRequest <jv>request</jv>, HttpContext <jv>context</jv>) <jk>throws</jk> IOException {
051 *          <jk>if</jk> (<jv>target</jv> == <jk>null</jk>)
052 *             <jk>return</jk> <jf>client</jf>.execute((HttpUriRequest)<jv>request</jv>, <jv>context</jv>);
053 *          <jk>return</jk> <jf>client</jf>.execute(<jv>target</jv>, <jv>request</jv>, <jv>context</jv>);
054 *       }
055 *    }
056 * </p>
057 *
058 * <h5 class='section'>See Also:</h5><ul>
059 *    <li class='jm'>{@link RestClient.Builder#callHandler()}
060 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-client">juneau-rest-client</a>
061 * </ul>
062 */
063public interface RestCallHandler {
064
065   //-----------------------------------------------------------------------------------------------------------------
066   // Instance
067   //-----------------------------------------------------------------------------------------------------------------
068
069   /**
070    * Execute the specified request.
071    *
072    * <p>
073    * Subclasses can override this method to provide specialized handling.
074    *
075    * @param target The target host for the request.
076    *    <br>Implementations may accept <jk>null</jk> if they can still determine a route, for example to a default
077    *       target or by inspecting the request.
078    * @param request The request to execute.  Must be an instance of {@link HttpUriRequest} if the target is <jk>null</jk>.
079    * @param context The context to use for the execution, or <jk>null</jk> to use the default context.
080    * @return
081    *    The response to the request.
082    *    <br>This is always a final response, never an intermediate response with an 1xx status code.
083    *    <br>Whether redirects or authentication challenges will be returned or handled automatically depends on the
084    *       implementation and configuration of this client.
085    * @throws IOException In case of a problem or the connection was aborted.
086    * @throws ClientProtocolException In case of an http protocol error.
087    */
088   HttpResponse run(HttpHost target, HttpRequest request, HttpContext context) throws ClientProtocolException, IOException;
089}