Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solr Change CommonsHttpSolrServer To HttpSolrServer

Tags:

solr

solrj

For Basic Authentication in solr 3.5 I am using the following code,

String url = "http://192.168.192.11:8080/solr/FormResponses";
CommonsHttpSolrServer server = new CommonsHttpSolrServer( url );
String username = "user";
String password = "user123";
Credentials defaultcreds = new UsernamePasswordCredentials(username, password);
server.getHttpClient().getState().setCredentials(AuthScope.ANY, defaultcreds);
server.getHttpClient().getParams().setAuthenticationPreemptive(true);

In solr 4.0 CommonsHttpSolrServer is not available, so I want to replace it with HttpSolrServer. Can anyone help me to fix this?

like image 521
Jayamurugan Avatar asked Dec 21 '12 09:12

Jayamurugan


4 Answers

Change the code as follows :

import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.solr.client.solrj.impl.HttpSolrServer;

String url = "http://192.168.192.11:8080/solr/FormResponses";
HttpSolrServer server = new HttpSolrServer( url );
DefaultHttpClient client = (DefaultHttpClient) server.getHttpClient();

UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials("user", "user123");
client.getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds);

For server.getHttpClient().getParams().setAuthenticationPreemptive(true) in HttpClient 4 you can use the solution described here.

like image 138
Parvin Gasimzade Avatar answered Oct 11 '22 16:10

Parvin Gasimzade


Finally I find the answer my self,

String url = "http://192.168.192.11:8080/solr/FormResponses";
DefaultHttpClient httpclient = new DefaultHttpClient();
    httpclient.getCredentialsProvider().setCredentials(
    AuthScope.ANY, new UsernamePasswordCredentials("user", "user123"));
SolrServer solrServer = new HttpSolrServer(url, httpclient);
like image 39
Jayamurugan Avatar answered Oct 11 '22 16:10

Jayamurugan


You need to add the JAR solr-solrj-4.0.0.jar for HttpClientUtil .

Then use below code :

HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/"+url);     
HttpClientUtil.setBasicAuth((DefaultHttpClient) solrServer.getHttpClient(),
                            "USERNAME", "PASSWORD");

That worked for me.

like image 26
ThmHarsh Avatar answered Oct 11 '22 17:10

ThmHarsh


This is the only way that works for me:

String url = "192.168.192.11:8080/solr/FormResponses";
String username = "user";
String password = "user123";
HttpSolrServer server = new HttpSolrServer("http://" + username + ":" + password + "@" + url);
like image 33
Nadine Avatar answered Oct 11 '22 17:10

Nadine