Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2015-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
package org.dbflute.remoteapi;
import java.lang.reflect.Type;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import javax.net.ssl.SSLContext;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.dbflute.optional.OptionalThing;
import org.dbflute.remoteapi.exception.retry.ClientErrorRetryDeterminer;
import org.dbflute.remoteapi.exception.translation.ClientErrorTranslator;
import org.dbflute.remoteapi.logging.SendReceiveLogOption;
import org.dbflute.remoteapi.receiver.ResponseBodyReceiver;
import org.dbflute.remoteapi.sender.body.RequestBodySender;
import org.dbflute.remoteapi.sender.query.QueryParameterSender;
import org.dbflute.remoteapi.validation.SendReceiveValidatorOption;
import org.dbflute.util.DfCollectionUtil;
/**
* The rule of remote API.
* Not thread safe, created per one request.
* @author awane
* @author jflute
*/
public class FlutyRemoteApiRule {
// ===================================================================================
// Attribute
// =========
// -----------------------------------------------------
// Required Rule
// -------------
protected QueryParameterSender queryParameterSender; // null allowed, but required
protected RequestBodySender requestBodySender; // null allowed, but required
protected ResponseBodyReceiver responseBodyReceiver; // null allowed, but required
// -----------------------------------------------------
// Optional Rule
// -------------
// default values are defined here
protected boolean sslUntrusted;
protected int connectTimeout = 3000;
protected int connectionRequestTimeout = 3000;
protected int socketTimeout = 3000;
protected Charset pathVariableCharset = StandardCharsets.UTF_8; // not null
protected Charset queryParameterCharset = StandardCharsets.UTF_8; // not null
protected Charset requestBodyCharset = StandardCharsets.UTF_8; // not null
protected Charset responseBodyCharset = StandardCharsets.UTF_8; // not null
protected Map> headers; // null allowed, not required, lazy-loaded
protected Type failureResponseType; // null allowed, not required
protected ClientErrorTranslator clientErrorTranslator; // null allowed, not required
protected ClientErrorRetryDeterminer clientErrorRetryDeterminer; // null allowed, not required
protected SendReceiveValidatorOption validatorOption = newValidatorOption(); // not null, as default, light instance
protected SendReceiveLogOption sendReceiveLogOption = newSendReceiveLogOption(); // not null, as default, light instance
// #hope jflute can accept response header, interface? mapping? (2017/09/13)
// #hope jflute request trace ID option (2017/09/13)
// #hope jflute improve tracebility like DBFlute (2017/09/13)
// #hope jflute remoteApi call count in request (2017/10/13)
// ===================================================================================
// Http Client
// ===========
// #hope jflute move it to factory (2017/09/27)
public CloseableHttpClient prepareHttpClient() { // not null
if (__xmockHttpClient != null) {
return __xmockHttpClient;
}
final RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(getConnectTimeout());
requestBuilder.setConnectionRequestTimeout(getConnectionRequestTimeout());
requestBuilder.setSocketTimeout(getSocketTimeout());
final HttpClientBuilder httpClientBuilder = HttpClients.custom();
if (isSslUntrusted()) {
final TrustStrategy trustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
final SSLContext sslContext;
try {
sslContext = SSLContexts.custom().loadTrustMaterial(trustStrategy).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new IllegalStateException("Failed to build SSL context: trustStrategy=" + trustStrategy, e);
}
httpClientBuilder.setSSLContext(sslContext).setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
}
return httpClientBuilder.setDefaultRequestConfig(requestBuilder.build()).build();
}
// ===================================================================================
// Setting Facade
// ==============
// -----------------------------------------------------
// Sender/Receiver
// ---------------
/**
* @param queryParameterSender The sender of (request) query parameter. (NotNull)
*/
public void sendQueryBy(QueryParameterSender queryParameterSender) {
assertArgumentNotNull("queryParameterSender", queryParameterSender);
this.queryParameterSender = queryParameterSender;
}
/**
* @param requestBodySender The sender of request body. (NotNull)
*/
public void sendBodyBy(RequestBodySender requestBodySender) {
assertArgumentNotNull("requestBodySender", requestBodySender);
this.requestBodySender = requestBodySender;
}
/**
* @param responseBodyReceiver The receiver of response body. (NotNull)
*/
public void receiveBodyBy(ResponseBodyReceiver responseBodyReceiver) {
assertArgumentNotNull("responseBodyReceiver", responseBodyReceiver);
this.responseBodyReceiver = responseBodyReceiver;
}
// -----------------------------------------------------
// Connection
// ----------
public void setSslUntrusted(boolean sslUntrusted) {
this.sslUntrusted = sslUntrusted;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public void setConnectionRequestTimeout(int connectionRequestTimeout) {
this.connectionRequestTimeout = connectionRequestTimeout;
}
public void setSocketTimeout(int socketTimeout) {
this.socketTimeout = socketTimeout;
}
// -----------------------------------------------------
// Encoding
// --------
/**
* @param pathVariableCharset The charset of request path variable. (NotNull)
*/
public void encodeRequestPathVariableAs(Charset pathVariableCharset) {
assertArgumentNotNull("pathVariableCharset", pathVariableCharset);
this.pathVariableCharset = pathVariableCharset;
}
/**
* @param requestQueryCharset The charset of request query parameter. (NotNull)
*/
public void encodeRequestQueryAs(Charset requestQueryCharset) {
assertArgumentNotNull("requestQueryCharset", requestQueryCharset);
this.queryParameterCharset = requestQueryCharset;
}
/**
* @param requestBodyCharset The charset of request body. (NotNull)
*/
public void encodeRequestBodyAs(Charset requestBodyCharset) {
assertArgumentNotNull("requestBodyCharset", requestBodyCharset);
this.requestBodyCharset = requestBodyCharset;
}
/**
* @param responseBodyCharset The charset of response body. (NotNull)
*/
public void encodeResponseBodyAs(Charset responseBodyCharset) {
assertArgumentNotNull("responseBodyCharset", responseBodyCharset);
this.responseBodyCharset = responseBodyCharset;
}
// -----------------------------------------------------
// HTTP Header
// -----------
/**
* Set header value by the name.
* It overwrites the same-name header if it already exists.
* @param name The name of the header. (NotNull)
* @param value The value of the header. (NotNull)
*/
public void setHeader(String name, String value) {
assertArgumentNotNull("name", name);
assertArgumentNotNull("value", value);
if (headers == null) {
headers = DfCollectionUtil.newLinkedHashMap();
}
headers.put(name, DfCollectionUtil.newArrayList(value));
}
/**
* Add header value by the name.
* It is added as the second-or-more value if the name already exists.
* @param name The name of the header. (NotNull)
* @param value The value of the header, which may be as the second-or-more value. (NotNull)
*/
public void addHeader(String name, String value) {
assertArgumentNotNull("name", name);
assertArgumentNotNull("value", value);
if (headers == null) {
headers = DfCollectionUtil.newLinkedHashMap();
}
List valueList = headers.get(name);
if (valueList == null) {
valueList = DfCollectionUtil.newArrayList();
headers.put(name, valueList);
}
valueList.add(value);
}
// -----------------------------------------------------
// Error Handling
// --------------
/**
* Handle failure response as specified type.
* You can get the failure response from exception.
*