Package com.comcast.cmb.common.util

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


            try {
             
                JSONObject msgObj = new JSONObject(getMessage());
               
                if (protocol == null) {
                    throw new CMBException(CMBErrorCodes.InternalError, "Subscription has no protocol");
                }
               
                if (protocol == CnsSubscriptionProtocol.email_json) { //special case
                 
                    if (msgObj.has("email-json")) {
                        msg = msgObj.getString("email-json");
                    } else {
                        msg = msgObj.getString("default");
                    }
                   
                } else {
                 
                    if (msgObj.has(protocol.name())) {
                        msg = msgObj.getString(protocol.name());
                    } else {
                        msg = msgObj.getString("default");
                    }
                }        
               
            } catch (JSONException e) {
                throw new CMBException(CMBErrorCodes.InternalError, "Could not parse JSON:" + e.getMessage());
            }
        }    
       
        return msg;
    }
View Full Code Here


                && !attribute.equals(CQSConstants.APPROXIMATE_NUMBER_OF_MESSAGES)
                && !attribute.equals(CQSConstants.APPROXIMATE_NUMBER_OF_MESSAGES_NOTVISIBLE)
                && !attribute.equals(CQSConstants.APPROXIMATE_NUMBER_OF_MESSAGES_DELAYED)
                && !attribute.equals(CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS)
                && !attribute.equals(CQSConstants.NUMBER_OF_PARTITIONS) && !attribute.equals(CQSConstants.NUMBER_OF_SHARDS) && !attribute.equals(CQSConstants.IS_COMPRESSED)) {
                throw new CMBException(CMBErrorCodes.InvalidAttributeName, "Unknown attribute " + attribute);
            }
        }

        String out = CQSQueuePopulator.getQueueAttributesResponse(queue, attributesList);
        writeResponse(out, response);
View Full Code Here

    } else if (queue.getNumberOfShards() > 1) {
        shard = rand.nextInt(queue.getNumberOfShards());
    }
   
    if (shard < 0 || shard >= queue.getNumberOfShards()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Shard number " + shard +" exceeds number of available shards in queue (" + queue.getNumberOfShards() + ").");
    }
   
    int maxNumberOfMessages = CMBProperties.getInstance().getCQSMaxReceiveMessageCount();
           
        if (useParams && request.getParameter(CQSConstants.MAX_NUMBER_OF_MESSAGES) != null) {
           
          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() + ").");
            }
        }

        List<CQSMessage> messageList = PersistenceFactory.getCQSMessagePersistence().peekQueue(queue.getRelativeUrl(), shard, previousReceiptHandle, nextReceiptHandle, maxNumberOfMessages);
       
View Full Code Here

        HttpServletResponse response = (HttpServletResponse)asyncContext.getResponse();
   
        String messageBody = request.getParameter(CQSConstants.MESSAGE_BODY);

        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>();
       
        String delaySecondsReq = request.getParameter(CQSConstants.DELAY_SECONDS);

        if (delaySecondsReq != null) {

          try {
           
            int delaySeconds = Integer.parseInt(delaySecondsReq);
               
            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);
                }
           
            } catch (NumberFormatException e) {
                throw new CMBException(CMBErrorCodes.InvalidParameterValue, "DelaySeconds must be integer value");
            }
        }
       
        attributes.put(CQSConstants.SENDER_ID, user.getUserId());
        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);
      CQSQueue queue = CQSCache.getCachedQueue(user, request);
     
      int shard = 0;
     
    String shardNumber = request.getParameter("Shard");

    if (shardNumber != null) {
      shard = Integer.parseInt(shardNumber);
    } else if (queue.getNumberOfShards() > 1) {
      shard = rand.nextInt(queue.getNumberOfShards());
    }
 
    if (shard < 0 || shard >= queue.getNumberOfShards()) {
          throw new CMBException(CMBErrorCodes.InvalidParameterValue, "Shard number " + shard +" exceeds number of available shards in queue (" + 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");
        }
       
        request.setReceiptHandles(Arrays.asList(new String[] {receiptHandle}));
        message.setReceiptHandle(receiptHandle);
        message.setMessageId(receiptHandle);
View Full Code Here

        String receiptHandle = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.RECEIPT_HANDLE);
       
        while (suppliedId != null && receiptHandle != 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, "You supplied same identifier for two messages");
            }
           
          idList.add(suppliedId);
           
          if (receiptHandle.isEmpty()) {
                failedList.add(new CQSBatchResultErrorEntry(suppliedId, true, "EmptyValue", "No Value Found for " + this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.RECEIPT_HANDLE));
            } else {
                idMap.put(suppliedId, receiptHandle);
            }
           
          index++;
            suppliedId = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + ".Id");
            receiptHandle = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.RECEIPT_HANDLE);
        }
       
        if (idMap.size() == 0) {
            throw new CMBException(CMBErrorCodes.InvalidQueryParameter, "Both user supplied message Id and receiptHandle are required");
        }

        for (Map.Entry<String, String> entry : idMap.entrySet()) {
            receiptHandle = entry.getValue();
          PersistenceFactory.getCQSMessagePersistence().deleteMessage(queue.getRelativeUrl(), receiptHandle);
View Full Code Here

      logger.debug("Sending request to url:" + url);
       //resp = sendHttpMessage(url);
      resp = send(url, "");
    } catch(Exception e) {
      logger.error("event=send_cqs_message endpoint=" + endPoint + " exception=" + e.toString(), e);
      throw new CMBException(CQSErrorCodes.InternalError, "internal service error");
    }
    return resp;
  }
