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

There is a newer version: 3.6.0
Show newest version
/**************************************************************************
 * (C) 2019-2021 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.Map;
import java.util.Objects;

import javax.jms.ConnectionFactory;
import javax.net.ssl.SSLContext;

import org.apache.qpid.jms.JmsConnectionFactory;

import com.sap.cds.mtx.impl.XsuaaParams;
import com.sap.cds.services.environment.ServiceBinding;
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.security.config.ClientIdentity;
import com.sap.cloud.security.config.OAuth2ServiceConfiguration;
import com.sap.cloud.security.mtls.SSLContextFactory;

/**
 * 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());

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

		this.uaaCredentials = (Map) binding.getCredentials().get("uaa");
		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 {
		XsuaaParams xsuaaParams = new XsuaaParams(uaaCredentials);
		OAuth2ServiceConfiguration oAuth2ServiceConfiguration = xsuaaParams.getOAuth2ServiceConfiguration();
		ClientIdentity clientIdentity = oAuth2ServiceConfiguration.getClientIdentity();
		return SSLContextFactory.getInstance().create(clientIdentity);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy