use of org.glassfish.api.Param in project Payara by payara.
the class MapInjectionResolver method checkAgainstAcceptableValues.
/**
* Check if the value string is one of the strings in the list of
* acceptable values in the @Param annotation on the target.
*
* @param target the target field
* @param paramValueStr the parameter value
*/
private static void checkAgainstAcceptableValues(AnnotatedElement target, String paramValueStr) {
Param param = target.getAnnotation(Param.class);
String acceptable = param.acceptableValues();
String paramName = CommandModel.getParamName(param, target);
if (ok(acceptable) && ok(paramValueStr)) {
String[] ss = acceptable.split(",");
for (String s : ss) {
if (paramValueStr.equals(s.trim()))
// matched, value is good
return;
}
// didn't match any, error
throw new UnacceptableValueException(localStrings.getLocalString("UnacceptableValue", "Invalid parameter: {0}. Its value is {1} " + "but it isn''t one of these acceptable values: {2}", paramName, paramValueStr, acceptable));
}
}
use of org.glassfish.api.Param in project Payara by payara.
the class MapInjectionResolver method getValue.
@Override
public <V> V getValue(Object component, AnnotatedElement target, Type genericType, Class<V> type) throws MultiException {
// look for the name in the list of parameters passed.
Param param = target.getAnnotation(Param.class);
String paramName = CommandModel.getParamName(param, target);
if (param.primary()) {
// this is the primary parameter for the command
List<String> value = parameters.get("DEFAULT");
if (value != null && value.size() > 0) {
/*
* If the operands are uploaded files, replace the
* client-provided values with the paths to the uploaded files.
* XXX - assume the lists are in the same order.
*/
final List<String> filePaths = getUploadedFileParamValues("DEFAULT", type, optionNameToUploadedFileMap);
if (filePaths != null) {
value = filePaths;
// replace the file name operands with the uploaded files
parameters.set("DEFAULT", value);
} else {
for (String s : value) {
checkAgainstAcceptableValues(target, s);
}
}
// let's also copy this value to the cmd with a real name
parameters.set(paramName, value);
V paramValue = (V) convertListToObject(target, type, value);
return paramValue;
}
}
if (param.multiple()) {
List<String> value = parameters.get(paramName);
if (value != null && value.size() > 0) {
final List<String> filePaths = getUploadedFileParamValues(paramName, type, optionNameToUploadedFileMap);
if (filePaths != null) {
value = filePaths;
// replace the file name operands with the uploaded files
parameters.set(paramName, value);
} else {
for (String s : value) {
checkAgainstAcceptableValues(target, s);
}
}
}
parameters.set(paramName, value);
V paramValue = (V) convertListToObject(target, type, value);
return paramValue;
}
String paramValueStr = getParamValueString(parameters, param, target, context);
/*
* If the parameter is an uploaded file, replace the client-provided
* value with the path to the uploaded file.
*/
final String fileParamValueStr = getUploadedFileParamValue(paramName, type, optionNameToUploadedFileMap);
if (fileParamValueStr != null) {
paramValueStr = fileParamValueStr;
parameters.set(paramName, paramValueStr);
}
checkAgainstAcceptableValues(target, paramValueStr);
return paramValueStr != null ? (V) convertStringToObject(target, type, paramValueStr) : (V) getParamField(component, target);
}
use of org.glassfish.api.Param in project Payara by payara.
the class ResourceUtil method getMethodMetaData.
/**
* Constructs and returns the resource method meta-data.
*
* @param command the command associated with the resource method
* @param commandParamsToSkip the command parameters for which not to
* include the meta-data.
* @param habitat the habitat
* @param logger the logger to use
* @return MethodMetaData the meta-data store for the resource method.
*/
public static MethodMetaData getMethodMetaData(String command, HashMap<String, String> commandParamsToSkip, ServiceLocator habitat) {
MethodMetaData methodMetaData = new MethodMetaData();
if (command != null) {
Collection<CommandModel.ParamModel> params;
if (commandParamsToSkip == null) {
params = getParamMetaData(command, habitat);
} else {
params = getParamMetaData(command, commandParamsToSkip.keySet(), habitat);
}
if (params != null) {
Iterator<CommandModel.ParamModel> iterator = params.iterator();
CommandModel.ParamModel paramModel;
while (iterator.hasNext()) {
paramModel = iterator.next();
Param param = paramModel.getParam();
ParameterMetaData parameterMetaData = getParameterMetaData(paramModel);
String parameterName = (param.primary()) ? "id" : paramModel.getName();
// If the Param has an alias, use it instead of the name
String alias = param.alias();
if (alias != null && (!alias.isEmpty())) {
parameterName = alias;
}
methodMetaData.putParameterMetaData(parameterName, parameterMetaData);
}
}
}
return methodMetaData;
}
use of org.glassfish.api.Param in project Payara by payara.
the class Util method getMethodParameterList.
public static String getMethodParameterList(CommandModel cm, boolean withType, boolean includeOptional) {
StringBuilder sb = new StringBuilder();
Collection<CommandModel.ParamModel> params = cm.getParameters();
if ((params != null) && (!params.isEmpty())) {
String sep = "";
for (CommandModel.ParamModel model : params) {
Param param = model.getParam();
boolean include = true;
if (param.optional() && !includeOptional) {
continue;
}
sb.append(sep);
if (withType) {
String type = model.getType().getName();
if (model.getType().isArray()) {
type = model.getType().getName().substring(2);
type = type.substring(0, type.length() - 1) + "[]";
} else if (type.startsWith("java.lang")) {
type = model.getType().getSimpleName();
}
sb.append(type);
}
sb.append(" _").append(Util.eleminateHypen(model.getName()));
sep = ", ";
}
}
return sb.toString();
}
use of org.glassfish.api.Param in project Payara by payara.
the class CommandRunnerImpl method injectParameters.
public static boolean injectParameters(final CommandModel model, final Object injectionTarget, final InjectionResolver<Param> injector, final ActionReport report) {
if (injectionTarget instanceof GenericCrudCommand) {
GenericCrudCommand c = GenericCrudCommand.class.cast(injectionTarget);
c.setInjectionResolver(injector);
}
// inject
try {
injectionMgr.inject(injectionTarget, injector);
} catch (UnsatisfiedDependencyException e) {
Param param = e.getAnnotation(Param.class);
CommandModel.ParamModel paramModel = null;
for (CommandModel.ParamModel pModel : model.getParameters()) {
if (pModel.getParam().equals(param)) {
paramModel = pModel;
break;
}
}
String errorMsg;
final String usage = getUsageText(model);
if (paramModel != null) {
String paramName = paramModel.getName();
String paramDesc = paramModel.getLocalizedDescription();
if (param.primary()) {
errorMsg = adminStrings.getLocalString("commandrunner.operand.required", "Operand required.");
} else if (param.password()) {
errorMsg = adminStrings.getLocalString("adapter.param.missing.passwordfile", "{0} command requires the passwordfile " + "parameter containing {1} entry.", model.getCommandName(), paramName);
} else if (paramDesc != null) {
errorMsg = adminStrings.getLocalString("admin.param.missing", "{0} command requires the {1} parameter ({2})", model.getCommandName(), paramName, paramDesc);
} else {
errorMsg = adminStrings.getLocalString("admin.param.missing.nodesc", "{0} command requires the {1} parameter", model.getCommandName(), paramName);
}
} else {
errorMsg = adminStrings.getLocalString("admin.param.missing.nofound", "Cannot find {1} in {0} command model, file a bug", model.getCommandName(), e.getUnsatisfiedName());
}
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(errorMsg);
report.setFailureCause(e);
ActionReport.MessagePart childPart = report.getTopMessagePart().addChild();
childPart.setMessage(usage);
return false;
} catch (MultiException e) {
// If the cause is UnacceptableValueException -- we want the message
// from it. It is wrapped with a less useful Exception.
Exception exception = null;
for (Throwable th : e.getErrors()) {
Throwable cause = th;
while (cause != null) {
if ((cause instanceof UnacceptableValueException) || (cause instanceof IllegalArgumentException)) {
exception = (Exception) th;
break;
}
cause = cause.getCause();
}
}
if (exception == null) {
// Not an UnacceptableValueException or IllegalArgumentException
exception = e;
}
logger.log(Level.SEVERE, KernelLoggerInfo.invocationException, exception);
report.setActionExitCode(ActionReport.ExitCode.FAILURE);
report.setMessage(exception.getMessage());
report.setFailureCause(exception);
ActionReport.MessagePart childPart = report.getTopMessagePart().addChild();
childPart.setMessage(getUsageText(model));
return false;
}
checkAgainstBeanConstraints(injectionTarget, model.getCommandName());
return true;
}
Aggregations