001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.client;
018
019/**
020 * Used to intercept http connection responses to allow modification of that response before processing and for
021 * listening for call lifecycle events.
022 *
023 * <p>
024 * The {@link BasicRestCallInterceptor} is provided as an adapter class for implementing this interface.
025 *
026 * <p>
027 * Note that the {@link RestClient} class itself implements this interface so you can achieve the same results by
028 * overriding the methods on the client class as well.
029 *
030 * <h5 class='figure'>Example:</h5>
031 * <p class='bjava'>
032 *    <jc>// Specialized client that adds a header to every request.</jc>
033 *    <jk>public class</jk> MyRestClient <jk>extends</jk> RestClient {
034 *       <ja>@Override</ja>
035 *       <jk>public void</jk> onInit(RestRequest <jv>req</jv>) {
036 *          <jv>req</jv>.header(<js>"Foo"</js>, <js>"bar"</js>);
037 *       }
038 *    }
039 *
040 * <jc>// Instantiate the client.</jc>
041 * MyRestClient <jv>client</jv> = RestClient
042 *    .<jsm>create</jsm>()
043 *    .json()
044 *    .build(MyRestClient.<jk>class</jk>);
045 * </p>
046 *
047 * <h5 class='section'>See Also:</h5><ul>
048 *    <li class='jm'>{@link RestClient.Builder#interceptors(Object...)}
049 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestClientBasics">juneau-rest-client Basics</a>
050 * </ul>
051 */
052public interface RestCallInterceptor {
053
054   /**
055    * Called immediately after {@link RestRequest} object is created and all headers/query/form-data has been
056    * copied from the client to the request object.
057    *
058    * @param req The HTTP request object.
059    * @throws Exception
060    *    Any exception can be thrown.
061    *    <br>If not a {@link RestCallException} or {@link RuntimeException}, will be wrapped in a {@link RestCallException}.
062    */
063   void onInit(RestRequest req) throws Exception;
064
065   /**
066    * Called immediately after an HTTP response has been received.
067    *
068    * @param req The HTTP request object.
069    * @param res The HTTP response object.
070    * @throws Exception
071    *    Any exception can be thrown.
072    *    <br>If not a {@link RestCallException} or {@link RuntimeException}, will be wrapped in a {@link RestCallException}.
073    */
074   void onConnect(RestRequest req, RestResponse res) throws Exception;
075
076   /**
077    * Called when the response body is consumed.
078    *
079    * @param req The request object.
080    * @param res The response object.
081    * @throws RestCallException Error occurred during call.
082    * @throws Exception
083    *    Any exception can be thrown.
084    *    <br>If not a {@link RestCallException} or {@link RuntimeException}, will be wrapped in a {@link RestCallException}.
085    */
086   void onClose(RestRequest req, RestResponse res) throws Exception;
087}