Package com.comcast.cqs.model

Examples of com.comcast.cqs.model.CQSQueue


  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
   
        CQSHttpServletRequest request = (CQSHttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();

      CQSQueue queue = CQSCache.getCachedQueue(user, request);
        List<CQSMessage> msgList = new ArrayList<CQSMessage>();
        List<String> idList = new ArrayList<String>();
        List<CQSBatchResultErrorEntry> invalidBodyIdList = new ArrayList<CQSBatchResultErrorEntry>();

        int totalMessageSize = 0;
        int index = 1;
       
        String suppliedId = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + ".Id");
        String messageBody = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.MESSAGE_BODY);
      
        while (suppliedId != null) {
           
          if (!Util.isValidId(suppliedId)) {
                throw new CMBException(CQSErrorCodes.InvalidBatchEntryId, "Id " + suppliedId + " is invalid. Only alphanumeric, hyphen, and underscore are allowed. It can be at most " + CMBProperties.getInstance().getCQSMaxMessageSuppliedIdLength() + " letters long.");
            }
           
          if (idList.contains(suppliedId)) {
                throw new CMBException(CQSErrorCodes.BatchEntryIdsNotDistinct, "Id " + suppliedId + " repeated");
            }
         
            idList.add(suppliedId);
           
            if (messageBody == null || messageBody.isEmpty()) {
                invalidBodyIdList.add(new CQSBatchResultErrorEntry(suppliedId, true, "EmptyValue", "No value found for " + this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.MESSAGE_BODY));
            } else if (!com.comcast.cmb.common.util.Util.isValidUnicode(messageBody)) {
              invalidBodyIdList.add(new CQSBatchResultErrorEntry(suppliedId, true, "InvalidMessageContents", "Invalid character was found in the message body."));
            } else {
               
              HashMap<String, String> attributes = new HashMap<String, String>();
                String delaySecondsStr = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.DELAY_SECONDS);
               
                if (delaySecondsStr != null) {
               
                  Integer delaySeconds = 0;
                 
                  try {
                    delaySeconds = Integer.parseInt(delaySecondsStr);
                  } catch (NumberFormatException ex) {
                        throw new CMBException(CMBErrorCodes.InvalidParameterValue, "DelaySeconds must be integer value");
                  }
                   
                    if (delaySeconds < 0 || delaySeconds > CMBProperties.getInstance().getCQSMaxMessageDelaySeconds()) {
                        throw new CMBException(CMBErrorCodes.InvalidParameterValue, "DelaySeconds should be from 0 to " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
                    } else {
                        attributes.put(CQSConstants.DELAY_SECONDS, "" + delaySeconds);
                    }
                }
               
                attributes.put(CQSConstants.SENDER_ID, user.getUserId());
                attributes.put(CQSConstants.SENT_TIMESTAMP, "" + Calendar.getInstance().getTimeInMillis());
                attributes.put(CQSConstants.APPROXIMATE_RECEIVE_COUNT, "0");
                attributes.put(CQSConstants.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP, "");
               
                CQSMessage msg = new CQSMessage(messageBody, attributes);
               
                msg.setSuppliedMessageId(suppliedId);
                msgList.add(msg);
            }
           
            if (msgList.size() > CMBProperties.getInstance().getCQSMaxMessageCountBatch()) {
                throw new CMBException(CQSErrorCodes.TooManyEntriesInBatchRequest, "Maximum number of entries per request are " + CMBProperties.getInstance().getCQSMaxMessageCountBatch() + ". You have sent " + msgList.size() + ".");
            }
           
            totalMessageSize += messageBody == null ? 0 : messageBody.length();
           
            if (totalMessageSize > CMBProperties.getInstance().getCQSMaxMessageSizeBatch()) {
                throw new CMBException(CQSErrorCodes.BatchRequestTooLong, "Batch requests cannot be longer than " + CMBProperties.getInstance().getCQSMaxMessageSizeBatch() + " bytes");
            }
           
            index++;
           
            suppliedId = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + ".Id");
            messageBody = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.MESSAGE_BODY);
        }
       
        if (msgList.size() == 0) {
            throw new CMBException(CMBErrorCodes.InvalidQueryParameter, "Both user supplied message Id and message body are required");
        }
       
      int shard = 0;
     
      if (queue.getNumberOfShards() > 1) {
        shard = rand.nextInt(queue.getNumberOfShards());
      }
       
    Map<String, String> result = PersistenceFactory.getCQSMessagePersistence().sendMessageBatch(queue, shard, msgList);

    try {
      CQSLongPollSender.send(queue.getArn(), msgList.size());
        } catch (Exception ex) {
          logger.warn("event=failed_to_send_longpoll_notification", ex);
        }
   
        List<String> receiptHandles = new ArrayList<String>();

        for (CQSMessage message: msgList) {
          message.setMessageId(result.get(message.getSuppliedMessageId()));
          message.setReceiptHandle(result.get(message.getSuppliedMessageId()));
          receiptHandles.add(message.getReceiptHandle());
        }
       
        request.setReceiptHandles(receiptHandles);
        String out = CQSMessagePopulator.getSendMessageBatchResponse(msgList, invalidBodyIdList);
        writeResponse(out, response);
       
        CQSMonitor.getInstance().addNumberOfMessagesReceived(queue.getRelativeUrl(), msgList.size());
       
        return true;
  }
