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);
}
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();
}
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;
}
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;
}
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);
}
Aggregations