Show TOC

 Examples for Using the Destination Service APILocate this document in the navigation structure

Imports

The following packages contain the classes needed for using the Destination service API.

  • com.sap.security.core.server.destinations.api.DestinationService;
  • com.sap.security.core.server.destinations.api.Destination;
  • com.sap.security.core.server.destinations.api.DestinationException;
  • com.sap.security.core.server.destinations.api.HTTPDestination;
  • com.sap.security.core.server.destinations.api.RFCDestination;
    Note

    To be able to import those classes and compile your sources, you have to first include these libraries in your project. Choose Set Additional Libraries… from the context menu for your project and add the items security.class and tc/sec/destinations/interface .

HTTP(S) Destinations

The following examples show how to use the Destination service to set up HTTP(S) connections. The first example shows how to establish the connection using an existing destination. The second example shows how to create a destination using Basic Authentication.

Establishing an HTTP Connection Using an Existing Destination

The following example shows how to set up a connection to the HTTP destination dst-1 . The URL and user authentication information have been stored in the Destination service and have been maintained in the SAP NetWeaver Administrator. The authentication method that is used for the connection depends on the settings in the destination (none, Basic Authentication, X.509 client certificates, or SAP logon tickets). If the URL saved in the destination uses HTTPS, then SSL with server authentication will be used to secure the connection.

Code Example for Establishing an HTTP Connection Using an Existing Destination

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.sap.security.core.server.destinations.api.DestinationException;
import com.sap.security.core.server.destinations.api.DestinationService;
import com.sap.security.core.server.destinations.api.HTTPDestination;

    Context ctx = new InitialContext();
    DestinationService dstService = (DestinationService) 
    ctx.lookup(DestinationService.JNDI_KEY);
    
    if (dstService == null)
        throw new NamingException("Destination Service not available");
    
    Destination destination = dstService.getDestination("HTTP","dst-1");
    //for HTTP destination: cast
    HTTPDestination httpDestination = (HTTPDestination) destination;
    //obtain a HTTPUrlConnection from the destinationHttpURLConnection
    httpConnection = httpDestination.getURLConnection();

Creating a New HTTP Destination

The following example shows how to create a new destination dst-2 and store it. All data is stored in the AS Java's secure storage area. The data is stored encrypted if the SAP Java Cryptographic Toolkit has been installed.

The created destination may be maintained using the SAP NetWeaver Administrator.

Code Example for Creating a New HTTP Destination

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.sap.security.core.server.destinations.api.DestinationException;
import com.sap.security.core.server.destinations.api.DestinationService;
import com.sap.security.core.server.destinations.api.HTTPDestination;

    Context ctx = new InitialContext();
    DestinationService dstService = (DestinationService)
    ctx.lookup(DestinationService.JNDI_KEY);
    if (dstService == null)
        throw new NamingException("Destination Service not available");
    
    //Note: this only created the destination
    //it needs to be stored using dstService.storeDestination
    
    HTTPDestination destination = (HTTPDestination)
    dstService.createDestination("HTTP");
    destination.setUrl(url);
    destination.setName(name);
    destination.setUsernamePassword("testuser", "abc123");
    dstService.storeDestination("HTTP", destination);

 

RFC Destinations

Using SAP JCo 3.0

If you are using SAP JCo 3.0 or higher, you do not have to explicitly call the Destination service. Set up the connection as described in the developer documentation for SAP Jco 3.0.

Using SAP JCo 2.x

The following examples show how to use the Destination service to set up an RFC connection when using the SAP JCo 2.x . The first example shows how to establish the connection using an existing destination. The second example shows how to create a destination using a user ID and password that is stored in the RFC destination.

Establishing an RFC Connection Using an Existing Destination

The following example shows how to set up a connection to the RFC destination dst-2 . The connection data and user authentication information have been maintained and are stored in the destination.

When establishing the connection, you can either use a direct connection or you can make use of a connection pool. See the corresponding code example sections.

Code Example for Establishing an RFC Connection Using an Existing Destination

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Exception;
import com.sap.security.core.server.destinations.api.DestinationException;
import com.sap.security.core.server.destinations.api.DestinationService;
import com.sap.security.core.server.destinations.api.RFCDestination;

    DestinationService dstService = (DestinationService) 
    ctx.lookup(DestinationService.JNDI_KEY);
    
    if (dstService == null)
        throw new NamingException("Destination Service not available");
    

    RFCDestination dst =
        (RFCDestination) dstService.getDestination("RFC", "dst-2");
    int maxPoolSize = dst.getMaxPoolSize();
    long maxWaitTime = dst.getMaxWaitTime();
    Properties jcoProperties = dst.getJCoProperties();

/**
* Code example for establishing a direct connection
*/
    JCO.Client client = JCO.createClient(jcoProperties);
    client.connect();
    client.ping();
    client.disconnect();

/**
* Code example for establishing a pooled connection
*/
    JCO.addClientPool("myPool", maxPoolSize, jcoProperties);
    JCO.getClientPoolManager().getPool("myPool").setMaxWaitTime(maxWaitTime);

    JCO.Client pooledClient = null;

//Check for a pool connection. If no pool connection exists, then an error //occurs. 
    try {
        pooledClient = JCO.getClient("myPool");
        pooledClient.ping();
    } catch (JCO.Exception exc) {
        if (exc.getGroup() == Exception.JCO_ERROR_RESOURCE) {
            //Error: no connection available from pool
            //Insert code to handle error.
        }

//Release pooled connection
    } finally {
        if (pooledClient != null) {
            JCO.releaseClient(pooledClient);
        }
    }

Creating a New RFC Destination

The following example shows how to create a new RFC destination dst-2 and store it. The destination's data are first set in the JCo's properties and then written to the RFC destination. The destination uses load balancing and a user ID and password for authentication. The created destination may be maintained afterwards using the SAP NetWeaver Administrator.

Code Example for Creating a New RFC Destination

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import com.sap.mw.jco.JCO;
import com.sap.mw.jco.JCO.Exception;
import com.sap.security.core.server.destinations.api.DestinationException;
import com.sap.security.core.server.destinations.api.DestinationService;
import com.sap.security.core.server.destinations.api.RFCDestination;

    DestinationService dstService = (DestinationService)
    ctx.lookup(DestinationService.JNDI_KEY);
    if (dstService == null)
        throw new NamingException("Destination Service not available");
    
    //Note: this only created the destination
    //it needs to be stored using dstService.storeDestination
    
    RFCDestination destination = (RFCDestination)
        dstService.createDestination("RFC");
    Properties jcoPropeties = new Properties();
    jcoPropeties.setProperty("jco.client.client", "000");
    jcoPropeties.setProperty("jco.client.user", "MY_USER");
    jcoPropeties.setProperty("jco.client.passwd", "SAPSAP");
    jcoPropeties.setProperty("jco.client.lang", "EN");
    jcoPropeties.setProperty("jco.client.r3name", "ABC");
    jcoPropeties.setProperty("jco.client.group", "PUBLIC");
    jcoPropeties.setProperty("jco.client.mshost", "myhost");
    jcoPropeties.setProperty("jco.client.type", "3");
    jcoPropeties.setProperty("jco.destination.peak_limit", "20");
    jcoPropeties.setProperty("jco.destination.pool_capacity", "10");
    rfcDestination.setJcoProperties(RFCDestination.AUTHENTICATION_MODE_CONFIGURED_USER, RFCDestination.CONNECTION_MODE_LOAD_BALANCING, jcoPropeties);
    dst.setName("dst-2");
    dstService.storeDestination("RFC", dst);