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