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