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

com.sap.cds.feature.messaging.mq.jms.MessageQueuingConnectionProvider Maven / Gradle / Ivy

The newest version!
/**************************************************************************
 * (C) 2019-2024 SAP SE or an SAP affiliate company. All rights reserved. *
 **************************************************************************/
package com.sap.cds.feature.messaging.mq.jms;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.net.ssl.SSLContext;

import org.apache.qpid.jms.JmsConnectionFactory;

import com.sap.cds.services.messaging.jms.BrokerConnection;
import com.sap.cds.services.messaging.jms.BrokerConnectionProvider;
import com.sap.cds.services.utils.CdsErrorStatuses;
import com.sap.cds.services.utils.ErrorStatusException;
import com.sap.cloud.environment.servicebinding.api.ServiceBinding;
import com.sap.cloud.security.config.ClientIdentity;
import com.sap.cloud.security.config.OAuth2ServiceConfiguration;
import com.sap.cloud.security.config.OAuth2ServiceConfigurationBuilder;
import com.sap.cloud.security.config.Service;
import com.sap.cloud.security.mtls.SSLContextFactory;

import jakarta.jms.ConnectionFactory;

/**
 * Implementation of the {@link BrokerConnectionProvider} class for message queuing.
 *
 */
public class MessageQueuingConnectionProvider extends BrokerConnectionProvider {

	private final String apiUrl;
	private final Map uaaCredentials;
	private final boolean isMtls;
	private String apiUser;
	private String apiPassword;

	@SuppressWarnings("unchecked")
	public MessageQueuingConnectionProvider(ServiceBinding binding) {
		super(binding.getName().get());

		Map clientApi = (Map) binding.getCredentials().get("amqp10");
		this.apiUrl = (String) clientApi.get("url");

		this.uaaCredentials = new HashMap<>();
		((Map) binding.getCredentials().get("uaa")).forEach((k, v) -> {
			if (v != null) {
				uaaCredentials.put(k, v.toString());
			}
		});
		this.isMtls = Objects.equals("x509", this.uaaCredentials.get("credential-type"));

		if (!isMtls) {
			Map credentials = (Map) ((Map) clientApi.get("auth")).get("basic");
			apiUser = (String) credentials.get("userName");
			apiPassword = (String) credentials.get("password");
		}
	}

	@Override
	protected BrokerConnection createBrokerConnection(String name, Map clientProperties) {
		ConnectionFactory factory = null;
		if (isMtls) {
			try {
				factory = new JmsConnectionFactory(apiUrl);
				((JmsConnectionFactory) factory).setSslContext(getSSLContextFromConfig());
			} catch (GeneralSecurityException | IOException e) {
				throw new ErrorStatusException(CdsErrorStatuses.INVALID_SSL_CONTEXT, e);
			}
		} else {
			factory = new JmsConnectionFactory(apiUser, apiPassword, apiUrl);
		}

		return new BrokerConnection(name, factory);
	}

	private SSLContext getSSLContextFromConfig() throws GeneralSecurityException, IOException {
		OAuth2ServiceConfiguration oAuth2ServiceConfiguration = OAuth2ServiceConfigurationBuilder
				.forService(Service.XSUAA).withProperties(uaaCredentials).build();
		ClientIdentity clientIdentity = oAuth2ServiceConfiguration.getClientIdentity();
		return SSLContextFactory.getInstance().create(clientIdentity);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy