Ejemplo n.º 1
0
  /** @return Returns list of active channels */
  @GET
  @Path("/channels/list")
  @Produces("text/html")
  public Response getActiveChannelsList() {

    Set<String> channelList = cbManager.getActiveChannelsList();
    StringBuilder htmlMessageString = new StringBuilder();

    // Build HTML doc to return
    htmlMessageString.append("<!DOCTYPE html>");
    htmlMessageString.append("<html>");
    htmlMessageString.append(
        "<head><title>REDIS PUBSUB Channel Data Output Service</title></head>");
    htmlMessageString.append("<body>");
    htmlMessageString.append("<p><big>Available active channels: </big></p>");
    htmlMessageString.append("<ul>");
    if (channelList != null) {
      Iterator<String> itr = channelList.iterator();
      while (itr.hasNext()) {
        htmlMessageString.append(
            "<li>" + itr.next().substring(CHANNEL_PREFIX_STRING.length()) + "</li>");
      }
    }
    htmlMessageString.append("</body></html>");
    if (channelList != null) channelList.clear();
    channelList = null;

    return Response.ok(htmlMessageString.toString()).build();
  }
Ejemplo n.º 2
0
 /**
  * @param channel fully qualified channel name
  * @return true if present, false if not
  */
 public boolean isChannelPresent(String channel) {
   Set<String> channelList = cbManager.getActiveChannelsList();
   if (channelList != null) {
     return channelList.contains(channel);
   }
   return false;
 }
Ejemplo n.º 3
0
  /**
   * @param callbackName JSONP callback name
   * @param count number of messages to fetch
   * @return the latest tweet data as a jsonp object from across all active channels
   */
  @GET
  @Path("/channels/latest")
  @Produces("application/json")
  public Response getLatestBufferedAIDRData(
      @QueryParam("callback") String callbackName,
      @DefaultValue("1") @QueryParam("count") String count) {

    logger.info("[getLatestBufferedAIDRData] request received");
    if (null != cbManager.jedisConn && cbManager.jedisConn.isPoolSetup()) { // Jedis pool is ready
      // Get the last count number of messages for channel=channelCode
      List<String> bufferedMessages = new ArrayList<String>();
      final int messageCount =
          Integer.parseInt(count); // number of latest messages across all channels to return
      List<String> temp = cbManager.getLatestFromAllChannels(messageCount);
      bufferedMessages.addAll(temp != null ? temp : new ArrayList<String>());
      if (temp != null) {
        temp.clear();
        temp = null;
      }
      final JsonDataFormatter taggerOutput =
          new JsonDataFormatter(callbackName); // Tagger specific JSONP output formatter
      final StringBuilder jsonDataList =
          taggerOutput.createList(bufferedMessages, messageCount, rejectNullFlag);
      final int sendCount = taggerOutput.getMessageCount();

      // Reset the messageList buffer and return
      bufferedMessages.clear();
      bufferedMessages = null;

      // Finally, send the retrieved list to client and close connection
      return Response.ok(jsonDataList.toString()).build();
    }
    return Response.ok(new String("[{}]")).build();
  }
Ejemplo n.º 4
0
 @GET
 @Path("/manage/restart/{passcode}")
 @Produces("application/json")
 public Response restartFetchService(@PathParam("passcode") String passcode) {
   logger.info("[restartFetchService] request received");
   if (passcode.equals("sysadmin2013")) {
     if (cbManager != null) {
       cbManager.close();
     }
     cbManager = new ChannelBufferManager(CHANNEL_REG_EX);
     logger.info("aidr-output fetch service restarted...");
     final String statusStr = "{\"aidr-output fetch service\":\"RESTARTED\"}";
     return Response.ok(statusStr).build();
   }
   return Response.ok(new String("{\"password\":\"invalid\"}")).build();
 }
Ejemplo n.º 5
0
 @Override
 public void contextDestroyed(ServletContextEvent sce) {
   cbManager.close();
   logger.info("Context destroyed");
 }
Ejemplo n.º 6
0
  /**
   * @param callbackName JSONP callback name
   * @param count number of buffered messages to fetch
   * @return returns the 'count' number of buffered messages from requested channel as jsonp data
   */
  @GET
  @Path("/channel/{crisisCode}")
  @Produces({"application/json"})
  public Response getBufferedAIDRData(
      @PathParam("crisisCode") String channelCode,
      @QueryParam("callback") String callbackName,
      @DefaultValue(DEFAULT_COUNT_STR) @QueryParam("count") String count) {

    logger.info("[getBufferedAIDRData] request received");
    if (null != cbManager.jedisConn && cbManager.jedisConn.isPoolSetup()) {
      boolean error = false;
      // Parse the HTTP GET request and generating results for output
      // Set the response MIME type of the response message
      if (null == channelCode) {
        error = true;
      }
      if (!error && channelCode.contains("*")) {
        // Got a wildcard fetch request - fetch from all channels
        return getLatestBufferedAIDRData(callbackName, count);
      }
      if (channelCode != null && channelCode.contains("?")) {
        error = true;
      }
      if (error) {
        Set<String> channelList = cbManager.getActiveChannelsList();
        StringBuilder htmlMessageString = new StringBuilder();

        // Build HTML doc to return
        htmlMessageString.append("<!DOCTYPE html>");
        htmlMessageString.append("<html>");
        htmlMessageString.append(
            "<head><title>REDIS PUBSUB Channel Data Output Service</title></head>");
        htmlMessageString.append("<body>");
        htmlMessageString.append("<h1>Invalid/No CrisisCode Provided! </h1>");
        htmlMessageString.append("<h2>Can not initiate REDIS channel subscription!</h2>");
        htmlMessageString.append("<p><big>Available active channels: </big></p>");
        htmlMessageString.append("<ul>");
        if (channelList != null) {
          Iterator<String> itr = channelList.iterator();
          while (itr.hasNext()) {
            htmlMessageString.append(
                "<li>" + itr.next().substring(CHANNEL_PREFIX_STRING.length()) + "</li>");
          }
        }
        htmlMessageString.append("</body></html>");
        if (channelList != null) channelList.clear();
        channelList = null;

        return Response.ok(htmlMessageString.toString()).build();
      } else {
        // Form fully qualified channelName and get other parameter values, if any
        String channelName = null;
        if (channelCode.startsWith(CHANNEL_PREFIX_STRING) || channelCode.contains(".")) {
          channelName = channelCode; // fully qualified channel name provided
        } else {
          channelName =
              CHANNEL_PREFIX_STRING.concat(
                  channelCode); // fully qualified channel name - same as REDIS channel
        }
        if (isChannelPresent(channelName)) {
          int msgCount = Integer.parseInt(count);
          int messageCount = DEFAULT_COUNT;
          if (msgCount > 0) {
            messageCount = Math.min(msgCount, MAX_MESSAGES_COUNT);
          }
          // Get the last messageCount messages for channel=channelCode
          List<String> bufferedMessages = new ArrayList<String>();
          List<String> temp = cbManager.getLastMessages(channelName, messageCount);
          bufferedMessages.addAll(temp != null ? temp : new ArrayList<String>());

          final JsonDataFormatter taggerOutput =
              new JsonDataFormatter(callbackName); // Tagger specific JSONP output formatter
          final StringBuilder jsonDataList =
              taggerOutput.createList(bufferedMessages, messageCount, rejectNullFlag);
          final int sendCount = taggerOutput.getMessageCount();

          // Cleanup, send the retrieved list to client and close connection
          if (temp != null) {
            temp.clear();
            temp = null;
          }
          bufferedMessages.clear();
          bufferedMessages = null;

          logger.info("[doGet] Sending jsonp data, count = " + sendCount);
          return Response.ok(jsonDataList.toString()).build();
        } else {
          if (callbackName != null) {
            StringBuilder respStr = new StringBuilder();
            respStr.append(callbackName).append("([{}])");
            return Response.ok(respStr.toString()).build();
          } else return Response.ok(new String("[{}]")).build();
        }
      }
    }
    return Response.ok(new String("[{}]")).build();
  }