Apache-httpclient-interceptors
Apache HttpClient-インターセプター
インターセプターは、要求または応答を妨害または変更するのに役立つものです。 プロトコルインターセプターは一般に、特定のヘッダーまたは関連するヘッダーのグループに基づいて動作します。 HttpClientライブラリは、インターセプターのサポートを提供します。
リクエストインターセプター
*HttpRequestInterceptor* インターフェースはリクエストインターセプターを表します。 このインターフェイスには、要求をインターセプトするコードチャンクを記述する必要があるプロセスと呼ばれるメソッドが含まれています。
クライアント側では、このメソッドはリクエストを検証/処理してからサーバーに送信し、サーバー側では、リクエストの本文を評価する前にこのメソッドを実行します。
リクエストインターセプターの作成
以下に示す手順に従って、要求インターセプターを作成できます。
- ステップ1-HttpRequestInterceptor *のオブジェクトを作成します
抽象メソッドプロセスを実装して、HttpRequestInterceptorインターフェイスのオブジェクトを作成します。
HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws
HttpException, IOException {
//Method implementation . . . . .
};
ステップ2-CloseableHttpClientオブジェクトのインスタンス化
以下に示すように、上記で作成したインターセプターを追加して、カスタム CloseableHttpClient オブジェクトを構築します-
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient =
HttpClients.custom().addInterceptorFirst(requestInterceptor).build();
このオブジェクトを使用して、通常どおりリクエストの実行を実行できます。
例
次の例は、リクエストインターセプターの使用方法を示しています。 この例では、HTTP GETリクエストオブジェクトを作成し、3つのヘッダーを追加しました:sample-header、demoheader、およびtest-header。
インターセプターの* processor()メソッドでは、送信されたリクエストのヘッダーを検証しています。それらのヘッダーのいずれかが *sample-header である場合、そのヘッダーを削除し、その特定のリクエストのヘッダーのリストを表示しようとしています。
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HttpContext;
public class InterceptorsExample {
public static void main(String args[]) throws Exception{
//Creating an HttpRequestInterceptor
HttpRequestInterceptor requestInterceptor = new HttpRequestInterceptor() {
@Override
public void process(HttpRequest request, HttpContext context) throws
HttpException, IOException {
if(request.containsHeader("sample-header")) {
System.out.println("Contains header sample-header, removing it..");
request.removeHeaders("sample-header");
}
//Printing remaining list of headers
Header[] headers= request.getAllHeaders();
for (int i = 0; i<headers.length;i++) {
System.out.println(headers[i].getName());
}
}
};
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient =
HttpClients.custom().addInterceptorFirst(requestInterceptor).build();
//Creating a request object
HttpGet httpget1 = new HttpGet("https://www.finddevguides.com/");
//Setting the header to it
httpget1.setHeader(new BasicHeader("sample-header","My first header"));
httpget1.setHeader(new BasicHeader("demo-header","My second header"));
httpget1.setHeader(new BasicHeader("test-header","My third header"));
//Executing the request
HttpResponse httpresponse = httpclient.execute(httpget1);
//Printing the status line
System.out.println(httpresponse.getStatusLine());
}
}
出力
上記のプログラムを実行すると、次の出力が生成されます-
Contains header sample-header, removing it..
demo-header
test-header
HTTP/1.1 200 OK
応答インターセプター
*HttpResponseInterceptor* インターフェースは、応答インターセプターを表します。 このインターフェイスには、* process()*と呼ばれるメソッドが含まれています。 このメソッドでは、応答をインターセプトするコードのチャンクを記述する必要があります。
サーバー側では、このメソッドは応答を検証/処理してからクライアントに送信し、クライアント側ではこのメソッドを実行してから応答の本文を評価します。
応答インターセプターの作成
次の手順に従って、応答インターセプターを作成できます-
ステップ1-HttpResponseInterceptorのオブジェクトを作成します
抽象メソッド process を実装して、 HttpResponseInterceptor インターフェイスのオブジェクトを作成します。
HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context) throws HttpException, IOException {
//Method implementation . . . . . . . .
}
};
ステップ2:CloseableHttpClientオブジェクトのインスタンス化
以下に示すように、上記で作成したインターセプターを追加して、カスタム CloseableHttpClient オブジェクトを構築します-
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient =
HttpClients.custom().addInterceptorFirst(responseInterceptor).build();
このオブジェクトを使用して、通常どおりリクエストの実行を実行できます。
例
次の例は、応答インターセプターの使用方法を示しています。 この例では、プロセッサの応答に3つのヘッダー、sample-header、demo-header、およびtest-headerを追加しました。
リクエストを実行してレスポンスを取得した後、* getAllHeaders()*メソッドを使用して、レスポンスのすべてのヘッダーの名前を出力しました。
また、出力では、リスト内の3つのヘッダーの名前を確認できます。
import java.io.IOException;
import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
public class ResponseInterceptorsExample {
public static void main(String args[]) throws Exception{
//Creating an HttpRequestInterceptor
HttpResponseInterceptor responseInterceptor = new HttpResponseInterceptor() {
@Override
public void process(HttpResponse response, HttpContext context) throws
HttpException, IOException {
System.out.println("Adding header sample_header, demo-header, test_header to the response");
response.setHeader("sample-header", "My first header");
response.setHeader("demo-header", "My second header");
response.setHeader("test-header", "My third header");
}
};
//Creating a CloseableHttpClient object
CloseableHttpClient httpclient = HttpClients.custom().addInterceptorFirst(responseInterceptor).build();
//Creating a request object
HttpGet httpget1 = new HttpGet("https://www.finddevguides.com/");
//Executing the request
HttpResponse httpresponse = httpclient.execute(httpget1);
//Printing remaining list of headers
Header[] headers = httpresponse.getAllHeaders();
for (int i = 0; i<headers.length;i++) {
System.out.println(headers[i].getName());
}
}
}
出力
実行すると、上記のプログラムは次の結果を生成します-
On executing the above program generates the following output.
Adding header sample_header, demo-header, test_header to the response
Accept-Ranges
Access-Control-Allow-Headers
Access-Control-Allow-Origin
Cache-Control
Content-Type
Date
Expires
Last-Modified
Server
Vary
X-Cache
sample-header
demo-header
test-header