Java Code Examples for org.apache.solr.client.solrj.impl.HttpSolrClient#getHttpClient()

The following examples show how to use org.apache.solr.client.solrj.impl.HttpSolrClient#getHttpClient() . 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: SolrSchemalessExampleTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testArbitraryJsonIndexing() throws Exception  {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.deleteByQuery("*:*");
  client.commit();
  assertNumFound("*:*", 0); // make sure it got in

  // two docs, one with uniqueKey, another without it
  String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
  HttpClient httpClient = client.getHttpClient();
  HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");
  post.setHeader("Content-Type", "application/json");
  post.setEntity(new InputStreamEntity(
      new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), -1));
  HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
  Utils.consumeFully(response.getEntity());
  assertEquals(200, response.getStatusLine().getStatusCode());
  client.commit();
  assertNumFound("*:*", 2);
}
 
Example 2
Source File: ResponseHeaderTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpResponse() throws SolrServerException, IOException {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  HttpClient httpClient = client.getHttpClient();
  URI uri = URI.create(client.getBaseURL() + "/withHeaders?q=*:*");
  HttpGet httpGet = new HttpGet(uri);
  HttpResponse response = httpClient.execute(httpGet);
  Header[] headers = response.getAllHeaders();
  boolean containsWarningHeader = false;
  for (Header header:headers) {
    if ("Warning".equals(header.getName())) {
      containsWarningHeader = true;
      assertEquals("This is a test warning", header.getValue());
      break;
    }
  }
  assertTrue("Expected header not found", containsWarningHeader);
}
 
Example 3
Source File: HttpSolrClientFactory.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void appendAuthentication(Credentials credentials, String authPolicy, SolrClient solrClient) {
	if (isHttpSolrClient(solrClient)) {
		HttpSolrClient httpSolrClient = (HttpSolrClient) solrClient;

		if (credentials != null && StringUtils.isNotBlank(authPolicy)
				&& assertHttpClientInstance(httpSolrClient.getHttpClient())) {
			AbstractHttpClient httpClient = (AbstractHttpClient) httpSolrClient.getHttpClient();
			httpClient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY), credentials);
			httpClient.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF, Arrays.asList(authPolicy));
		}
	}
}
 
Example 4
Source File: SolrSchemalessExampleTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testFieldMutating() throws Exception {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  client.deleteByQuery("*:*");
  client.commit();
  assertNumFound("*:*", 0); // make sure it got in
  // two docs, one with uniqueKey, another without it
  String json = "{\"name one\": \"name\"} " +
      "{\"name  two\" : \"name\"}" +
      "{\"first-second\" : \"name\"}" +
      "{\"x+y\" : \"name\"}" +
      "{\"p%q\" : \"name\"}" +
      "{\"p.q\" : \"name\"}" +
      "{\"a&b\" : \"name\"}"
      ;
  HttpClient httpClient = client.getHttpClient();
  HttpPost post = new HttpPost(client.getBaseURL() + "/update/json/docs");
  post.setHeader("Content-Type", "application/json");
  post.setEntity(new InputStreamEntity(
      new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), -1));
  HttpResponse response = httpClient.execute(post);
  assertEquals(200, response.getStatusLine().getStatusCode());
  client.commit();
  List<String> expected = Arrays.asList(
      "name_one",
      "name__two",
      "first-second",
      "a_b",
      "p_q",
      "p.q",
      "x_y");
  @SuppressWarnings({"rawtypes"})
  HashSet set = new HashSet();
  QueryResponse rsp = assertNumFound("*:*", expected.size());
  for (SolrDocument doc : rsp.getResults()) set.addAll(doc.getFieldNames());
  for (String s : expected) {
    assertTrue(s+" not created "+ rsp ,set.contains(s) );
  }

}
 
Example 5
Source File: CacheHeaderTestBase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected HttpClient getClient() {
  HttpSolrClient client = (HttpSolrClient) getSolrClient();
  return client.getHttpClient();
}