Search in sources :

Example 26 with UriBuilder

use of com.linkedin.jersey.api.uri.UriBuilder in project rest.li by linkedin.

the class CreateResponseBuilder method buildRestLiResponseData.

/**
 * {@inheritDoc}
 *
 * @param result The result of a Rest.li CREATE method. It is an instance of {@link CreateResponse}; or subclass
 *               {@link CreateKVResponse}, if the CREATE method returns the entity.
 */
@Override
public RestLiResponseData<CreateResponseEnvelope> buildRestLiResponseData(Request request, RoutingResult routingResult, Object result, Map<String, String> headers, List<HttpCookie> cookies) {
    CreateResponse createResponse = (CreateResponse) result;
    boolean isGetAfterCreate = createResponse instanceof CreateKVResponse;
    if (createResponse.hasError()) {
        RestLiServiceException exception = createResponse.getError();
        return new RestLiResponseDataImpl<>(new CreateResponseEnvelope(exception, isGetAfterCreate), headers, cookies);
    }
    Object id = null;
    if (createResponse.hasId()) {
        id = ResponseUtils.translateCanonicalKeyToAlternativeKeyIfNeeded(createResponse.getId(), routingResult);
        final ProtocolVersion protocolVersion = routingResult.getContext().getRestliProtocolVersion();
        String stringKey = URIParamUtils.encodeKeyForUri(id, UriComponent.Type.PATH_SEGMENT, protocolVersion);
        UriBuilder uribuilder = UriBuilder.fromUri(request.getURI());
        uribuilder.path(stringKey);
        uribuilder.replaceQuery(null);
        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());
    }
    final ResourceContext resourceContext = routingResult.getContext();
    RecordTemplate idResponse;
    if (createResponse instanceof CreateKVResponse && resourceContext.isReturnEntityRequested()) {
        RecordTemplate entity = ((CreateKVResponse<?, ?>) createResponse).getEntity();
        // Verify that a null entity was not passed into the CreateKVResponse. If so, this is a developer error.
        if (entity == null) {
            throw new RestLiServiceException(HttpStatus.S_500_INTERNAL_SERVER_ERROR, "Unexpected null encountered. Entity is null inside of a CreateKVResponse when the entity should be returned. In resource method: " + routingResult.getResourceMethod());
        }
        DataMap entityData = entity.data();
        TimingContextUtil.beginTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
        final DataMap data = RestUtils.projectFields(entityData, resourceContext);
        TimingContextUtil.endTiming(resourceContext.getRawRequestContext(), FrameworkTimingKeys.SERVER_RESPONSE_RESTLI_PROJECTION_APPLY.key());
        idResponse = new AnyRecord(data);
    // Ideally, we should set an IdEntityResponse to the envelope. But we are keeping AnyRecord
    // to make sure the runtime object is backwards compatible.
    // idResponse = new IdEntityResponse<>(id, new AnyRecord(data));
    } else // Instance of idResponse
    {
        idResponse = new IdResponse<>(id);
    }
    return new RestLiResponseDataImpl<>(new CreateResponseEnvelope(createResponse.getStatus(), idResponse, isGetAfterCreate), headers, cookies);
}
Also used : 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) RecordTemplate(com.linkedin.data.template.RecordTemplate) CreateKVResponse(com.linkedin.restli.server.CreateKVResponse) UriBuilder(com.linkedin.jersey.api.uri.UriBuilder)

Example 27 with UriBuilder

use of com.linkedin.jersey.api.uri.UriBuilder in project rest.li by linkedin.

the class TestBatchFinderResponseBuilder method buildURI.

private static URI buildURI(List<Foo> criteria, ProtocolVersion version) {
    UriBuilder builder = UriBuilder.fromPath("/");
    DataMap param = new DataMap();
    param.put("bq", BATCH_METHOD);
    QueryParamsDataMap.addSortedParams(builder, param);
    return builder.build();
}
Also used : UriBuilder(com.linkedin.jersey.api.uri.UriBuilder) DataMap(com.linkedin.data.DataMap) QueryParamsDataMap(com.linkedin.restli.internal.common.QueryParamsDataMap)

Example 28 with UriBuilder

use of com.linkedin.jersey.api.uri.UriBuilder in project rest.li by linkedin.

