Package com.comcast.cmb.common.util

Examples of com.comcast.cmb.common.util.CMBException


        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();
   
    String queueName = request.getParameter("QueueName");

        if (queueName == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "Parameter QueueName not found");
        }

        CQSQueue queue = PersistenceFactory.getQueuePersistence().getQueue(user.getUserId(), queueName);

        if (queue == null) {
            throw new CMBException(CQSErrorCodes.NonExistentQueue, "Queue not found with name " + queueName + " for user " + user.getUserId());
        }
       
        String out = CQSQueuePopulator.getQueueUrlResponse(queue);
        writeResponse(out, response);
       
View Full Code Here


    long ts1 = System.currentTimeMillis();

      CQSQueue queue = CQSCache.getCachedQueue(relativeQueueUrl);

      if (queue == null) {
        throw new CMBException(CMBErrorCodes.InternalError, "Unknown queue " + relativeQueueUrl);
      }
       
    if (messageBody == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "MessageBody not found");
        }

        if (messageBody.length() > CMBProperties.getInstance().getCQSMaxMessageSize()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Value for parameter MessageBody is invalid. Reason: Message body must be shorter than " + CMBProperties.getInstance().getCQSMaxMessageSize() + " bytes");
        }
       
        if (!Util.isValidUnicode(messageBody)) {
            throw new CMBException(CMBErrorCodes.InvalidMessageContents, "The message contains characters outside the allowed set.");
        }
       
        HashMap<String, String> attributes = new HashMap<String, String>();

        if (delaySeconds != null) {
          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, userId);
        attributes.put(CQSConstants.SENT_TIMESTAMP, "" + System.currentTimeMillis());
        attributes.put(CQSConstants.APPROXIMATE_RECEIVE_COUNT, "0");
        attributes.put(CQSConstants.APPROXIMATE_FIRST_RECEIVE_TIMESTAMP, "");
       
        CQSMessage message = new CQSMessage(messageBody, attributes);
       
    int shard = rand.nextInt(queue.getNumberOfShards());

        String receiptHandle = PersistenceFactory.getCQSMessagePersistence().sendMessage(queue, shard, message);

        if (receiptHandle == null || receiptHandle.isEmpty()) {
            throw new CMBException(CMBErrorCodes.InternalError, "Failed to add message to queue");
        }
       
        try {
          CQSLongPollSender.send(queue.getArn());
        } catch (Exception ex) {
View Full Code Here

    List<CQSMessage> messages = null;

    CQSQueue queue = CQSCache.getCachedQueue(relativeQueueUrl);

      if (queue == null) {
        throw new CMBException(CMBErrorCodes.InternalError, "Unknown queue " + relativeQueueUrl);
      }
       
    HashMap<String, String> msgParam = new HashMap<String, String>();
   
    if (maxNumberOfMessages != null) {

      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 (visibilityTimeout != null) {
   
      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);
    }
View Full Code Here

  public static CQSQueue createQueue(String userId, String queueName, Integer visibilityTimeout, Integer messageRetentionPeriod, Integer delaySeconds, Integer receiveMessageWaitTimeSeconds, Integer numberOfPartitions, Integer numberOfShards, Boolean isCompressed, String policy) throws Exception {
   
    long ts1 = System.currentTimeMillis();
   
        if (queueName == null || queueName.length() == 0) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "New queue must have a name");
        }
       
        queueName = queueName.trim();

        if (queueName.length() > CMBProperties.getInstance().getCQSMaxNameLength()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "QueueName " + queueName + " is too long. Maximum is " + CMBProperties.getInstance().getCQSMaxNameLength());
        }

        Pattern p = Pattern.compile("[a-zA-Z0-9-_]+");
        Matcher m = p.matcher(queueName);
       
        if (!m.matches()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "QueueName " + queueName + " is invalid. Only alphanumeric and hyphen and underscore allowed.");
        }

        String relativeQueueUrl = new CQSQueue(queueName, userId).getRelativeUrl();
        CQSQueue queue = PersistenceFactory.getQueuePersistence().getQueue(relativeQueueUrl);
       
        if (queue != null) {
          return queue;
        }
       
        queue = new CQSQueue(queueName, userId);
       
        if (visibilityTimeout != null) {
        if (visibilityTimeout < 0 || visibilityTimeout > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.VISIBILITY_TIMEOUT + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut());
        }
        queue.setVisibilityTO(visibilityTimeout);
        }
       
        if (policy != null) {
          //TODO: validate policy
          queue.setPolicy(policy);
        }
       
        if (messageRetentionPeriod != null) {
        if (messageRetentionPeriod < 0 || messageRetentionPeriod > CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod()) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MESSAGE_RETENTION_PERIOD + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod());
        }
        queue.setMsgRetentionPeriod(messageRetentionPeriod);
        }
       
        if (delaySeconds != null) {
        if (delaySeconds < 0 || delaySeconds > CMBProperties.getInstance().getCQSMaxMessageDelaySeconds()) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.DELAY_SECONDS + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
        }
        queue.setDelaySeconds(delaySeconds);
        }
       
        if (receiveMessageWaitTimeSeconds != null) {
        if (receiveMessageWaitTimeSeconds < 0 || receiveMessageWaitTimeSeconds > CMBProperties.getInstance().getCMBRequestTimeoutSec()) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
        }
        queue.setReceiveMessageWaitTimeSeconds(receiveMessageWaitTimeSeconds);
        }
       
        if (numberOfPartitions != null) {
        if (numberOfPartitions < 1) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_PARTITIONS + " should be at least 1");
        }
          queue.setNumberOfPartitions(numberOfPartitions);
        }
       
        if (numberOfShards != null) {
        if (numberOfShards < 1 || numberOfShards > 100) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_SHARDS + " should be between 1 and 100");
        }
            queue.setNumberOfShards(numberOfShards);
        }
       
        if (isCompressed != null) {
View Full Code Here

    long ts1 = System.currentTimeMillis();
   
      CQSQueue queue = CQSCache.getCachedQueue(relativeQueueUrl);
     
      if (queue == null) {
        throw new CMBException(CMBErrorCodes.InternalError, "Unknown queue " + relativeQueueUrl);
      }
     
      int numberOfShards = queue.getNumberOfShards();
      PersistenceFactory.getQueuePersistence().deleteQueue(queue.getRelativeUrl());
     
View Full Code Here

  public static CQSQueue getQueueUrl(String userId, String queueName) throws Exception {
   
    long ts1 = System.currentTimeMillis();
   
        if (queueName == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "Missing parameter QueueName");
        }

        CQSQueue queue = PersistenceFactory.getQueuePersistence().getQueue(userId, queueName);

        if (queue == null) {
            throw new CMBException(CQSErrorCodes.NonExistentQueue, "Queue not found with name " + queueName + " for user " + userId);
        }
       
    long ts2 = System.currentTimeMillis();
   
    emitLogLine(userId, "SendMessage", queue.getRelativeUrl(), null, ts2-ts1);
View Full Code Here

    long ts1 = System.currentTimeMillis();
   
      CQSQueue queue = CQSCache.getCachedQueue(relativeQueueUrl);
     
      if (queue == null) {
        throw new CMBException(CMBErrorCodes.InternalError, "Unknown queue " + relativeQueueUrl);
      }
     
        if (receiptHandle == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "ReceiptHandle not found");
        }

        if (visibilityTimeout == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "VisibilityTimeout not found");
        }

        if (visibilityTimeout < 0 || visibilityTimeout > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "VisibilityTimeout is limited from 0 to " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut() + " seconds");
        }
       
        PersistenceFactory.getCQSMessagePersistence().changeMessageVisibility(queue, receiptHandle, visibilityTimeout);
     
    long ts2 = System.currentTimeMillis();
