1+ package com .genexus .sap ;
2+
3+ import java .io .*;
4+ import java .io .FileOutputStream ;
5+ import java .io .IOException ;
6+ import java .nio .file .Files ;
7+ import java .nio .file .Paths ;
8+
9+ import com .sap .conn .jco .AbapClassException ;
10+ import com .sap .conn .jco .AbapException ;
11+
12+ import com .sap .conn .jco .JCoFunction ;
13+ import com .sap .conn .jco .JCoParameterList ;
14+ import com .sap .conn .jco .JCoTable ;
15+ import com .sap .conn .jco .server .JCoServer ;
16+ import com .sap .conn .jco .server .JCoServerContext ;
17+ import com .sap .conn .jco .server .JCoServerContextInfo ;
18+ import com .sap .conn .jco .server .JCoServerErrorListener ;
19+ import com .sap .conn .jco .server .JCoServerExceptionListener ;
20+
21+ import com .sap .conn .jco .server .JCoServerFunctionHandler ;
22+
23+ public class DocumentClient {
24+ private static final int BLOB_LENGTH = 1022 ;
25+
26+ static class ErrorHandler implements JCoServerErrorListener , JCoServerExceptionListener {
27+
28+ @ Override
29+ public void serverExceptionOccurred (JCoServer server , String connectionID , JCoServerContextInfo serverCtx , Exception error ) {
30+ // Technical problem in server connection (network, logon data, etc.)
31+ error .printStackTrace ();
32+ }
33+
34+ @ Override
35+ public void serverErrorOccurred (JCoServer server , String connectionID , JCoServerContextInfo serverCtx , Error error ) {
36+ // Technical problem in server connection (out-of-memory, etc.)
37+ error .printStackTrace ();
38+ }
39+
40+ }
41+
42+ // BAPI_DOCUMENT_CHECKOUTVIEW2 will send the file data via this function module.
43+ static class FTP_R3_TO_CLIENTHandler implements JCoServerFunctionHandler {
44+
45+ @ Override
46+ public void handleRequest (JCoServerContext serverCtx , JCoFunction function ) throws AbapException , AbapClassException {
47+ String fname ;
48+ int length ;
49+ JCoTable blob ;
50+
51+ // In the case of BAPI_DOCUMENT_CHECKOUTVIEW2, MODE is always binary, so the MODE and TEXT parameters of FTP_R3_TO_CLIENT can be ignored.
52+ JCoParameterList imports = function .getImportParameterList ();
53+ fname = imports .getString ("FNAME" );
54+ length = imports .getInt ("LENGTH" );
55+ blob = function .getTableParameterList ().getTable ("BLOB" );
56+ FileOutputStream out = null ;
57+ try {
58+ out = new FileOutputStream (fname );
59+ boolean hasNextRow = false ;
60+ if (!blob .isEmpty ()){
61+ hasNextRow = true ;
62+ blob .firstRow ();
63+ }
64+ while (length > BLOB_LENGTH ){
65+ if (hasNextRow ){
66+ out .write (blob .getByteArray (0 ), 0 , BLOB_LENGTH );
67+ length -= BLOB_LENGTH ;
68+ hasNextRow = blob .nextRow ();
69+ }
70+ else throw new IOException ("Not enough data in table BLOB (" +String .valueOf (BLOB_LENGTH * blob .getNumRows ())+") for requested file size (" + String .valueOf (length ) + ")" );
71+ }
72+ if (length > 0 ){
73+ if (hasNextRow ) out .write (blob .getByteArray (0 ), 0 , length );
74+ else throw new IOException ("Not enough data in table BLOB (" +String .valueOf (BLOB_LENGTH * blob .getNumRows ())+") for requested file size (" + String .valueOf (length ) + ")" );
75+ }
76+ }
77+ catch (IOException e ) {
78+ e .printStackTrace ();
79+ function .getExportParameterList ().setValue ("ERROR" , 3 );
80+ }
81+ finally {
82+ if (out != null ){
83+ try {
84+ out .close ();
85+ }
86+ catch (IOException ioe ){}
87+ }
88+ }
89+ }
90+
91+ }
92+
93+ static class FTP_CLIENT_TO_R3Handler implements JCoServerFunctionHandler {
94+ @ Override
95+ public void handleRequest (JCoServerContext serverCtx , JCoFunction function ) throws AbapException , AbapClassException {
96+ String fname ;
97+
98+ fname = function .getImportParameterList ().getString ("FNAME" );
99+ fname = fname .replace ("#" ,"" );
100+
101+ try (InputStream source = Files .newInputStream (Paths .get (fname ));) {
102+
103+ byte [] file2 = new byte [BLOB_LENGTH ];
104+ int bytesread ;
105+ int totallenght =0 ;
106+
107+ InputStream source2 = Files .newInputStream (Paths .get (fname ));
108+
109+ JCoTable blobtable = function .getTableParameterList ().getTable ("BLOB" );
110+ while ((bytesread = source2 .read (file2 ,0 ,file2 .length )) >0 )
111+ {
112+
113+ blobtable .appendRow ();
114+ blobtable .setValue ("LINE" , file2 );
115+ totallenght += bytesread ;
116+ }
117+
118+ JCoParameterList exports = function .getExportParameterList ();
119+ exports .setValue ("LENGTH" , totallenght );
120+ }
121+ catch (IOException e ) {
122+ e .printStackTrace ();
123+ function .getExportParameterList ().setValue ("ERROR" , 3 );
124+ }
125+ }
126+ }
127+
128+
129+
130+ }
0 commit comments