the class RouteLookupClient method createNewRequestWithNewServiceName.

/**
 * Creates a new request identical to the original request, but changes the d2 service name to
 * the one passed in.
 * @param origRequest original request to copy
 * @param newServiceName the new service name to rewrite the request with
 * @return a new RestRequest
 */
private static RestRequest createNewRequestWithNewServiceName(RestRequest origRequest, String newServiceName) {
    RestRequestBuilder modifiedBuilder = origRequest.builder();
    URI originalURI = modifiedBuilder.getURI();
    if (!("d2".equals(originalURI.getScheme()))) {
        throw new IllegalArgumentException("Unsupported scheme in URI: " + originalURI);
    }
    UriBuilder modifiedUriBuilder = UriBuilder.fromUri(originalURI);
    modifiedUriBuilder.host(newServiceName);
    URI resultUri = modifiedUriBuilder.build();
    modifiedBuilder.setURI(resultUri);
    RestRequest resultRequest = modifiedBuilder.build();
    return resultRequest;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) UriBuilder(com.linkedin.jersey.api.uri.UriBuilder) URI(java.net.URI)

Example 29 with UriBuilder

use of com.linkedin.jersey.api.uri.UriBuilder in project rest.li by linkedin.

the class D2URIRewriter method rewriteURI.

@Override
public URI rewriteURI(URI d2Uri) {
    String path = d2Uri.getRawPath();
    UriBuilder builder = UriBuilder.fromUri(_httpURI);
    if (path != null) {
        builder.path(path);
    }
    builder.replaceQuery(d2Uri.getRawQuery());
    builder.fragment(d2Uri.getRawFragment());
    URI rewrittenUri = builder.build();
    LOGGER.debug("rewrite uri {} -> {}", d2Uri, rewrittenUri);
    return rewrittenUri;
}
Also used : UriBuilder(com.linkedin.jersey.api.uri.UriBuilder) URI(java.net.URI)

Example 30 with UriBuilder

use of com.linkedin.jersey.api.uri.UriBuilder in project rest.li by linkedin.

the class HealthCheckClientBuilder method build.

public HealthCheck build() throws URISyntaxException {
    URI curUri = _client.getUri();
    String fullPath = _healthCheckPath;
    if (_healthCheckPath == null || _healthCheckPath.isEmpty()) {
        // If the path is not specified, always use the service's path
        fullPath = curUri.getPath();
        if (_servicePath != null && !_servicePath.isEmpty()) {
            fullPath += _servicePath;
        }
    }
    UriBuilder uriBuilder = UriBuilder.fromUri(curUri);
    URI newUri = uriBuilder.replacePath(fullPath).build();
    HealthCheckOperations operations = _healthOperations;
    if (operations == null) {
        operations = new HealthCheckOperations();
    }
    return new TransportHealthCheck(_clock, _client.getTransportClient(), operations.buildRestRequest(_method, newUri), operations.buildRequestContextSupplier(), operations.buildWireAttributesSupplier(), operations.buildResponseValidate(), _latency);
}
Also used : UriBuilder(com.linkedin.jersey.api.uri.UriBuilder) URI(java.net.URI)

Aggregations

UriBuilder (com.linkedin.jersey.api.uri.UriBuilder)45 DataMap (com.linkedin.data.DataMap)9 URI (java.net.URI)6 Test (org.testng.annotations.Test)5 DataList (com.linkedin.data.DataList)3 ProtocolVersion (com.linkedin.restli.common.ProtocolVersion)2 AnyRecord (com.linkedin.restli.internal.server.methods.AnyRecord)2 CreateKVResponse (com.linkedin.restli.server.CreateKVResponse)2 CreateResponse (com.linkedin.restli.server.CreateResponse)2 ResourceContext (com.linkedin.restli.server.ResourceContext)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 HashMap (java.util.HashMap)2 MaskMap (com.linkedin.data.schema.MaskMap)1 RecordTemplate (com.linkedin.data.template.RecordTemplate)1 RestRequest (com.linkedin.r2.message.rest.RestRequest)1 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)1 IdResponse (com.linkedin.restli.common.IdResponse)1 QueryParamsDataMap (com.linkedin.restli.internal.common.QueryParamsDataMap)1 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)1 ArrayList (java.util.ArrayList)1