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

com.sap.ipe.ble.remotehandler.handler.RemoteHandler Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package com.sap.ipe.ble.remotehandler.handler;

import cds.gen.com.sap.ipe.ble.remotehandler.remotehandlerservice.HandleContext;
import cds.gen.com.sap.ipe.ble.remotehandler.remotehandlerservice.Json;
import cds.gen.com.sap.ipe.ble.remotehandler.remotehandlerservice.RemoteHandlerService_;
import cds.gen.com.sap.ipe.ble.remotehandler.remotehandlerservice.Result;
import com.sap.cds.Struct;
import com.sap.cds.services.EventContext;
import com.sap.cds.services.cds.ApplicationService;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.On;
import com.sap.cds.services.handler.annotations.ServiceName;
import com.sap.cds.services.runtime.CdsRuntime;
import com.sap.ipe.ble.remotehandler.authentication.CustomAuthenticationInfoProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.annotation.RequestScope;

import java.util.*;

/***
 * This is the Odata service which will invoke the call to the extended action based on the
 * Payload received
 */
@RequestScope
@Component
@ServiceName(RemoteHandlerService_.CDS_NAME)
public class RemoteHandler implements EventHandler {

	@Autowired
	private CdsRuntime cdsRuntime;

	@Autowired
	CustomAuthenticationInfoProvider authInfoProvider;

	Object returnValue = null;

	/***
	 * Method used to invoke call to the extended action and get the response
	 * @param handle Context which holds the details of the extended actions i.e. Service and the entity name
	 */
	@On(service = RemoteHandlerService_.CDS_NAME)
	public void onHandle(HandleContext handle) {

		String service = handle.getServiceName();
		String event = handle.getEntity();
		Map payload = handle.getData();
		Result result = Result.create();
		String[] serviceEntity = service.split("[.]");
		String entityName;
		String serviceName;

		authInfoProvider.setJwt(handle.getUserClaim());

		if(serviceEntity.length > 1){
			serviceName = serviceEntity[0];
			entityName = service;
		} else {
			serviceName = service;
			entityName = null;
		}

		try {
			cdsRuntime
					.requestContext()
					.providedUser()
					.run(
							context -> {
								ApplicationService applicationService = handle.getCdsRuntime().getServiceCatalog()
										.getService(ApplicationService.class, serviceName);
								EventContext eventcontext = EventContext.create(event, entityName);
								eventcontext.put("data",payload);
								payload.entrySet().forEach(
										arg -> eventcontext.put(arg.getKey(),arg.getValue())
								);
								applicationService.emit(eventcontext);
								returnValue = eventcontext.get("result");
							});

			if(returnValue != null) {
				Json reponseJson= Struct.create(Json.class);
				reponseJson.putAll((Map) returnValue);
				result.setData(reponseJson);
			}

			ArrayList messages = new ArrayList<>();
			if(handle.getMessages() != null) {
				handle.getMessages().stream().forEach(
						msg -> {
							Json messageJson= Json.create();
							messageJson.put(msg.getSeverity().toString(),msg.getMessage());
							messages.add(messageJson);
						}
				);
			}

			result.setMessages(messages);
			handle.setResult(result);

			handle.setCompleted();

		} finally {
			authInfoProvider.removeJwt();
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy