Search in sources :

Example 66 with HttpClient

use of org.apache.http.client.HttpClient in project lucene-solr by apache.

the class ResponseHeaderTest method testHttpResponse.

@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);
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) Header(org.apache.http.Header) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) URI(java.net.URI) Test(org.junit.Test)

Example 67 with HttpClient

use of org.apache.http.client.HttpClient in project lucene-solr by apache.

the class TestPullReplica method testRealTimeGet.

public void testRealTimeGet() throws SolrServerException, IOException, KeeperException, InterruptedException {
    // should be redirected to Replica.Type.NRT
    int numReplicas = random().nextBoolean() ? 1 : 2;
    CollectionAdminRequest.createCollection(collectionName, "conf", 1, numReplicas, 0, numReplicas).setMaxShardsPerNode(100).process(cluster.getSolrClient());
    waitForState("Unexpected replica count", collectionName, activeReplicaCount(numReplicas, 0, numReplicas));
    DocCollection docCollection = assertNumberOfReplicas(numReplicas, 0, numReplicas, false, true);
    HttpClient httpClient = cluster.getSolrClient().getHttpClient();
    int id = 0;
    Slice slice = docCollection.getSlice("shard1");
    List<String> ids = new ArrayList<>(slice.getReplicas().size());
    for (Replica rAdd : slice.getReplicas()) {
        try (HttpSolrClient client = getHttpSolrClient(rAdd.getCoreUrl(), httpClient)) {
            client.add(new SolrInputDocument("id", String.valueOf(id), "foo_s", "bar"));
        }
        SolrDocument docCloudClient = cluster.getSolrClient().getById(collectionName, String.valueOf(id));
        assertEquals("bar", docCloudClient.getFieldValue("foo_s"));
        for (Replica rGet : slice.getReplicas()) {
            try (HttpSolrClient client = getHttpSolrClient(rGet.getCoreUrl(), httpClient)) {
                SolrDocument doc = client.getById(String.valueOf(id));
                assertEquals("bar", doc.getFieldValue("foo_s"));
            }
        }
        ids.add(String.valueOf(id));
        id++;
    }
    SolrDocumentList previousAllIdsResult = null;
    for (Replica rAdd : slice.getReplicas()) {
        try (HttpSolrClient client = getHttpSolrClient(rAdd.getCoreUrl(), httpClient)) {
            SolrDocumentList allIdsResult = client.getById(ids);
            if (previousAllIdsResult != null) {
                assertTrue(compareSolrDocumentList(previousAllIdsResult, allIdsResult));
            } else {
                // set the first response here
                previousAllIdsResult = allIdsResult;
                assertEquals("Unexpected number of documents", ids.size(), allIdsResult.getNumFound());
            }
        }
        id++;
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) SolrDocument(org.apache.solr.common.SolrDocument) Slice(org.apache.solr.common.cloud.Slice) HttpClient(org.apache.http.client.HttpClient) ArrayList(java.util.ArrayList) DocCollection(org.apache.solr.common.cloud.DocCollection) SolrDocumentList(org.apache.solr.common.SolrDocumentList) Replica(org.apache.solr.common.cloud.Replica)

Example 68 with HttpClient

use of org.apache.http.client.HttpClient in project lucene-solr by apache.

the class BasicAuthIntegrationTest method testBasicAuth.

@Test
public void testBasicAuth() throws Exception {
    boolean isUseV2Api = random().nextBoolean();
    String authcPrefix = "/admin/authentication";
    String authzPrefix = "/admin/authorization";
    if (isUseV2Api) {
        authcPrefix = "/____v2/cluster/security/authentication";
        authzPrefix = "/____v2/cluster/security/authorization";
    }
    NamedList<Object> rsp;
    HttpClient cl = null;
    try {
        cl = HttpClientUtil.createClient(null);
        JettySolrRunner randomJetty = cluster.getRandomJetty(random());
        String baseUrl = randomJetty.getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "/errorMessages", null, 20);
        zkClient().setData("/security.json", STD_CONF.replaceAll("'", "\"").getBytes(UTF_8), true);
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);
        randomJetty.stop();
        randomJetty.start(false);
        baseUrl = randomJetty.getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/class", "solr.BasicAuthPlugin", 20);
        String command = "{\n" + "'set-user': {'harry':'HarryIsCool'}\n" + "}";
        final SolrRequest genericReq;
        if (isUseV2Api) {
            genericReq = new V2Request.Builder("/cluster/security/authentication").withMethod(SolrRequest.METHOD.POST).build();
        } else {
            genericReq = new GenericSolrRequest(SolrRequest.METHOD.POST, authcPrefix, new ModifiableSolrParams());
            ((GenericSolrRequest) genericReq).setContentStreams(Collections.singletonList(new ContentStreamBase.ByteArrayStream(command.getBytes(UTF_8), "")));
        }
        HttpSolrClient.RemoteSolrException exp = expectThrows(HttpSolrClient.RemoteSolrException.class, () -> {
            cluster.getSolrClient().request(genericReq);
        });
        assertEquals(401, exp.code());
        command = "{\n" + "'set-user': {'harry':'HarryIsUberCool'}\n" + "}";
        HttpPost httpPost = new HttpPost(baseUrl + authcPrefix);
        setBasicAuthHeader(httpPost, "solr", "SolrRocks");
        httpPost.setEntity(new ByteArrayEntity(command.getBytes(UTF_8)));
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication.enabled", "true", 20);
        HttpResponse r = cl.execute(httpPost);
        int statusCode = r.getStatusLine().getStatusCode();
        Utils.consumeFully(r.getEntity());
        assertEquals("proper_cred sent, but access denied", 200, statusCode);
        baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/credentials/harry", NOT_NULL_PREDICATE, 20);
        command = "{\n" + "'set-user-role': {'harry':'admin'}\n" + "}";
        executeCommand(baseUrl + authzPrefix, cl, command, "solr", "SolrRocks");
        baseUrl = cluster.getRandomJetty(random()).getBaseUrl().toString();
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/user-role/harry", NOT_NULL_PREDICATE, 20);
        executeCommand(baseUrl + authzPrefix, cl, Utils.toJSONString(singletonMap("set-permission", Utils.makeMap("collection", "x", "path", "/update/*", "role", "dev"))), "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[1]/collection", "x", 20);
        executeCommand(baseUrl + authzPrefix, cl, Utils.toJSONString(singletonMap("set-permission", Utils.makeMap("name", "collection-admin-edit", "role", "admin"))), "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authzPrefix, "authorization/permissions[2]/name", "collection-admin-edit", 20);
        CollectionAdminRequest.Reload reload = CollectionAdminRequest.reloadCollection(COLLECTION);
        try (HttpSolrClient solrClient = getHttpSolrClient(baseUrl)) {
            try {
                rsp = solrClient.request(reload);
                fail("must have failed");
            } catch (HttpSolrClient.RemoteSolrException e) {
            }
            reload.setMethod(SolrRequest.METHOD.POST);
            try {
                rsp = solrClient.request(reload);
                fail("must have failed");
            } catch (HttpSolrClient.RemoteSolrException e) {
            }
        }
        cluster.getSolrClient().request(CollectionAdminRequest.reloadCollection(COLLECTION).setBasicAuthCredentials("harry", "HarryIsUberCool"));
        try {
            cluster.getSolrClient().request(CollectionAdminRequest.reloadCollection(COLLECTION).setBasicAuthCredentials("harry", "Cool12345"));
            fail("This should not succeed");
        } catch (HttpSolrClient.RemoteSolrException e) {
        }
        executeCommand(baseUrl + authzPrefix, cl, "{set-permission : { name : update , role : admin}}", "harry", "HarryIsUberCool");
        SolrInputDocument doc = new SolrInputDocument();
        doc.setField("id", "4");
        UpdateRequest update = new UpdateRequest();
        update.setBasicAuthCredentials("harry", "HarryIsUberCool");
        update.add(doc);
        update.setCommitWithin(100);
        cluster.getSolrClient().request(update, COLLECTION);
        executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: true}}", "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication/blockUnknown", "true", 20, "harry", "HarryIsUberCool");
        verifySecurityStatus(cl, baseUrl + "/admin/info/key?wt=json", "key", NOT_NULL_PREDICATE, 20);
        String[] toolArgs = new String[] { "status", "-solr", baseUrl };
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintStream stdoutSim = new PrintStream(baos, true, StandardCharsets.UTF_8.name());
        SolrCLI.StatusTool tool = new SolrCLI.StatusTool(stdoutSim);
        try {
            System.setProperty("basicauth", "harry:HarryIsUberCool");
            tool.runTool(SolrCLI.processCommandLineArgs(SolrCLI.joinCommonAndToolOptions(tool.getOptions()), toolArgs));
            Map obj = (Map) Utils.fromJSON(new ByteArrayInputStream(baos.toByteArray()));
            assertTrue(obj.containsKey("version"));
            assertTrue(obj.containsKey("startTime"));
            assertTrue(obj.containsKey("uptime"));
            assertTrue(obj.containsKey("memory"));
        } catch (Exception e) {
            log.error("RunExampleTool failed due to: " + e + "; stdout from tool prior to failure: " + baos.toString(StandardCharsets.UTF_8.name()));
        }
        executeCommand(baseUrl + authcPrefix, cl, "{set-property : { blockUnknown: false}}", "harry", "HarryIsUberCool");
    } finally {
        if (cl != null) {
            HttpClientUtil.close(cl);
        }
    }
}
Also used : GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) HttpPost(org.apache.http.client.methods.HttpPost) SolrRequest(org.apache.solr.client.solrj.SolrRequest) GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) SolrCLI(org.apache.solr.util.SolrCLI) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) PrintStream(java.io.PrintStream) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) V2Request(org.apache.solr.client.solrj.request.V2Request) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpClient(org.apache.http.client.HttpClient) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.Test)

