1

I am trying to connect SolrCloud using SolrJ API using following code :

  String zkHostString = "localhost:9983"; 
  String USER = "solr"; 
  String PASSWORD = "SolrRocks"; 


  CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); 
  credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, PASSWORD)); 
  CloseableHttpClient httpClient =    HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build(); 

  CloudSolrClient solr = new CloudSolrClient.Builder().withZkHost(zkHostString).withHttpClient(httpClient).build(); 
  ((CloudSolrClient)solr).setDefaultCollection("gettingstarted"); 

But getting Error As :

Exception in thread "main" org.apache.solr.client.solrj.impl.CloudSolrClient$RouteException: IOException occured when talking to server at: http://192.168.0.104:8983/solr/gettingstarted_shard2_replica1 at org.apache.solr.client.solrj.impl.CloudSolrClient.directUpdate(CloudSolrClient.java:767) at org.apache.solr.client.solrj.impl.CloudSolrClient.sendRequest(CloudSolrClient.java:1173) at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:1062) at org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:1004) at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149) at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:173) at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:190) at com.app.graphiti.TextParser.main(TextParser.java:92) Caused by: org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http://192.168.0.104:8983/solr/gettingstarted_shard2_replica1 at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:607) at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:262) at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:251) at org.apache.solr.client.solrj.impl.LBHttpSolrClient.doRequest(LBHttpSolrClient.java:435) at org.apache.solr.client.solrj.impl.LBHttpSolrClient.request(LBHttpSolrClient.java:387) at org.apache.solr.client.solrj.impl.CloudSolrClient.lambda$directUpdate$0(CloudSolrClient.java:742) at java.util.concurrent.FutureTask.run(Unknown Source) at org.apache.solr.common.util.ExecutorUtil$MDCAwareThreadPoolExecutor.lambda$execute$0(ExecutorUtil.java:229) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: org.apache.http.client.ClientProtocolException at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:498) ... 10 more Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity. at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225) at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ... 13 more 16:55:40.289 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x15a3bc76e1f000e after 1ms 16:55:43.624 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x15a3bc76e1f000e after 1ms 16:55:46.958 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x15a3bc76e1f000e after 1ms \

Please help. Vrinda Davda

Jerry Stratton
  • 3,287
  • 1
  • 22
  • 30
vrindavda
  • 341
  • 1
  • 4
  • 5

3 Answers3

2
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password);
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
solrClient = new HttpSolrClient.Builder().withBaseSolrUrl(endpoint).withHttpClient(client).build();
YongJiang Zhang
  • 1,989
  • 2
  • 20
  • 25
1

I believe what's happening here is that SolrJ is using POST to add your document, and is not doing preemptive authentication. This used to be easier, but now appears to require that you use HttpClientBuilder to provision both a CredentialsProvider and an HttpRequestInterceptor that, when necessary, updates the TARGET_AUTH_STATE of the request context with a new BasicScheme and UsernamePasswordCredentials.

I spent most of the day fighting this myself, until I discovered Brian Teggart's March 12, 2015 posting in this thread:

Preemptive Basic authentication with Apache HttpClient 4

This is working for me with the Solr 6.4.1 server and the SolrJ 5.3.1 client.

Community
  • 1
  • 1
Eric Schoen
  • 668
  • 9
  • 16
0

I found a slightly easier way to do this.

You add a request interceptor like this so you do not have to worry about creating a properly configured HttpClient instance yourself. This will just add the interceptor to the default HttpClient that Solrj creates.

org.apache.solr.client.solrj.impl.HttpClientUtil.addRequestInterceptor(new SolrPreemptiveAuthInterceptor());

The RequestInterceptor looks like this:

public class SolrPreemptiveAuthInterceptor implements HttpRequestInterceptor {

    final static Logger log = LoggerFactory.getLogger(SolrPreemptiveAuthInterceptor.class); 

    @Override
    public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
         AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
            // If no auth scheme available yet, try to initialize it preemptively
            if (authState.getAuthScheme() == null) {
                log.info("No AuthState: set Basic Auth");

                HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
                AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());

                CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);

                Credentials creds = credsProvider.getCredentials(authScope);
                if(creds == null){
                    log.info("No Basic Auth credentials: add them");
                    creds = getCredentials(authScope);
                }
                authState.update(new BasicScheme(), creds);
            }

    }


    private Credentials getCredentials(AuthScope authScope) {


        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);

        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(authScope, creds);
        log.info("Creating Basic Auth credentials for user {}", user);

        return credsProvider.getCredentials(authScope);
    }

}
s1m3n
  • 623
  • 6
  • 21