private Response internalHeadAutoAckSubscription(UriInfo uriInfo, String consumerId) {
    QueueConsumer consumer = findAutoAckSubscription(consumerId);
    Response.ResponseBuilder builder = Response.noContent();
    String pathToPullSubscriptions = uriInfo.getMatchedURIs().get(1);
    headAutoAckSubscriptionResponse(uriInfo, consumer, builder, pathToPullSubscriptions);

    return builder.build();
  }
 private void headAcknowledgedConsumerResponse(
     UriInfo uriInfo, AcknowledgedQueueConsumer consumer, Response.ResponseBuilder builder) {
   // we synchronize just in case a failed request is still processing
   synchronized (consumer) {
     Acknowledgement ack = consumer.getAck();
     if (ack == null || ack.wasSet()) {
       AcknowledgedQueueConsumer.setAcknowledgeNextLink(
           serviceManager.getLinkStrategy(),
           builder,
           uriInfo,
           uriInfo.getMatchedURIs().get(1) + "/acknowledged/" + consumer.getId(),
           Long.toString(consumer.getConsumeIndex()));
     } else {
       consumer.setAcknowledgementLink(
           builder,
           uriInfo,
           uriInfo.getMatchedURIs().get(1) + "/acknowledged/" + consumer.getId());
     }
   }
 }
 private void headAutoAckSubscriptionResponse(
     UriInfo uriInfo, QueueConsumer consumer, Response.ResponseBuilder builder) {
   // we synchronize just in case a failed request is still processing
   synchronized (consumer) {
     QueueConsumer.setConsumeNextLink(
         serviceManager.getLinkStrategy(),
         builder,
         uriInfo,
         uriInfo.getMatchedURIs().get(1) + "/acknowledged/" + consumer.getId(),
         Long.toString(consumer.getConsumeIndex()));
   }
 }
 @GET
 @Produces(MediaType.APPLICATION_JSON)
 public Map<String, Object> get() {
   Map<String, Object> m = new HashMap<>();
   m.put("message", "Hello " + greeting + "!!!");
   List<Item> items = new ArrayList<>(2);
   items.add(new Item("who I love", "Sun Cai"));
   items.add(new Item("Test title", "happy everyday"));
   m.put("content", items);
   m.put("boolean", Boolean.TRUE);
   m.put("integer", 123);
   m.put("double", 123.4);
   Map<String, Object> m2 = new HashMap<>();
   m2.put("Path", uriInfo.getPath());
   m2.put("Absolute Path", uriInfo.getAbsolutePath());
   m2.put("Base Uri", uriInfo.getBaseUri());
   m2.put("Matched Uris", String.join(",", uriInfo.getMatchedURIs()));
   m2.put("Request Uri", uriInfo.getRequestUri());
   m.put("Additional Info", m2);
   return m;
 }
  @POST
  public Response createSubscription(
      @FormParam("durable") @DefaultValue("false") boolean durable,
      @FormParam("autoAck") @DefaultValue("true") boolean autoAck,
      @FormParam("name") String subscriptionName,
      @FormParam("selector") String selector,
      @FormParam("delete-when-idle") Boolean destroyWhenIdle,
      @FormParam("idle-timeout") Long timeout,
      @Context UriInfo uriInfo) {
    ActiveMQRestLogger.LOGGER.debug("Handling POST request for \"" + uriInfo.getPath() + "\"");

    if (timeout == null) timeout = Long.valueOf(consumerTimeoutSeconds * 1000);
    boolean deleteWhenIdle = !durable; // default is true if non-durable
    if (destroyWhenIdle != null) deleteWhenIdle = destroyWhenIdle.booleanValue();

    if (subscriptionName != null) {
      // see if this is a reconnect
      QueueConsumer consumer = queueConsumers.get(subscriptionName);
      if (consumer != null) {
        boolean acked = consumer instanceof AcknowledgedSubscriptionResource;
        acked = !acked;
        if (acked != autoAck) {
          throw new WebApplicationException(
              Response.status(412)
                  .entity("Consumer already exists and ack-modes don't match.")
                  .type("text/plain")
                  .build());
        }
        Subscription sub = (Subscription) consumer;
        if (sub.isDurable() != durable) {
          throw new WebApplicationException(
              Response.status(412)
                  .entity("Consumer already exists and durability doesn't match.")
                  .type("text/plain")
                  .build());
        }
        Response.ResponseBuilder builder = Response.noContent();
        String pathToPullSubscriptions = uriInfo.getMatchedURIs().get(0);
        if (autoAck) {
          headAutoAckSubscriptionResponse(uriInfo, consumer, builder, pathToPullSubscriptions);
          consumer.setSessionLink(
              builder, uriInfo, pathToPullSubscriptions + "/auto-ack/" + consumer.getId());
        } else {
          headAcknowledgedConsumerResponse(uriInfo, (AcknowledgedQueueConsumer) consumer, builder);
          consumer.setSessionLink(
              builder, uriInfo, pathToPullSubscriptions + "/acknowledged/" + consumer.getId());
        }
        return builder.build();
      }
    } else {
      subscriptionName = generateSubscriptionName();
    }
    ClientSession session = null;
    try {
      // if this is not a reconnect, create the subscription queue
      if (!subscriptionExists(subscriptionName)) {
        session = sessionFactory.createSession();

        if (durable) {
          session.createQueue(destination, subscriptionName, true);
        } else {
          session.createTemporaryQueue(destination, subscriptionName);
        }
      }
      QueueConsumer consumer =
          createConsumer(durable, autoAck, subscriptionName, selector, timeout, deleteWhenIdle);
      queueConsumers.put(consumer.getId(), consumer);
      serviceManager.getTimeoutTask().add(this, consumer.getId());

      UriBuilder location = uriInfo.getAbsolutePathBuilder();
      if (autoAck) location.path("auto-ack");
      else location.path("acknowledged");
      location.path(consumer.getId());
      Response.ResponseBuilder builder = Response.created(location.build());
      if (autoAck) {
        QueueConsumer.setConsumeNextLink(
            serviceManager.getLinkStrategy(),
            builder,
            uriInfo,
            uriInfo.getMatchedURIs().get(0) + "/auto-ack/" + consumer.getId(),
            "-1");
      } else {
        AcknowledgedQueueConsumer.setAcknowledgeNextLink(
            serviceManager.getLinkStrategy(),
            builder,
            uriInfo,
            uriInfo.getMatchedURIs().get(0) + "/acknowledged/" + consumer.getId(),
            "-1");
      }
      return builder.build();

    } catch (ActiveMQException e) {
      throw new RuntimeException(e);
    } finally {
      if (session != null) {
        try {
          session.close();
        } catch (ActiveMQException e) {
        }
      }
    }
  }
 private URI getViewProcessorUri(UriInfo uriInfo) {
   return UriBuilder.fromUri(uriInfo.getMatchedURIs().get(1)).build();
 }