Created
May 26, 2015 12:52
-
-
Save anonymous/9b95fd71405e8a1fcd51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.scn.pi.nfe; | |
import java.io.ByteArrayOutputStream; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.util.Collection; | |
import java.util.Iterator; | |
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.OutputAttachments; | |
import com.sap.aii.mapping.api.StreamTransformationException; | |
import com.sap.aii.mapping.api.TransformationInput; | |
import com.sap.aii.mapping.api.TransformationOutput; | |
/** | |
* This class is an PI Java Mapping which switch the main payload with | |
* the Mail Package attachment | |
* @author Jose Nunes | |
*/ | |
public class AttachmentReader extends AbstractTransformation { | |
InputAttachments inputAttachments = null; | |
OutputAttachments outputAttachments = null; | |
DynamicConfiguration conf = null; | |
/** | |
* This method is for testing purposes only. | |
*/ | |
public static void main(String[] args) throws Exception { | |
InputStream in = new FileInputStream("C:/Temp/nfe_b2b_in.xml"); | |
OutputStream out = new FileOutputStream("C:/Temp/nfe_b2b _out.xml"); | |
new AttachmentReader().execute(in, out); | |
in.close(); | |
out.flush(); | |
out.close(); | |
} | |
/** | |
* @param tf_in The incoming message | |
* @param tf_out The transformed message | |
* @return void | |
* @throws StreamTransformationException | |
*/ | |
public void transform(TransformationInput tf_in, TransformationOutput tf_out) | |
throws StreamTransformationException { | |
inputAttachments = tf_in.getInputAttachments(); | |
outputAttachments = tf_out.getOutputAttachments(); | |
conf = tf_in.getDynamicConfiguration(); | |
this.execute(tf_in.getInputPayload().getInputStream(), tf_out | |
.getOutputPayload().getOutputStream()); | |
} | |
/** | |
* @param in The incoming message | |
* @param out The transformed message | |
* @return void | |
* @throws StreamTransformationException | |
*/ | |
public void execute(InputStream in, OutputStream out) | |
throws StreamTransformationException { | |
Attachment attachment; | |
String aId; | |
if (inputAttachments.areAttachmentsAvailable()) { | |
Collection<String> attachments = inputAttachments.getAllContentIds(true); | |
Iterator<String> it = attachments.iterator(); | |
// Only one attachment is generated on standard proxy | |
while (it.hasNext()) { | |
aId = it.next(); | |
attachment = inputAttachments.getAttachment(aId); | |
//Remove the attachment, leaving only the main payload | |
outputAttachments.removeAttachment(aid); | |
} | |
} | |
// Move the Mail Package to the Main Payload | |
try { | |
out.write(attachment.getContent()); | |
} catch (Exception e) { | |
throw new StreamTransformationException(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment