Solr - instantiate HttpSolrServer with Httpclient - java

I need to connect to our solr server which is behind a proxy(?).
Following I tried (nothing special):
SolrServer server = new HttpSolrServer("https://urltosolr/solr");
try {
SolrPingResponse pingResponse = server.ping();
} catch (SolrServerException e) {
....
}
stacktrace:
org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: https://urltosolr/solr
...
Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
After that I tried the other constructor for HttpSolrServer but I don't know how to set username and password correctly into HttpClient. Can anyone help?

I don't see where you're trying to connect through a proxy in your code, you only provided the solr url while creating the HttpSolrServer. You can provide your own HttpClient instance while creating your HttpSolrServer instance. Your HttpClient instance can contain the information about the proxy. The needed code should be the following, which you can find in the http-components examples:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http");
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
httpclient.getCredentialsProvider().setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
SolrServer solrServer = new HttpSolrServer(solrUrl, httpclient);
Looking more at your question, I don't think you need to connect using a proxy. You error is about https. Have a look at this other question and answers to see what you need to do. The version of httpclient you need to look at is 4.x.

Related

HttpClient : hostname didn't match - accessible from browser but not from code

I am trying to access a website from my code using HttpClient :
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("https://www.datamed.org/search.php?query=gene&searchtype=data");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
This is the error i am getting :
Exception in thread "main" javax.net.ssl.SSLException: hostname in certificate didn't match: <www.datamed.org> != <ucrexdc.ucsd.edu> OR <ucrexdc.ucsd.edu>
I checked the certificate from browser, it seems correct, with correct names.
Not sure from where it is picking up ucrexdc.ucsd.edu .
The code does work if I use a proxy.
Gone through a lot of similar issues on StackOverflow, but in most cases the server was under user's control. In my case, this is an already existing website. and i have this problem only for this website.
Can it be a problem with my environment?
UPDATE:
I found out that both the websites (datamed.org and ucrexdc.ucsd.edu) have the same IP , 169.228.51.21 . Can it be a problem, why doesn't the browser have issues with this?
UPDATE 2:
I was using apache http-client 4.3.1,
When i updated to 4.4.1, it was resolved. the issue was most possibly related to SNI.
HttpClient provides two implementations for Hostname verification.
DefaultHostnameVerifier
NoopHostnameVerifier
by default HttpClient uses DefaultHostnameVerifier implementation. You can try the different hostname verifier implementation.
SSLContext sslContext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
HttpClient httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslsf).build();

Apache Fluent HttpClient behind a proxy results in unknown host

