Friday, 20 December 2013

Configuring EmailNotification in Oracle Soa 11g


Hi Guys

Oracle SOA Suite 11g supports a number of different notification channels through the User Messaging Service (UMS). This blog provides a step-by-step guide for configuring and testing email notification. Once email notification has been configured, emails can be sent from SOA Suite components such as BPEL processes and Human Tasks. For this example, Gmail will be used as the mail server.


Before anything, I'll just introduce you to the protocols used in EMAIL communication
Sending a Mail
SMTP : protocol to send mail to a mail server

Retrieving a Mail
POP3 : Downloads the mail from the mail server in to a local machine. This will be helpful if you have only one machine in which you'll always check your emails.
IMAP : Doesn't actually download the email to the local server(you can always do it if required). It helps you sync up your mail across machines, and gives the capability of having hierarchical maintenance of your emails ie, you can maintain a folder structure for specific mails. You create a folder in one machine, drag some msg's into it, that will be automatically synced up when you chk your mails from another machine as the changes are not done in your local machine, but the server.  Also, when your INBOX is too huge, it will just retrieve headers to your mail client, only when you click on it, it will actually download the message to show you the details. This makes the performance better than POP3.

For using EMAIL notification service, you may not have your own email servers. You can always use a freely available mail like gmail.

This example shows you how to use gmail as the mail server for Email Notifications.

