/**
  * Check if a stream is allowed to play
  *
  * @return true if allowed, false otherwise.
  */
 public boolean checkTicket(IMediaStream stream) {
   String name = stream.getName();
   IClient client = stream.getClient();
   if (client == null) {
     logger.debug("No client, returning ", stream);
     return false;
   }
   String clientQuery = stream.getClient().getQueryStr();
   logger.trace(
       "checkTicket(IMediaStream stream="
           + stream
           + ", String name="
           + name
           + ", String clientQuery="
           + clientQuery
           + ")");
   try {
     Ticket streamingTicket = StringAndTextUtil.getTicket(clientQuery, ticketTool);
     logger.debug(
         "Ticket received: " + (streamingTicket != null ? streamingTicket.getId() : "null"));
     if (streamingTicket != null
         && isClientAllowed(stream, streamingTicket)
         && ticketForThisPresentationType(streamingTicket)
         && doesTicketAllowThisStream(name, streamingTicket)) {
       logger.info(
           "checkTicket(IMediaStream stream="
               + stream
               + ", String name="
               + name
               + ", String clientQuery="
               + clientQuery
               + ") successful.");
       return true;
     } else {
       logger.info(
           "Client not allowed to get content streamed for IMediaStream stream="
               + stream
               + ", String name="
               + name
               + ", String clientQuery="
               + clientQuery
               + ")");
       return false;
     }
   } catch (IllegallyFormattedQueryStringException e) {
     logger.warn("Illegally formatted query string [" + clientQuery + "].");
     return false;
   }
 }
  /**
   * This method checks if the ticket is given to the same IP address as the client
   *
   * @param stream the stream
   * @param streamingTicket the ticket
   * @return true if the ip is the same for the ticket and the user
   */
  private boolean isClientAllowed(IMediaStream stream, Ticket streamingTicket) {
    String ipOfClient = stream.getClient().getIp();

    boolean isAllowed = (ipOfClient != null) && (ipOfClient.equals(streamingTicket.getIpAddress()));
    logger.debug(
        "isClientAllowed - ipOfClient: "
            + ipOfClient
            + ", streamingTicket.getIpAddress(): "
            + streamingTicket.getIpAddress()
            + ", isAllowed: "
            + isAllowed);
    return isAllowed;
  }
  @Exposed(method = "GET", url = "streams")
  public ArrayList<String> getStreams() {
    Pattern p = Pattern.compile("^\\[.+\\].+");

    ArrayList<String> streams = new ArrayList<String>();
    for (IMediaStream s : appInstance.getStreams().getStreams()) {
      String name = s.getName();
      Matcher m = p.matcher(name);

      if (name.equalsIgnoreCase(liveStreamName())) continue;
      if (name.equalsIgnoreCase(previewStreamName())) continue;
      if (m.matches()) continue;

      streams.add(name);
    }

    /* list content */
    File contentDir = new File(appInstance.getStreamStorageDir());
    for (final File fileEntry : contentDir.listFiles()) {
      if (!fileEntry.isDirectory()) {
        String ext = Utils.fileExtension(fileEntry.getName());
        if (ext.equalsIgnoreCase("mp4")
            || ext.equalsIgnoreCase("mov")
            || ext.equalsIgnoreCase("3gp")
            || ext.equalsIgnoreCase("m4v")) {
          streams.add("mp4:" + fileEntry.getName());
        } else if (ext.equalsIgnoreCase("flv")) {
          streams.add("flv:" + fileEntry.getName());
        } else if (ext.equalsIgnoreCase("mp3")) {
          streams.add("mp3:" + fileEntry.getName());
        }
      }
    }

    return streams;
  }