Skip to content

Commit

Permalink
Add api version handling to controllers (#3059)
Browse files Browse the repository at this point in the history
* Add version handling

* Add standalone support

* Propagate base url

* Propagate api versions to entity dictionary

* Add config

* Update configuration

* Update OpenAPI

* Fix controllers

* Update default standalone settings

* Fix endpoints for standalone

* Fix rebase

* Remove todo comment

* add comments for api docs controller getBaseUrl

---------

Co-authored-by: Aaron Klish <[email protected]>
  • Loading branch information
justin-tay and aklish authored Sep 7, 2023
1 parent f02e2a2 commit 62510cc
Show file tree
Hide file tree
Showing 47 changed files with 1,251 additions and 477 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public ElideResponse execute(AsyncApi queryObj, RequestScope scope) throws URISy
throw new InvalidOperationException("Invalid API Version");
}
UUID requestUUID = UUID.fromString(queryObj.getRequestId());
//TODO - we need to add the baseUrlEndpoint to the queryObject.
ElideResponse response = runner.run("", queryObj.getQuery(), user, requestUUID, scope.getRequestHeaders());
ElideResponse response = runner.run(scope.getBaseUrlEndPoint(), queryObj.getQuery(), user, requestUUID,
scope.getRequestHeaders());
log.debug("GRAPHQL_V1_0 getResponseCode: {}, GRAPHQL_V1_0 getBody: {}",
response.getResponseCode(), response.getBody());
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public ElideResponse execute(AsyncApi queryObj, RequestScope scope)
log.debug("Extracted QueryParams from AsyncQuery Object: {}", queryParams);

