Search in sources :

Example 1 with Action

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);
}
Also used : StringMap(com.linkedin.data.template.StringMap) Action(com.linkedin.restli.server.annotations.Action)

Example 2 with Action

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;
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) Tone(com.linkedin.restli.examples.greetings.api.Tone) AtomicLong(java.util.concurrent.atomic.AtomicLong) Action(com.linkedin.restli.server.annotations.Action)

Example 3 with Action

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));
}
Also used : FieldDef(com.linkedin.data.template.FieldDef) Action(com.linkedin.restli.server.annotations.Action) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) ResourceConfigException(com.linkedin.restli.server.ResourceConfigException) DataMap(com.linkedin.data.DataMap)

Example 4 with Action

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);
}
Also used : StringMap(com.linkedin.data.template.StringMap) Action(com.linkedin.restli.server.annotations.Action)

Aggregations

Action (com.linkedin.restli.server.annotations.Action)4 StringMap (com.linkedin.data.template.StringMap)2 DataMap (com.linkedin.data.DataMap)1 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 TyperefDataSchema (com.linkedin.data.schema.TyperefDataSchema)1 FieldDef (com.linkedin.data.template.FieldDef)1 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)1 Tone (com.linkedin.restli.examples.greetings.api.Tone)1 ResourceConfigException (com.linkedin.restli.server.ResourceConfigException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1