Search in sources :

Example 6 with LookupData

use of org.apache.pulsar.common.lookup.data.LookupData in project incubator-pulsar by apache.

the class NamespaceService method internalGetWebServiceUrl.

private CompletableFuture<Optional<URL>> internalGetWebServiceUrl(NamespaceBundle bundle, boolean authoritative, boolean isRequestHttps, boolean readOnly) {
    return findBrokerServiceUrl(bundle, authoritative, readOnly).thenApply(lookupResult -> {
        if (lookupResult.isPresent()) {
            try {
                LookupData lookupData = lookupResult.get().getLookupData();
                final String redirectUrl = isRequestHttps ? lookupData.getHttpUrlTls() : lookupData.getHttpUrl();
                return Optional.of(new URL(redirectUrl));
            } catch (Exception e) {
                // just log the exception, nothing else to do
                LOG.warn("internalGetWebServiceUrl [{}]", e.getMessage(), e);
            }
        }
        return Optional.empty();
    });
}
Also used : LookupData(org.apache.pulsar.common.lookup.data.LookupData) ServiceLookupData(org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData) URL(java.net.URL) ServiceUnitNotReadyException(org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException) ServerMetadataException(org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException) KeeperException(org.apache.zookeeper.KeeperException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Example 7 with LookupData

use of org.apache.pulsar.common.lookup.data.LookupData in project incubator-pulsar by apache.

the class AdminApiTest method testPulsarAdminForUriAndUrlEncoding.

/**
 * This test-case verifies that broker should support both url/uri encoding for topic-name. It calls below api with
 * url-encoded and also uri-encoded topic-name in http request: a. PartitionedMetadataLookup b. TopicLookup c. Topic
 * Stats
 *
 * @param topicName
 * @throws Exception
 */
@Test(dataProvider = "topicName")
public void testPulsarAdminForUriAndUrlEncoding(String topicName) throws Exception {
    final String ns1 = "prop-xyz/use/ns1";
    final String topic1 = "persistent://" + ns1 + "/" + topicName;
    final String urlEncodedTopic = Codec.encode(topicName);
    final String uriEncodedTopic = urlEncodedTopic.replaceAll("\\+", "%20");
    final int numOfPartitions = 4;
    admin.persistentTopics().createPartitionedTopic(topic1, numOfPartitions);
    // Create a consumer to get stats on this topic
    pulsarClient.newConsumer().topic(topic1).subscriptionName("my-subscriber-name").subscribe();
    PersistentTopicsImpl persistent = (PersistentTopicsImpl) admin.persistentTopics();
    Field field = PersistentTopicsImpl.class.getDeclaredField("adminPersistentTopics");
    field.setAccessible(true);
    WebTarget persistentTopics = (WebTarget) field.get(persistent);
    // (1) Get PartitionedMetadata : with Url and Uri encoding
    final CompletableFuture<PartitionedTopicMetadata> urlEncodedPartitionedMetadata = new CompletableFuture<>();
    // (a) Url encoding
    persistent.asyncGetRequest(persistentTopics.path(ns1).path(urlEncodedTopic).path("partitions"), new InvocationCallback<PartitionedTopicMetadata>() {

        @Override
        public void completed(PartitionedTopicMetadata response) {
            urlEncodedPartitionedMetadata.complete(response);
        }

        @Override
        public void failed(Throwable e) {
            Assert.fail(e.getMessage());
        }
    });
    final CompletableFuture<PartitionedTopicMetadata> uriEncodedPartitionedMetadata = new CompletableFuture<>();
    // (b) Uri encoding
    persistent.asyncGetRequest(persistentTopics.path(ns1).path(uriEncodedTopic).path("partitions"), new InvocationCallback<PartitionedTopicMetadata>() {

        @Override
        public void completed(PartitionedTopicMetadata response) {
            uriEncodedPartitionedMetadata.complete(response);
        }

        @Override
        public void failed(Throwable e) {
            uriEncodedPartitionedMetadata.completeExceptionally(e);
        }
    });
    assertEquals(urlEncodedPartitionedMetadata.get().partitions, numOfPartitions);
    assertEquals(urlEncodedPartitionedMetadata.get().partitions, (uriEncodedPartitionedMetadata.get().partitions));
    // (2) Get Topic Lookup
    LookupImpl lookup = (LookupImpl) admin.lookups();
    Field field2 = LookupImpl.class.getDeclaredField("v2lookup");
    field2.setAccessible(true);
    WebTarget target2 = (WebTarget) field2.get(lookup);
    // (a) Url encoding
    LookupData urlEncodedLookupData = lookup.request(target2.path("/destination/persistent").path(ns1 + "/" + urlEncodedTopic)).get(LookupData.class);
    // (b) Uri encoding
    LookupData uriEncodedLookupData = lookup.request(target2.path("/destination/persistent").path(ns1 + "/" + uriEncodedTopic)).get(LookupData.class);
    Assert.assertNotNull(urlEncodedLookupData.getBrokerUrl());
    assertEquals(urlEncodedLookupData.getBrokerUrl(), uriEncodedLookupData.getBrokerUrl());
    // (3) Get Topic Stats
    final CompletableFuture<PersistentTopicStats> urlStats = new CompletableFuture<>();
    // (a) Url encoding
    persistent.asyncGetRequest(persistentTopics.path(ns1).path(urlEncodedTopic + "-partition-1").path("stats"), new InvocationCallback<PersistentTopicStats>() {

        @Override
        public void completed(PersistentTopicStats response) {
            urlStats.complete(response);
        }

        @Override
        public void failed(Throwable e) {
            urlStats.completeExceptionally(e);
        }
    });
    // (b) Uri encoding
    final CompletableFuture<PersistentTopicStats> uriStats = new CompletableFuture<>();
    persistent.asyncGetRequest(persistentTopics.path(ns1).path(uriEncodedTopic + "-partition-1").path("stats"), new InvocationCallback<PersistentTopicStats>() {

        @Override
        public void completed(PersistentTopicStats response) {
            uriStats.complete(response);
        }

        @Override
        public void failed(Throwable e) {
            uriStats.completeExceptionally(e);
        }
    });
    assertEquals(urlStats.get().subscriptions.size(), 1);
    assertEquals(uriStats.get().subscriptions.size(), 1);
}
Also used : PersistentTopicsImpl(org.apache.pulsar.client.admin.internal.PersistentTopicsImpl) LookupData(org.apache.pulsar.common.lookup.data.LookupData) PersistentTopicStats(org.apache.pulsar.common.policies.data.PersistentTopicStats) Field(java.lang.reflect.Field) LookupImpl(org.apache.pulsar.client.admin.internal.LookupImpl) CompletableFuture(java.util.concurrent.CompletableFuture) WebTarget(javax.ws.rs.client.WebTarget) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

LookupData (org.apache.pulsar.common.lookup.data.LookupData)7 URI (java.net.URI)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Test (org.testng.annotations.Test)3 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 URL (java.net.URL)2 Optional (java.util.Optional)2 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)2 PulsarService (org.apache.pulsar.broker.PulsarService)2 ServerMetadataException (org.apache.pulsar.broker.service.BrokerServiceException.ServerMetadataException)2 ServiceUnitNotReadyException (org.apache.pulsar.broker.service.BrokerServiceException.ServiceUnitNotReadyException)2 NamespaceBundle (org.apache.pulsar.common.naming.NamespaceBundle)2 TopicName (org.apache.pulsar.common.naming.TopicName)2 Codec (org.apache.pulsar.common.util.Codec)2 ServiceLookupData (org.apache.pulsar.policies.data.loadbalancer.ServiceLookupData)2 KeeperException (org.apache.zookeeper.KeeperException)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1