//TODO - we need to add the baseUrlEndpoint to the queryObject.
ElideResponse response = elide.get("", getPath(uri), queryParams, scope.getRequestHeaders(), user, apiVersion,
requestUUID);
ElideResponse response = elide.get(scope.getBaseUrlEndPoint(), getPath(uri), queryParams,
scope.getRequestHeaders(), user, apiVersion, requestUUID);
log.debug("JSONAPI_V1_0 getResponseCode: {}, JSONAPI_V1_0 getBody: {}",
response.getResponseCode(), response.getBody());
return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ public abstract RequestScope getRequestScope(TableExport exportObj, RequestScope
public String generateDownloadURL(TableExport exportObj, RequestScope scope) {
String downloadPath = scope.getElideSettings().getExportApiPath();
String baseURL = scope.getBaseUrlEndPoint();
String jsonApiPath = scope.getElideSettings().getJsonApiPath();
if (jsonApiPath != null && baseURL.endsWith(jsonApiPath)) {
baseURL = baseURL.substring(0, baseURL.length() - jsonApiPath.length());
}
String graphqlApiPath = scope.getElideSettings().getGraphQLApiPath();
if (graphqlApiPath != null && baseURL.endsWith(graphqlApiPath)) {
baseURL = baseURL.substring(0, baseURL.length() - graphqlApiPath.length());
}
String extension = this.engine.isExtensionEnabled()
? exportObj.getResultType().getFileExtensionType().getExtension()
: FileExtensionType.NONE.getExtension();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,10 @@ public Map<String, String> getRelationshipLinks(PersistentResource resource, Str
protected String getResourceUrl(PersistentResource resource) {
StringBuilder result = new StringBuilder();

if (StringUtils.isEmpty(baseUrl)) {
if (resource.getRequestScope().getBaseUrlEndPoint() != null) {
result.append(resource.getRequestScope().getBaseUrlEndPoint());
String jsonApiPath = resource.getRequestScope().getElideSettings().getJsonApiPath();
if (StringUtils.isNotEmpty(jsonApiPath)) {
result.append(jsonApiPath);
}
result.append("/");
}
} else {
if (resource.getRequestScope().getBaseUrlEndPoint() != null) {
result.append(resource.getRequestScope().getBaseUrlEndPoint());
result.append("/");
} else if (!StringUtils.isEmpty(baseUrl)) {
result.append(baseUrl);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
import com.yahoo.elide.Elide;
import com.yahoo.elide.ElideResponse;
import com.yahoo.elide.annotation.PATCH;
import com.yahoo.elide.core.dictionary.EntityDictionary;
import com.yahoo.elide.core.request.route.BasicApiVersionValidator;
import com.yahoo.elide.core.request.route.FlexibleRouteResolver;
import com.yahoo.elide.core.request.route.NullRouteResolver;
import com.yahoo.elide.core.request.route.Route;
import com.yahoo.elide.core.request.route.RouteResolver;
import com.yahoo.elide.core.security.User;
import com.yahoo.elide.jsonapi.JsonApi;
import com.yahoo.elide.utils.HeaderUtils;
Expand All @@ -29,13 +35,16 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.SecurityContext;
import jakarta.ws.rs.core.UriInfo;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

/**
Expand All @@ -47,12 +56,21 @@
public class JsonApiEndpoint {
protected final Elide elide;
protected final HeaderUtils.HeaderProcessor headerProcessor;
protected final RouteResolver routeResolver;

@Inject
public JsonApiEndpoint(
@Named("elide") Elide elide) {
@Named("elide") Elide elide, Optional<RouteResolver> optionalRouteResolver) {
this.elide = elide;
this.headerProcessor = elide.getElideSettings().getHeaderProcessor();
this.routeResolver = optionalRouteResolver.orElseGet(() -> {
Set<String> apiVersions = elide.getElideSettings().getDictionary().getApiVersions();
if (apiVersions.size() == 1 && apiVersions.contains(EntityDictionary.NO_VERSION)) {
return new NullRouteResolver();
} else {
return new FlexibleRouteResolver(new BasicApiVersionValidator(), elide.getElideSettings()::getBaseUrl);
}
});
}

/**
Expand All @@ -69,17 +87,30 @@ public JsonApiEndpoint(
@Path("{path:.*}")
@Consumes(JsonApi.MEDIA_TYPE)
public Response post(
@HeaderParam("Content-Type") String contentType,
@HeaderParam("accept") String accept,
@PathParam("path") String path,
@Context UriInfo uriInfo,
@Context HttpHeaders headers,
@Context SecurityContext securityContext,
String jsonapiDocument) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String apiVersion = HeaderUtils.resolveApiVersion(headers.getRequestHeaders());
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(uriInfo.getQueryParameters());
Map<String, List<String>> requestHeaders = headerProcessor.process(headers.getRequestHeaders());
User user = new SecurityContextUser(securityContext);
return build(elide.post(getBaseUrlEndpoint(uriInfo), path, jsonapiDocument,
queryParams, requestHeaders, user, apiVersion, UUID.randomUUID()));

String baseUrl = getBaseUrlEndpoint(uriInfo);
String pathname = path;
Route route = routeResolver.resolve(JSONAPI_CONTENT_TYPE, baseUrl, pathname, requestHeaders,
uriInfo.getQueryParameters());

if ("operations".equals(route.getPath())) {
// Atomic Operations
return build(elide.operations(route.getBaseUrl(), contentType, accept, route.getPath(), jsonapiDocument,
queryParams, requestHeaders, user, route.getApiVersion(), UUID.randomUUID()));
}

return build(elide.post(route.getBaseUrl(), route.getPath(), jsonapiDocument,
queryParams, requestHeaders, user, route.getApiVersion(), UUID.randomUUID()));
}

/**
Expand All @@ -98,13 +129,17 @@ public Response get(
@Context UriInfo uriInfo,
@Context HttpHeaders headers,
@Context SecurityContext securityContext) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String apiVersion = HeaderUtils.resolveApiVersion(headers.getRequestHeaders());
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(uriInfo.getQueryParameters());
Map<String, List<String>> requestHeaders = headerProcessor.process(headers.getRequestHeaders());
User user = new SecurityContextUser(securityContext);

return build(elide.get(getBaseUrlEndpoint(uriInfo), path, queryParams,
requestHeaders, user, apiVersion, UUID.randomUUID()));
String baseUrl = getBaseUrlEndpoint(uriInfo);
String pathname = path;
Route route = routeResolver.resolve(JSONAPI_CONTENT_TYPE, baseUrl, pathname, requestHeaders,
uriInfo.getQueryParameters());

return build(elide.get(route.getBaseUrl(), route.getPath(), queryParams,
requestHeaders, user, route.getApiVersion(), UUID.randomUUID()));
}

/**
Expand All @@ -130,12 +165,17 @@ public Response patch(
@Context HttpHeaders headers,
@Context SecurityContext securityContext,
String jsonapiDocument) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String apiVersion = HeaderUtils.resolveApiVersion(headers.getRequestHeaders());
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(uriInfo.getQueryParameters());
Map<String, List<String>> requestHeaders = headerProcessor.process(headers.getRequestHeaders());
User user = new SecurityContextUser(securityContext);
return build(elide.patch(getBaseUrlEndpoint(uriInfo), contentType, accept, path,
jsonapiDocument, queryParams, requestHeaders, user, apiVersion, UUID.randomUUID()));

String baseUrl = getBaseUrlEndpoint(uriInfo);
String pathname = path;
Route route = routeResolver.resolve(JSONAPI_CONTENT_TYPE, baseUrl, pathname, requestHeaders,
uriInfo.getQueryParameters());

return build(elide.patch(route.getBaseUrl(), contentType, accept, route.getPath(), jsonapiDocument, queryParams,
requestHeaders, user, route.getApiVersion(), UUID.randomUUID()));
}

/**
Expand All @@ -157,42 +197,17 @@ public Response delete(
@Context HttpHeaders headers,
@Context SecurityContext securityContext,
String jsonApiDocument) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String apiVersion =
HeaderUtils.resolveApiVersion(headers.getRequestHeaders());
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>(uriInfo.getQueryParameters());
Map<String, List<String>> requestHeaders = headerProcessor.process(headers.getRequestHeaders());
User user = new SecurityContextUser(securityContext);
return build(elide.delete(getBaseUrlEndpoint(uriInfo), path, jsonApiDocument, queryParams, requestHeaders,
user, apiVersion, UUID.randomUUID()));
}

/**
* Operations handler.
*
* @param path request path
* @param uriInfo URI info
* @param headers the request headers
* @param securityContext security context
* @param jsonapiDocument post data as jsonapi document
* @return response
*/
@POST
@Path("/operations")
@Consumes(JsonApi.AtomicOperations.MEDIA_TYPE)
public Response operations(
@HeaderParam("Content-Type") String contentType,
@HeaderParam("accept") String accept,
@PathParam("path") String path,
@Context UriInfo uriInfo,
@Context HttpHeaders headers,
@Context SecurityContext securityContext,
String jsonapiDocument) {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
String apiVersion = HeaderUtils.resolveApiVersion(headers.getRequestHeaders());
Map<String, List<String>> requestHeaders = headerProcessor.process(headers.getRequestHeaders());
User user = new SecurityContextUser(securityContext);
return build(elide.operations(getBaseUrlEndpoint(uriInfo), contentType, accept, path, jsonapiDocument,
queryParams, requestHeaders, user, apiVersion, UUID.randomUUID()));
String baseUrl = getBaseUrlEndpoint(uriInfo);
String pathname = path;
Route route = routeResolver.resolve(JSONAPI_CONTENT_TYPE, baseUrl, pathname, requestHeaders,
uriInfo.getQueryParameters());

return build(elide.delete(route.getBaseUrl(), route.getPath(), jsonApiDocument, queryParams, requestHeaders,
user, route.getApiVersion(), UUID.randomUUID()));
}

private static Response build(ElideResponse response) {
Expand All @@ -201,12 +216,14 @@ private static Response build(ElideResponse response) {

protected String getBaseUrlEndpoint(UriInfo uriInfo) {
String baseUrl = elide.getElideSettings().getBaseUrl();

if (StringUtils.isEmpty(baseUrl)) {
//UriInfo has full path appended here already.
baseUrl = ResourceUtils.resolveBaseUrl(uriInfo);
}

return baseUrl;
String path = uriInfo.getBaseUri().getPath();
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
return baseUrl + path;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
public class JsonApiTest {
private JsonApiMapper mapper;
private User user = new TestUser("0");
private static String BASE_URL = "http://localhost:8080";
private static String BASE_URL = "http://localhost:8080/json";

private EntityDictionary dictionary;
private DataStoreTransaction tx = mock(DataStoreTransaction.class, Answers.CALLS_REAL_METHODS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public void populateEntityDictionary(EntityDictionary dictionary) {
metadataModelClasses.forEach(
cls -> dictionary.bindEntity(cls, IS_FIELD_HIDDEN)
);
this.metadataDictionary.getApiVersions().forEach(dictionary.getApiVersions()::add);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public void populateEntityDictionary(EntityDictionary dictionary) {

// bind to multiplex dictionary
dictionary.bindEntity(binding);
subordinateDictionary.getApiVersions().forEach(dictionary.getApiVersions()::add);
}

for (Map.Entry<Type<?>, Function<RequestScope, PermissionExecutor>> entry
Expand Down
Loading

0 comments on commit 62510cc

Please sign in to comment.