Skip to content
32 changes: 8 additions & 24 deletions java/src/main/java/com/genexus/sap/DestinationProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static DestinationProvider Instance()
return _instance;
}

private String SAP_SERVER = "SAP_SERVER";
private String SAP_DOC_SERVER = "DMS_SERVER";
private static final String SAP_SERVER = "SAP_SERVER";
private static final String SAP_DOC_SERVER = "DMS_SERVER";
private Properties connectionProperties;
private Hashtable<String, Properties> connectionList = new Hashtable<String, Properties>();
private DestinationDataEventListener eventListener;
Expand All @@ -33,16 +33,8 @@ public static DestinationProvider Instance()
public Properties getDestinationProperties(String sessionName)
{
if (sessionName != null)
{
Properties val = connectionList.get(sessionName);
if (val != null)
{
return val;
}
else
{
return null;
}
{
return connectionList.get(sessionName);
}
else
{
Expand All @@ -65,16 +57,8 @@ public void setDestinationDataEventListener(DestinationDataEventListener eventLi
@Override
public Properties getServerProperties(String serverName) {
if (serverName != null)
{
Properties val = connectionList.get(serverName);
if (val != null)
{
return val;
}
else
{
return null;
}
{
return connectionList.get(serverName);
}
else
{
Expand Down Expand Up @@ -118,7 +102,7 @@ public void setServerProperties( String serverName, Properties properties)
}
else
{
Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + Integer.toString(connectionList.size()));
Log.info("GX SAP Doc Server - Setting Properties : " + serverName + " total : " + connectionList.size());
connectionProperties = properties;
connectionList.put( serverName, connectionProperties);
if (eventListener != null)
Expand Down Expand Up @@ -160,7 +144,7 @@ public void setConnectionProperties( String sessionName, Properties properties)
}
else
{
Log.info("GX SAP - Setting Properties : " + sessionName + " total : " + Integer.toString(connectionList.size()));
Log.info("GX SAP - Setting Properties : " + sessionName + " total : " + connectionList.size());
connectionProperties = properties;
connectionList.put( sessionName, connectionProperties);
if (eventListener != null)
Expand Down
43 changes: 18 additions & 25 deletions java/src/main/java/com/genexus/sap/DocumentClient.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package com.genexus.sap;

import java.io.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import com.sap.conn.jco.AbapClassException;
import com.sap.conn.jco.AbapException;

import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoParameterList;
import com.sap.conn.jco.JCoTable;
import com.sap.conn.jco.server.JCoServer;
import com.sap.conn.jco.server.JCoServerContext;
import com.sap.conn.jco.server.JCoServerContextInfo;
import com.sap.conn.jco.server.JCoServerErrorListener;
import com.sap.conn.jco.server.JCoServerExceptionListener;
import com.sap.conn.jco.server.*;

import com.sap.conn.jco.server.JCoServerFunctionHandler;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

public class DocumentClient {
private static final int BLOB_LENGTH = 1022;
Expand All @@ -43,7 +35,7 @@ public void serverErrorOccurred(JCoServer server, String connectionID, JCoServer
static class FTP_R3_TO_CLIENTHandler implements JCoServerFunctionHandler {

@Override
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) {
String fname;
int length;
JCoTable blob;
Expand All @@ -67,14 +59,18 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro
length -= BLOB_LENGTH;
hasNextRow = blob.nextRow();
}
else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")");
else throw new IOException("Not enough data in table BLOB ("+ BLOB_LENGTH * blob.getNumRows() +") for requested file size (" + length + ")");
}
if (length > 0){
if (hasNextRow) out.write(blob.getByteArray(0), 0, length);
else throw new IOException("Not enough data in table BLOB ("+String.valueOf(BLOB_LENGTH * blob.getNumRows())+") for requested file size (" + String.valueOf(length) + ")");
else throw new IOException("Not enough data in table BLOB ("+ BLOB_LENGTH * blob.getNumRows() +") for requested file size (" + length + ")");
}
}
catch (IOException e) {

// Unfortunately there is no way of transmitting error details back to SAP, so we better log it here,
// if we want to keep the chance of troubleshooting later, what exactly went wrong...

e.printStackTrace();
function.getExportParameterList().setValue("ERROR", 3);
}
Expand All @@ -92,19 +88,19 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro

static class FTP_CLIENT_TO_R3Handler implements JCoServerFunctionHandler {
@Override
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) throws AbapException, AbapClassException {
public void handleRequest(JCoServerContext serverCtx, JCoFunction function) {
String fname;

fname = function.getImportParameterList().getString("FNAME");
fname = fname.replace("#","");

try (InputStream source = Files.newInputStream(Paths.get(fname));) {
Path inputFile = Paths.get(fname);
try (InputStream source = Files.newInputStream(inputFile)) {

byte[] file2 = new byte[BLOB_LENGTH];
int bytesread;
int totallenght =0;

InputStream source2 = Files.newInputStream(Paths.get(fname));
InputStream source2 = Files.newInputStream(inputFile);

JCoTable blobtable = function.getTableParameterList().getTable("BLOB");
while((bytesread = source2.read(file2,0,file2.length)) >0 )
Expand All @@ -124,7 +120,4 @@ public void handleRequest(JCoServerContext serverCtx, JCoFunction function) thro
}
}
}



}
122 changes: 122 additions & 0 deletions java/src/main/java/com/genexus/sap/DocumentReceiver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.genexus.sap;

import com.genexus.ModelContext;
import com.sap.conn.jco.JCoException;

import com.sap.conn.jco.JCoMetaData;
import com.sap.conn.jco.JCoRecordMetaData;
import com.sap.conn.jco.server.JCoServer;
import com.sap.conn.jco.server.DefaultServerHandlerFactory;

import com.sap.conn.jco.server.JCoServerFactory;
import com.sap.conn.jco.JCoCustomRepository;
import com.sap.conn.jco.JCoFunctionTemplate;
import com.sap.conn.jco.JCoListMetaData;
import com.sap.conn.jco.JCo;
import com.genexus.diagnostics.Log;

public class DocumentReceiver
{
private String serverName;
private String repositoryName;
private ModelContext _context;
JCoServer receiver = null;


public DocumentReceiver(String server, String repository, ModelContext context)
{
this.serverName = server;
this.repositoryName = repository;
this._context = context;
}

public void start()
{
Log.info("GX SAP - DMS Receiver Start " + serverName + " " + repositoryName);
try {
receiver = setupServerDownload(serverName, repositoryName);
receiver.start();
}
catch (JCoException ex)
{
Log.error("GX SAP - Error Starting " + ex) ;
}
}

public void stop()
{

String receiverName = "";
if (serverName == null || serverName.equals(""))
{
Object receiverObj = _context.getContextProperty("SAPReceiverServerName");
if (receiverObj != null)
{
receiverName = (String)receiverObj;
}
}
else
{
receiverName = serverName;
}

try {
if (!receiverName.equals(""))
{
receiver = JCoServerFactory.getServer(receiverName);
if (receiver != null) receiver.stop();
Log.info("GX SAP - DMS Receiver stop " + receiverName );
}
else
{
Log.error("GX SAP - Error Stopping receiver." );
}
}
catch (JCoException ex)
{
Log.error("GX SAP - Error Stopping " + ex) ;
}

}

public JCoServer setupServerDownload(String serverName, String repositoryName) throws JCoException
{
JCoServer server = JCoServerFactory.getServer(serverName);
JCoCustomRepository repo = JCo.createCustomRepository(repositoryName);
JCoListMetaData imports, exports, tables;
JCoFunctionTemplate FTP_R3_TO_CLIENT;

imports = JCo.createListMetaData("IMPORTING");
imports.add("FNAME", JCoMetaData.TYPE_CHAR, 256, 512, JCoListMetaData.IMPORT_PARAMETER);
imports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.IMPORT_PARAMETER);
imports.add("MODE", JCoMetaData.TYPE_CHAR, 1, 2, JCoListMetaData.IMPORT_PARAMETER | JCoListMetaData.OPTIONAL_PARAMETER);
imports.lock();

exports = JCo.createListMetaData("EXPORTING");
exports.add("ERROR", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER);
exports.add("LENGTH", JCoMetaData.TYPE_INT, 4, 4, JCoListMetaData.EXPORT_PARAMETER);
exports.lock();

JCoRecordMetaData tabLine = JCo.createRecordMetaData("BLOB");
tabLine.add("LINE", JCoMetaData.TYPE_BYTE, EnterpriseConnect.BLOB_LENGTH, 0, EnterpriseConnect.BLOB_LENGTH, 0);
tabLine.lock();
tables = JCo.createListMetaData("TABLES");
tables.add("BLOB", JCoMetaData.TYPE_TABLE, tabLine, 0);
tables.lock();

FTP_R3_TO_CLIENT = JCo.createFunctionTemplate("FTP_R3_TO_CLIENT", imports, exports, null, tables, null);
repo.addFunctionTemplateToCache(FTP_R3_TO_CLIENT);
server.setRepository(repo);

DefaultServerHandlerFactory.FunctionHandlerFactory handlerFactory = new DefaultServerHandlerFactory.FunctionHandlerFactory();
handlerFactory.registerHandler("FTP_R3_TO_CLIENT", new DocumentClient.FTP_R3_TO_CLIENTHandler());
server.setCallHandlerFactory(handlerFactory);

DocumentClient.ErrorHandler hdl = new DocumentClient.ErrorHandler();
server.addServerErrorListener(hdl);
server.addServerExceptionListener(hdl);

return server;
}

}
Loading