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
015/**
016 * Used to intercept http connection responses to allow modification of that response before processing and for
017 * listening for call lifecycle events.
018 *
019 * <p>
020 * The {@link BasicRestCallInterceptor} is provided as an adapter class for implementing this interface.
021 *
022 * <p>
023 * Note that the {@link RestClient} class itself implements this interface so you can achieve the same results by
024 * overriding the methods on the client class as well.
025 *
026 * <h5 class='figure'>Example:</h5>
027 * <p class='bcode w800'>
028 *    <jc>// Specialized client that adds a header to every request.</jc>
029 *    <jk>public class</jk> MyRestClient <jk>extends</jk> RestClient {
030 *       <ja>@Override</ja>
031 *       <jk>public void</jk> onInit(RestRequest <jv>req</jv>) {
032 *          <jv>req</jv>.header(<js>"Foo"</js>, <js>"bar"</js>);
033 *       }
034 *    }
035 *
036 * <jc>// Instantiate the client.</jc>
037 * MyRestClient <jv>client</jv> = RestClient
038 *    .<jsm>create</jsm>()
039 *    .json()
040 *    .build(MyRestClient.<jk>class</jk>);
041 * </p>
042 *
043 * <ul class='seealso'>
044 *    <li class='jf'>{@link RestClient#RESTCLIENT_interceptors}
045 *    <li class='jm'>{@link RestClientBuilder#interceptors(Object...)}
046 * </ul>
047 */
048public interface RestCallInterceptor {
049
050   /**
051    * Called immediately after {@link RestRequest} object is created and all headers/query/form-data has been
052    * copied from the client to the request object.
053    *
054    * @param req The HTTP request object.
055    * @throws Exception
056    *    Any exception can be thrown.
057    *    <br>If not a {@link RestCallException} or {@link RuntimeException}, will be wrapped in a {@link RestCallException}.
058    */
059   void onInit(RestRequest req) throws Exception;
060
061   /**
062    * Called immediately after an HTTP response has been received.
063    *
064    * @param req The HTTP request object.
065    * @param res The HTTP response object.
066    * @throws Exception
067    *    Any exception can be thrown.
068    *    <br>If not a {@link RestCallException} or {@link RuntimeException}, will be wrapped in a {@link RestCallException}.
069    */
070   void onConnect(RestRequest req, RestResponse res) throws Exception;
071
072   /**
073    * Called when the response body is consumed.
074    *
075    * @param req The request object.
076    * @param res The response object.
077    * @throws RestCallException Error occurred during call.
078    * @throws Exception
079    *    Any exception can be thrown.
080    *    <br>If not a {@link RestCallException} or {@link RuntimeException}, will be wrapped in a {@link RestCallException}.
081    */
082   void onClose(RestRequest req, RestResponse res) throws Exception;
083}