Example: IDoc Client 2 for tRFC
Definition
This example shows how to create an IDoc document from IDoc XML data:
1. import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.naming.InitialContext;
import javax.resource.cci.*;
import com.sap.conn.idoc.IDocDocumentList;
import com.sap.conn.idoc.IDocRepository;
import com.sap.conn.idoc.IDocXMLProcessor;
import com.sap.mw.jco.jra.InteractionSpecFactory;
import com.sap.mw.jco.jra.idoc.JRAIDoc;
import com.sap.mw.jco.jra.idoc.JRAIDocFactory;
/**
* Servlet implementation for IDoc client example
*
* A client scenario for parsing an IDoc XML-String into an IDoc document and send it to ABAP backend
*
*/
public class IDocClient2 extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet
{
private static final String CONTENT_TYPE = "text/html";
public void init() throws ServletException
{
}
/**Process the HTTP Get request*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Connection connection = null;
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>ExampleServlet</title></head>");
out.println("<body>");
try
{
InitialContext initialcontext = new InitialContext();
// look up the ConnectionFactory
ConnectionFactory connectionFactory =
(ConnectionFactory)initialcontext.lookup("java:comp/env/H9B");
out.println("Got factory version:
"+connectionFactory.getMetaData().getAdapterVersion());
out.println("<br>");
// get the IDocFactory instance
JRAIDocFactory idocFactory = JRAIDoc.getIDocFactory();
// get the IDoc repository via the specified connection factory
IDocRepository idocRepository = idocFactory.getIDocRepository(connectionFactory);
out.println("parsing IDoc XML String into an IDoc document...");
out.println("<br>");
// use an already provided IDoc-XML
String iDocXml =
"<?xml version='1.0'?><RSINFO><IDOC BEGIN='1'><EDI_DC40 SEGMENT='1'><TABNAM>EDI_DC40</TABNAM>" +
"<IDOCTYP>RSINFO</IDOCTYP><MESTYP>RSINFO</MESTYP>
<SNDPOR>SAPJCOIDOC</SNDPOR><SNDPRT>LS</SNDPRT>" +
"<SNDPRN>JCOCLNT000</SNDPRN><RCVPRT>LS</RCVPRT>
<RCVPRN>TSTCLNT000</RCVPRN><CREDAT>20101022</CREDAT>" +
"<CRETIM>145006</CRETIM></EDI_DC40><E1RSHIN SEGMENT='1'>
<REQUEST>1</REQUEST><INFOIDOCNR>0815</INFOIDOCNR>" +
"<SELDATE>20101020</SELDATE><SELTIME>174500</SELTIME>
<RQSTATE>1</RQSTATE><RQRECORD>10</RQRECORD>" +
"<E1RSPIN SEGMENT='1'><DATAPAKID>111</DATAPAKID>
<RQDRECORD>10</RQDRECORD></E1RSPIN><E1RSPIN SEGMENT='1'>" +
"<DATAPAKID>222</DATAPAKID><RQDRECORD>10</RQDRECORD>
</E1RSPIN><E1RSEIN SEGMENT='1'><MSGID>1</MSGID>" + "<MSGTY>1</MSGTY><MSGNO>001</MSGNO><MSGV1>msg variable I</MSGV1>
<MSGV2>msg variable II</MSGV2>" +"<MSGV3>msg variable III</MSGV3>
<MSGV4>msg variable IV</MSGV4></E1RSEIN></E1RSHIN></IDOC></RSINFO>";
IDocXMLProcessor processor=idocFactory.getIDocXMLProcessor();
// parse the XML-data into the IDoc document
IDocDocumentList docList = processor.parse(idocRepository, iDocXml);
// create the IDoc MappedRecord object to be sent
out.println("<p>sending IDoc... </p>");
MappedRecord idocMR = idocFactory.createMappedRecord(docList);
// get a connection
connection = connectionFactory.getConnection();
out.println("Got connection meta data: <br> "+connection.getMetaData());
// create an interaction
Interaction interaction = connection.createInteraction();
// create a new transaction ID (TID will be created automatically by the connector layer)
InteractionSpec interactionSpec =
((InteractionSpecFactory) connectionFactory).createInteractionSpec();
// send the IDoc document to the SAP system asynchronously
interaction.execute(interactionSpec, idocMR);
out.println("<p>...IDoc sent</p>");
// release the interaction
interaction.close();
}
catch ( java.lang.Exception ex )
{
out.println("<p>Application error: <br><code>" + ex + "</code></p>");
ex.printStackTrace(out);
}
finally
{
if (connection != null)
{
try
{
connection.close();
}
catch(Exception e)
{
out.println("<p>Error during close operation:
<br><code>" + e + "</code></p>");
}
}
out.println("</body>");
}
}
/**Clean up resources*/
public void destroy()
{
}
}
//For the sake of clarity, below the XML-String used:
<?xml version="1.0"?>
<RSINFO>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
<IDOCTYP>RSINFO</IDOCTYP>
<MESTYP>RSINFO</MESTYP>
<SNDPOR>SAPJCOIDOC</SNDPOR>
<SNDPRT>LS</SNDPRT>
<SNDPRN>JCOCLNT000</SNDPRN>
<RCVPRT>LS</RCVPRT>
<RCVPRN>TSTCLNT000</RCVPRN>
<CREDAT>20101022</CREDAT>
<CRETIM>144541</CRETIM>
</EDI_DC40>
<E1RSHIN SEGMENT="1">
<REQUEST>1</REQUEST>
<INFOIDOCNR>0815</INFOIDOCNR>
<SELDATE>20101020</SELDATE>
<SELTIME>174500</SELTIME>
<RQSTATE>1</RQSTATE>
<RQRECORD>10</RQRECORD>
<E1RSPIN SEGMENT="1">
<DATAPAKID>111</DATAPAKID>
<RQDRECORD>10</RQDRECORD>
</E1RSPIN>
<E1RSPIN SEGMENT="1">
<DATAPAKID>222</DATAPAKID>
<RQDRECORD>10</RQDRECORD>
</E1RSPIN>
<E1RSEIN SEGMENT="1">
<MSGID>1</MSGID>
<MSGTY>1</MSGTY>
<MSGNO>001</MSGNO>
<MSGV1>msg variable I</MSGV1>
<MSGV2>msg variable II</MSGV2>
<MSGV3>msg variable III</MSGV3>
<MSGV4>msg variable IV</MSGV4>
</E1RSEIN>
</E1RSHIN>
</IDOC>
</RSINFO>