Java Code Examples for org.apache.solr.client.solrj.impl.HttpClientUtil#close()

The following examples show how to use org.apache.solr.client.solrj.impl.HttpClientUtil#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestLBHttpSolrClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void tearDown() throws Exception {
  for (SolrInstance aSolr : solr) {
    if (aSolr != null)  {
      aSolr.tearDown();
    }
  }
  HttpClientUtil.close(httpClient);
  super.tearDown();
}
 
Example 2
Source File: TestLBHttpSolrClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testReliability() throws Exception {
  String[] s = new String[solr.length];
  for (int i = 0; i < solr.length; i++) {
    s[i] = solr[i].getUrl();
  }

  CloseableHttpClient myHttpClient = HttpClientUtil.createClient(null);
  try {
    try (LBHttpSolrClient client = getLBHttpSolrClient(myHttpClient, 500, 500, s)) {
      client.setAliveCheckInterval(500);

      // Kill a server and test again
      solr[1].jetty.stop();
      solr[1].jetty = null;

      // query the servers
      for (String value : s)
        client.query(new SolrQuery("*:*"));

      // Start the killed server once again
      solr[1].startJetty();
      // Wait for the alive check to complete
      waitForServer(30, client, 3, solr[1].name);
    }
  } finally {
    HttpClientUtil.close(myHttpClient);
  }
}
 
Example 3
Source File: SolrExceptionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
// commented out on: 24-Dec-2018   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Sep-2018
public void testSolrException() throws Throwable {
  // test a connection to a solr server that probably doesn't exist
  // this is a very simple test and most of the test should be considered verified 
  // if the compiler won't let you by without the try/catch
  boolean gotExpectedError = false;
  CloseableHttpClient httpClient = null;
  try {
    // switched to a local address to avoid going out on the net, ns lookup issues, etc.
    // set a 1ms timeout to let the connection fail faster.
    httpClient = HttpClientUtil.createClient(null);
    try (HttpSolrClient client = getHttpSolrClient("http://" + SolrTestCaseJ4.DEAD_HOST_1 + "/solr/", httpClient, 1)) {
      SolrQuery query = new SolrQuery("test123");
      client.query(query);
    }
    httpClient.close();
  } catch (SolrServerException sse) {
    gotExpectedError = true;
    /***
    assertTrue(UnknownHostException.class == sse.getRootCause().getClass()
            //If one is using OpenDNS, then you don't get UnknownHostException, instead you get back that the query couldn't execute
            || (sse.getRootCause().getClass() == SolrException.class && ((SolrException) sse.getRootCause()).code() == 302 && sse.getMessage().equals("Error executing query")));
    ***/
  } finally {
    if (httpClient != null) HttpClientUtil.close(httpClient);
  }
  assertTrue(gotExpectedError);
}
 
Example 4
Source File: SolrEntityProcessor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void destroy() {
  try {
    solrClient.close();
  } catch (IOException e) {

  } finally {
    HttpClientUtil.close(((HttpSolrClient) solrClient).getHttpClient());
  }
}
 
Example 5
Source File: IterativeMergeStrategy.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void merge(ResponseBuilder rb, ShardRequest sreq) {
  rb._responseDocs = new SolrDocumentList(); // Null pointers will occur otherwise.
  rb.onePassDistributedQuery = true;   // Turn off the second pass distributed.
  executorService = ExecutorUtil.newMDCAwareCachedThreadPool(new SolrNamedThreadFactory("IterativeMergeStrategy"));
  httpClient = getHttpClient();
  try {
    process(rb, sreq);
  } catch (Exception e) {
    throw new RuntimeException(e);
  } finally {
    HttpClientUtil.close(httpClient);
    executorService.shutdownNow();
  }
}
 
Example 6
Source File: SolrCLI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
public static void closeHttpClient(CloseableHttpClient httpClient) {
  if (httpClient != null) {
    try {
      HttpClientUtil.close(httpClient);
    } catch (Exception exc) {
      // safe to ignore, we're just shutting things down
    }
  }
}
 
Example 7
Source File: SolrCLI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Get the ZooKeeper connection string from either the zkHost command-line option or by looking it
 * up from a running Solr instance based on the solrUrl option.
 */
public static String getZkHost(CommandLine cli) throws Exception {
  String zkHost = cli.getOptionValue("zkHost");
  if (zkHost != null)
    return zkHost;

  // find it using the localPort
  String solrUrl = cli.getOptionValue("solrUrl");
  if (solrUrl == null)
    throw new IllegalStateException(
        "Must provide either the -zkHost or -solrUrl parameters to use the create_collection command!");

  if (!solrUrl.endsWith("/"))
    solrUrl += "/";

  String systemInfoUrl = solrUrl+"admin/info/system";
  CloseableHttpClient httpClient = getHttpClient();
  try {
    // hit Solr to get system info
    Map<String,Object> systemInfo = getJson(httpClient, systemInfoUrl, 2, true);

    // convert raw JSON into user-friendly output
    StatusTool statusTool = new StatusTool();
    Map<String,Object> status = statusTool.reportStatus(solrUrl, systemInfo, httpClient);
    @SuppressWarnings("unchecked")
    Map<String,Object> cloud = (Map<String, Object>)status.get("cloud");
    if (cloud != null) {
      String zookeeper = (String) cloud.get("ZooKeeper");
      if (zookeeper.endsWith("(embedded)")) {
        zookeeper = zookeeper.substring(0, zookeeper.length() - "(embedded)".length());
      }
      zkHost = zookeeper;
    }
  } finally {
    HttpClientUtil.close(httpClient);
  }

  return zkHost;
}
 
Example 8
Source File: PackageTool.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private String getZkHost(CommandLine cli) throws Exception {
  String zkHost = cli.getOptionValue("zkHost");
  if (zkHost != null)
    return zkHost;

  String systemInfoUrl = solrUrl+"/admin/info/system";
  CloseableHttpClient httpClient = SolrCLI.getHttpClient();
  try {
    // hit Solr to get system info
    Map<String,Object> systemInfo = SolrCLI.getJson(httpClient, systemInfoUrl, 2, true);

    // convert raw JSON into user-friendly output
    StatusTool statusTool = new StatusTool();
    Map<String,Object> status = statusTool.reportStatus(solrUrl+"/", systemInfo, httpClient);
    @SuppressWarnings({"unchecked"})
    Map<String,Object> cloud = (Map<String, Object>)status.get("cloud");
    if (cloud != null) {
      String zookeeper = (String) cloud.get("ZooKeeper");
      if (zookeeper.endsWith("(embedded)")) {
        zookeeper = zookeeper.substring(0, zookeeper.length() - "(embedded)".length());
      }
      zkHost = zookeeper;
    }
  } finally {
    HttpClientUtil.close(httpClient);
  }

  return zkHost;
}
 
Example 9
Source File: JWTAuthPluginIntegrationTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testMetrics() throws Exception {
  boolean isUseV2Api = random().nextBoolean();
  String authcPrefix = "/admin/authentication";
  if(isUseV2Api){
    authcPrefix = "/____v2/cluster/security/authentication";
  }
  String baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
  CloseableHttpClient cl = HttpClientUtil.createClient(null);
  
  createCollection(COLLECTION);
  
  // Missing token
  getAndFail(baseUrl + "/" + COLLECTION + "/query?q=*:*", null);
  assertAuthMetricsMinimums(2, 1, 0, 0, 1, 0);
  executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: false}}", jws);
  verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/blockUnknown", "false", 20, jws);
  // Pass through
  verifySecurityStatus(cl, baseUrl + "/admin/info/key", "key", NOT_NULL_PREDICATE, 20);
  // Now succeeds since blockUnknown=false 
  get(baseUrl + "/" + COLLECTION + "/query?q=*:*", null);
  executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: true}}", null);
  verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/blockUnknown", "true", 20, jws);

  assertAuthMetricsMinimums(9, 4, 4, 0, 1, 0);
  
  // Wrong Credentials
  getAndFail(baseUrl + "/" + COLLECTION + "/query?q=*:*", jwtTokenWrongSignature);
  assertAuthMetricsMinimums(10, 4, 4, 1, 1, 0);

  // JWT parse error
  getAndFail(baseUrl + "/" + COLLECTION + "/query?q=*:*", "foozzz");
  assertAuthMetricsMinimums(11, 4, 4, 1, 1, 1);
  
  HttpClientUtil.close(cl);
}
 
Example 10
Source File: TestConfigSetsAPI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void unprotectConfigsHandler() throws Exception {
  HttpClient cl = null;
  try {
    cl = HttpClientUtil.createClient(null);
    zkClient().setData("/security.json", "{}".getBytes(UTF_8), true);
  } finally {
    if (cl != null) {
      HttpClientUtil.close(cl);
    }
  }
  Thread.sleep(1000); // TODO: Without a delay, the test fails. Some problem with Authc/Authz framework?
}
 