View Full Code Here


  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
     
        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();
       
        CQSQueue queue = CQSCache.getCachedQueue(user, request);
        List<String> idList = new ArrayList<String>();
        List<CQSBatchResultErrorEntry> failedList = new ArrayList<CQSBatchResultErrorEntry>();
        Map<String, List<String>> idMap = new HashMap<String, List<String>>();
        int index = 1;
       
View Full Code Here

            continue;
          }

          first = false;
          lastArn = c.getName();
          CQSQueue queue = new CQSQueue(Util.getNameForArn(lastArn), Util.getQueueOwnerFromArn(lastArn));
          queue.setRelativeUrl(Util.getRelativeQueueUrlForArn(lastArn));
          queue.setArn(lastArn);
          queue.setCreatedTime(c.getClock());
         
          if (queueNamePrefix != null && !queue.getName().startsWith(queueNamePrefix)) {
            continue;
          }
         
          if (containingMessagesOnly) {
            try {
              if (PersistenceFactory.getCQSMessagePersistence().getQueueMessageCount(queue.getRelativeUrl(), true) <= 0) {
                continue;
              }
            } catch (Exception ex) {
              continue;
            }
View Full Code Here

      int numShards = slice.getColumnByName(CQSConstants.COL_NUMBER_SHARDS) == null ? 1 : (new Long(slice.getColumnByName(CQSConstants.COL_NUMBER_SHARDS).getValue())).intValue();
      String policy = slice.getColumnByName(CQSConstants.COL_POLICY).getValue();
      long createdTime = (new Long(slice.getColumnByName(CQSConstants.COL_CREATED_TIME).getValue())).longValue();
      String hostName = slice.getColumnByName(CQSConstants.COL_HOST_NAME) == null ? null : slice.getColumnByName(CQSConstants.COL_HOST_NAME).getValue();
      boolean isCompressed = slice.getColumnByName(CQSConstants.COL_COMPRESSED) == null ? false : (new Boolean(slice.getColumnByName(CQSConstants.COL_COMPRESSED).getValue())).booleanValue();
      CQSQueue queue = new CQSQueue(name, ownerUserId);
      queue.setRelativeUrl(url);
      queue.setServiceEndpoint(hostName);
      queue.setArn(arn);
            queue.setRegion(region);
      queue.setPolicy(policy);
      queue.setVisibilityTO(visibilityTO);
      queue.setMaxMsgSize(maxMsgSize);
      queue.setMsgRetentionPeriod(msgRetentionPeriod);
      queue.setDelaySeconds(delaySeconds);
      queue.setReceiveMessageWaitTimeSeconds(waitTimeSeconds);
      queue.setNumberOfPartitions(numPartitions);
      queue.setNumberOfShards(numShards);
      queue.setCreatedTime(createdTime);
      queue.setCompressed(isCompressed);
      return queue;
    } catch (Exception ex) {
      return null;
    }
  }
View Full Code Here

  private CQSQueue getQueueByUrl(String queueUrl) throws PersistenceException {
    CmbColumnSlice<String, String> slice = cassandraHandler.readColumnSlice(AbstractDurablePersistence.CQS_KEYSPACE, COLUMN_FAMILY_QUEUES, queueUrl, null, null, 15, CMB_SERIALIZER.STRING_SERIALIZER, CMB_SERIALIZER.STRING_SERIALIZER, CMB_SERIALIZER.STRING_SERIALIZER);
    if (slice == null) {       
      return null;
    }
      CQSQueue queue = fillQueueFromCqlSlice(queueUrl, slice);
      return queue;
  }
View Full Code Here

      return queue;
  }

  @Override
  public CQSQueue getQueue(String userId, String queueName) throws PersistenceException {
    CQSQueue queue = new CQSQueue(queueName, userId);
    return getQueueByUrl(queue.getRelativeUrl());
  }
View Full Code Here

    public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
     
        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();

        CQSQueue queue = CQSCache.getCachedQueue(user, request);
        String label = request.getParameter(CQSConstants.LABEL);

        if (label == null) {
          throw new CMBException(CMBErrorCodes.ValidationError, "Validation error detected: Value null at 'label' failed to satisfy constraint: Member must not be null");
        }

        if (!Util.isValidId(label)) {
            throw new CMBException(CQSErrorCodes.InvalidBatchEntryId, "Label " + label + " is invalid. Only alphanumeric, hyphen, and underscore are allowed. It can be at most " + CMBProperties.getInstance().getCQSMaxMessageSuppliedIdLength() + " letters long.");
        }
       
        List<String> userList = new ArrayList<String>();
        int index = 1;
       
        String userId = request.getParameter(CQSConstants.AWS_ACCOUNT_ID + "." + index);

        IUserPersistence userHandler = PersistenceFactory.getUserPersistence();
       
        while (userId != null) {

          if (userId.equals("*") || userHandler.getUserById(userId) != null) { // only add user if they exist
                userList.add(userId);
            }
           
          index++;
            userId = request.getParameter(CQSConstants.AWS_ACCOUNT_ID + "." + index);
        }
       
        if (userList.size() == 0) {
            throw new CMBException(CMBErrorCodes.NotFound, "AWSAccountId is required");
        }

        List<String> actionList = new ArrayList<String>();
        index = 1;
        String action = request.getParameter(CQSConstants.ACTION_NAME + "." + index);

        while (action != null) {

          if (action.equals("")) {
          throw new CMBException(CMBErrorCodes.ValidationError, "Blank action parameter is invalid");
          }
         
          if (!CMBPolicy.CQS_ACTIONS.contains(action) && !action.equals("*")) {
          throw new CMBException(CQSErrorCodes.InvalidAction, "Invalid action parameter " + action);
          }
         
          actionList.add(action);
            index++;
            action = request.getParameter(CQSConstants.ACTION_NAME + "." + index);
        }
       
        if (actionList.size() == 0) {
            throw new CMBException(CMBErrorCodes.NotFound, "ActionName is required");
        }
       
        CMBPolicy policy = new CMBPolicy(queue.getPolicy());
       
        if (policy.addStatement(CMBPolicy.SERVICE.CQS, label, "Allow", userList, actionList, queue.getArn(), null)) {
            PersistenceFactory.getQueuePersistence().updatePolicy(queue.getRelativeUrl(), policy.toString());
            queue.setPolicy(policy.toString());
        } else {
          throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Value " + label + " for parameter Label is invalid. Reason: Already exists.");
        }
       
        String out = CQSQueuePopulator.getAddPermissionResponse();
View Full Code Here

  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
   
        CQSHttpServletRequest request = (CQSHttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();
       
      CQSQueue queue = CQSCache.getCachedQueue(user, request);
       
        Map<String, String[]> requestParams = request.getParameterMap();
        List<String> filterAttributes = new ArrayList<String>();
       
        for (String k: requestParams.keySet()) {
          if (k.contains(CQSConstants.ATTRIBUTE_NAME)) {
            filterAttributes.add(requestParams.get(k)[0]);
          }
        }
     
      request.setFilterAttributes(filterAttributes);
      request.setQueue(queue);
     
        HashMap<String, String> msgParam = new HashMap<String, String>();
       
        if (request.getParameter(CQSConstants.MAX_NUMBER_OF_MESSAGES) != null) {
           
          int maxNumberOfMessages = Integer.parseInt(request.getParameter(CQSConstants.MAX_NUMBER_OF_MESSAGES));
           
          if (maxNumberOfMessages < 1 || maxNumberOfMessages > CMBProperties.getInstance().getCQSMaxReceiveMessageCount()) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, "The value for MaxNumberOfMessages is not valid (must be from 1 to " + CMBProperties.getInstance().getCQSMaxReceiveMessageCount() + ").");
            }
         
            msgParam.put(CQSConstants.MAX_NUMBER_OF_MESSAGES, "" + maxNumberOfMessages);
        }

        if (request.getParameter(CQSConstants.VISIBILITY_TIMEOUT) != null) {
         
          int visibilityTimeout = Integer.parseInt(request.getParameter(CQSConstants.VISIBILITY_TIMEOUT));
         
          if (visibilityTimeout < 0 || visibilityTimeout > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, "The value for VisibilityTimeout is not valid (must be from 0 to " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut() + ").");
          }
         
          msgParam.put(CQSConstants.VISIBILITY_TIMEOUT, "" + visibilityTimeout);
        }
       
        // receive timeout overrides queue default timeout if present
       
        int waitTimeSeconds = queue.getReceiveMessageWaitTimeSeconds();
       
        if (request.getParameter(CQSConstants.WAIT_TIME_SECONDS) != null) {
          try {
            waitTimeSeconds = Integer.parseInt(request.getParameter(CQSConstants.WAIT_TIME_SECONDS));
          } catch (NumberFormatException ex) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.WAIT_TIME_SECONDS + " must be an integer number.");
          }
        }
         
      if (waitTimeSeconds < 0 || waitTimeSeconds > CMBProperties.getInstance().getCMBRequestTimeoutSec()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.WAIT_TIME_SECONDS + " must be an integer number between 0 and 20.");
      }

      // we are already setting wait time in main controller servlet, we are just doing
      // this here again to throw appropriate error messages for invalid parameters
     
      if (!CMBProperties.getInstance().isCQSLongPollEnabled()) {
            //throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Long polling not enabled.");
        waitTimeSeconds = 0;
        logger.warn("event=invalid_parameter param=wait_time_seconds reason=long_polling_disabled action=force_to_zero");
      }

      //asyncContext.setTimeout(waitTimeSeconds * 1000);
        //request.setWaitTime(waitTimeSeconds * 1000);

        List<CQSMessage> messageList = PersistenceFactory.getCQSMessagePersistence().receiveMessage(queue, msgParam);
        request.setReceiveAttributes(msgParam);
       
        // wait for long poll if desired
       
        if (messageList.size() == 0 && waitTimeSeconds > 0) {
          //start long poll. First y means long poll, second n means message not found yet.
          request.setAttribute("lp", "yn");
          // put context on async queue to wait for long poll
         
          logger.debug("event=queueing_context queue_arn=" + queue.getArn() + " wait_time_sec=" + waitTimeSeconds);
         
          CQSLongPollReceiver.contextQueues.putIfAbsent(queue.getArn(), new ConcurrentLinkedQueue<AsyncContext>());
      ConcurrentLinkedQueue<AsyncContext> contextQueue = CQSLongPollReceiver.contextQueues.get(queue.getArn());
     
      if (contextQueue.offer(asyncContext)) {
              request.setIsQueuedForProcessing(true);
      }
     
        } else {

            CQSMonitor.getInstance().addNumberOfMessagesReturned(queue.getRelativeUrl(), messageList.size());
            List<String> receiptHandles = new ArrayList<String>();
           
            for (CQSMessage message : messageList) {
              receiptHandles.add(message.getReceiptHandle());
            }
View Full Code Here

  public boolean doAction(User user, AsyncContext asyncContext) throws Exception {
     
        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();

    CQSQueue queue = CQSCache.getCachedQueue(user, request);
     
        String receiptHandle = request.getParameter(CQSConstants.RECEIPT_HANDLE);

        if (receiptHandle == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "ReceiptHandle not found");
        }
       
        if (!isValidReceiptHandle(receiptHandle)) {
            throw new CMBException(CQSErrorCodes.ReceiptHandleInvalid, "The input receipt handle is invalid");
        }

        PersistenceFactory.getCQSMessagePersistence().deleteMessage(queue.getRelativeUrl(), receiptHandle);
       
        String out = CQSMessagePopulator.getDeleteMessageResponse();
        writeResponse(out, response);
       
        return true;
View Full Code Here

TOP

Related Classes of com.comcast.cqs.model.CQSQueue

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.