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.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.solr.client.solrj.impl;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.lang.invoke.MethodHandles;
import java.net.CookieHandler;
import java.net.InetSocketAddress;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpTimeoutException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Collection;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.net.ssl.SSLContext;
import org.apache.solr.client.solrj.ResponseParser;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.request.RequestWriter;
import org.apache.solr.client.solrj.util.AsyncListener;
import org.apache.solr.client.solrj.util.Cancellable;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.ContentStream;
import org.apache.solr.common.util.ExecutorUtil;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.ObjectReleaseTracker;
import org.apache.solr.common.util.SolrNamedThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A SolrClient implementation that communicates to a Solr server using the built-in Java 11+ Http
* Client. This client is targeted for those users who wish to minimize application dependencies.
* This client will connect to solr using Http/2 but can seamlessly downgrade to Http/1.1 when
* connecting to Solr hosts running on older versions.
*/
public class HttpJdkSolrClient extends HttpSolrClientBase {
private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private static final String USER_AGENT =
"Solr[" + MethodHandles.lookup().lookupClass().getName() + "] 1.0";
protected HttpClient httpClient;
protected ExecutorService executor;
private boolean forceHttp11;
private final boolean shutdownExecutor;
protected HttpJdkSolrClient(String serverBaseUrl, HttpJdkSolrClient.Builder builder) {
super(serverBaseUrl, builder);
HttpClient.Redirect followRedirects =
Boolean.TRUE.equals(builder.followRedirects)
? HttpClient.Redirect.NORMAL
: HttpClient.Redirect.NEVER;
HttpClient.Builder b = HttpClient.newBuilder().followRedirects(followRedirects);
if (builder.sslContext != null) {
b.sslContext(builder.sslContext);
}
if (builder.executor != null) {
this.executor = builder.executor;
this.shutdownExecutor = false;
} else {
BlockingQueue queue = new LinkedBlockingQueue<>(1024);
this.executor =
new ExecutorUtil.MDCAwareThreadPoolExecutor(
4,
256,
60,
TimeUnit.SECONDS,
queue,
new SolrNamedThreadFactory(this.getClass().getSimpleName()));
this.shutdownExecutor = true;
}
b.executor(this.executor);
if (builder.useHttp1_1) {
this.forceHttp11 = true;
b.version(HttpClient.Version.HTTP_1_1);
}
if (builder.cookieHandler != null) {
b.cookieHandler(builder.cookieHandler);
}
if (builder.proxyHost != null) {
if (builder.proxyIsSocks4) {
log.warn(
"Socks4 is likely not supported by this client. See https://bugs.openjdk.org/browse/JDK-8214516");
}
b.proxy(ProxySelector.of(new InetSocketAddress(builder.proxyHost, builder.proxyPort)));
}
this.httpClient = b.build();
updateDefaultMimeTypeForParser();
assert ObjectReleaseTracker.track(this);
}
@Override
public Cancellable asyncRequest(
SolrRequest> solrRequest,
String collection,
AsyncListener> asyncListener) {
asyncListener.onStart();
CompletableFuture> cf =
requestAsync(solrRequest, collection)
.whenComplete(
(nl, t) -> {
if (t != null) {
asyncListener.onFailure(t);
} else {
asyncListener.onSuccess(nl);
}
});
return new HttpSolrClientCancellable(cf);
}
@Override
public CompletableFuture> requestAsync(
final SolrRequest> solrRequest, String collection) {
try {
PreparedRequest pReq = prepareRequest(solrRequest, collection);
return httpClient
.sendAsync(pReq.reqb.build(), HttpResponse.BodyHandlers.ofInputStream())
.thenApply(
httpResponse -> {
try {
return processErrorsAndResponse(
solrRequest, pReq.parserToUse, httpResponse, pReq.url);
} catch (SolrServerException e) {
throw new RuntimeException(e);
}
});
} catch (Exception e) {
CompletableFuture> cf = new CompletableFuture<>();
cf.completeExceptionally(e);
return cf;
}
}
@Override
public NamedList