use of org.apache.http.client.HttpClient in project lucene-solr by apache.
the class SolrClusterReporter method setCoreContainer.
public void setCoreContainer(CoreContainer cc) {
if (reporter != null) {
reporter.close();
;
}
if (!enabled) {
log.info("Reporter disabled for registry " + registryName);
return;
}
// start reporter only in cloud mode
if (!cc.isZooKeeperAware()) {
log.warn("Not ZK-aware, not starting...");
return;
}
if (period < 1) {
// don't start it
log.info("Turning off node reporter, period=" + period);
return;
}
HttpClient httpClient = cc.getUpdateShardHandler().getHttpClient();
ZkController zk = cc.getZkController();
String reporterId = zk.getNodeName();
reporter = SolrReporter.Builder.forReports(metricManager, reports).convertRatesTo(TimeUnit.SECONDS).convertDurationsTo(TimeUnit.MILLISECONDS).withHandler(handler).withReporterId(reporterId).setCompact(true).cloudClient(// we want to send reports specifically to a selected leader instance
false).skipAggregateValues(// we don't want to transport details of aggregates
true).skipHistograms(// we don't want to transport histograms
true).build(httpClient, new OverseerUrlSupplier(zk));
reporter.start(period, TimeUnit.SECONDS);
}
use of org.apache.http.client.HttpClient in project lucene-solr by apache.
the class SolrEntityProcessor method firstInit.
@Override
protected void firstInit(Context context) {
super.firstInit(context);
try {
String serverPath = context.getResolvedEntityAttribute(SOLR_SERVER);
if (serverPath == null) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE, "SolrEntityProcessor: parameter 'url' is required");
}
HttpClient client = getHttpClient();
URL url = new URL(serverPath);
// (wt="javabin|xml") default is javabin
if ("xml".equals(context.getResolvedEntityAttribute(CommonParams.WT))) {
// TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
solrClient = new Builder(url.toExternalForm()).withHttpClient(client).withResponseParser(new XMLResponseParser()).build();
LOG.info("using XMLResponseParser");
} else {
// TODO: it doesn't matter for this impl when passing a client currently, but we should close this!
solrClient = new Builder(url.toExternalForm()).withHttpClient(client).build();
LOG.info("using BinaryResponseParser");
}
} catch (MalformedURLException e) {
throw new DataImportHandlerException(DataImportHandlerException.SEVERE, e);
}
}
use of org.apache.http.client.HttpClient in project lucene-solr by apache.
the class HttpClientUtil method createClient.
public static CloseableHttpClient createClient(final SolrParams params, PoolingHttpClientConnectionManager cm, boolean sharedConnectionManager, HttpRequestExecutor httpRequestExecutor) {
final ModifiableSolrParams config = new ModifiableSolrParams(params);
if (logger.isDebugEnabled()) {
logger.debug("Creating new http client, config:" + config);
}
cm.setMaxTotal(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS, 10000));
cm.setDefaultMaxPerRoute(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 10000));
cm.setValidateAfterInactivity(Integer.getInteger(VALIDATE_AFTER_INACTIVITY, VALIDATE_AFTER_INACTIVITY_DEFAULT));
HttpClientBuilder newHttpClientBuilder = HttpClientBuilder.create();
if (sharedConnectionManager) {
newHttpClientBuilder.setConnectionManagerShared(true);
} else {
newHttpClientBuilder.setConnectionManagerShared(false);
}
ConnectionKeepAliveStrategy keepAliveStrat = new ConnectionKeepAliveStrategy() {
@Override
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
// we only close connections based on idle time, not ttl expiration
return -1;
}
};
if (httpClientBuilder.getAuthSchemeRegistryProvider() != null) {
newHttpClientBuilder.setDefaultAuthSchemeRegistry(httpClientBuilder.getAuthSchemeRegistryProvider().getAuthSchemeRegistry());
}
if (httpClientBuilder.getCookieSpecRegistryProvider() != null) {
newHttpClientBuilder.setDefaultCookieSpecRegistry(httpClientBuilder.getCookieSpecRegistryProvider().getCookieSpecRegistry());
}
if (httpClientBuilder.getCredentialsProviderProvider() != null) {
newHttpClientBuilder.setDefaultCredentialsProvider(httpClientBuilder.getCredentialsProviderProvider().getCredentialsProvider());
}
newHttpClientBuilder.addInterceptorLast(new DynamicInterceptor());
newHttpClientBuilder = newHttpClientBuilder.setKeepAliveStrategy(keepAliveStrat).evictIdleConnections((long) Integer.getInteger(EVICT_IDLE_CONNECTIONS, EVICT_IDLE_CONNECTIONS_DEFAULT), TimeUnit.MILLISECONDS);
if (httpRequestExecutor != null) {
newHttpClientBuilder.setRequestExecutor(httpRequestExecutor);
}
HttpClientBuilder builder = setupBuilder(newHttpClientBuilder, params);
HttpClient httpClient = builder.setConnectionManager(cm).build();
assert ObjectReleaseTracker.track(httpClient);
return (CloseableHttpClient) httpClient;
}
use of org.apache.http.client.HttpClient in project lucene-solr by apache.
the class SolrExampleJettyTest method testArbitraryJsonIndexing.
@Test
public void testArbitraryJsonIndexing() throws Exception {
HttpSolrClient client = (HttpSolrClient) getSolrClient();
client.deleteByQuery("*:*");
client.commit();
// make sure it got in
assertNumFound("*:*", 0);
// two docs, one with uniqueKey, another without it
String json = "{\"id\":\"abc1\", \"name\": \"name1\"} {\"name\" : \"name2\"}";
HttpClient httpClient = client.getHttpClient();
HttpPost post = new HttpPost(getUri(client));
post.setHeader("Content-Type", "application/json");
post.setEntity(new InputStreamEntity(new ByteArrayInputStream(json.getBytes("UTF-8")), -1));
HttpResponse response = httpClient.execute(post, HttpClientUtil.createNewHttpClientRequestContext());
assertEquals(200, response.getStatusLine().getStatusCode());
client.commit();
QueryResponse rsp = getSolrClient().query(new SolrQuery("*:*"));
assertEquals(2, rsp.getResults().getNumFound());
SolrDocument doc = rsp.getResults().get(0);
String src = (String) doc.getFieldValue("_src_");
Map m = (Map) ObjectBuilder.fromJSON(src);
assertEquals("abc1", m.get("id"));
assertEquals("name1", m.get("name"));
doc = rsp.getResults().get(1);
src = (String) doc.getFieldValue("_src_");
m = (Map) ObjectBuilder.fromJSON(src);
assertEquals("name2", m.get("name"));
}
use of org.apache.http.client.HttpClient in project lucene-solr by apache.
the class PKIAuthenticationIntegrationTest method testPkiAuth.
@Test
public void testPkiAuth() throws Exception {
CollectionAdminRequest.createCollection("collection", "conf", 2, 1).process(cluster.getSolrClient());
// TODO make a SolrJ helper class for this
byte[] bytes = Utils.toJSON(makeMap("authorization", singletonMap("class", MockAuthorizationPlugin.class.getName()), "authentication", singletonMap("class", MockAuthenticationPlugin.class.getName())));
zkClient().setData(ZkStateReader.SOLR_SECURITY_CONF_PATH, bytes, true);
HttpClient httpClient = cluster.getSolrClient().getHttpClient();
for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
String baseUrl = jetty.getBaseUrl().toString();
verifySecurityStatus(httpClient, baseUrl + "/admin/authorization", "authorization/class", MockAuthorizationPlugin.class.getName(), 20);
verifySecurityStatus(httpClient, baseUrl + "/admin/authentication", "authentication.enabled", "true", 20);
}
log.info("Starting test");
ModifiableSolrParams params = new ModifiableSolrParams();
params.add("q", "*:*");
params.add("__user", "solr");
params.add("__pwd", "SolrRocks");
// This should work fine.
final AtomicInteger count = new AtomicInteger();
MockAuthorizationPlugin.predicate = new Predicate<AuthorizationContext>() {
@Override
public boolean test(AuthorizationContext context) {
if ("/select".equals(context.getResource())) {
Principal principal = context.getUserPrincipal();
log.info("principalIs : {}", principal);
if (principal != null && principal.getName().equals("solr")) {
count.incrementAndGet();
}
}
return true;
}
};
MockAuthenticationPlugin.predicate = new Predicate<ServletRequest>() {
@Override
public boolean test(ServletRequest servletRequest) {
String s = ((HttpServletRequest) servletRequest).getQueryString();
if (s != null && s.contains("__user=solr") && s.contains("__pwd=SolrRocks")) {
servletRequest.setAttribute(Principal.class.getName(), "solr");
}
return true;
}
};
QueryRequest query = new QueryRequest(params);
query.process(cluster.getSolrClient(), "collection");
assertTrue("all nodes must get the user solr , no:of nodes got solr : " + count.get(), count.get() > 2);
}
Aggregations