View Full Code Here

             
                found = true;
              String v = m.group(1) + "Value";
               
                if (requestParams.get(v) == null) {
                    throw new CMBException(CMBErrorCodes.MissingParameter, "The request must contain the parameter Attribute." + m.group(1) + "Value");
                }
               
                filterRequests.put(requestParams.get(k)[0], requestParams.get(v)[0]);
            }
        }
       
        if (!found) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "The request must contain the parameter Attribute.Name");
        }
       
        return filterRequests;
    }
View Full Code Here

            String value = attributes.get(attributeName);
       
            if (attributeName.equals(CQSConstants.VISIBILITY_TIMEOUT)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 0 || v > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.VISIBILITY_TIMEOUT + " must be between 0 and " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut() + " seconds");
                }
               
              queue.setVisibilityTO(v);
                postVars.put(CQSConstants.COL_VISIBILITY_TO, value);
           
            } else if (attributeName.equals(CQSConstants.POLICY)) {
             
            if (value != null && !value.equals("")) {
               
              // validate policy before updating
             
              try {
                new CMBPolicy(value);
              } catch (Exception ex) {
                      logger.warn("event=invalid_policy queue_url=" + queue.getRelativeUrl() + " policy=" + value, ex);
                throw ex;
              }
            }
             
                queue.setPolicy(value);
                postVars.put(CQSConstants.COL_POLICY, value);
           
            } else if (attributeName.equals(CQSConstants.MAXIMUM_MESSAGE_SIZE)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v > CMBProperties.getInstance().getCQSMaxMessageSize()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.MAXIMUM_MESSAGE_SIZE + " cannot be over " + CMBProperties.getInstance().getCQSMaxMessageSize() + " bytes");
                }
               
              queue.setMaxMsgSize(v);
                postVars.put(CQSConstants.COL_MAX_MSG_SIZE, value);
           
            } else if (attributeName.equals(CQSConstants.MESSAGE_RETENTION_PERIOD)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < CMBProperties.getInstance().getCQSMinMessageRetentionPeriod() || v > CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.MESSAGE_RETENTION_PERIOD + " must be between 1 and " + CMBProperties.getInstance().getCQSMaxMessageRetentionPeriod() + " seconds");
                }
             
                queue.setMsgRetentionPeriod(v);
                postVars.put(CQSConstants.COL_MSG_RETENTION_PERIOD, value);
           
            } else if (attributeName.equals(CQSConstants.DELAY_SECONDS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 0 || v > CMBProperties.getInstance().getCQSMaxMessageDelaySeconds()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.DELAY_SECONDS + " must be less than " + CMBProperties.getInstance().getCQSMaxMessageDelaySeconds() + " seconds");
                }
               
              queue.setDelaySeconds(v);
                postVars.put(CQSConstants.COL_DELAY_SECONDS, value);
          
            } else if (attributeName.equals(CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 0 || v > CMBProperties.getInstance().getCMBRequestTimeoutSec()) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, CQSConstants.RECEIVE_MESSAGE_WAIT_TIME_SECONDS + " must be 20 seconds or less");
                }
               
              queue.setReceiveMessageWaitTimeSeconds(v);
                postVars.put(CQSConstants.COL_WAIT_TIME_SECONDS, value);
          
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_PARTITIONS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 1) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_PARTITIONS + " should be at least 1");
                }
               
              queue.setNumberOfPartitions(v);
                postVars.put(CQSConstants.COL_NUMBER_PARTITIONS, value);
          
            } else if (attributeName.equals(CQSConstants.NUMBER_OF_SHARDS)) {
               
              if (!Util.isParsableToInt(value)) {
                    throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value " + value + " for the parameter " + attributeName);
                }
               
              int v = Integer.parseInt(value);
               
              if (v < 1 || v > 100) {
                    throw new CMBException(CMBErrorCodes.InvalidParameterValue, CQSConstants.NUMBER_OF_SHARDS + " should be between 1 and 100");
                }
               
              queue.setNumberOfShards(v);
                postVars.put(CQSConstants.COL_NUMBER_SHARDS, value);
               
              for (int shard=0; shard<v; shard++) {
                PersistenceFactory.getCQSMessagePersistence().checkCacheConsistency(queue.getRelativeUrl(), shard, false);
              }

            } else if (attributeName.equals(CQSConstants.IS_COMPRESSED)) {
             
              boolean isCompressed = Boolean.parseBoolean(value);
              queue.setCompressed(isCompressed);
                postVars.put(CQSConstants.COL_COMPRESSED, value);
             
            } else {
                throw new CMBException(CMBErrorCodes.InvalidAttributeName, "Attribute.Name: " + attributeName + " is not a valid attribute");
            }
        }

        PersistenceFactory.getQueuePersistence().updateQueueAttribute(queue.getRelativeUrl(), postVars);
       
