Search in sources :

Example 1 with ActionResult

use of com.linkedin.restli.server.ActionResult in project rest.li by linkedin.

the class ActionResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    final Object value;
    final HttpStatus status;
    if (result instanceof ActionResult) {
        final ActionResult<?> actionResult = (ActionResult<?>) result;
        value = actionResult.getValue();
        status = actionResult.getStatus();
        if (status == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null HttpStatus inside of an ActionResult returned by the resource method: " + routingResult.getResourceMethod());
        }
    } else {
        value = result;
        status = HttpStatus.S_200_OK;
    }
    RecordDataSchema actionReturnRecordDataSchema = routingResult.getResourceMethod().getActionReturnRecordDataSchema();
    @SuppressWarnings("unchecked") FieldDef<Object> actionReturnFieldDef = (FieldDef<Object>) routingResult.getResourceMethod().getActionReturnFieldDef();
    final ActionResponse<?> actionResponse = new ActionResponse<Object>(value, actionReturnFieldDef, actionReturnRecordDataSchema);
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(status, headers, cookies);
    responseData.setResponseEnvelope(new ActionResponseEnvelope(actionResponse, responseData));
    return responseData;
}
Also used : HttpStatus(com.linkedin.restli.common.HttpStatus) ActionResponse(com.linkedin.restli.common.ActionResponse) FieldDef(com.linkedin.data.template.FieldDef) ActionResult(com.linkedin.restli.server.ActionResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema)

Example 2 with ActionResult

use of com.linkedin.restli.server.ActionResult in project rest.li by linkedin.

the class TestRestLiResponseHandler method testWrapperResults.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusesData")
public void testWrapperResults(AcceptTypeData acceptTypeData, String expectedStatus, String expectedActionStatus, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    RestResponse response;
    final Status status = buildStatusRecord();
    final GetResult<Status> getResult = new GetResult<Status>(status, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    response = invokeResponseHandler("/test", getResult, ResourceMethod.GET, acceptTypeData.acceptHeaders, protocolVersion);
    checkResponse(response, HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), 2, acceptTypeData.responseContentType, Status.class.getName(), null, true, errorResponseHeaderName);
    assertEquals(response.getEntity().asAvroString(), expectedStatus);
    final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
    final ActionResult<Status> actionResult = new ActionResult<Status>(status, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
    response = _responseHandler.buildResponse(request, buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders), actionResult);
    checkResponse(response, HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), 2, acceptTypeData.responseContentType, ActionResponse.class.getName(), Status.class.getName(), true, errorResponseHeaderName);
    assertEquals(response.getEntity().asAvroString(), expectedActionStatus);
}
Also used : UpdateStatus(com.linkedin.restli.common.UpdateStatus) CreateStatus(com.linkedin.restli.common.CreateStatus) Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) RestRequest(com.linkedin.r2.message.rest.RestRequest) GetResult(com.linkedin.restli.server.GetResult) ActionResult(com.linkedin.restli.server.ActionResult) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) ActionResponse(com.linkedin.restli.common.ActionResponse) Test(org.testng.annotations.Test)

Example 3 with ActionResult

use of com.linkedin.restli.server.ActionResult in project rest.li by linkedin.

the class ExampleRequestResponseGenerator method buildActionResult.

private ActionResult<?> buildActionResult(ActionSchema actionSchema) {
    DynamicRecordMetadata returnsMetadata = _resourceSpec.getActionResponseMetadata(actionSchema.getName());
    if (actionSchema.hasReturns()) {
        FieldDef<?> fieldDef = returnsMetadata.getFieldDef("value");
        Object returnValue = generateFieldDefValue(fieldDef);
        return new ActionResult<Object>(returnValue);
    } else {
        return null;
    }
}
Also used : DynamicRecordMetadata(com.linkedin.data.template.DynamicRecordMetadata) ActionResult(com.linkedin.restli.server.ActionResult)

Aggregations

ActionResult (com.linkedin.restli.server.ActionResult)3 ActionResponse (com.linkedin.restli.common.ActionResponse)2 HttpStatus (com.linkedin.restli.common.HttpStatus)2 RecordDataSchema (com.linkedin.data.schema.RecordDataSchema)1 DynamicRecordMetadata (com.linkedin.data.template.DynamicRecordMetadata)1 FieldDef (com.linkedin.data.template.FieldDef)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestResponse (com.linkedin.r2.message.rest.RestResponse)1 CreateStatus (com.linkedin.restli.common.CreateStatus)1 UpdateStatus (com.linkedin.restli.common.UpdateStatus)1 PartialRestResponse (com.linkedin.restli.internal.server.response.PartialRestResponse)1 GetResult (com.linkedin.restli.server.GetResult)1 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)1 Status (com.linkedin.restli.server.twitter.TwitterTestDataModels.Status)1 Test (org.testng.annotations.Test)1