Configuring Email notification service involves 4 steps(first step can be ignored if your mail server doesn't need SSL)


Configuring Email Notification

To support email notification from SOA Suite 11g the UMS email driver and workflow notification properties to be configured with the mail server details. This can be done through the Enterprise Manager Fusion Middleware Control console (e.g. http://localhost:7001/em) as follows:
  1. Navigate to the Email Driver Properties page available under User Messaging Service > usermessagingdriver-email


  2. Update the following properties according to your mail server configuration:
    OutgoingMailServer - The SMTP hostname
    OutgoingMailServerPort - The SMTP port
    OutgoingMailServerSecurity - The type of security (e.g. SSL)
    OutgoingUsername - The mail user account
    OutgoingPassword - The mail user password


    1. Click Apply to save the changes.
       
    2. Right-click on soa-infra under SOA and navigate to the Workflow Notification Properties page available under SOA Administration.


       
    3. Set the Notification Mode to 'Email' and enter the default values which you wish to use for from, actionable and reply to email addresses.

      Configure the below mentioned properties
    Property
    Value
    Comments
    -----------------------------
    FOR SENDING MAIL
    -------------------------------------
    OutgoingMailServer
    smtp.gmail.com
    As mentioned, SMTP is used to send mails
    OutgoingMailServerPort
    465
    This is the port gmail uses for SMTP
    OutgoingMailServerSecurity
    SSL

    OutgoingDefaultFromAddr
    <mydefaultmailId@gmail.com>
    The default FROM address (if one is not provided in the outgoing message).
    OutgoingUsername
    <mymailId@gmail.com>

    OutgoingPassword
    Password of the gmail id
    Option to be selected is "Use Cleartext Password"
    ---------similarly for----
    FOR INCOMING MAIL
    -------------------------------------
    IncomingMailServer
    imap.gmail.com
    As mentioned, IMAP is used to receive mails
    IncomingMailServerPort
    993
    This is the port gmail uses for IMAP
    MailAccessProtocol
    IMAP

    IncomingMailServerSSL
    No need to check this option

    IncomingMailIDs


    IncomingUserIDs


    IncomingUserPasswords


       Email: From Address  = taskdeliveryflow@gmail.com
       Email: Actionable Address       =  taskdeliveryflow@gmail.com
       Email: Reply To Address =Notaskflow@gmail.com
    1. Click Apply to save the changes to the workflow notification properties.
       
    2. Restart the SOA server for the changes to take effect. If you are using Gmail or any other mail server that requires SSL you will need to perform the steps in the next section. In this case, you may wish to hold off on restarting the server before these steps are performed. This will save you the trouble of having to restart twice.

    Import the SSL Certificates to the Java Keystore

    Before SOA Suite can send emails with Gmail there are some SSL certificates that will need to be imported. This step can be ignored if sending from an alternative mail server that does not require SSL.
  3. Download the Gmail IMAP and SMTP certificates. This can be done with OpenSSL (Unix / Windows) by running the following from command prompt:
    • openssl s_client -connect host:port
      • where Gmail SMTP host:port is smtp.gmail.com:465
      • where Gmail IMAP host:port is imap.gmail.com:993
         
  4. Create a new file for the SMTP and IMAP certificates using the content between BEGIN CERTIFICATE & END CERTIFICATE



  1. Use the Java Keytool to import the two certificates into a new or existing keystore. The keytool executable is available under the JAVA_HOME/bin directory. The following command can be used to import a certificate:
    • keytool -import -alias AliasName -keystore KeystoreLocation -file CertificateLocation
      • For example:
        • keytool -import -alias gmail-imap -keystore gmail-keystore.jks -file imap.txt
        • keytool -import -alias gmail-smtp -keystore gmail-keystore.jks -file smtp.txt
    • When prompted, enter a password for the keystore.
       
  2. Add the following system properties to the WebLogic server startup arguments:
    -Djavax.net.ssl.trustStore=CertificateLocation-Djavax.net.ssl.trustStorePassword=CertificatePassword
    This can be done in Arguments field available under the Server Start tab for the SOA Server (e.g. soa_server1) in the WebLogic Administration Console (e.g. http://localhost:7001/console) or by adding the properties to the DOMAIN_HOME/bin/setDomainEnv.cmd (or setDomainEnv.sh). For example:
    set EXTRA_JAVA_PROPERTIES=%EXTRA_JAVA_PROPERTIES% -Djavax.net.ssl.trustStore=%WL_HOME%\server\lib\gmail-keystore.jks -Djavax.net.ssl.trustStorePassword=changeit
  3. Once you are done with this edit, one step is pending, where you will tell the managed server that a custom keystore is setup and has to be considered.
    This is done by opening the Admin Console(<adminHost>:<adminPort>/console --> Environments --> Servers --> click on soa_server1)
    Click on Keystores, and change the Keystores to "Custom Identity and Java Standard Trust"

    Start the SOA server (or restart it if it is already running).

Testing Email Notification

Now that we have configured outbound email notification, we want to test our configuration easily without having to create a BPEL process or human task to do this. Fortuntately, we can do this from Enterprise Manager Fusion Middleware Control.
  1. Right-click on soa-infra under SOA and navigate to the Human Workflow page available under Service Engines.


     
  2. Select the Notification Management tab.
     
  3. Click Send Test Notification


     
  4. The Send Test Notification dialog box should now appear. Enter the email subject, content, to address and be sure to select 'Email' for the channel. Click SendOnce the email has been sent, you should receive a value of 'SENT' for ResponseYou can now close the dialog box.




          Note: Ensure that the email has been received.



    HappyLearning


                                                                                                                       By DeepthiReddy

Sending Emails in Orcle Soa 11g with Attachments

Hi,


First, you will have to have the basic set up for sending  e-mails in Oracle SOA Suite 11g. The following blog will get you started :-



Once you have your environment set up for emails, we can get started. Open your JDeveloper if it is not already open. I am using JDeveloper 11.1.1.6.
Create a new SOA application and a new SOA project. Click on File->New in JDeveloper :-

In the “New Gallery”, select the “Applications” node on the left pane. On the right pane, select the option “SOA Application” and hit OK.
In the “Create SOA Application” window that pops up, enter an application name of your choice.  Hit next one you are done.
In the next screen, enter a name for the SOA project and hit Finish.
Next, we are going to create an XSD which is going to define the types for our SOA component. Right click on the “xsd” node under your project in the project navigator pane and select the “New” option:-
In the “New Gallery”, select the “XML” node on the left pane under the “General” node and on the right side, select the option “XML Schema” . Hit OK once you are done.
In the “Create XML Schema” window, enter a name for the schema and hit OK.
The schema definition file will be created. Go to the source of the schema by clicking on the “source” tab as indicated in the image below :-
Next, replace the contents of the schema file with the following code.
<?xml version=”1.0″ encoding=”windows-1252″ ?>
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema;
xmlns:tns=”http://www.localhost.com/email-attachment;
targetNamespace=”http://www.localhost.com/email-attachment;
elementFormDefault=”qualified”>
<xsd:element name=”EMail” type=”tns:EMailType”/>
<xsd:complexType name=”EMailType”>
<xsd:sequence>
<xsd:sequence>
<xsd:element name=”EMailBody” type=”xsd:string”/>
<xsd:element name=”EMailSubject” type=”xsd:string”/>
<xsd:element name=”EMailAddress” type=”xsd:string”/>
<xsd:element name=”Attachment” type=”xsd:string”/>
<xsd:element name=”AttachmentName” type=”xsd:string”/>
<xsd:element name=”AttachmentType” type=”xsd:string”/>
</xsd:sequence>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Next, open your composite if it is not already open. Drag and drop a BPEL process from the component palette onto the composite.xml :-
The “Create BPEL Process” window will open up. Enter a suitable name for the BPEL process and the service. Select the “Based on a WSDL” option from the “Template” drop down. Keep the option “Expose as a SOAP service” checked. Next, click on the “Generate WSDL from schema” button as indicated in the image below :-
In the “Create WSDL” screen, enter a suitable port type name and operation name. Select “One-Way Interface” as the interface type. Keep the option “Generate partnerlinkType extension” option checked. Next, click on the green plus button as indicated in the image below to add the input parameter :-
In the add message part that pops up, enter a suitable part name. Next click on the magnifying glass sign as indicated in the image below :-
In the “Type Chooser” window that pops up, expand the “EMailNotificationUtility.xsd” node under the “Project Schema Files” node and select the “EMails” node. Hit OK once you are done.
Back in the “Add Message Part” window, verify the settings. Hit OK once you are done.
Verify the settings once in the “Create WSDL” window.Hit OK once you are satisfied.
The composite should look like the following image :-
Next, double click on the BPEL process to open it. Expand the “Oracle Extensions” node in the component palette. Now, drag and drop an “EMail” activity onto the BPEL process after the receive activity.
The BPEL process should look like the following image now :-
Next double click on the email activity to configure it. Click on the expression button beside the “To” field as indicated in the image below :-
In the expression builder window, in the BPEL variables pane, expand the “input” node under the “Process”->”Input Variable” node and select “EMailAddress” element. Next, click on the “Insert into Expression” button. Hit OK once you are done.
Next, click on the expression builder button beside the “Subject ” filed.
Once the expression builder is open,  in the BPEL variables pane, expand the “input” node under the “Process”->”Input Variable” node and select “EMailAddress” element. Next, click on the “Insert into Expression” button. Hit OK once you are done.
Next, click on the expression builder button beside the “Body” field.
Once the expression builder is open,  in the BPEL variables pane, expand the “input” node under the “Process”->”Input Variable” node and select “EMailBody” element. Next, click on the “Insert into Expression” button. Hit OK once you are done.
Next, click on the “Attachments” tab as indicated in the image below :-
In the attachments tab, click on the green plus icon to add a new attachment.
One attachment will be added. Modify the attachment name. You can hard code the attachment name or use an expression. To use an expression, click on the name field. Once you click on the attachment name, a button will appear beside the name. Click on that to bring up the expression builder.
Once the expression builder is open,  in the BPEL variables pane, expand the “input” node under the “Process”->”Input Variable” node and select “AttachmentName” element. Next, click on the “Insert into Expression” button. Hit OK once you are done.

[Go to source and make sure the expression looks something like below. JDeveloper sometimes acts a bit weiredly
<copy>
<from expression="bpws:getVariableData('inputVariable','input','/ns1:EMail/ns1:AttachmentName')"/>
<to variable="varNotificationReq" part="EmailPayload"
query="/EmailPayload/ns2:Content/ns2:ContentBody/ns2:MultiPart/ns2:BodyPart[2]/ns2:BodyPartName”/>
</copy>
]
Next, click on the “Mime Type” field the default value for which is “text/html;”. This value is variable according to the type of the attachment. The following are the values that you can use :-
  • file-name.doc –  application/msword
  • file-name.pdf –  application/pdf
  • file-name.jpg –  image/jpeg
  • file-name.gif –  image/gif
  • file-name.txt –text/plain
  • file-name.html – text/html
Once you click on the “Mime Type” field, a button is going to appear. Click on that to bring up the expression builder :-
Once the expression builder is open,  in the BPEL variables pane, expand the “input” node under the “Process”->”Input Variable” node and select “AttachmentType” element. Next, click on the “Insert into Expression” button. Hit OK once you are done.
[Sometimes the expression does not delete the prior value "text/html;" ; please go to the source for the email activity and make sure the expression reads like the following :-
<copy>
<from expression="bpws:getVariableData('inputVariable','input','/ns1:EMail/ns1:AttachmentType')"/>
<to variable="varNotificationReq" part="EmailPayload"
query="/EmailPayload/ns2:Content/ns2:ContentBody/ns2:MultiPart/ns2:BodyPart[2]/ns2:MimeType”/>
</copy>
]
Next, click on the “Value” field. Again a button will appear. Click on that to bring up the expression builder.
Once the expression builder is open,  in the BPEL variables pane, expand the “input” node under the “Process”->”Input Variable” node and select “Attachment” element. Next, click on the “Insert into Expression” button. Hit OK once you are done.
Verify the setup of the email attachment tab. Hit OK once you are done.
Our component is ready to be used.
Now, we are going to test this component. We are going to read a file (JPEG/PDF/MS-WORD/MS_EXCEL), encode the contents into  a base64 string and feed that as an input to the email component.
In the project navigator pane, expand the current project and right click on the “xsd” node. Select the “New” option from the context menu.
In the “New” gallery, select the “XML” node in the left pane. On the right pane, select the option “XML Schema”.Hit OK once you are done.
Enter a suitable name for the schema. Hit OK once you are done.
The schema file will be created. Go to the source of the schema file by clicking on the source tab at the bottom.
Replace the contents of the XSD file with the following code :-
<?xml version=”1.0″ encoding=”windows-1252″ ?>
<xsd:schema xmlns:xsd=”http://www.w3.org/2001/XMLSchema&#8221;
xmlns=”http://www.unoswar.com/wordpress/email-attachment-test&#8221;
targetNamespace=”http://www.unoswar.com/wordpress/email-attachment-test&#8221;
elementFormDefault=”qualified”>
<xsd:element name=”EmailNotificationTestInput”>
<xsd:complexType>
<xsd:sequence>
<xsd:element name=”EmaildAddress” type=”xsd:string”/>
<xsd:element name=”FullyQualifiedFilePath” type=”xsd:string”/>
<xsd:element name=”MimeType” type=”xsd:string”/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Next, drag and drop another BPEL process onto the composite.
The “Create BPEL Process” window will come up. Enter a suitable name and service name for the BPEL process. Select the “Based on a WSDL” option from the “Template” drop down. Leave the other defaults. Next, click on the “Generate WSDL from schema(s)” button beside the WSDL URL text box.
In the “Create WSDL” window that pops up, enter a Port Type name and an operation name. Select the “One-Way Interface” option from the “Interface Type” drop down. Next, click on the green plus icon to add an input.
In the add message part window, enter a suitable part name. Next, click on the magnifying glass icon just beside the URL text box.
In the type choose, select the “EMailNotificationTestInput” element from the newly created “EmailNotificationTest.xsd” file. Hit OK once you are done.
Verify the settings on the “Add Message Part” window. Hit OK once you are satisfied.
Verify the settings on the “Create WSDL” window. Hit OK once you are done.
Verify the settings on the “Create BPEL process” window. Hit OK once you are done.
The composite should look like the following image :-
Double click on the “EMailAttachmentTestService” BPEL process to open it. First, click on the create variable button as indicated in the image below :-
The “Variables” window will come up. Click on the green plus button.
In the create variable window, create a variable of type string named “attachmentData”. Enter the name “attachmentData” in the name field. Next, click on the magnifying glass beside the “Type” text box.
In the type chooser window,expand the node “XML Schema Simple Types” and select the node “string”. Hit OK once you are done.
Verify the settings on the “Create Variable” window. Hit OK once  you are done.
You will be able to see the newly created variable.
Create two string type variables in a similar way named “fileLocation” and “fileName”.
Verify the settings on the “Variables” window and hit OK.
Now, drag and drop an assign activity from the component palette onto the BPEL process just after the receive activity. The BPEL process should look like the following image :-
Double click on the assign activity and create a new copy operation as shown in the image below. Hit OK once you are done.
Next, expand the “Oracle Extensions” node on the component palette and drag and drop a java embedding activity onto the BPEL process just after the assign activity.
The BPEL process should look like the following image.
Next, double click on the java embedding activity and copy and paste the following code :-
try{
String file=(String)getVariableData(“fileLocation”);
String fileName=file.substring(file.lastIndexOf(“\\”)+1);
addAuditTrailEntry(fileName);
java.io.InputStream is=new java.io.FileInputStream(file);
byte[] inputStreamBytes=org.apache.commons.io.IOUtils.toByteArray(is);
String base64edString = oracle.soa.common.util.Base64Encoder.encode(inputStreamBytes);
setVariableData(“attachmentData”,base64edString);
setVariableData(“fileName”,fileName);
}catch(Exception e){
addAuditTrailEntry(e);
}
Here, we are basically fetching the file’s location, retrieving the contents and encoding the bytes of the file into a Base64 encoded string. In order to compile the code, we are going to need a couple of jars.
The Apache commons io jar (version 1.7) using which we extract the bytes from the input stream :-
byte[] inputStreamBytes=org.apache.commons.io.IOUtils.toByteArray(is);
And the Apache commons codec jar (version 2.4) using which we encode the bytes into a base64 string :-
String base64edString = oracle.soa.common.util.Base64Encoder.Base64Encoder.encode(inputStreamString);
You can download the binary for Apache commons codec from http://apache.techartifact.com/mirror//commons/codec/binaries/commons-codec-1.7-bin.zip
Once you download, extract the jar file “commons-codec-1.7.jar” present inside the zip into a directory of your choice. I copied the jars inside “E:\jars”.
You can download the jar file for Apache IO from
Next, go back to JDeveloper and right click on your project. Click on the “Project Properties” option from the context menu.
In the “Project Properties” window that pops up, select the “Libraries and Classpath” node from the left pane. Next, click on the “Add JAR/Directory” button :-
In the “Add Archive or Directory” window, select the two jars :- commons-codec-1.7-bin.jar and  commons-io-2.4.jar.  Hit Select once you are done :-
Verify the set up in the “Project Properties” window. Hit OK once you are done.
Back in the BPEL process, drag and drop an assign activity just after the java embedding activity onto the “EMailAttachmentTesterService” BPEL process.
Next, drag and drop an invoke activity after the newly create assign activity.
The BPEL process should look like the following image :-
Back in the composite.xml, create a wire from the “EMailAttachmentTesterService” BPEL to the “EMailUtilityService” BPEL.
The composite should look like the following image :-
Back in the “EMailAttachmentTesterService” BPEL process, double click on the invoke activity to open it’s configuration. Enter a suitable name. Next, click on the magnifying glass beside the Partner Link box as indicated in the image below :-
In the “Type Chooser” window that pops up, select the “EMailUtilityService.EMailUtilityService” partner link.  Hit OK once you are done.
Back in the previous window, click on the green plus icon as indicated in the image below to create the input variable for the invoke activity.
Accept the defaults in the window that pops up. Hit OK.
Verify the condition once for the invoke activity. Click OK once you are done.
The BPEL process should look like the following image:-
Next double click on the second “Assign” activity. On the right hand side, expand the variable “InvokeEMailUtilityService_sendEMail_InputVariable”. Next, drag the expression builder button onto the “EMailBody” node on the right pane as depicted in the image below :-
Enter the following with quotes in the expression builder window which pops out. Hit OK once you are done.
‘Test Attachment’
One copy operation will be created. Next, drag the expression builder button onto the “EMailSubject” node on the right pane as depicted in the image below :-
Enter the following expression with quotes in the expression builder.Hit OK once you are done.
‘Test Attachment’
Next, create a copy operation from the input variable’s “EMailAdress” element to the InvokeEMailUtilityService_sendEMail_InputVariable variable’s  ”EMailAddress” element.
Next, create another copy operation from the “fileName” variable to the InvokeEMailUtilityService_sendEMail_InputVariable variable’s  ”AttachmentName” element.
Next, create yet another copy operation from inputVariable’s  ”MimeType” element to the InvokeEMailUtilityService_sendEMail_InputVariable variable’s  ”AttachmentType” element.
Finally, create the copy operation from the variable “attachmentData” to  the InvokeEMailUtilityService_sendEMail_InputVariable variable’s  ”Attachment” element. Verify the settings once and hit OK once you are done.
We are done. Next, copy the jars  commons-codec-1.7.jar and commons-io-2.4.jar inside the <Appplication Root>/<SOA Project Root>/SCA-INF/lib directory which, in my case is “C:\JDeveloper\mywork\EMailAttachmentDemo\EmailAttachmentDemo\SCA-INF\lib” .
Deploy the project to your server. Next, we are going to test the application. I am using Apache James 2.3.2 as my email server and WindowsLiveMail  as my mail client.
Once the application is deployed, go to the Enterprise Manager and click on the project. Next, click on the Test drop down button and select the “EMailAttachmentTesterService_ep” option.
In the test page, enter the email address. Next, enter the fully qualified path name. Next, enter the MimeType as I mentioned above depending upon the file type you are sending. Make sure you enter a “;” after the mimetype e.g. application/pdf;. Hit “Test Web Service” button on the top right. Next, check your mail box. A new email should be present in your email should be waiting in your inbox with a nice attachment.
I have tested this component for MS-WORD, MS-EXCEL, JPEG, PDFs and PNG files. I was surprised to see that it is working for PNG files as well if you use mimetype as “application/png;” although it is not listed in Oracle’s documentation.



HappyLearning


                                                                                                              By DeepthiReddy