Search in sources :

Example 21 with CreateResponse

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

the class MixedResource method create.

@Create
public void create(Greeting entity, @CallbackParam final Callback<CreateResponse> callback) {
    final Runnable requestHandler = new Runnable() {

        public void run() {
            callback.onSuccess(new CreateResponse(HttpStatus.S_200_OK));
        }
    };
    scheduler.schedule(requestHandler, DELAY, TimeUnit.MILLISECONDS);
}
Also used : CreateResponse(com.linkedin.restli.server.CreateResponse) Create(com.linkedin.restli.server.annotations.RestMethod.Create)

Example 22 with CreateResponse

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

the class AssociationAltKeyResource method create.

public CreateResponse create(Greeting entity) {
    CompoundKey key = new CompoundKey();
    key.append("message", "h");
    key.append("greetingId", 3L);
    return new CreateResponse(key, HttpStatus.S_201_CREATED);
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) CreateResponse(com.linkedin.restli.server.CreateResponse)

Example 23 with CreateResponse

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

the class ComplexKeyAltKeyResource method create.

@Override
public CreateResponse create(Message entity) {
    TwoPartKey key = new TwoPartKey();
    key.setMajor("testKey");
    key.setMinor("testKey");
    ComplexResourceKey<TwoPartKey, TwoPartKey> complexKey = new ComplexResourceKey<>(key, new TwoPartKey());
    return new CreateResponse(complexKey, HttpStatus.S_201_CREATED);
}
Also used : TwoPartKey(com.linkedin.restli.examples.greetings.api.TwoPartKey) CreateResponse(com.linkedin.restli.server.CreateResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 24 with CreateResponse

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

the class ExceptionsResource method create.

/**
 * Responds with an error for requests to create insulting greetings, responds
 * with 201 created for all other requests.
 */
@RestMethod.Create
public CreateResponse create(Greeting g) {
    if (g.hasTone() && g.getTone() == Tone.INSULTING) {
        RestLiServiceException notAcceptableException = new RestLiServiceException(HttpStatus.S_406_NOT_ACCEPTABLE, "I will not tolerate your insolence!");
        DataMap details = new DataMap();
        details.put("reason", "insultingGreeting");
        notAcceptableException.setErrorDetails(new EmptyRecord(details));
        notAcceptableException.setServiceErrorCode(999);
        throw notAcceptableException;
    } else {
        return new CreateResponse(g.getId(), HttpStatus.S_201_CREATED);
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) CreateResponse(com.linkedin.restli.server.CreateResponse) DataMap(com.linkedin.data.DataMap)

Example 25 with CreateResponse

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

the class BatchCreateResponseBuilder method buildRestLiResponseData.

/**
 * {@inheritDoc}
 *
 * @param result The result for a Rest.li BATCH_CREATE method. It's an instance of {@link BatchCreateResult}, if the
 *               BATCH_CREATE method doesn't return the entity; or an instance of {@link BatchCreateKVResult}, if it
 *               does.
 */
@Override
public RestLiResponseData<BatchCreateResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    Object altKey = null;
    if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
        altKey = routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM);
    }
    final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);
    final ResourceContext resourceContext = routingResult.getContext();
    if (result instanceof BatchCreateKVResult && resourceContext.isReturnEntityRequested()) {
        BatchCreateKVResult<?, ?> list = (BatchCreateKVResult<?, ?>) result;
        if (list.getResults() == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(list.getResults().size());
        TimingContextUtil.beginTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
        for (CreateKVResponse<?, ?> createKVResponse : list.getResults()) {
            if (createKVResponse == null) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateKVResult returned by the resource method: " + routingResult.getResourceMethod());
            } else {
                Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createKVResponse.getId(), routingResult);
                if (createKVResponse.getError() == null) {
                    DataMap entityData = createKVResponse.getEntity() != null ? createKVResponse.getEntity().data() : null;
                    final DataMap data = RestUtils.projectFields(entityData, resourceContext);
                    CreateIdEntityStatus<Object, RecordTemplate> entry = new CreateIdEntityStatus<>(createKVResponse.getStatus().getCode(), id, new AnyRecord(data), // location uri
                    getLocationUri(request, id, altKey, protocolVersion), null, protocolVersion);
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
                } else {
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createKVResponse.getError()));
                }
            }
        }
        TimingContextUtil.endTiming(routingResult.getContext().getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
        return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, true), headers, cookies);
    } else {
        List<? extends CreateResponse> createResponses = extractCreateResponseList(result);
        // Verify that a null list was not passed into the BatchCreateResult. If so, this is a developer error.
        if (createResponses == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
        }
        List<BatchCreateResponseEnvelope.CollectionCreateResponseItem> collectionCreateList = new ArrayList<>(createResponses.size());
        for (CreateResponse createResponse : createResponses) {
            // Verify that a null element was not passed into the BatchCreateResult list. If so, this is a developer error.
            if (createResponse == null) {
                throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Null element inside of List inside of a BatchCreateResult returned by the resource method: " + routingResult.getResourceMethod());
            } else {
                Object id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
                if (createResponse.getError() == null) {
                    CreateIdStatus<Object> entry = new CreateIdStatus<>(createResponse.getStatus().getCode(), id, // location uri
                    getLocationUri(request, id, altKey, protocolVersion), null, protocolVersion);
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(entry));
                } else {
                    collectionCreateList.add(new BatchCreateResponseEnvelope.CollectionCreateResponseItem(createResponse.getError()));
                }
            }
        }
        return new RestLiResponseDataImpl<>(new BatchCreateResponseEnvelope(HttpStatus.S_200_OK, collectionCreateList, false), headers, cookies);
    }
}
Also used : ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CreateIdEntityStatus(com.linkedin.restli.common.CreateIdEntityStatus) CreateResponse(com.linkedin.restli.server.CreateResponse) ArrayList(java.util.ArrayList) CreateIdStatus(com.linkedin.restli.common.CreateIdStatus) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RecordTemplate(com.linkedin.data.template.RecordTemplate) BatchCreateKVResult(com.linkedin.restli.server.BatchCreateKVResult)

Aggregations

CreateResponse (com.linkedin.restli.server.CreateResponse)50 Test (org.testng.annotations.Test)25 Datastream (com.linkedin.datastream.common.Datastream)17 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)17 DatastreamDestination (com.linkedin.datastream.common.DatastreamDestination)10 PathKeys (com.linkedin.restli.server.PathKeys)8 ArrayList (java.util.ArrayList)7 RestRequest (com.linkedin.r2.message.rest.RestRequest)6 BatchCreateResult (com.linkedin.restli.server.BatchCreateResult)6 DataMap (com.linkedin.data.DataMap)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 EmptyRecord (com.linkedin.restli.common.EmptyRecord)5 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 CreateKVResponse (com.linkedin.restli.server.CreateKVResponse)5 Mockito.anyString (org.mockito.Mockito.anyString)5 KafkaDestination (com.linkedin.datastream.kafka.KafkaDestination)4 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)4 CreateIdStatus (com.linkedin.restli.common.CreateIdStatus)4 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)4