Package com.linkedin.restli.internal.server.methods.arguments

Source Code of com.linkedin.restli.internal.server.methods.arguments.ActionArgumentBuilder

/*
   Copyright (c) 2012 LinkedIn Corp.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

/**
* $Id: $
*/

package com.linkedin.restli.internal.server.methods.arguments;

import com.linkedin.data.DataMap;
import com.linkedin.data.schema.validation.CoercionMode;
import com.linkedin.data.schema.validation.RequiredMode;
import com.linkedin.data.schema.validation.ValidateDataAgainstSchema;
import com.linkedin.data.schema.validation.ValidationOptions;
import com.linkedin.data.schema.validation.ValidationResult;
import com.linkedin.data.template.DynamicRecordTemplate;
import com.linkedin.data.template.TemplateOutputCastException;
import com.linkedin.r2.message.rest.RestRequest;
import com.linkedin.restli.common.HttpStatus;
import com.linkedin.restli.internal.server.RoutingResult;
import com.linkedin.restli.internal.server.model.Parameter;
import com.linkedin.restli.internal.server.model.ResourceMethodDescriptor;
import com.linkedin.restli.internal.server.util.DataMapUtils;
import com.linkedin.restli.server.RestLiRequestData;
import com.linkedin.restli.server.RestLiRequestDataImpl;
import com.linkedin.restli.server.RoutingException;

import java.util.List;

/**
* @author Josh Walker
* @version $Revision: $
*/

public class ActionArgumentBuilder implements RestLiArgumentBuilder
{
  @Override
  public Object[] buildArguments(RestLiRequestData requestData, RoutingResult routingResult)
  {
    ResourceMethodDescriptor resourceMethodDescriptor = routingResult.getResourceMethod();
    List<Parameter<?>> parameters = resourceMethodDescriptor.getParameters();
    DynamicRecordTemplate template = (DynamicRecordTemplate) requestData.getEntity();
    DataMap data = template.data();
    Object[] arguments = new Object[parameters.size()];
    int i = 0;
    for (Parameter<?> param : parameters)
    {
      Object value;
      if (!data.containsKey(param.getName()))
      {
        if (param.isOptional() && param.hasDefaultValue())
        {
          value = param.getDefaultValue();
        }
        else if (param.isOptional())
        {
          value = null;
        }
        else if (param.getParamType() == Parameter.ParamType.CALLBACK)
        {
          value = null;
        }
        else if (param.getParamType() == Parameter.ParamType.PARSEQ_CONTEXT_PARAM || param.getParamType() == Parameter.ParamType.PARSEQ_CONTEXT)
        {
          value = null;
        }
        else
        {
          throw new RoutingException("Parameter '" + param.getName() + "' of method '"
              + resourceMethodDescriptor.getActionName() + "' is required", HttpStatus.S_400_BAD_REQUEST.getCode());
        }
      }
      else
      {
        try
        {
          value = template.getValue(param);
        }
        catch (TemplateOutputCastException e)
        {
          throw new RoutingException("Parameter '" + param.getName() + "' of method '"
                                         + resourceMethodDescriptor.getActionName() + "' must be of type '"
                                         + param.getType().getName() + "'",
                                     HttpStatus.S_400_BAD_REQUEST.getCode());
        }
      }
      arguments[i++] = value;
    }
    return arguments;
  }

  @Override
  public RestLiRequestData extractRequestData(RoutingResult routingResult, RestRequest request)
  {
    ResourceMethodDescriptor resourceMethodDescriptor = routingResult.getResourceMethod();
    final DataMap data;
    if (request.getEntity() == null || request.getEntity().length() == 0)
    {
      data = new DataMap();
    }
    else
    {
      data = DataMapUtils.readMap(request);
    }
    DynamicRecordTemplate template = new DynamicRecordTemplate(data, resourceMethodDescriptor.getRequestDataSchema());
    ValidationResult result =
        ValidateDataAgainstSchema.validate(data, template.schema(), new ValidationOptions(RequiredMode.IGNORE,
                                                                                          CoercionMode.NORMAL));
    if (!result.isValid())
    {
      throw new RoutingException("Parameters of method '" + resourceMethodDescriptor.getActionName()
          + "' failed validation with error '" + result.getMessages() + "'", HttpStatus.S_400_BAD_REQUEST.getCode());
    }
    return new RestLiRequestDataImpl.Builder().entity(template).build();
  }
}
TOP

Related Classes of com.linkedin.restli.internal.server.methods.arguments.ActionArgumentBuilder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.