use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class HttpClientFactory method getClient.
/**
* Create a new {@link TransportClient} with the specified properties,
* {@link SSLContext} and {@link SSLParameters}
*
* @param properties map of properties for the {@link TransportClient}
* @param sslContext {@link SSLContext} to be used for requests over SSL/TLS.
* @param sslParameters {@link SSLParameters} to configure secure connections.
* @return an appropriate {@link TransportClient} instance, as specified by the properties.
*/
private TransportClient getClient(Map<String, ? extends Object> properties, SSLContext sslContext, SSLParameters sslParameters) {
LOG.info("Getting a client with configuration {} and SSLContext {}", properties, sslContext);
TransportClient client = getRawClient(properties, sslContext, sslParameters);
List<String> httpRequestServerSupportedEncodings = ConfigValueExtractor.buildList(properties.remove(HTTP_REQUEST_CONTENT_ENCODINGS), LIST_SEPARATOR);
// In the old model, responses were compressed according to the type of method, so clients would send
// the Accept-Encoding header if the method was in HTTP_RESPONSE_COMPRESSION_OPERATIONS.
// In the new model, responses are compressed according to its size, so clients send the Accept-Encoding header
// if the server enabled response compression by setting HTTP_USE_RESPONSE_COMPRESSION to true.
// Until all servers migrate to the new model, clients will understand both models,
// and send the Accept-Encoding header if either the old or the new criterion is satisfied.
List<String> httpResponseCompressionOperations = ConfigValueExtractor.buildList(properties.remove(HTTP_RESPONSE_COMPRESSION_OPERATIONS), LIST_SEPARATOR);
String useResponseCompressionProperty = (String) properties.get(HTTP_USE_RESPONSE_COMPRESSION);
if (useResponseCompressionProperty != null && Boolean.parseBoolean(useResponseCompressionProperty)) {
httpResponseCompressionOperations.add(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR);
}
FilterChain filters = _filters;
if (_useClientCompression) {
List<String> responseEncodings = null;
if (properties.containsKey(HTTP_RESPONSE_CONTENT_ENCODINGS)) {
responseEncodings = ConfigValueExtractor.buildList(properties.remove(HTTP_RESPONSE_CONTENT_ENCODINGS), LIST_SEPARATOR);
}
String httpServiceName = (String) properties.get(HTTP_SERVICE_NAME);
EncodingType restRequestContentEncoding = getRestRequestContentEncoding(httpRequestServerSupportedEncodings);
StreamEncodingType streamRequestContentEncoding = getStreamRequestContentEncoding(httpRequestServerSupportedEncodings);
if (restRequestContentEncoding != EncodingType.IDENTITY || !httpResponseCompressionOperations.isEmpty()) {
filters = filters.addLastRest(new ClientCompressionFilter(restRequestContentEncoding, getRestRequestCompressionConfig(httpServiceName, restRequestContentEncoding), buildRestAcceptEncodingSchemaNames(responseEncodings), _responseCompressionConfigs.get(httpServiceName), httpResponseCompressionOperations));
}
if (streamRequestContentEncoding != StreamEncodingType.IDENTITY || !httpResponseCompressionOperations.isEmpty()) {
CompressionConfig compressionConfig = getStreamRequestCompressionConfig(httpServiceName, streamRequestContentEncoding);
filters = filters.addLast(new ClientStreamCompressionFilter(streamRequestContentEncoding, compressionConfig, buildStreamAcceptEncodingSchemas(responseEncodings), _responseCompressionConfigs.get(httpServiceName), httpResponseCompressionOperations, _compressionExecutor));
}
}
Integer queryPostThreshold = chooseNewOverDefault(getIntValue(properties, HTTP_QUERY_POST_THRESHOLD), Integer.MAX_VALUE);
ClientQueryTunnelFilter clientQueryTunnelFilter = new ClientQueryTunnelFilter(queryPostThreshold);
filters = filters.addLastRest(clientQueryTunnelFilter);
filters = filters.addLast(clientQueryTunnelFilter);
// Add the disruptor filter to the end of the filter chain to get the most accurate simulation of disrupt
Integer requestTimeout = chooseNewOverDefault(getIntValue(properties, HTTP_REQUEST_TIMEOUT), DEFAULT_REQUEST_TIMEOUT);
DisruptFilter disruptFilter = new DisruptFilter(_executor, _eventLoopGroup, requestTimeout);
filters = filters.addLastRest(disruptFilter);
filters = filters.addLast(disruptFilter);
client = new FilterChainClient(client, filters);
client = new FactoryClient(client);
synchronized (_mutex) {
if (!_running) {
throw new IllegalStateException("Factory is shutting down");
}
_clientsOutstanding++;
return client;
}
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class Bootstrap method createHttp2Client.
public static Client createHttp2Client(FilterChain filters, boolean restOverStream) {
HashMap<String, String> properties = new HashMap<>();
properties.put(HttpClientFactory.HTTP_PROTOCOL_VERSION, HttpProtocolVersion.HTTP_2.name());
final TransportClient client = new HttpClientFactory.Builder().setFilterChain(filters).build().getClient(properties);
return new TransportClientAdapter(client, restOverStream);
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class FilterUtil method fireStreamRequestError.
public static void fireStreamRequestError(FilterChain fc, StreamRequest req, Exception ex) {
final RequestContext context = new RequestContext();
wrapFilterChain(fc).onStreamRequest(req, context, emptyWireAttrs());
wrapFilterChain(fc).onStreamError(ex, context, emptyWireAttrs());
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class FilterUtil method fireRestRequestResponse.
// Fires a request, saving the local attributes, and then fires a response with the local
// attributes.
public static void fireRestRequestResponse(FilterChain fc, RestRequest req, RestResponse res) {
final RequestContext context = new RequestContext();
fc.onRestRequest(req, context, emptyWireAttrs());
fc.onRestResponse(res, context, emptyWireAttrs());
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class FilterUtil method fireRestRequestError.
public static void fireRestRequestError(FilterChain fc, RestRequest req, Exception ex) {
final RequestContext context = new RequestContext();
fc.onRestRequest(req, context, emptyWireAttrs());
fc.onRestError(ex, context, emptyWireAttrs());
}
Aggregations