Search in sources :

Example 41 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project sirix by sirixdb.

the class NodeIdRepresentation method performQueryOnResource.

/**
 * This method is responsible to perform a XPath query expression on the XML
 * resource which is addressed through a unique node id.
 *
 * @param resourceName
 *          The name of the database, the node id belongs to.
 * @param nodeId
 *          The node id of the requested resource.
 * @param query
 *          The XPath expression.
 * @param queryParams
 *          The optional query parameters (output, wrap, revision).
 * @return The result of the XPath query expression.
 */
public StreamingOutput performQueryOnResource(final String resourceName, final long nodeId, final String query, final Map<QueryParameter, String> queryParams) {
    final StreamingOutput sOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, JaxRxException {
            final File dbFile = new File(mStoragePath, resourceName);
            final String revision = queryParams.get(QueryParameter.REVISION);
            final String wrap = queryParams.get(QueryParameter.WRAP);
            final String doNodeId = queryParams.get(QueryParameter.OUTPUT);
            final boolean wrapResult = (wrap == null) ? true : wrap.equalsIgnoreCase(YESSTRING);
            final boolean nodeid = (doNodeId == null) ? false : doNodeId.equalsIgnoreCase(YESSTRING);
            final Integer rev = revision == null ? null : Integer.valueOf(revision);
            final RestXPathProcessor xpathProcessor = new RestXPathProcessor(mStoragePath);
            try {
                xpathProcessor.getXpathResource(dbFile, nodeId, query, nodeid, rev, output, wrapResult);
            } catch (final SirixException exce) {
                throw new JaxRxException(exce);
            }
        }
    };
    return sOutput;
}
Also used : RestXPathProcessor(org.sirix.service.jaxrx.util.RestXPathProcessor) OutputStream(java.io.OutputStream) SirixException(org.sirix.exception.SirixException) StreamingOutput(javax.ws.rs.core.StreamingOutput) File(java.io.File) JaxRxException(org.jaxrx.core.JaxRxException)

Example 42 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project sirix by sirixdb.

the class NodeIdRepresentation method getResource.

/**
 * This method is responsible to deliver the whole XML resource addressed by a
 * unique node id.
 *
 * @param resourceName
 *          The name of the database, where the node id belongs.
 * @param nodeId
 *          The unique node id of the requested resource.
 * @param queryParams
 *          The optional query parameters.
 * @return The whole XML resource addressed by a unique node id.
 * @throws JaxRxException
 *           The exception occurred.
 */
public StreamingOutput getResource(final String resourceName, final long nodeId, final Map<QueryParameter, String> queryParams) throws JaxRxException {
    final StreamingOutput sOutput = new StreamingOutput() {

        @Override
        public void write(final OutputStream output) throws IOException, JaxRxException {
            // final String xPath = queryParams.get(QueryParameter.QUERY);
            final String revision = queryParams.get(QueryParameter.REVISION);
            final String wrap = queryParams.get(QueryParameter.WRAP);
            final String doNodeId = queryParams.get(QueryParameter.OUTPUT);
            final boolean wrapResult = (wrap == null) ? false : wrap.equalsIgnoreCase(YESSTRING);
            final boolean nodeid = (doNodeId == null) ? false : doNodeId.equalsIgnoreCase(YESSTRING);
            final Long rev = revision == null ? null : Long.valueOf(revision);
            serialize(resourceName, nodeId, rev, nodeid, output, wrapResult);
        }
    };
    return sOutput;
}
Also used : OutputStream(java.io.OutputStream) StreamingOutput(javax.ws.rs.core.StreamingOutput)

Example 43 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalPeekNthMessage.

