use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.
the class RestLiAnnotationReader method buildActionParam.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Parameter buildActionParam(final Method method, final AnnotationSet annotations, final Class<?> paramType) {
ActionParam actionParam = annotations.get(ActionParam.class);
Optional optional = annotations.get(Optional.class);
String paramName = actionParam.value();
Class<? extends TyperefInfo> typerefInfoClass = actionParam.typeref();
try {
Parameter param = new Parameter(paramName, paramType, getDataSchema(paramType, getSchemaFromTyperefInfo(typerefInfoClass)), optional != null, getDefaultValueData(optional), Parameter.ParamType.POST, true, annotations);
return param;
} catch (TemplateRuntimeException e) {
throw new ResourceConfigException("DataSchema for parameter '" + paramName + "' of type " + paramType.getSimpleName() + " on " + buildMethodMessage(method) + "cannot be found; type is invalid or requires typeref", e);
} catch (Exception e) {
throw new ResourceConfigException("Typeref for parameter '" + paramName + "' on " + buildMethodMessage(method) + " cannot be instantiated, " + e.getMessage(), e);
}
}
use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.
the class RestLiAnnotationReader method buildAssocKeyParam.
private static Parameter<?> buildAssocKeyParam(final ResourceModel model, final Method method, final AnnotationSet annotations, final Class<?> paramType, final Class<?> paramAnnotationType) {
Parameter.ParamType parameter = null;
String assocKeyParamValue = null;
Class<? extends TyperefInfo> typerefInfoClass = null;
if (paramAnnotationType.equals(AssocKey.class)) {
parameter = Parameter.ParamType.KEY;
assocKeyParamValue = annotations.get(AssocKey.class).value();
typerefInfoClass = annotations.get(AssocKey.class).typeref();
} else if (paramAnnotationType.equals(AssocKeyParam.class)) {
parameter = Parameter.ParamType.ASSOC_KEY_PARAM;
assocKeyParamValue = annotations.get(AssocKeyParam.class).value();
typerefInfoClass = annotations.get(AssocKeyParam.class).typeref();
} else {
throw new ResourceConfigException("Param Annotation type must be 'AssocKeysParam' or the deprecated 'AssocKey' for AssocKey");
}
Optional optional = annotations.get(Optional.class);
if (!checkAssocKey(model.getKeys(), assocKeyParamValue)) {
throw new ResourceConfigException("Non-existing assocKey '" + assocKeyParamValue + "' on " + buildMethodMessage(method));
}
try {
@SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter(assocKeyParamValue, paramType, getDataSchema(paramType, getSchemaFromTyperefInfo(typerefInfoClass)), optional != null, getDefaultValueData(optional), parameter, true, annotations);
return param;
} catch (TemplateRuntimeException e) {
throw new ResourceConfigException("DataSchema for assocKey '" + assocKeyParamValue + "' of type " + paramType.getSimpleName() + " on " + buildMethodMessage(method) + "cannot be found; type is invalid or requires typeref", e);
} catch (Exception e) {
throw new ResourceConfigException("Typeref for assocKey '" + assocKeyParamValue + "' on " + buildMethodMessage(method) + " cannot be instantiated, " + e.getMessage(), e);
}
}
use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.
the class Parameter method getDefaultValue.
public Object getDefaultValue() {
if (_defaultValueData == null) {
return null;
}
final Object result;
if (_defaultValueData instanceof String) {
final String defaultValueString = (String) _defaultValueData;
try {
if (getType().isArray()) {
final DataList valueAsDataList = _codec.stringToList(defaultValueString);
result = DataTemplateUtil.convertDataListToArray(valueAsDataList, getItemType());
} else if (DataTemplate.class.isAssignableFrom(getType())) {
final Object input;
if (AbstractArrayTemplate.class.isAssignableFrom(getType())) {
input = _codec.stringToList(defaultValueString);
} else if (AbstractMapTemplate.class.isAssignableFrom(getType()) || UnionTemplate.class.isAssignableFrom(getType()) || RecordTemplate.class.isAssignableFrom(getType())) {
input = _codec.stringToMap(defaultValueString);
} else {
input = defaultValueString;
}
result = DataTemplateUtil.wrap(input, getType().asSubclass(DataTemplate.class));
validate((DataTemplate<?>) result, getType());
} else {
result = ValueConverter.coerceString(defaultValueString, getType());
}
} catch (TemplateOutputCastException e) {
throw new ResourceConfigException(e.getMessage(), e);
} catch (IllegalArgumentException e) {
throw new ResourceConfigException("Default value for parameter of type \"" + getType().getName() + "\" is not supported: " + e.getMessage(), e);
} catch (IOException e) {
throw new ResourceConfigException("Default value for parameter of type \"" + getType().getName() + "\" is not supported: " + e.getMessage(), e);
}
} else {
result = _defaultValueData;
}
return result;
}
use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.
the class RestLiAnnotationReader method getActionReturnClass.
private static Class<?> getActionReturnClass(ResourceModel model, Method method, Action actionAnno, String actionName) {
final Type returnType = getLogicalReturnType(method);
ResourceMethodDescriptor existingMethodDescriptor = model.findActionMethod(actionName, getActionResourceLevel(actionAnno, model));
if (existingMethodDescriptor != null) {
throw new ResourceConfigException("Found duplicate @Action method named '" + actionName + "' on class '" + model.getResourceClass().getName() + '\'');
}
Class<?> returnClass = getBoxedTypeFromPrimitive(getLogicalReturnClass(method));
if (ActionResult.class.isAssignableFrom(returnClass)) {
assert (returnType instanceof ParameterizedType);
final ParameterizedType paramReturnType = (ParameterizedType) returnType;
final Type[] actualReturnTypes = paramReturnType.getActualTypeArguments();
assert (actualReturnTypes.length == 1);
if (!(actualReturnTypes[0] instanceof Class<?>)) {
throw new ResourceConfigException("Unsupported type parameter for ActionResult<?>.");
}
returnClass = (Class<?>) actualReturnTypes[0];
if (returnClass == Void.class) {
returnClass = Void.TYPE;
}
}
return returnClass;
}
use of com.linkedin.restli.server.ResourceConfigException in project rest.li by linkedin.
the class RestLiAnnotationReader method buildPagingContextParam.
private static Parameter<?> buildPagingContextParam(final AnnotationSet annotations, final Class<?> paramType, final Class<?> paramAnnotationType) {
if (!paramType.equals(PagingContext.class)) {
throw new ResourceConfigException("Incorrect data type for param: @" + PagingContextParam.class.getSimpleName() + " or @" + Context.class.getSimpleName() + " parameter annotation must be of type " + PagingContext.class.getName());
}
PagingContext defaultContext = null;
Parameter.ParamType parameter = null;
if (paramAnnotationType.equals(PagingContextParam.class)) {
PagingContextParam pagingContextParam = annotations.get(PagingContextParam.class);
defaultContext = new PagingContext(pagingContextParam.defaultStart(), pagingContextParam.defaultCount(), false, false);
parameter = Parameter.ParamType.PAGING_CONTEXT_PARAM;
} else if (paramAnnotationType.equals(Context.class)) {
Context contextParam = annotations.get(Context.class);
defaultContext = new PagingContext(contextParam.defaultStart(), contextParam.defaultCount(), false, false);
parameter = Parameter.ParamType.CONTEXT;
} else {
throw new ResourceConfigException("Param Annotation type must be 'PagingContextParam' or the deprecated 'Context' for PagingContext");
}
Optional optional = annotations.get(Optional.class);
@SuppressWarnings({ "unchecked", "rawtypes" }) Parameter<?> param = new Parameter("", paramType, null, optional != null, defaultContext, parameter, false, annotations);
return param;
}
Aggregations