Search in sources :

Example 31 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class CreateResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    CreateResponse createResponse = (CreateResponse) result;
    if (createResponse.hasError()) {
        RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getError(), headers, cookies);
        responseData.setResponseEnvelope(new CreateResponseEnvelope(null, responseData));
        return responseData;
    }
    Object id = null;
    if (createResponse.hasId()) {
        id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
        final ProtocolVersion protocolVersion = ((ServerResourceContext) routingResult.getContext()).getRestliProtocolVersion();
        String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
        UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
        uribuilder.path(stringKey);
        if (routingResult.getContext().hasParameter(RestConstants.ALT_KEY_PARAM)) {
            // add altkey param to location URI
            uribuilder.queryParam(RestConstants.ALT_KEY_PARAM, routingResult.getContext().getParameter(RestConstants.ALT_KEY_PARAM));
        }
        headers.put(RestConstants.HEADER_LOCATION, uribuilder.build((Object) null).toString());
        headers.put(HeaderUtil.getIdHeaderName(protocolVersion), URIParamUtils.encodeKeyForHeader(id, protocolVersion));
    }
    //Verify that a null status was not passed into the CreateResponse. If so, this is a developer error.
    if (createResponse.getStatus() == null) {
        throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. HttpStatus is null inside of a CreateResponse from the resource method: " + routingResult.getResourceMethod());
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(createResponse.getStatus(), headers, cookies);
    CreateResponseEnvelope responseEnvelope;
    if (createResponse instanceof CreateKVResponse) {
        final ResourceContext resourceContext = routingResult.getContext();
        DataMap entityData = ((CreateKVResponse) createResponse).getEntity().data();
        final DataMap data = RestUtils.projectFields(entityData, resourceContext.getProjectionMode(), resourceContext.getProjectionMask());
        responseEnvelope = new CreateResponseEnvelope(new AnyRecord(data), true, responseData);
    } else //Instance of idResponse
    {
        IdResponse<?> idResponse = new IdResponse<Object>(id);
        responseEnvelope = new CreateResponseEnvelope(idResponse, responseData);
    }
    responseData.setResponseEnvelope(responseEnvelope);
    return responseData;
}
Also used : IdResponse(com.linkedin.restli.common.IdResponse) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceContext(com.linkedin.restli.server.ResourceContext) AnyRecord(com.linkedin.restli.internal.server.methods.AnyRecord) CreateResponse(com.linkedin.restli.server.CreateResponse) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) UriBuilder(com.linkedin.jersey.api.uri.UriBuilder) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse)

Example 32 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class ErrorResponseBuilder method buildRestLiResponseData.

@Override
public RestLiResponseData buildRestLiResponseData(RestRequest request, RoutingResult routingResult, Object object, Map<String, String> headers, List<HttpCookie> cookies) {
    RestLiServiceException exceptionResult = (RestLiServiceException) object;
    if (_errorResponseFormat.showHeaders()) {
        final ProtocolVersion protocolVersion = ProtocolVersionUtil.extractProtocolVersion(headers);
        headers.put(HeaderUtil.getErrorResponseHeaderName(protocolVersion), RestConstants.HEADER_VALUE_ERROR);
    }
    final ResourceMethod type;
    if (routingResult != null && routingResult.getResourceMethod() != null) {
        type = routingResult.getResourceMethod().getMethodType();
    } else {
        RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(exceptionResult, headers, cookies);
        return responseData;
    }
    RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(exceptionResult, headers, cookies);
    responseData.setResponseEnvelope(EnvelopeBuilderUtil.buildBlankResponseEnvelope(type, responseData));
    return responseData;
}
Also used : RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 33 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class URIDetails method testUriGeneration.

/**
   * Tests the deprecated API for getting the URI of a request, as well as the new way of constructing the URI using
   * a builder. Requires an URIDetails object with the broken down URI to make sure that out of URIs are still considered
   * valid.
   *
   * @param request
   * @param expectedURIDetails
   */
@SuppressWarnings({ "deprecation" })
public static void testUriGeneration(Request<?> request, URIDetails expectedURIDetails) {
    final ProtocolVersion version = expectedURIDetails.getProtocolVersion();
    final String createdURIString = RestliUriBuilderUtil.createUriBuilder(request, version).build().toString();
    testUriGeneration(createdURIString, expectedURIDetails);
}
Also used : ProtocolVersion(com.linkedin.restli.common.ProtocolVersion)

Example 34 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method dataProvider.

@DataProvider(name = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "testData")
public Object[][] dataProvider() {
    Map<CompoundKey, Foo> results = new HashMap<CompoundKey, Foo>();
    CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    CompoundKey c2 = new CompoundKey().append("a", "a2").append("b", 2);
    CompoundKey c3 = new CompoundKey().append("a", "a3").append("b", 3);
    Foo record1 = new Foo().setStringField("record1").setFruitsField(Fruits.APPLE);
    Foo projectedRecord1 = new Foo().setStringField("record1");
    Foo record2 = new Foo().setStringField("record2").setIntField(7);
    Foo projectedRecord2 = new Foo().setStringField("record2");
    results.put(c1, record1);
    results.put(c2, record2);
    DataMap projectionDataMap = new DataMap();
    projectionDataMap.put("stringField", MaskOperation.POSITIVE_MASK_OP.getRepresentation());
    MaskTree maskTree = new MaskTree(projectionDataMap);
    Map<String, Foo> protocol1TransformedResults = new HashMap<String, Foo>();
    protocol1TransformedResults.put("a=a1&b=1", record1);
    protocol1TransformedResults.put("a=a2&b=2", record2);
    Map<String, Foo> protocol1TransformedResultsWithProjection = new HashMap<String, Foo>();
    protocol1TransformedResultsWithProjection.put("a=a1&b=1", projectedRecord1);
    protocol1TransformedResultsWithProjection.put("a=a2&b=2", projectedRecord2);
    Map<String, Foo> protocol2TransformedResults = new HashMap<String, Foo>();
    protocol2TransformedResults.put("(a:a1,b:1)", record1);
    protocol2TransformedResults.put("(a:a2,b:2)", record2);
    Map<String, Foo> protocol2TransformedResultsWithProjection = new HashMap<String, Foo>();
    protocol2TransformedResultsWithProjection.put("(a:a1,b:1)", projectedRecord1);
    protocol2TransformedResultsWithProjection.put("(a:a2,b:2)", projectedRecord2);
    Map<String, ErrorResponse> protocol1Errors = Collections.singletonMap("a=a3&b=3", new ErrorResponse().setStatus(404));
    Map<String, ErrorResponse> protocol2Errors = Collections.singletonMap("(a:a3,b:3)", new ErrorResponse().setStatus(404));
    Map<CompoundKey, HttpStatus> statuses = new HashMap<CompoundKey, HttpStatus>();
    statuses.put(c1, HttpStatus.S_200_OK);
    statuses.put(c2, HttpStatus.S_200_OK);
    Map<CompoundKey, RestLiServiceException> exceptions = new HashMap<CompoundKey, RestLiServiceException>();
    exceptions.put(c3, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
    BatchResult<CompoundKey, Foo> batchResult = new BatchResult<CompoundKey, Foo>(results, statuses, exceptions);
    Map<Object, RestLiServiceException> exceptionsWithUntypedKey = new HashMap<Object, RestLiServiceException>(exceptions);
    ProtocolVersion protocolVersion1 = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
    ProtocolVersion protocolVersion2 = AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
    ProjectionMode auto = ProjectionMode.AUTOMATIC;
    ProjectionMode manual = ProjectionMode.MANUAL;
    return new Object[][] { // automatic projection mode with null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, auto }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, auto }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, auto }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, auto }, // manual projection mode with null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, manual }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, manual }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, null, manual }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, null, manual }, // manual projection mode with non-null mask tree
    { results, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, maskTree, manual }, { results, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, maskTree, manual }, { batchResult, protocolVersion1, protocol1TransformedResults, protocol1Errors, exceptionsWithUntypedKey, maskTree, manual }, { batchResult, protocolVersion2, protocol2TransformedResults, protocol2Errors, exceptionsWithUntypedKey, maskTree, manual }, // automatic projection mode with non-null mask tree
    { results, protocolVersion1, protocol1TransformedResultsWithProjection, protocol1Errors, exceptionsWithUntypedKey, maskTree, auto }, { results, protocolVersion2, protocol2TransformedResultsWithProjection, protocol2Errors, exceptionsWithUntypedKey, maskTree, auto }, { batchResult, protocolVersion1, protocol1TransformedResultsWithProjection, protocol1Errors, exceptionsWithUntypedKey, maskTree, auto }, { batchResult, protocolVersion2, protocol2TransformedResultsWithProjection, protocol2Errors, exceptionsWithUntypedKey, maskTree, auto } };
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompoundKey(com.linkedin.restli.common.CompoundKey) HttpStatus(com.linkedin.restli.common.HttpStatus) Foo(com.linkedin.pegasus.generator.examples.Foo) BatchResult(com.linkedin.restli.server.BatchResult) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) DataMap(com.linkedin.data.DataMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) ProjectionMode(com.linkedin.restli.server.ProjectionMode) DataProvider(org.testng.annotations.DataProvider)

Example 35 with ProtocolVersion

use of com.linkedin.restli.common.ProtocolVersion in project rest.li by linkedin.

the class TestBatchGetResponseBuilder method unsupportedNullKeyMapData.

/* Note that we use java.util.concurrent.ConcurrentHashMap when possible. This is because rest.li checks
  * for the presence of nulls returned from maps which are returned from resource methods. The checking for nulls
  * is prone to a NullPointerException since contains(null) can throw an NPE from certain map implementations such as
  * java.util.concurrent.ConcurrentHashMap. We want to make sure our check for the presence of nulls is done in a
  * way that doesn't throw an NullPointerException.
  */
@DataProvider(name = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "unsupportedNullKeyMapData")
public Object[][] unsupportedNullKeyMapData() {
    Map<CompoundKey, Foo> results = new ConcurrentHashMap<CompoundKey, Foo>();
    CompoundKey c1 = new CompoundKey().append("a", "a1").append("b", 1);
    Foo record1 = new Foo().setStringField("record1").setFruitsField(Fruits.APPLE);
    results.put(c1, record1);
    Map<CompoundKey, HttpStatus> statuses = new ConcurrentHashMap<CompoundKey, HttpStatus>();
    statuses.put(c1, HttpStatus.S_200_OK);
    final BatchResult<CompoundKey, Foo> batchResult = new BatchResult<CompoundKey, Foo>(results, statuses, new ConcurrentHashMap<CompoundKey, RestLiServiceException>());
    final Map<String, Foo> protocol1TransformedResults = new ConcurrentHashMap<String, Foo>();
    protocol1TransformedResults.put("a=a1&b=1", record1);
    final Map<String, Foo> protocol2TransformedResults = new ConcurrentHashMap<String, Foo>();
    protocol2TransformedResults.put("(a:a1,b:1)", record1);
    ProtocolVersion protocolVersion1 = AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion();
    ProtocolVersion protocolVersion2 = AllProtocolVersions.RESTLI_PROTOCOL_2_0_0.getProtocolVersion();
    return new Object[][] { { results, protocolVersion1, protocol1TransformedResults }, { results, protocolVersion2, protocol2TransformedResults }, { batchResult, protocolVersion1, protocol1TransformedResults }, { batchResult, protocolVersion2, protocol2TransformedResults } };
}
Also used : CompoundKey(com.linkedin.restli.common.CompoundKey) HttpStatus(com.linkedin.restli.common.HttpStatus) Foo(com.linkedin.pegasus.generator.examples.Foo) BatchResult(com.linkedin.restli.server.BatchResult) ProtocolVersion(com.linkedin.restli.common.ProtocolVersion) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DataProvider(org.testng.annotations.DataProvider)

Aggregations

ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)35 DataMap (com.linkedin.data.DataMap)12 Test (org.testng.annotations.Test)8 CompoundKey (com.linkedin.restli.common.CompoundKey)7 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)7 URI (java.net.URI)7 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 HashMap (java.util.HashMap)5 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)4 ResourceMethod (com.linkedin.restli.common.ResourceMethod)4 ByteString (com.linkedin.data.ByteString)3 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)3 ErrorResponse (com.linkedin.restli.common.ErrorResponse)3 IdResponse (com.linkedin.restli.common.IdResponse)3 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)3 HttpCookie (java.net.HttpCookie)3 HashSet (java.util.HashSet)3 RecordTemplate (com.linkedin.data.template.RecordTemplate)2 MaskTree (com.linkedin.data.transform.filter.request.MaskTree)2 Foo (com.linkedin.pegasus.generator.examples.Foo)2