protected Response internalPeekNthMessage(String subName, int messagePosition, boolean authoritative) {
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    PartitionedTopicMetadata partitionMetadata = getPartitionedTopicMetadata(topicName, authoritative);
    if (partitionMetadata.partitions > 0) {
        throw new RestException(Status.METHOD_NOT_ALLOWED, "Peek messages on a partitioned topic is not allowed");
    }
    validateAdminOperationOnTopic(authoritative);
    if (!(getTopicReference(topicName) instanceof PersistentTopic)) {
        log.error("[{}] Not supported operation of non-persistent topic {} {}", clientAppId(), topicName, subName);
        throw new RestException(Status.METHOD_NOT_ALLOWED, "Skip messages on a non-persistent topic is not allowed");
    }
    PersistentTopic topic = (PersistentTopic) getTopicReference(topicName);
    PersistentReplicator repl = null;
    PersistentSubscription sub = null;
    Entry entry = null;
    if (subName.startsWith(topic.replicatorPrefix)) {
        repl = getReplicatorReference(subName, topic);
    } else {
        sub = (PersistentSubscription) getSubscriptionReference(subName, topic);
    }
    try {
        if (subName.startsWith(topic.replicatorPrefix)) {
            entry = repl.peekNthMessage(messagePosition).get();
        } else {
            entry = sub.peekNthMessage(messagePosition).get();
        }
        checkNotNull(entry);
        PositionImpl pos = (PositionImpl) entry.getPosition();
        ByteBuf metadataAndPayload = entry.getDataBuffer();
        // moves the readerIndex to the payload
        MessageMetadata metadata = Commands.parseMessageMetadata(metadataAndPayload);
        ResponseBuilder responseBuilder = Response.ok();
        responseBuilder.header("X-Pulsar-Message-ID", pos.toString());
        for (KeyValue keyValue : metadata.getPropertiesList()) {
            responseBuilder.header("X-Pulsar-PROPERTY-" + keyValue.getKey(), keyValue.getValue());
        }
        if (metadata.hasPublishTime()) {
            responseBuilder.header("X-Pulsar-publish-time", DateFormatter.format(metadata.getPublishTime()));
        }
        if (metadata.hasEventTime()) {
            responseBuilder.header("X-Pulsar-event-time", DateFormatter.format(metadata.getEventTime()));
        }
        if (metadata.hasNumMessagesInBatch()) {
            responseBuilder.header("X-Pulsar-num-batch-message", metadata.getNumMessagesInBatch());
        }
        // Decode if needed
        CompressionCodec codec = CompressionCodecProvider.getCompressionCodec(metadata.getCompression());
        ByteBuf uncompressedPayload = codec.decode(metadataAndPayload, metadata.getUncompressedSize());
        // Copy into a heap buffer for output stream compatibility
        ByteBuf data = PooledByteBufAllocator.DEFAULT.heapBuffer(uncompressedPayload.readableBytes(), uncompressedPayload.readableBytes());
        data.writeBytes(uncompressedPayload);
        uncompressedPayload.release();
        StreamingOutput stream = new StreamingOutput() {

            @Override
            public void write(OutputStream output) throws IOException, WebApplicationException {
                output.write(data.array(), data.arrayOffset(), data.readableBytes());
                data.release();
            }
        };
        return responseBuilder.entity(stream).build();
    } catch (NullPointerException npe) {
        throw new RestException(Status.NOT_FOUND, "Message not found");
    } catch (Exception exception) {
        log.error("[{}] Failed to get message at position {} from {} {}", clientAppId(), messagePosition, topicName, subName, exception);
        throw new RestException(exception);
    } finally {
        if (entry != null) {
            entry.release();
        }
    }
}
Also used : PersistentReplicator(org.apache.pulsar.broker.service.persistent.PersistentReplicator) KeyValue(org.apache.pulsar.common.api.proto.PulsarApi.KeyValue) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) OutputStream(java.io.OutputStream) RestException(org.apache.pulsar.broker.web.RestException) StreamingOutput(javax.ws.rs.core.StreamingOutput) ByteBuf(io.netty.buffer.ByteBuf) PersistentSubscription(org.apache.pulsar.broker.service.persistent.PersistentSubscription) NotAllowedException(org.apache.pulsar.broker.service.BrokerServiceException.NotAllowedException) NotFoundException(org.apache.pulsar.client.admin.PulsarAdminException.NotFoundException) PreconditionFailedException(org.apache.pulsar.client.admin.PulsarAdminException.PreconditionFailedException) RestException(org.apache.pulsar.broker.web.RestException) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) SubscriptionBusyException(org.apache.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException) WebApplicationException(javax.ws.rs.WebApplicationException) KeeperException(org.apache.zookeeper.KeeperException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) TopicBusyException(org.apache.pulsar.broker.service.BrokerServiceException.TopicBusyException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) Entry(org.apache.bookkeeper.mledger.Entry) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) CompressionCodec(org.apache.pulsar.common.compression.CompressionCodec) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata)