I'm trying to use Apache fluent client with a proxy but I'm getting UnknownHostException for the target host.
Why httpclient is trying to resolve the host using dns directly instead of using the proxy?
Here's my code so far:
String response = Executor.newInstance()
.auth(new HttpHost(proxyHost, proxyPort), "user", "pass")
.authPreemptiveProxy(new HttpHost(proxyHost, proxyPort))
.execute(Request.Get("http://example.com")).returnContent().asString();
I would need somehow to inform httpclient to use dns resolver that is based on the proxy (or don't use it at all, as all it needs is connecting to the proxy server).
Setting auth credentials is not enough. Try using Request#viaProxy to execute the request via a proxy
String response = Executor.newInstance()
.auth(new HttpHost(proxyHost, proxyPort), "user", "pass")
.authPreemptiveProxy(new HttpHost(proxyHost, proxyPort))
.execute(Request.Get("http://example.com").viaProxy(new HttpHost(proxyHost, proxyPort))).returnContent().asString();

Jetty: How to use SSL in Jetty client side

I am using Jetty to develop my client application side.
I am not using Jetty in the server part.
What I need to configure on the client side to be able send "https" request using the Jetty client?
That is what I do for HTTP client:
httpClient = new HttpClient();
// Configure HttpClient
httpClient.setFollowRedirects(false);
httpClient.start();
Request request = httpClient.newRequest(url);
//code
httpClient.stop();
I got this exception if I try to send request using "https":
java.util.concurrent.ExecutionException: java.lang.NullPointerException
at org.eclipse.jetty.client.util.FutureResponseListener.getResult(FutureResponseListener.java:118)
at org.eclipse.jetty.client.util.FutureResponseListener.get(FutureResponseListener.java:101)
at org.eclipse.jetty.client.HttpRequest.send(HttpRequest.java:653)
at egm.httpClient.jetty.TestBackend.POST(TestBackend.java:204)
at egm.httpClient.jetty.TestStep.execute(TestStep.java:77)
at egm.httpClient.jetty.TestSuite.execute(TestSuite.java:57)
at egm.httpClient.jetty.TestLauncher.main(TestLauncher.java:139)
Caused by: java.lang.NullPointerException
at org.eclipse.jetty.io.ssl.SslClientConnectionFactory.newConnection(SslClientConnectionFactory.java:57)
at org.eclipse.jetty.client.AbstractHttpClientTransport$ClientSelectorManager.newConnection(AbstractHttpClientTransport.java:187)
at org.eclipse.jetty.io.ManagedSelector.createEndPoint(ManagedSelector.java:411)
at org.eclipse.jetty.io.ManagedSelector.access$1600(ManagedSelector.java:56)
at org.eclipse.jetty.io.ManagedSelector$CreateEndPoint.run(ManagedSelector.java:587)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.produceAndRun(ExecuteProduceConsume.java:213)
at org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume.execute(ExecuteProduceConsume.java:101)
at org.eclipse.jetty.io.ManagedSelector.run(ManagedSelector.java:136)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:654)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:572)
at java.lang.Thread.run(Unknown Source)
Since jetty-client >= 10
HttpClient supports HTTPS requests out-of-the-box like a browser does.
Prior jetty-client 9
In order to perform HTTPS requests, you should create first a SslContextFactory.Client
You have to pass a SslContextFactory into the HttpClient like this:
HttpClient httpClient = new HttpClient(new SslContextFactory());
You can even configure the SslContextFactory by trusting all or give it a keystore:
new SslContextFactory(true);
new SslContextFactory("/path/to/.keystore");
this is what worked for me with jetty-11
SslContextFactory.Client sslContextFactory = new SslContextFactory.Client();
sslContextFactory.setTrustAll(true); // you might want to think about this first
ClientConnector clientConnector = new ClientConnector();
clientConnector.setSslContextFactory(sslContextFactory);
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
httpClient.start();
for explanation, check out the official documentation here https://www.eclipse.org/jetty/documentation/jetty-11/programming-guide/index.html#pg-client-http-configuration-tls

How to authenticate with httpclient 4.3

I've just upgraded to org.apache.httpcomponents.httpclient 4.3. Previously I created a client and did pre-emptive authentication like this:
final HttpClient client = new DefaultHttpClient();
final HttpRequestBase request = new HttpGet(url);
request.addHeader(BasicScheme.authenticate( new UsernamePasswordCredentials(username,password), "UTF-8", false));
Now that this is deprecated I have to create a client and authenticate like this:
HttpClientBuilder clientBuilder = HttpClientBuilder.create();
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
clientBuilder = clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
However instead of authenticating and making a successful GET I get a 302 in response. I think this could be fixed by setting pre-emptive authentication, but I can't find how. Maybe I have authentication in this second example all wrong?
Can I get some pointers, please?
It's not clear whether this is actually a real problem.
HTTP 302 is a redirection. You need to follow the redirect.

Exception connecting to Remote Solr Instance (407 error)

I have written the code mentioned below.
HttpSolrServer solrServer = new HttpSolrServer("http://10.40.4.171/solr/prime-core");
List<UserSolr> userList=null;
SolrQuery q = new SolrQuery();
q.setQuery("*:*");
q.setStart(0);
q.setRows(10);
//SolrDocumentList results = null;
try {
QueryResponse response = solrServer.query(q);
userList=response.getBeans(UserSolr.class);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e);
}
But for the above I get the following error :-
org.apache.solr.client.solrj.impl.HttpSolrServer$RemoteSolrException: Server at http://10.40.4.171/solr/prime-core returned non ok status:407, message:Proxy Authorization Required
I am not able to resolve this problem. Same code is working fine with the url http://localhost:8080/solr/prime-core Please let me know how to modify to connect to the server without error.
Thanks.
Note:
prime-core is my Solr Core
I am using Solr 4.3
HTTP code 407 implies Proxy Authentication Required, you need to set authentication information while making connection to server via HttpClient:
httpclient.getCredentialsProvider().setCredentials(
AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
solrServer = new HttpSolrServer(solrUrl, httpclient);//with credentials
you may like to check similar discussions:
Solr - instantiate HttpSolrServer with Httpclient
Solr 4 with basic authentication

Categories

Resources