use of com.linkedin.restli.server.annotations.Action in project rest.li by linkedin.
the class TwitterAccountsResource method closeAccounts.
@Action(name = "closeAccounts")
public StringMap closeAccounts(@ActionParam("emailAddresses") StringArray emailAddresses, @ActionParam("someFlag") boolean someFlag, @ActionParam("options") @Optional StringMap options) {
StringMap res = new StringMap();
res.put("numClosed", "5");
res.put("resultCode", "11");
return new StringMap(res);
}
use of com.linkedin.restli.server.annotations.Action in project rest.li by linkedin.
the class GreetingsResourceImpl method updateTone.
/**
* a more concrete example of custom action<br>
* resource level determines the granularity of the action<br>
* mismatching the resource level in the request throws exception and will respond HTTP
* 400
*
* @param resource
* Instance of the resource class. This is not part of the action method and is
* needed because this implementation is not an actual resource.
*/
@Action(name = "updateTone", resourceLevel = ResourceLevel.ENTITY)
public // resource class instance, and is not part of the generated REST method.
Greeting updateTone(BaseResource resource, @ActionParam("newTone") @Optional Tone newTone, @ActionParam("delOld") @Optional("false") Boolean delOld) {
// the way to get entity key in action
Long key = resource.getContext().getPathKeys().get(_resourceName + "Id");
Greeting g = _db.get(key);
if (g == null) {
// HTTP 404
return g;
}
// delete existing Greeting and assign new key
if (delOld) {
_db.remove(key);
key = _idSeq.incrementAndGet();
g.setId(key);
}
Tone t;
// omitting it in request results a null value
if (newTone == null) {
t = DEFAULT_TONE;
} else {
t = newTone;
}
g.setTone(t);
_db.put(key, g);
return g;
}
use of com.linkedin.restli.server.annotations.Action in project rest.li by linkedin.
the class RestLiAnnotationReader method addActionResourceMethod.
/**
* Add the given action method to the given resource model, validating the method is a action before adding.
* @param model provides the model to add the method to.
* @param method provides the method to add to the model.
* @throws ResourceConfigException on validation errors.
*/
private static void addActionResourceMethod(final ResourceModel model, final Method method) {
Action actionAnno = method.getAnnotation(Action.class);
if (actionAnno == null) {
return;
}
String actionName = actionAnno.name();
List<Parameter<?>> parameters = getParameters(model, method, ResourceMethod.ACTION);
Class<?> returnClass = getActionReturnClass(model, method, actionAnno, actionName);
TyperefDataSchema returnTyperefSchema = getActionTyperefDataSchema(model, actionAnno, actionName);
validateActionReturnType(model, method, returnClass, returnTyperefSchema);
if (!Modifier.isPublic(method.getModifiers())) {
throw new ResourceConfigException(String.format("Resource '%s' contains non-public action method '%s'.", model.getName(), method.getName()));
}
RecordDataSchema recordDataSchema = DynamicRecordMetadata.buildSchema(method.getName(), parameters);
RecordDataSchema actionReturnRecordDataSchema;
FieldDef<?> returnFieldDef;
if (returnClass != Void.TYPE) {
@SuppressWarnings({ "unchecked", "rawtypes" }) FieldDef<?> nonVoidFieldDef = new FieldDef(ActionResponse.VALUE_NAME, returnClass, getDataSchema(returnClass, returnTyperefSchema));
returnFieldDef = nonVoidFieldDef;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.singleton((returnFieldDef)));
} else {
returnFieldDef = null;
actionReturnRecordDataSchema = DynamicRecordMetadata.buildSchema(ActionResponse.class.getName(), Collections.<FieldDef<?>>emptyList());
}
if (model.getResourceLevel() == ResourceLevel.ENTITY && actionAnno.resourceLevel() == ResourceLevel.COLLECTION) {
throw new ResourceConfigException(String.format("Resource '%s' is a simple resource, it cannot contain actions at resource level \"COLLECTION\".", model.getName()));
}
DataMap annotationsMap = ResourceModelAnnotation.getAnnotationsMap(method.getAnnotations());
addDeprecatedAnnotation(annotationsMap, method);
model.addResourceMethodDescriptor(ResourceMethodDescriptor.createForAction(method, parameters, actionName, getActionResourceLevel(actionAnno, model), returnFieldDef, actionReturnRecordDataSchema, recordDataSchema, getInterfaceType(method), annotationsMap));
}
use of com.linkedin.restli.server.annotations.Action in project rest.li by linkedin.
the class TwitterAccountsResource method closeAccounts.
/**
* This is a sample Javadoc comment for an action. This method below takes in parameters that
* specify what accounts to close
*
* @return
*
* a map that contains details about account closures. This return description here is intentionally
* long and poorly spaced in between
* so that I can make sure it shows up correctly in the restspec.json
*
*
* @param emailAddresses Array of email addresses
* @param someFlag flag for some custom behavior
* @param options a map specifying some custom options
*
*/
@Action(name = "closeAccounts")
public StringMap closeAccounts(@ActionParam("emailAddresses") StringArray emailAddresses, @ActionParam("someFlag") boolean someFlag, @ActionParam("options") @Optional StringMap options) {
StringMap res = new StringMap();
res.put("numClosed", "5");
res.put("resultCode", "11");
return new StringMap(res);
}
Aggregations