@SuppressWarnings("rawtypes") @Override public void handle(final DispatchTask task) { final BaseCmd cmd = task.getCmd(); final Map params = task.getParams(); final List<String> expectedParamNames = getParamNamesForCommand(cmd); final StringBuilder errorMsg = new StringBuilder(ERROR_MSG_PREFIX); boolean foundUnknownParam = false; for (final Object actualParamName : params.keySet()) { // If none of the expected params matches, we have an unknown param boolean matchedCurrentParam = false; for (final String expectedName : expectedParamNames) { if (expectedName.equalsIgnoreCase((String) actualParamName)) { matchedCurrentParam = true; break; } } if (!matchedCurrentParam && !((String)actualParamName).equalsIgnoreCase("expires") && !((String)actualParamName).equalsIgnoreCase("signatureversion")) { errorMsg.append(" ").append(actualParamName); foundUnknownParam= true; } } if (foundUnknownParam) { s_logger.warn(String.format("Received unknown parameters for command %s. %s", cmd.getActualCommandName(), errorMsg)); } }
@SuppressWarnings("unchecked") @Override public void handle(final DispatchTask task) { task.getCmd().validateSpecificParameters(task.getParams()); }
/** * display flag is used to control the display of the resource only to the end user. It doesnt affect Root Admin. * @return display flag */ public boolean isDisplay(){ CallContext context = CallContext.current(); Map<Object, Object> contextMap = context.getContextParameters(); boolean isDisplay = true; // Iterate over all the first class entities in context and check their display property. for(Map.Entry<Object, Object> entry : contextMap.entrySet()){ try{ Object key = entry.getKey(); Class clz = Class.forName((String)key); if(Displayable.class.isAssignableFrom(clz)){ final Object objVO = getEntityVO(clz, entry.getValue()); isDisplay = ((Displayable) objVO).isDisplay(); } // If the flag is false break immediately if(!isDisplay) break; } catch (Exception e){ s_logger.trace("Caught exception while checking first class entities for display property, continuing on", e); } } context.setEventDisplayEnabled(isDisplay); return isDisplay; }
cmdObj.configure(); cmdObj.setFullUrlParams(paramMap); cmdObj.setResponseType(responseType); cmdObj.setHttpMethod(paramMap.get(ApiConstants.HTTPMETHOD).toString());
final List<Field> cmdFields = cmd.getParamFields(); if (parameterAnnotation.required()) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Unable to execute API command " + cmd.getCommandName().substring(0, cmd.getCommandName().length() - 8) + " due to missing parameter " + parameterAnnotation.name()); } catch (final IllegalArgumentException argEx) { if (s_logger.isDebugEnabled()) { s_logger.debug("Unable to execute API command " + cmd.getCommandName() + " due to invalid value " + paramObj + " for parameter " + parameterAnnotation.name()); cmd.getCommandName().substring(0, cmd.getCommandName().length() - 8) + " due to invalid value " + paramObj + " for parameter " + parameterAnnotation.name()); } catch (final ParseException parseEx) { if (s_logger.isDebugEnabled()) { s_logger.debug("Invalid date parameter " + paramObj + " passed to command " + cmd.getCommandName().substring(0, cmd.getCommandName().length() - 8)); cmd.getCommandName().substring(0, cmd.getCommandName().length() - 8) + ", please pass dates in the format mentioned in the api documentation"); } catch (final InvalidParameterValueException invEx) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Unable to execute API command " + cmd.getCommandName().substring(0, cmd.getCommandName().length() - 8) + " due to invalid value. " + invEx.getMessage()); } catch (final CloudRuntimeException cloudEx) { s_logger.error("CloudRuntimeException", cloudEx); cmd.getCommandName().substring(0, cmd.getCommandName().length() - 8)); throw new CloudRuntimeException("Internal error initializing parameters for command " + cmd.getCommandName() + " [field " + field.getName() + " is not accessible]", e);
s_logger.error("Error initializing command " + cmdObj.getCommandName() + ", field " + field.getName() + " is not accessible."); throw new CloudRuntimeException("Internal error initializing parameters for command " + cmdObj.getCommandName() + " [field " + field.getName() + " is not accessible]");
protected List<String> getParamNamesForCommand(final BaseCmd cmd) { final List<String> paramNames = new ArrayList<String>(); // The expected param names are all the specific for the current command class ... for (final Field field : cmd.getParamFields()) { final Parameter parameterAnnotation = field.getAnnotation(Parameter.class); paramNames.add(parameterAnnotation.name()); } // ... plus the default ones paramNames.addAll(defaultParamNames); return paramNames; } }
private void doAccessChecks(BaseCmd cmd, Map<Object, AccessType> entitiesToAccess) { Account caller = CallContext.current().getCallingAccount(); // due to deleteAccount design flaw CLOUDSTACK-6588, we should still include those removed account as well to clean up leftover resources from that account Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId()); if (cmd instanceof BaseAsyncCreateCmd) { // check that caller can access the owner account. _accountMgr.checkAccess(caller, null, false, owner); } if (!entitiesToAccess.isEmpty()) { // check that caller can access the owner account. _accountMgr.checkAccess(caller, null, false, owner); for (Map.Entry<Object,AccessType>entry : entitiesToAccess.entrySet()) { Object entity = entry.getKey(); if (entity instanceof ControlledEntity) { _accountMgr.checkAccess(caller, entry.getValue(), true, (ControlledEntity) entity); } else if (entity instanceof InfrastructureEntity) { // FIXME: Move this code in adapter, remove code from // Account manager } } } }
final List<Field> allFields = getAllFieldsForClass(this.getClass()); final List<Field> validFields = new ArrayList<Field>(); final Account caller = CallContext.current().getCallingAccount();
sb.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date)fieldValue)).append("</").append(serializedName.value()).append(">"); log.append("<").append(serializedName.value()).append(">").append(BaseCmd.getDateString((Date)fieldValue)).append("</").append(serializedName.value()).append(">"); } else { String resultString = escapeSpecialXmlChars(fieldValue.toString());
cmdClass = getCmdClass(cmdName); if (cmdClass != null) { responseName = ((BaseCmd)cmdClass.newInstance()).getCommandName(); } else { responseName = "errorresponse";
@Test public void testGetActualCommandName(){ BaseCmd cmd1 = new Cmd1(); BaseCmd cmd2 = new Cmd2(); assertEquals(NON_EXPECTED_COMMAND_NAME, CMD1_NAME, cmd1.getActualCommandName()); assertEquals(NON_EXPECTED_COMMAND_NAME, CMD2_NAME, cmd2.getActualCommandName()); } }
@Override public void validateSpecificParameters(final Map<String, String> params){ super.validateSpecificParameters(params); final Object pageSizeObj = params.get(ApiConstants.PAGE_SIZE); Long pageSize = null; if (pageSizeObj != null) { pageSize = Long.valueOf((String)pageSizeObj); } if (params.get(ApiConstants.PAGE) == null && pageSize != null && !pageSize.equals(BaseListCmd.s_pageSizeUnlimited)) { final ServerApiException ex = new ServerApiException(ApiErrorCode.PARAM_ERROR, "\"page\" parameter is required when \"pagesize\" is specified"); ex.setCSErrorCode(CSExceptionErrorCode.getCSErrCode(ex.getClass().getName())); throw ex; } else if (pageSize == null && (params.get(ApiConstants.PAGE) != null)) { throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "\"pagesize\" parameter is required when \"page\" is specified"); } } }
cmdClass = getCmdClass(cmdName); if (cmdClass != null) { responseName = ((BaseCmd)cmdClass.newInstance()).getCommandName(); } else { responseName = "errorresponse";