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;
}
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;
}
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();
}
}
}
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);
}
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
}
}
Aggregations