Example 69 with HttpClient

use of org.apache.http.client.HttpClient in project lucene-solr by apache.

the class BasicAuthStandaloneTest method testBasicAuth.

@Test
@LogLevel("org.apache.solr=DEBUG")
public void testBasicAuth() throws Exception {
    String authcPrefix = "/admin/authentication";
    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" + "}";
        GenericSolrRequest genericReq = new GenericSolrRequest(SolrRequest.METHOD.POST, authcPrefix, new ModifiableSolrParams());
        genericReq.setContentStreams(Collections.singletonList(new ContentStreamBase.ByteArrayStream(command.getBytes(UTF_8), "")));
        HttpSolrClient finalHttpSolrClient = httpSolrClient;
        HttpSolrClient.RemoteSolrException exp = expectThrows(HttpSolrClient.RemoteSolrException.class, () -> {
            finalHttpSolrClient.request(genericReq);
        });
        assertEquals(401, exp.code());
        command = "{\n" + "'set-user': {'harry':'HarryIsUberCool'}\n" + "}";
        HttpPost httpPost = new HttpPost(baseUrl + authcPrefix);
        setBasicAuthHeader(httpPost, "solr", "SolrRocks");
        httpPost.setEntity(new ByteArrayEntity(command.getBytes(UTF_8)));
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        verifySecurityStatus(cl, baseUrl + authcPrefix, "authentication.enabled", "true", 20);
        HttpResponse r = cl.execute(httpPost);
        int statusCode = r.getStatusLine().getStatusCode();
        Utils.consumeFully(r.getEntity());
        assertEquals("proper_cred sent, but access denied", 200, statusCode);
        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"));
    } finally {
        if (cl != null) {
            HttpClientUtil.close(cl);
            httpSolrClient.close();
        }
    }
}
Also used : HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) HttpPost(org.apache.http.client.methods.HttpPost) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) Test(org.junit.Test) LogLevel(org.apache.solr.util.LogLevel)