Example 11
Source File: TestConfigSetsAPI.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void protectConfigsHandler() throws Exception {
  String authcPrefix = "/admin/authentication";
  String authzPrefix = "/admin/authorization";

  String securityJson = "{\n" +
      "  'authentication':{\n" +
      "    'class':'solr.BasicAuthPlugin',\n" +
      "    'blockUnknown': false,\n" +
      "    'credentials':{'solr':'orwp2Ghgj39lmnrZOTm7Qtre1VqHFDfwAEzr0ApbN3Y= Ju5osoAqOX8iafhWpPP01E5P+sg8tK8tHON7rCYZRRw='}},\n" +
      "  'authorization':{\n" +
      "    'class':'solr.RuleBasedAuthorizationPlugin',\n" +
      "    'user-role':{'solr':'admin'},\n" +
      "    'permissions':[{'name':'security-edit','role':'admin'}, {'name':'config-edit','role':'admin'}]}}";

  HttpClient cl = null;
  try {
    cl = HttpClientUtil.createClient(null);
    JettySolrRunner randomJetty = solrCluster.getRandomJetty(random());
    String baseUrl = randomJetty.getBaseUrl().toString();

    zkClient().setData("/security.json", securityJson.replaceAll("'", "\"").getBytes(UTF_8), true);
    BasicAuthIntegrationTest.verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 50);
    BasicAuthIntegrationTest.verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/class", "solr.RuleBasedAuthorizationPlugin", 50);
  } finally {
    if (cl != null) {
      HttpClientUtil.close(cl);
    }
  }
  Thread.sleep(1000); // TODO: Without a delay, the test fails. Some problem with Authc/Authz framework?
}
 
Example 12
Source File: RestTestHarness.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public void close() throws IOException {
  HttpClientUtil.close(httpClient);
}
 
Example 13
Source File: IndexFetcher.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void destroy() {
  abortFetch();
  HttpClientUtil.close(myHttpClient);
}
 
Example 14
Source File: BasicAuthStandaloneTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testBasicAuth() throws Exception {

  String authcPrefix = "/admin/authentication";
  String authzPrefix = "/admin/authorization";

  HttpClient cl = null;
  HttpSolrClient httpSolrClient = null;
  try {
    cl = HttpClientUtil.createClient(null);
    String baseUrl = buildUrl(jetty.getLocalPort(), "/solr"); 
    httpSolrClient = getHttpSolrClient(baseUrl);
    
    verifySecurityStatus(cl, baseUrl + authcPrefix, "/errorMessages", null, 20);

    // Write security.json locally. Should cause security to be initialized
    securityConfHandler.persistConf(new SecurityConfHandler.SecurityConfig()
        .setData(Utils.fromJSONString(STD_CONF.replaceAll("'", "\""))));
    securityConfHandler.securityConfEdited();
    verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);

    String command = "{\n" +
        "'set-user': {'harry':'HarryIsCool'}\n" +
        "}";

    doHttpPost(cl, baseUrl + authcPrefix, command, null, null, 401);
    verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication.enabled", "true", 20);

    command = "{\n" +
        "'set-user': {'harry':'HarryIsUberCool'}\n" +
        "}";


    doHttpPost(cl, baseUrl + authcPrefix, command, "solr", "SolrRocks");
    verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/credentials/harry", NOT_NULL_PREDICATE, 20);

    // Read file from SOLR_HOME and verify that it contains our new user
    assertTrue(new String(Utils.toJSON(securityConfHandler.getSecurityConfig(false).getData()), 
        Charset.forName("UTF-8")).contains("harry"));

    // Edit authorization
    verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[1]/role", null, 20);
    doHttpPost(cl, baseUrl + authzPrefix, "{'set-permission': {'name': 'update', 'role':'updaterole'}}", "solr", "SolrRocks");
    command = "{\n" +
        "'set-permission': {'name': 'read', 'role':'solr'}\n" +
        "}";
    doHttpPost(cl, baseUrl + authzPrefix, command, "solr", "SolrRocks");
    try {
      httpSolrClient.query("collection1", new MapSolrParams(Collections.singletonMap("q", "foo")));
      fail("Should return a 401 response");
    } catch (Exception e) {
      // Test that the second doPost request to /security/authorization went through
      verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[2]/role", "solr", 20);
    }
  } finally {
    if (cl != null) {
      HttpClientUtil.close(cl);
      httpSolrClient.close();
    }
  }
}