View Full Code Here

        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();

    String queueName = request.getParameter("QueueName");
       
        if (queueName == null || queueName.length() == 0) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "Missing parameter QueueName");
        }
       
        queueName = queueName.trim();

        if (queueName.length() > CMBProperties.getInstance().getCQSMaxNameLength()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "QueueName " + queueName + " is too long. Maximum is " + CMBProperties.getInstance().getCQSMaxNameLength());
        }

        Pattern p = Pattern.compile("[a-zA-Z0-9-_]+");
        Matcher m = p.matcher(queueName);
       
        if (!m.matches()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "QueueName " + queueName + " is invalid. Only alphanumeric and hyphen and underscore allowed.");
        }

        CQSQueue newQueue = new CQSQueue(queueName, user.getUserId());
        CQSQueue existingQueue = PersistenceFactory.getQueuePersistence().getQueue(newQueue.getRelativeUrl());
       
        boolean throwQueueExistsError = false;
        int index = 1;
        String attributeName = request.getParameter("Attribute." + index + ".Name");
        int numberOfShards = 1;
       
        while (attributeName != null) {
           
            String attributeValue = request.getParameter("Attribute." + index + ".Value");

            if (attributeValue == null) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Attribute: " + attributeName + " does not have a corresponding attribute value");
            }

            if (attributeName.equals(CQSConstants.VISIBILITY_TIMEOUT)) {
             
              int visibilityTo = 0;
             
              try {
                visibilityTo = Integer.parseInt(attributeValue);
              } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.VISIBILITY_TIMEOUT + " must be an integer value");
              }
             
            if (visibilityTo < 0 || visibilityTo > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.VISIBILITY_TIMEOUT + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut());
            }

            if (existingQueue != null && existingQueue.getVisibilityTO() != visibilityTo) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setVisibilityTO(visibilityTo);
               
            } else if (attributeName.equals(CQSConstants.POLICY)) {
             
                if (existingQueue != null && !existingQueue.getPolicy().equals(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setPolicy(attributeValue);
               
            } else if (attributeName.equals(CQSConstants.MAXIMUM_MESSAGE_SIZE)) {
             
              int maxMessageSize = 0;
             
              try {
                maxMessageSize = Integer.parseInt(attributeValue);
              } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MAXIMUM_MESSAGE_SIZE + " must be an integer value");
              }
             
            if (maxMessageSize < 0 || maxMessageSize > CMBProperties.getInstance().getCQSMaxMessageSize()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MAXIMUM_MESSAGE_SIZE + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageSize());
            }

            if (existingQueue != null && existingQueue.getMaxMsgSize() != maxMessageSize) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setMaxMsgSize(maxMessageSize);
               
            } else if (attributeName.equals(CQSConstants.MESSAGE_RETENTION_PERIOD)) {
             
              int messageRetentionPeriod = 0;
             
              try {
                messageRetentionPeriod = Integer.parseInt(attributeValue);
              } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MESSAGE_RETENTION_PERIOD + " must be an integer value");
              }
             
            if (messageRetentionPeriod < 0 || messageRetentionPeriod > CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.MESSAGE_RETENTION_PERIOD + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod());
            }
             
                if (existingQueue != null && existingQueue.getMsgRetentionPeriod() != messageRetentionPeriod) {
                    throwQueueExistsError = true;
                    break;
                }
               
                newQueue.setMsgRetentionPeriod(messageRetentionPeriod);
               
            } else if (attributeName.equals(CQSConstants.DELAY_SECONDS)) {
             
                if (existingQueue != null && existingQueue.getDelaySeconds() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                int delaySeconds = 0;
               
                try {
                  delaySeconds = Integer.parseInt(attributeValue);
                } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.DELAY_SECONDS + " must be an integer value");
                }
               
            if (delaySeconds < 0 || delaySeconds > CMBProperties.getInstance().getCQSMaxMessageDelaySeconds()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.DELAY_SECONDS + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
            }
               
                newQueue.setDelaySeconds(delaySeconds);
               
            } else if (attributeName.equals(CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS)) {
             
                if (existingQueue != null && existingQueue.getReceiveMessageWaitTimeSeconds() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                int receiveMessageWaitTimeSeconds = 0;
               
                try {
                  receiveMessageWaitTimeSeconds = Integer.parseInt(attributeValue);
                } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS + " must be an integer value");
                }
               
            if (receiveMessageWaitTimeSeconds < 0 || receiveMessageWaitTimeSeconds > CMBProperties.getInstance().getCMBRequestTimeoutSec()) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS + " should be between 0 and " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds());
            }
               
                newQueue.setReceiveMessageWaitTimeSeconds(receiveMessageWaitTimeSeconds);
               
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_PARTITIONS)) {
             
              // number of Cassandra rows used to store messages, default is 100 - this number can be set at queue creation time
              // or changed later via SetQueueAttributes() API

              if (existingQueue != null && existingQueue.getNumberOfPartitions() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                int numberOfPartitions = 0;
               
                try {
                  numberOfPartitions = Integer.parseInt(attributeValue);
                } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_PARTITIONS + " must be an integer value");
                }
               
            if (numberOfPartitions < 1) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_PARTITIONS + " should be at least 1");
            }
               
                newQueue.setNumberOfPartitions(numberOfPartitions);
               
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_SHARDS)) {
             
              // number of internal queues used for random sharding to trade message ordering for enhanced single-queue
              // throughput - this number can only be set at queue creation time and cannot be changed later
             
                if (existingQueue != null && existingQueue.getNumberOfShards() != Integer.parseInt(attributeValue)) {
                    throwQueueExistsError = true;
                    break;
                }
               
                try {
                  numberOfShards = Integer.parseInt(attributeValue);
                } catch (NumberFormatException ex) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_SHARDS + " must be an integer value");
                }
               
            if (numberOfShards < 1 || numberOfShards > 100) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_SHARDS + " should be between 1 and 100");
            }
               
                newQueue.setNumberOfShards(numberOfShards);
               
            } else if (attributeName.equals(CQSConstants.IS_COMPRESSED)) {
             
              boolean isCompressed = Boolean.parseBoolean(attributeValue);
              newQueue.setCompressed(isCompressed);

            } else {
                throw new CMBException(CMBErrorCodes.InvalidRequest, "Attribute: " + attributeName + " is not a valid attribute");
            }

            index++;
            attributeName = request.getParameter("Attribute." + index + ".Name");
        }
       
        if (throwQueueExistsError) {
            throw new CMBException(CQSErrorCodes.QueueNameExists, "Queue name with " + queueName + " exists");
        }
       
        // create queue and initialize all shards in redis
       
      PersistenceFactory.getQueuePersistence().createQueue(newQueue);
View Full Code Here

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

        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.");
        }
       
        CMBPolicy policy = new CMBPolicy(queue.getPolicy());
       
        if (policy.removeStatement(label)) {
View Full Code Here

      List<String> normalizedActionList = new ArrayList<String>();
     
      for (String action : actionList) {
       
        if (action.equals("")) {
          throw new CMBException(CMBErrorCodes.ValidationError, "Blank action parameter is invalid");
        }
       
        if (!CNS_ACTIONS.contains(action) && !CQS_ACTIONS.contains(action) && !action.equals("*")) {
          throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Invalid action parameter " + action);
        }
       
        normalizedActionList.add(action.contains(":") ? action : service + ":" + action);
      }
       
View Full Code Here

TOP

Related Classes of com.comcast.cmb.common.util.CMBException

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.