View Full Code Here

     
    String task = request.getParameter("Task");

    if (task == null || task.equals("")) {
      logger.error("event=cqs_manage_service error_code=missing_parameter_task");
      throw new CMBException(CQSErrorCodes.MissingParameter,"Request parameter Task missing.");
    }
   
    String host = request.getParameter("Host");
   
    if (task.equals("ClearCache")) {

      PersistenceFactory.getCQSMessagePersistence().flushAll();
        String out = CQSPopulator.getResponseMetadata();
            writeResponse(out, response);
          return true;
       
    } else if (task.equals("ClearAPIStats")) {

            CMBControllerServlet.initStats();
            String out = CQSPopulator.getResponseMetadata();
            writeResponse(out, response);
        return true;
     
    } else if (task.equals("RemoveRecord")) {
     
      DurablePersistenceFactory.getInstance().delete(AbstractDurablePersistence.CQS_KEYSPACE, CQS_API_SERVERS, host, null, CMB_SERIALIZER.STRING_SERIALIZER, CMB_SERIALIZER.STRING_SERIALIZER);
      String out = CNSPopulator.getResponseMetadata();
            writeResponse(out, response);
      return true;
     
    } else {
      logger.error("event=cqs_manage_service error_code=invalid_task_parameter valid_values=ClearCache,ClearAPIStats,RemoveRecord");
      throw new CMBException(CNSErrorCodes.InvalidParameterValue,"Request parameter Task is missing or invalid. Valid values are ClearQueues, ClearAPIStats, RemoveRecord.");
    }
    }
View Full Code Here

      CQSQueue queue = CQSCache.getCachedQueue(user, request);
        String receiptHandle = request.getParameter(CQSConstants.RECEIPT_HANDLE);
       
        if (receiptHandle == null) {
            throw new CMBException(CMBErrorCodes.MissingParameter, "ReceiptHandle not found");
        }

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

        int visibilityTO = Integer.parseInt(visibilityTimeout);

        if (visibilityTO < 0 || visibilityTO > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
            throw new CMBException(CMBErrorCodes.InvalidParameterValue, "VisibilityTimeout is limited from 0 to " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut() + " seconds");
        }
       
    PersistenceFactory.getCQSMessagePersistence().changeMessageVisibility(queue, receiptHandle, visibilityTO);

        String out = CQSMessagePopulator.getChangeMessageVisibilityResponse();
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.