Friday, August 7, 2015

Write Email Attachment to a File

WriteIncomingAttachmentToFile

To be able to do this, you must be able to first retrieve the attachment from the Receive variable as described in the previous post. Also this post assumes that you have some basic knowledge of BPEL, Adapters and XSLT functions. Just for understanding, by default, Attachment is stored in the ATTACHMENT table in SOA_INFRA user schema and the table's row reference (called href) is received in the Receive variable. When ora:getAttachmentContent() function is called, it fetches the attachment content from the table in base64 encoded format and assigns to the target custom variable.

There are 2 ways you can write incoming attachment to an external file.

ora:writeBinaryToFile()

There's inbuilt function ora:writeBinaryToFile() that automatically extracts the attachment from the Attachment href (in the binary format) and writes to the specified target file (hence you don’t need to use ora:getAttachmentContent() in front of it). Follow the below steps to write it to file using ora:writeBinaryToFile() .

  • If you're starting from the beginning, then create a new Simple BPEL project with Define service later option
  • Drag and Drop the UMS Adapter and configure the adapter. It will automatically create the schema for UMS Adapter and WSDL file.
  • Go to BPEL process and drag and drop Receive Activity on the swim lane. Connect the activity to UMS Adapter and create a Receive variable ( Receive1_ReceiveNotification_InputVariable )
  • Create another variable of the type attachment and name $CustomAttachmentVariable
  • Next drag and drop the Assign Activity and drop Expression on the $CustomAttachmentVariable on the Target side
  • Assign the following in the Source side. In BPEL source , comple Assign statement would look like as shown below. -
  • Receive1_ReceiveNotification_InputVariable','body','/ns2:message/ns2:attachment[1] - is the BPEL 1.1. notation of the variable as mentioned earlier. Even in BPEL 2.0 you must use the same format.
  • /u01/app/oracle/userdir/NewAttachment.xml - specifies the full path of the file where attachment content needs to be written
  • $CustomAttachmentVariable/ns2:attachment - is just meant to complete the Assign Statement. It doesn’t have any significance in writing the file. Run the project, it should work.

Using the File/FTP Adapter

The other way is to use a File Adapter through a opaque schema variable. Follow the below steps to write a file with the content retrieved from attachment. Note - You don’t have any control over the content of the file, so whatever comes in the attachment will be written directly. If you want to parse the content of the file before write read my another post - Parse and Read contents of Email Attachment in the BPEL process.

  • Drag and Drop a File adapter to the above project (created with UMS Adapter) and choose option Native Format Translation is not Required (Schema is Opaque).
  • Go to BPEL, drag and drop an Invoke activity, connect to File Adapter just created and create an Invoke Variable (Invoke1_Write_InputVariable.opaque )
  • Assign the Attachment content to the variable just created as shown below. Note - we are using ora:getAttachmentContent() to first fetch the content in opaque base64 format which is then assigned to the opaque variable of the File Adapter.
  • Run the project, it will write the attachment content in a file with the name specified in the File Adapter. You can also retrieve the name of the attachment and write with the same name if required. Read post - How to retrieve name of the Email Attachment .

Let me know if any issues and I'll try to help!

5 comments:

  1. Hi Neeraj,
    I had query regarding saving attachments to oracle content manager(UCM). Any idea how can this be achieved ?
    My understanding is : Retrieve the attachment and convert it into byte stream and then upload in UCM . Can you please upload if you have any sample code for the same.

    ReplyDelete
  2. Hi Neeraj, my requirement to pass the incoming attachment content to the Java web service via attachment. For that iam trying to convert the incoming href to base64 content as below in assign activity:
    ora:getAttachmentContent('ReceiveGetInputVariable','attach','/ns3:attachmentElement/@href'); and also tried this......
    ora:getAttachmentContent('ReceiveGetInputVariable','attach','/ns3:attachmentElement[1]')

    But getting below errors for both of the above:


    An error occurs while processing the XPath expression;

    Could you please let me know,where exactly my syntax is going wrong;

    Thanks in advance.

    ReplyDelete
  3. Hi Can we read Email attachment Name?

    ReplyDelete
  4. assigns to the target custom variable. - What is data type? I get errors from String data type

    ReplyDelete
  5. QUANTUM BINARY SIGNALS

    Professional trading signals delivered to your cell phone daily.

    Follow our signals today & gain up to 270% per day.

    ReplyDelete