Example 44 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project incubator-pulsar by apache.

the class PersistentTopicsBase method internalGetManagedLedgerInfo.

protected void internalGetManagedLedgerInfo(AsyncResponse asyncResponse) {
    validateAdminAccessOnProperty(topicName.getProperty());
    if (topicName.isGlobal()) {
        validateGlobalNamespaceOwnership(namespaceName);
    }
    String managedLedger = topicName.getPersistenceNamingEncoding();
    pulsar().getManagedLedgerFactory().asyncGetManagedLedgerInfo(managedLedger, new ManagedLedgerInfoCallback() {

        @Override
        public void getInfoComplete(ManagedLedgerInfo info, Object ctx) {
            asyncResponse.resume((StreamingOutput) output -> {
                jsonMapper().writer().writeValue(output, info);
            });
        }

        @Override
        public void getInfoFailed(ManagedLedgerException exception, Object ctx) {
            asyncResponse.resume(exception);
        }
    }, null);
}
Also used : ManagedLedgerException(org.apache.bookkeeper.mledger.ManagedLedgerException) ManagedLedgerInfoCallback(org.apache.bookkeeper.mledger.AsyncCallbacks.ManagedLedgerInfoCallback) StreamingOutput(javax.ws.rs.core.StreamingOutput) ManagedLedgerInfo(org.apache.bookkeeper.mledger.ManagedLedgerInfo)

Example 45 with StreamingOutput

use of javax.ws.rs.core.StreamingOutput in project incubator-pulsar by apache.

the class AdminTest method brokerStats.

@Test
void brokerStats() throws Exception {
    doReturn("client-id").when(brokerStats).clientAppId();
    Collection<Metrics> metrics = brokerStats.getMetrics();
    assertNotNull(metrics);
    LocalBrokerData loadReport = (LocalBrokerData) brokerStats.getLoadReport();
    assertNotNull(loadReport);
    assertNotNull(loadReport.getCpu());
    Collection<Metrics> mBeans = brokerStats.getMBeans();
    assertTrue(!mBeans.isEmpty());
    AllocatorStats allocatorStats = brokerStats.getAllocatorStats("default");
    assertNotNull(allocatorStats);
    Map<String, Map<String, PendingBookieOpsStats>> bookieOpsStats = brokerStats.getPendingBookieOpsStats();
    assertTrue(bookieOpsStats.isEmpty());
    StreamingOutput topic = brokerStats.getTopics2();
    assertNotNull(topic);
    try {
        brokerStats.getBrokerResourceAvailability("prop", "use", "ns2");
        fail("should have failed as ModularLoadManager doesn't support it");
    } catch (RestException re) {
    // Ok
    }
}
Also used : Metrics(org.apache.pulsar.common.stats.Metrics) LocalBrokerData(org.apache.pulsar.policies.data.loadbalancer.LocalBrokerData) AllocatorStats(org.apache.pulsar.common.stats.AllocatorStats) RestException(org.apache.pulsar.broker.web.RestException) StreamingOutput(javax.ws.rs.core.StreamingOutput) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

StreamingOutput (javax.ws.rs.core.StreamingOutput)191 OutputStream (java.io.OutputStream)85 Response (javax.ws.rs.core.Response)76 Path (javax.ws.rs.Path)53 Produces (javax.ws.rs.Produces)52 IOException (java.io.IOException)51 GET (javax.ws.rs.GET)50 File (java.io.File)45 InputStream (java.io.InputStream)45 Test (org.junit.Test)44 WebApplicationException (javax.ws.rs.WebApplicationException)34 ByteArrayOutputStream (java.io.ByteArrayOutputStream)32 List (java.util.List)26 MediaType (javax.ws.rs.core.MediaType)24 ByteArrayInputStream (java.io.ByteArrayInputStream)20 ArrayList (java.util.ArrayList)20 Consumes (javax.ws.rs.Consumes)20 HashMap (java.util.HashMap)19 POST (javax.ws.rs.POST)19 FileOutputStream (java.io.FileOutputStream)17