Example 70 with HttpClient

use of org.apache.http.client.HttpClient in project jena by apache.

the class UpdateProcessRemoteBase method applyServiceConfig.

/**
     * <p>
     * Helper method which applies configuration from the Context to the query
     * engine if a service context exists for the given URI
     * </p>
     * <p>
     * Based off proposed patch for JENA-405 but modified to apply all relevant
     * configuration, this is in part also based off of the private
     * {@code configureQuery()} method of the {@link Service} class though it
     * omits parameter merging since that will be done automatically whenever
     * the {@link QueryEngineHTTP} instance makes a query for remote submission.
     * </p>
     * 
     * @param serviceURI
     *            Service URI
     */
private static void applyServiceConfig(String serviceURI, UpdateProcessRemoteBase engine) {
    @SuppressWarnings("unchecked") Map<String, Context> serviceContextMap = (Map<String, Context>) engine.context.get(Service.serviceContext);
    if (serviceContextMap != null && serviceContextMap.containsKey(serviceURI)) {
        Context serviceContext = serviceContextMap.get(serviceURI);
        if (log.isDebugEnabled())
            log.debug("Endpoint URI {} has SERVICE Context: {} ", serviceURI, serviceContext);
        // Apply client settings
        HttpClient client = serviceContext.get(Service.queryClient);
        if (client != null) {
            if (log.isDebugEnabled())
                log.debug("Using context-supplied client for endpoint URI {}", serviceURI);
            engine.setClient(client);
        }
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) HttpContext(org.apache.http.protocol.HttpContext) HttpClient(org.apache.http.client.HttpClient) Map(java.util.Map)

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