Package com.comcast.cmb.common.util

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


    while (policyIterator.hasNext()) {
     
      String policyAttribute = policyIterator.next();
     
      if (!CMBPolicy.POLICY_ATTRIBUTES.contains(policyAttribute)) {
          throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value for the parameter Policy");
      }
     
    }
 
    JSONArray stmts = json.getJSONArray("Statement");
   
    if (stmts != null) {
     
      for (int i=0; i<stmts.length(); i++) {
       
        Iterator<String> stmtIterator = stmts.getJSONObject(i).keys();
       
        while (stmtIterator.hasNext()) {
         
          String statementAttribute = stmtIterator.next();
         
          if (!CMBPolicy.STATEMENT_ATTRIBUTES.contains(statementAttribute)) {
                throw new CMBException(CMBErrorCodes.InvalidAttributeValue, "Invalid value for the parameter Policy");
          }
        }
      }
    }
       
View Full Code Here


        HttpServletRequest request = (HttpServletRequest)asyncContext.getRequest();
     
        writeHeartBeat();
     
      if (!CMBProperties.getInstance().getCQSServiceEnabled()) {
            throw new CMBException(CMBErrorCodes.InternalError, "CQS service is disabled");
      }
     
        if (!actionMap.containsKey(action)) {
            throw new CMBException(CMBErrorCodes.InvalidAction, action + " is not a valid action");
        }
       
      long ts1 = System.currentTimeMillis();
       
        if (messagePersistence == null || actionMap == null) {
            init();
        }
             
        CQSQueue queue = null;
       
        if (!action.equals("CreateQueue") && !action.equals("healthCheckShallow") && !action.equals("HealthCheck") && !action.equals("ManageService") && !action.equals("GetQueueUrl") && !action.equals("ListQueues") && !action.equals("GetAPIStats")) {
            queue = CQSCache.getCachedQueue(user, request);
      }

        if (isAuthenticationRequired(action)) {
       
            CMBPolicy policy = new CMBPolicy();
           
            if (queue != null) {
              policy.fromString(queue.getPolicy());
            }
           
            if (!actionMap.get(action).isActionAllowed(user, request, "CQS", policy)) {
                throw new CMBException(CMBErrorCodes.AccessDenied, "You don't have permission for " + actionMap.get(action).getName());
            }
        }
       
      valueAccumulator.addToCounter(AccumulatorName.CQSPreDoAction, System.currentTimeMillis() - ts1);
View Full Code Here

        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) {
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, "Id " + suppliedId + " repeated");
            }
         
          idList.add(suppliedId);
           
          if (receiptHandle.isEmpty()) {
                failedList.add(new CQSBatchResultErrorEntry(suppliedId, true, "ReceiptHandleIsInvalid", "No Value Found for " + this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.RECEIPT_HANDLE));
            } else {
           
              String visibilityTimeoutStr = request.getParameter(this.actionName + CQSConstants.REQUEST_ENTRY + index + "." + CQSConstants.VISIBILITY_TIMEOUT);
               
              if (visibilityTimeoutStr != null) {
               
                Integer visibilityTimeout = Integer.parseInt(visibilityTimeoutStr);
                   
                if (visibilityTimeout < 0 || visibilityTimeout > CMBProperties.getInstance().getCQSMaxVisibilityTimeOut()) {
                        throw new CMBException(CMBErrorCodes.InvalidParameterValue, "VisibilityTimeout is limited from 0 to " + CMBProperties.getInstance().getCQSMaxVisibilityTimeOut() + " seconds");
                    }
                }
                idMap.put(suppliedId, Arrays.asList(receiptHandle, visibilityTimeoutStr));
            }
         
            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");
        }

        List<String> successList = new ArrayList<String>();
       
        for (Map.Entry<String, List<String>> entry : idMap.entrySet()) {
View Full Code Here

        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();
        writeResponse(out, response);
       
View Full Code Here

        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
     
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");
        }
       
        if (!isValidReceiptHandle(receiptHandle)) {
            throw new CMBException(CQSErrorCodes.ReceiptHandleInvalid, "The input receipt handle is invalid");
        }

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

        instream.close();
      }

      logger.debug("event=http_post_error endpoint=" + endpoint + " error_code=" + statusCode);
      throw new CMBException(new CMBErrorCodes(statusCode, "HttpError"), "Unreachable endpoint " + endpoint + " returns status code " + statusCode);

    } else {

      if (entity != null) {
        EntityUtils.consume(entity);
View Full Code Here

            + endpoint);
      }

    } catch (Exception ex) {
      logger.warn("event=send_cqs_message endpoint=" + endpoint + "\" message=\"" + message, ex);
      throw new CMBException(CNSErrorCodes.InternalError, "internal service error");
    }

    logger.debug("event=send_cqs_message endpoint=" + endpoint + " message=\"" + message + "\"");
  }
View Full Code Here

      sqs.sendMessage(new SendMessageRequest(url, msg));     
     
    } catch(Exception ex) {
      logger.warn("event=send_sqs_message endpoint=" + endpoint + "\" message=\"" + message, ex);
      throw new CMBException(CNSErrorCodes.InternalError, "internal service error");
    }

    logger.debug("event=send_sqs_message endpoint=" + endpoint + " message=\"" + message + "\"");
  }
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.