I tried accessing the system on-premises from an SAP Cloud Platform application.
Create a simple JAVA application to check the functionality and try to access the on-premise ABAP system from SAP Cloud Platform.
I will briefly introduce the operation of the application.
This is the SAP Cloud Platform authentication screen.
Enter any text and press the submit button to call the function module STFC_CONNECTION
.
The character string entered in Input Text is passed to the parameter REQUTEXT
when calling.
The value returned by the function module STFC_CONNECTION
is displayed.
RESPTEXT
shows the release of SAP Netweaver, the current date and time, and information about the user who used the access.
To connect with JCo, pass the destination name set in SAP Cloud Platform to the argument of JCoDestinationManager # getDestination
and get the JCoDestination
object. You can connect to the on-premises ABAP server from this object via the SAP Cloud Connector.
The following is an example when the destination name set in SAP Cloud Platform can be obtained with p.getProperty (JCO_DESTINATION)
.
return JCoDestinationManager.getDestination(p.getProperty(JCO_DESTINATION));
Specifically, it was implemented in the following form. Actually, it should be easier to use if you create Interface and inherit it, but I will omit it because it is a trial.
StfcConnection.java
public class StfcConnection {
public static final String PRAM_REQUTEXT = "REQUTEXT";
public static final String PRAM_ECHOTEXT = "ECHOTEXT";
public static final String PRAM_RESPTEXT = "RESPTEXT";
private static final String FUNCTION_NAME = "STFC_CONNECTION";
private static final String JCO_DESTINATION = "jco.destination";
private static final String JCO_PROPERTIES = "jco.properties";
private String propFilePath;
private String requText;
private String echoText;
private String respText;
public StfcConnection(String propDirPath, String requText) {
this.propFilePath = Paths.get(propDirPath, JCO_PROPERTIES).toString();
this.requText = requText;
}
public void execute() throws JCoException, FileNotFoundException, IOException {
JCoDestination d = getDestiation();
JCoFunction f = d.getRepository().getFunction(FUNCTION_NAME);
//Function module call parameter setting
f.getImportParameterList().setValue(PRAM_REQUTEXT, requText);
//General-purpose module execution
f.execute(d);
//Acquisition of function module execution result
JCoParameterList exports = f.getExportParameterList();
echoText = exports.getString(PRAM_ECHOTEXT);
respText = exports.getString(PRAM_RESPTEXT);
}
private JCoDestination getDestiation() throws FileNotFoundException, IOException, JCoException {
InputStream is = new FileInputStream(propFilePath);
Properties p = new Properties();
// load jco destination name
p.load(is);
is.close();
return JCoDestinationManager.getDestination(p.getProperty(JCO_DESTINATION));
}
public String getEchoText() {
return echoText;
}
public String getRespText() {
return respText;
}
}
I was able to successfully access the on-premise ABAP system from SAP Cloud Platform via the SAP Cloud Connector. I felt that the implementation on the JAVA side, including authentication, has become easy.
I implemented programmatic authentication by referring to the following help page.
For implementation, we prepared a dedicated class and called it from each Servlet.
Authenication.java
public class Authenication {
private final static Logger logger = LoggerFactory.getLogger(Authenication.class);
protected static void login(String user) throws LoginException {
if (user == null) {
logger.debug("User is not logged in.");
//authenticate the User
LoginContext loginContext = LoginContextFactory.createLoginContext("FORM");
loginContext.login();
}
}
protected static void logout(String user) throws LoginException {
if (user != null) {
logger.debug("User is logged in.");
//logout
LoginContext loginContext = LoginContextFactory.createLoginContext("FORM");
loginContext.logout();
}
}
protected static boolean isLoggedin(HttpServletRequest request) {
if (request.getRemoteUser() == null) {
return false;
} else {
return true;
}
}
}
The source code has been uploaded to GitHub. For your reference. GitHub - ScpJcoTest1
Recommended Posts