use of io.crnk.core.engine.information.repository.RepositoryInformation in project crnk-framework by crnk-project.
the class DefaultRegistryEntryBuilder method findRelationshipMatch.
private ResponseRelationshipEntry findRelationshipMatch(ResourceField relationshipField) {
Collection<Object> repositories = moduleRegistry.getRepositories();
RepositoryInformationProvider repositoryInformationBuilder = moduleRegistry.getRepositoryInformationBuilder();
ResponseRelationshipEntry matchedEntry = null;
for (Object repository : repositories) {
RepositoryInformation repositoryInformation = repositoryInformationBuilder.build(repository, new DefaultRepositoryInformationProviderContext(moduleRegistry));
if (repositoryInformation instanceof RelationshipRepositoryInformation) {
RelationshipRepositoryInformation relationshipRepositoryInformation = (RelationshipRepositoryInformation) repositoryInformation;
RelationshipMatcher matcher = relationshipRepositoryInformation.getMatcher();
if (matcher.matches(relationshipField)) {
if (matchedEntry != null) {
throw new IllegalStateException("multiple repositories for " + relationshipField + ": " + repository + ", " + matchedEntry);
}
matcher.matches(relationshipField);
matchedEntry = setupRelationship(relationshipField, relationshipRepositoryInformation, repository);
}
}
}
return matchedEntry;
}
use of io.crnk.core.engine.information.repository.RepositoryInformation in project crnk-framework by crnk-project.
the class HomeModule method setupModule.
@Override
public void setupModule(final ModuleContext context) {
this.moduleContext = context;
context.addHttpRequestProcessor(new HttpRequestProcessor() {
@Override
public void process(HttpRequestContext requestContext) throws IOException {
if (isHomeRequest(requestContext)) {
Set<String> pathSet = new HashSet<>();
ResourceRegistry resourceRegistry = context.getResourceRegistry();
for (RegistryEntry resourceEntry : resourceRegistry.getResources()) {
RepositoryInformation repositoryInformation = resourceEntry.getRepositoryInformation();
if (repositoryInformation != null && context.getResourceFilterDirectory().get(resourceEntry.getResourceInformation(), HttpMethod.GET) == FilterBehavior.NONE) {
ResourceInformation resourceInformation = resourceEntry.getResourceInformation();
String resourceType = resourceInformation.getResourceType();
pathSet.add("/" + resourceType);
}
}
if (extensions != null) {
for (HomeModuleExtension extension : extensions) {
pathSet.addAll(extension.getPaths());
}
}
String requestPath = requestContext.getPath();
pathSet = pathSet.stream().map(it -> getChildName(requestPath, it)).filter(it -> it != null).collect(Collectors.toSet());
List<String> pathList = new ArrayList<>(pathSet);
Collections.sort(pathList);
if (defaultFormat == HomeFormat.JSON_HOME || requestContext.accepts(JSON_HOME_CONTENT_TYPE)) {
boolean acceptsHome = requestContext.accepts(JSON_HOME_CONTENT_TYPE);
if (acceptsHome) {
requestContext.setContentType(JSON_HOME_CONTENT_TYPE);
} else {
requestContext.setContentType(JSON_CONTENT_TYPE);
}
writeJsonHome(requestContext, pathList);
} else {
boolean jsonapi = requestContext.accepts(HttpHeaders.JSONAPI_CONTENT_TYPE);
if (jsonapi) {
requestContext.setContentType(HttpHeaders.JSONAPI_CONTENT_TYPE);
} else {
requestContext.setContentType(JSON_CONTENT_TYPE);
}
writeJsonApi(requestContext, pathList);
}
}
}
private String getChildName(String requestPath, String it) {
if (it.startsWith(requestPath)) {
String subPath = UrlUtils.removeTrailingSlash(it.substring(requestPath.length()));
int sep = subPath.indexOf('/');
return sep == -1 ? subPath : subPath.substring(0, sep) + "/";
}
return null;
}
});
}
Aggregations