package yourpackage;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Collection;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.Attachment;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.InputAttachments;
// import com.sap.aii.mapping.api.StreamTransformation;
// import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
public class RenameAttachments extends AbstractTransformation {
public static void main(String[] args) throws Exception {
FileInputStream inFile = new FileInputStream("C:\\Users\\luzholar\\Desktop\\Payload.xml");
FileOutputStream outFile = new FileOutputStream("C:\\Users\\luzholar\\Desktop\\Payload2.xml");
new RenameAttachments().local(inFile, outFile);
}
private void local(FileInputStream inFile, FileOutputStream outFile) throws Exception{
String xmlPayload= "";
xmlPayload="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
xmlPayload = xmlPayload.concat("<ns0:FileInfoGeneric xmlns:ns0=\"http://roche.com/rp/EM/CH/LAL/Common\">");
xmlPayload = xmlPayload.concat("<Receivers>");
xmlPayload = xmlPayload.concat("<Receiver/>");
xmlPayload = xmlPayload.concat("</Receivers>");
xmlPayload = xmlPayload.concat("<Name>");
xmlPayload = xmlPayload.concat("test");
xmlPayload = xmlPayload.concat("</Name>");
xmlPayload = xmlPayload.concat("</ns0:FileInfoGeneric>");
outFile.write(xmlPayload.getBytes("UTF-8"));
}
private static final DynamicConfigurationKey KEY_FILENAME = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {
try {
AuditLogHelper logger = AuditLogHelper.getInstance(in.getInputHeader().getMessageId());
logger.log("**** Java Mapping Starting ******", AuditLogStatus.SUCCESS);
OutputStream outputstream = out.getOutputPayload().getOutputStream();
String xmlPayload= "xml";
DynamicConfiguration conf = in.getDynamicConfiguration();
String fileName = "";
fileName = conf.get(KEY_FILENAME);
logger.log("Dynamic name of XML: "+ fileName, AuditLogStatus.SUCCESS);
String fileNamepdf = fileName.replace(".xml", "");
logger.log("Dynamic name of PDF Attachment(s): "+ fileNamepdf, AuditLogStatus.SUCCESS);
Object[] arrayObj = null;
String attachmentID = null;
InputAttachments inputAttachments = in.getInputAttachments();
if (inputAttachments.areAttachmentsAvailable()) {
// gets the attachmentIds and store it in an Object array
Collection<String> collectionIDs = inputAttachments.getAllContentIds(true);
arrayObj = collectionIDs.toArray();
// Object[] arrayObj = collectionIDs.toArray();
// Loops at the input attachments to get the content of the attachment and then
int counter = 1;
for (int i = 0; i < arrayObj.length; i++) {
attachmentID = (String) arrayObj[i];
Attachment attachment = inputAttachments.getAttachment(attachmentID);
byte[] attachmentBytes = attachment.getContent();
logger.log("Content-Type: "+ attachment.getContentType(), AuditLogStatus.SUCCESS);
Attachment newopAttachment = out.getOutputAttachments().create(fileNamepdf+"_" + counter +".pdf", "application/pdf; name="+fileNamepdf+"_" +counter +".pdf",attachmentBytes);
out.getOutputAttachments().setAttachment(newopAttachment);
out.getOutputAttachments().removeAttachment(attachmentID);
logger.log("New Attachment: "+ newopAttachment.getContentType(), AuditLogStatus.SUCCESS);
counter++;
}
logger.log("**** Java Mapping End ******", AuditLogStatus.SUCCESS);
}
outputstream.write(xmlPayload.getBytes("UTF-8"));
} catch (Exception e) {
throw new StreamTransformationException("Error: " + e.getMessage());
}
}
}
import com.sap.engine.interfaces.messaging.api.MessageDirection;
import com.sap.engine.interfaces.messaging.api.MessageKey;
import com.sap.engine.interfaces.messaging.api.PublicAPIAccessFactory;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditAccess;
import com.sap.engine.interfaces.messaging.api.auditlog.AuditLogStatus;
public class AuditLogHelper {
private static final String DASH = "-";
private static boolean isInitialize = false;
private static AuditAccess auditAccess;
private MessageKey messageKey;
private static void initialize() throws Exception {
auditAccess = PublicAPIAccessFactory.getPublicAPIAccess().getAuditAccess();
}
private AuditLogHelper(MessageKey messageKey) throws Exception {
this.messageKey = messageKey;
}
public static AuditLogHelper getInstance(String messageId) throws Exception {
if(!isInitialize)
initialize();
String msgUUID = messageId.substring(0, 😎 + DASH + messageId.substring(8, 12) + DASH
+ messageId.substring(12, 16) + DASH + messageId.substring(16, 18) + messageId.substring(18, 20) + DASH
+ messageId.substring(20, 32);
MessageKey messageKey = new MessageKey(msgUUID, MessageDirection.OUTBOUND);
return new AuditLogHelper(messageKey);
}
public void log(String message, AuditLogStatus status) {
auditAccess.addAuditLogEntry(messageKey, status, message);
}
public void setMessageKey(MessageKey messageKey) {
this.messageKey = messageKey;
}
}
MappingTrace trace;
trace = container.getTrace();
trace.addInfo("FileName: " + FName);
try
{
DynamicConfiguration conf=(DynamicConfiguration) container.getTransformationParameters().get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
// SAP standard
DynamicConfigurationKey key=DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File","FileName");
if (conf != null) {
conf.put(key,FName);
}
// SAP SFTP Standard
DynamicConfigurationKey sapSFTPkey=DynamicConfigurationKey.create("http://sap.com/xi/XI/System","FileName");
conf.put(sapSFTPkey,FName);
// SFTP Seeburger
DynamicConfigurationKey sftpkey=DynamicConfigurationKey.create("http://seeburger.com/xi/common","dtSubject");
conf.put(sftpkey,FName);
DynamicConfigurationKey sftpAN=DynamicConfigurationKey.create("http://seeburger.com/xi/common","dtAttachmentName");
conf.put(sftpAN,FName);
// AS2 Seeburger
DynamicConfigurationKey as2Key=DynamicConfigurationKey.create("http://seeburger.com/xi/AS2","dtAS2FileName");
conf.put(as2Key,FName);
}
catch (Exception e)
{
return ("ERROR");
}
return (FName);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
13 | |
10 | |
7 | |
7 | |
6 | |
5 | |
5 | |
5 | |
4 | |
4 |