Example usage for org.apache.solr.client.solrj.impl CloudSolrClient getLbClient

List of usage examples for org.apache.solr.client.solrj.impl CloudSolrClient getLbClient

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl CloudSolrClient getLbClient.

Prototype

public LBHttpSolrClient getLbClient() 

Source Link

Usage

From source file:com.frank.search.solr.server.support.SolrClientUtils.java

License:Apache License

private static SolrClient cloneCloudSolrClient(SolrClient solrClient, String core) {
    if (VersionUtil.isSolr3XAvailable() || solrClient == null) {
        return null;
    }// w w  w  .ja  v  a2  s.  c o  m

    CloudSolrClient cloudServer = (CloudSolrClient) solrClient;
    String zkHost = readField(solrClient, "zkHost");

    Constructor<? extends SolrClient> constructor = (Constructor<? extends SolrClient>) ClassUtils
            .getConstructorIfAvailable(solrClient.getClass(), String.class, LBHttpSolrClient.class);

    CloudSolrClient clone = (CloudSolrClient) BeanUtils.instantiateClass(constructor, zkHost,
            cloneLBHttpSolrClient(cloudServer.getLbClient(), core));

    if (org.springframework.util.StringUtils.hasText(core)) {
        clone.setDefaultCollection(core);
    }
    return clone;
}

From source file:org.apache.sentry.tests.e2e.solr.AbstractSolrSentryTestCase.java

License:Apache License

/**
 * Make a raw http request to specific cluster node.  Node is of the format
 * host:port/context, i.e. "localhost:8983/solr"
 *//*from  w  w w  .j a va  2s  .c o  m*/
protected String makeHttpRequest(CloudSolrClient client, String node, String httpMethod, String path,
        byte[] content, String contentType, int expectedStatusCode) throws Exception {
    HttpClient httpClient = client.getLbClient().getHttpClient();
    URI uri = new URI("http://" + node + path);
    HttpRequestBase method = null;
    if ("GET".equals(httpMethod)) {
        method = new HttpGet(uri);
    } else if ("HEAD".equals(httpMethod)) {
        method = new HttpHead(uri);
    } else if ("POST".equals(httpMethod)) {
        method = new HttpPost(uri);
    } else if ("PUT".equals(httpMethod)) {
        method = new HttpPut(uri);
    } else {
        throw new IOException("Unsupported method: " + method);
    }

    if (method instanceof HttpEntityEnclosingRequestBase) {
        HttpEntityEnclosingRequestBase entityEnclosing = (HttpEntityEnclosingRequestBase) method;
        ByteArrayEntity entityRequest = new ByteArrayEntity(content);
        entityRequest.setContentType(contentType);
        entityEnclosing.setEntity(entityRequest);
    }

    HttpEntity httpEntity = null;
    boolean success = false;
    String retValue = "";
    try {
        final HttpResponse response = httpClient.execute(method);
        httpEntity = response.getEntity();

        assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode());

        if (httpEntity != null) {
            InputStream is = httpEntity.getContent();
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            try {
                IOUtils.copyLarge(is, os);
                os.flush();
            } finally {
                IOUtils.closeQuietly(os);
                IOUtils.closeQuietly(is);
            }
            retValue = os.toString();
        }
        success = true;
    } finally {
        if (!success) {
            EntityUtils.consumeQuietly(httpEntity);
            method.abort();
        }
    }
    return retValue;
}

From source file:org.springframework.data.solr.server.support.SolrClientUtilTests.java

License:Apache License

@Test
public void testClonesCloudSolrClientCorrectly() throws MalformedURLException {
    LBHttpSolrClient lbSolrClient = new LBHttpSolrClient(BASE_URL, ALTERNATE_BASE_URL);
    CloudSolrClient cloudClient = new CloudSolrClient(ZOO_KEEPER_URL, lbSolrClient);

    CloudSolrClient clone = SolrClientUtils.clone(cloudClient);
    Assert.assertEquals(ZOO_KEEPER_URL, ReflectionTestUtils.getField(clone, FIELD_ZOO_KEEPER));

    LBHttpSolrClient lbClone = clone.getLbClient();
    Assert.assertEquals(ReflectionTestUtils.getField(lbSolrClient, FIELD_ALIVE_SERVERS),
            ReflectionTestUtils.getField(lbClone, FIELD_ALIVE_SERVERS));

    assertLBHttpSolrClientProperties(lbSolrClient, lbClone);
}

From source file:org.springframework.data.solr.server.support.SolrClientUtilTests.java

License:Apache License

@Test
@SuppressWarnings("unchecked")
public void testClonesCloudSolrClientForCoreCorrectlyWhenCoreNameIsNotEmpty() throws MalformedURLException {
    LBHttpSolrClient lbSolrClient = new LBHttpSolrClient(BASE_URL, ALTERNATE_BASE_URL);
    CloudSolrClient cloudClient = new CloudSolrClient(ZOO_KEEPER_URL, lbSolrClient);

    CloudSolrClient clone = SolrClientUtils.clone(cloudClient, CORE_NAME);
    Assert.assertEquals(ZOO_KEEPER_URL, ReflectionTestUtils.getField(clone, FIELD_ZOO_KEEPER));

    LBHttpSolrClient lbClone = clone.getLbClient();
    Map<String, ?> aliveServers = (Map<String, ?>) ReflectionTestUtils.getField(lbClone, FIELD_ALIVE_SERVERS);
    Assert.assertThat(aliveServers.keySet(), hasItems(CORE_URL, ALTERNATE_CORE_URL));

    assertLBHttpSolrClientProperties(lbSolrClient, lbClone);
    Assert.assertThat(clone.getDefaultCollection(), equalTo(CORE_NAME));
}

From source file:org.springframework.data.solr.server.support.SolrClientUtilTests.java

License:Apache License

@Test
public void testClonesCloudSolrClientForCoreCorrectlyWhenNoLBHttpServerPresent() throws MalformedURLException {
    CloudSolrClient cloudClient = new CloudSolrClient(ZOO_KEEPER_URL);

    CloudSolrClient clone = SolrClientUtils.clone(cloudClient, CORE_NAME);
    Assert.assertEquals(ZOO_KEEPER_URL, ReflectionTestUtils.getField(clone, FIELD_ZOO_KEEPER));

    LBHttpSolrClient lbClone = clone.getLbClient();

    assertLBHttpSolrClientProperties(cloudClient.getLbClient(), lbClone);
    Assert.assertThat(clone.getDefaultCollection(), equalTo(CORE_NAME));
}