com.sap.cds.adapter.odata.v4.ODataV4IndexContentProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cds-adapter-odata-v4 Show documentation
Show all versions of cds-adapter-odata-v4 Show documentation
OData V4 adapter for CDS Services Java
/**************************************************************************
* (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cds.adapter.odata.v4;
import java.io.PrintWriter;
import java.util.stream.Stream;
import com.sap.cds.adapter.IndexContentProvider;
import com.sap.cds.reflect.CdsDefinition;
import com.sap.cds.reflect.CdsEntity;
import com.sap.cds.reflect.CdsService;
import com.sap.cds.services.runtime.CdsRuntime;
import com.sap.cds.services.utils.DraftUtils;
import com.sap.cds.services.utils.ODataUtils;
import com.sap.cds.services.utils.model.CdsAnnotations;
import com.sap.cds.services.utils.path.CdsResourcePath;
import com.sap.cds.services.utils.path.CdsServicePath;
import com.sap.cds.services.utils.path.UrlPathUtil;
public class ODataV4IndexContentProvider implements IndexContentProvider {
private static final String SERVICE = """
%s/
""";
private static final String ENTITY_START = """
""";
private static final String ENTITY = """
-
""";
private static final String ENTITY_END = """
""";
private final CdsRuntime runtime;
public ODataV4IndexContentProvider(CdsRuntime runtime) {
this.runtime = runtime;
}
@Override
public String getSectionTitle() {
return "OData V4 endpoints";
}
@Override
public int order() {
return -10;
}
@Override
public void writeContent(PrintWriter out, String contextPath) {
Stream cdsServicePaths = CdsServicePath.servicePaths(runtime, CdsODataV4ServletFactory.PROTOCOL_KEY);
String basePath = UrlPathUtil.normalizeBasePath(runtime.getEnvironment().getCdsProperties().getOdataV4().getEndpoint().getPath());
String theBasePath = contextPath + (basePath.equals("/") ? "" : basePath);
cdsServicePaths.filter(p -> p.getCdsDefinition() instanceof CdsService).sorted().forEach(s -> {
String path = theBasePath + "/" + s.getPath();
out.format(SERVICE, path, path, path);
out.write(ENTITY_START);
s.subPaths().filter(e -> isExposedEntity(e.getCdsDefinition())).sorted().forEach(e -> {
String odataEntity = ODataUtils.toODataName(e.getPath());
String entityPath = path + "/" + odataEntity;
out.format(ENTITY, entityPath, e.getPath(), s.getCdsDefinition().getQualifiedName(), odataEntity);
});
out.write(ENTITY_END);
});
}
public static boolean isExposedEntity(CdsDefinition definition) {
if(definition instanceof CdsEntity entity) {
boolean autoexposed = CdsAnnotations.AUTOEXPOSED.isTrue(definition);
boolean autoexpose = CdsAnnotations.AUTOEXPOSE.isTrue(definition);
String name = definition.getName();
return !(name.endsWith("_drafts") ||
name.endsWith("_texts") || name.endsWith(".texts") ||
name.endsWith("DraftAdministrativeData") ||
(!DraftUtils.isDraftEnabled(entity) && autoexposed && !autoexpose));
}
return false;
}
}