Search in sources :

Example 61 with HttpClient

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);
}
Also used : ZkController(org.apache.solr.cloud.ZkController) HttpClient(org.apache.http.client.HttpClient)

Example 62 with HttpClient

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);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HttpClient(org.apache.http.client.HttpClient) Builder(org.apache.solr.client.solrj.impl.HttpSolrClient.Builder) XMLResponseParser(org.apache.solr.client.solrj.impl.XMLResponseParser) URL(java.net.URL)

Example 63 with HttpClient

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;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 64 with 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"));
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) HttpPost(org.apache.http.client.methods.HttpPost) SolrDocument(org.apache.solr.common.SolrDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) HttpResponse(org.apache.http.HttpResponse) Map(java.util.Map) SolrQuery(org.apache.solr.client.solrj.SolrQuery) InputStreamEntity(org.apache.http.entity.InputStreamEntity) Test(org.junit.Test)

Example 65 with HttpClient

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);
}
Also used : ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(org.apache.http.client.HttpClient) Principal(java.security.Principal) Test(org.junit.Test)

Aggregations

HttpClient (org.apache.http.client.HttpClient)878 HttpResponse (org.apache.http.HttpResponse)548 HttpGet (org.apache.http.client.methods.HttpGet)394 Test (org.junit.Test)273 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)264 IOException (java.io.IOException)258 HttpPost (org.apache.http.client.methods.HttpPost)197 HttpEntity (org.apache.http.HttpEntity)118 URI (java.net.URI)87 InputStream (java.io.InputStream)81 ArrayList (java.util.ArrayList)64 ClientProtocolException (org.apache.http.client.ClientProtocolException)61 InputStreamReader (java.io.InputStreamReader)59 StringEntity (org.apache.http.entity.StringEntity)58 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)56 MockResponse (com.google.mockwebserver.MockResponse)48 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)48 BufferedReader (java.io.BufferedReader)46 URISyntaxException (java.net.URISyntaxException)45