All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cds.adapter.rest.CdsRestServletFactory Maven / Gradle / Ivy

/**************************************************************************
 * (C) 2019-2021 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.adapter.rest;

import java.util.Objects;
import java.util.stream.Stream;

import javax.servlet.Servlet;

import com.sap.cds.adapter.ServletAdapterFactory;
import com.sap.cds.adapter.UrlResourcePath;
import com.sap.cds.reflect.CdsEntity;
import com.sap.cds.services.runtime.CdsRuntime;
import com.sap.cds.services.runtime.CdsRuntimeAware;
import com.sap.cds.services.utils.path.CdsResourcePath;
import com.sap.cds.services.utils.path.CdsServicePath;
import com.sap.cds.services.utils.path.UrlPathUtil;
import com.sap.cds.services.utils.path.UrlResourcePathBuilder;

public class CdsRestServletFactory implements ServletAdapterFactory, CdsRuntimeAware {

	public final static String PROTOCOL_KEY = "rest";

	private CdsRuntime runtime;

	@Override
	public void setCdsRuntime(CdsRuntime runtime) {
		this.runtime = runtime;
	}

	@Override
	public Servlet create() {
		return new CdsRestServlet(runtime);
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public String getBasePath() {
		return "/rest";
	}

	@Override
	public String[] getMappings() {
		Stream cdsServicePaths = CdsServicePath.servicePaths(runtime, PROTOCOL_KEY);
		return cdsServicePaths.map(servicePath -> UrlResourcePathBuilder.path(getBasePath(), servicePath.getPath()).recursive().build().getPath()).toArray(l -> new String[l]);
	}

	@Override
	public UrlResourcePath getServletPath() {

		Stream cdsServicePaths = CdsServicePath.servicePaths(runtime, PROTOCOL_KEY);
		return UrlResourcePathBuilder.path(getBasePath()).recursive().subPaths(
				cdsServicePaths.flatMap(cdsServicePath -> {
					return Stream.of(
							// Path: /service
							UrlResourcePathBuilder.path(cdsServicePath.getPath())
							.isPublic(cdsServicePath.isPublic())
							.subPaths( cdsServicePath.subPaths().filter(p -> p.hasType(CdsEntity.class)).flatMap(cdsEntityPath -> {

								return Stream.of(
										// Path: /service/entity/**
										UrlResourcePathBuilder.path( cdsEntityPath.getPath() )
										.recursive()
										.isPublic(cdsServicePath.isPublic() && cdsEntityPath.isPublic())
										.publicEvents(cdsEntityPath.publicEvents().map(UrlPathUtil::cdsEvent2HttpMethod).filter(Objects::nonNull))
										.build()
										);
							}) )
							.build()
							);
				})
				).build();

	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy