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”
xmlns=”http://www.unoswar.com/wordpress/email-attachment-test”
targetNamespace=”http://www.unoswar.com/wordpress/email-attachment-test”
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
No comments:
Post a Comment