Sunday

Google Analytics api released

Finally googles long waited analytics api released on last week. Api will help you to write client for gathering and analyze data for your web site. Now through client we could analyze data from our standalone java client and no longer need to logon to our Google Analytics site. Api released with three flavour:
1) Java Script;
2) Java;
3) Http

Along with client libraries there are also reference document and Sample codes to start coding and test api. Also there are brief explanation of account, profile, metrics and dimensions related to google analytics. It will take a couple of minutes to write down your code or just run sample code distributed by google to get the action. The best part: this sophisticated, full-featured web analytics package is free.

For getting started see here.

Tuesday

Upgrade osb project with new advance feature of MTOM support on version 10gR3

Last week we decided to upgrade all our OSB project from OSB3 to OSB10GR3. A few of our project worked with attachment's which failed on new version of OSB. In this post we will explain briefly all the necessary step to upgrade project working with attachments to version 10g release 3.

Assume that, we have a proxy service which will get arbitrary MTOM attachment. First of all, after exporting the project on workspace we must enable mtom support on proxy properties page. When XOP/MTOM Support is enabled, you can further select how to handle binary data in the $header and $body message context variables from among the following options:

* Include Binary Data by Reference: (Default) In an inbound request message, replace xop:Include elements with ctx:binary-content elements when setting up the $header and $body message context variables.
* Include Binary Data by Value: In an inbound request message, replace xop:Include elements with Base64-encoded text versions of corresponding binary data when setting up the $header and $body message context variables.

You can use Include Binary Data by Reference when you need direct access to binary data, for example to pass data to a Java callout or Message Format Language (MFL) transform.

You can use Include Binary Data by Value in the following cases:

* To bridge between MTOM and non-MTOM services. For example, consider an MTOM-enabled proxy service that receives a request that is then routed to a non-MTOM-enabled service. You could use this option to comply with existing standards for sending binary data in XML in Base64-encoded form.
* To validate the contents of the message against an XML schema that requires a base64binary element to be used in place of binary data

Note that if XOP/MTOM Support is enabled for a proxy service, it is not required that every inbound message be in the MTOM format. Instead, this setting specifies that when an MTOM-formatted message arrives, the proxy service should handle it accordingly. Note also that when proxy services not enabled for XOP/MTOM Support receive an MTOM-formatted message, the service rejects the message and issues a runtime error.
When XOP/MTOM support enable on proxy service, the variable $attachment will no longer active. For example on version OSB3 we could define total attachment by following xpath statement as follows:
fn:count($attachment/attachments)
Above frugment of xpath will return 0, because on version 10gR3, MTOM attachment will no longer available in variable $attachment.
When the Binary by Reference option is selected, Oracle Service Bus parses the root of the message checking for the presence of xop:Include tags. These tags, when found, are converted to ctx:binary-content elements with a reference pointing to the corresponding source in binary repository. The resulting document is represented by the $body message context variable.
This means that when pipeline actions access the contents of the $body message context variable, the actions do not encounter xop:Include elements, but instead work with ctx:binary-content elements.
As example:
Guess, from the client side we have sent following soap message with attachment
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:SubmitAttachmentRequestEle xmlns:ns2="http://www.ru.fors.nsi/SOAPwithAttachment/">
<messageId>messageId</messageId>
<senderAppName>fts NSI</senderAppName>
<receiver>rtu-1</receiver>
<department>department</department>
<attaches>
<Attach>
<documentName>test.doc</documentName>
<zipFile>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:83842c6c-2dbc-459d-98b1-f185f89ce9ec@example.jaxws.sun.com"/>
</zipFile>
<comment>comment</comment>
</Attach>
<Attach>
<documentName>README.txt</documentName>
<zipFile>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:ea1779f1-4477-46b5-8364-9a525f49c734@example.jaxws.sun.com"/>
</zipFile>
<comment>comment2</comment>
</Attach>
</attaches>
</ns2:SubmitAttachmentRequestEle>
</S:Body>
</S:Envelope>

In Osb $body variable you will get following transformed of code:
<attaches xmlns:ns2="http://www.ru.fors.nsi/SOAPwithAttachment/" xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<Attach>
<documentName>test.doc</documentName>
<zipFile>
<con:binary-content ref="cid:1c6b1828:120c78cd5fd:-7fd4" xmlns:con="http://www.bea.com/wli/sb/context"/>
</zipFile>
<comment>comment1</comment>
</Attach>
<Attach>
<documentName>README.txt</documentName>
<zipFile>
<con:binary-content ref="cid:1c6b1828:120c78cd5fd:-7fd3" xmlns:con="http://www.bea.com/wli/sb/context"/>
</zipFile>
<comment>comment2</comment>
</Attach>
</attaches> 

con:binary-content element will hold the reference of the binary data on hash table. We can easily get the content of the binary data and send it to save on file or drive it to ftp or email attachment. For more information see my previous post.
See for detail information.

Wednesday

Send e-mail with attachment through OSB

Oracle Service Bus (OSB) contains a good collection of adapter to integrate with any legacy application, including ftp, email, MQ, tuxedo. However e-mail still recognize as a stable protocol to integrate with any application asynchronously. Send e-mail with attachment is a common task of any business process. Inbound e-mail adapter which, integrated with OSB support attachment but outbound adapter doesn't. This post is all about sending attachment though JavaCallout action.
There are two ways to handle attachment in OSB:
1) Use JavaCallout action to pass the binary data for further manipulation. It means write down a small java library which will get the attachment and send the e-mail.
2) Use integrated outbound e-mail adapter to send attachment, here you have to add a custom variable named attachment and assign the binary data to the body of the attachment variable.
First option is very common and easy to implement through javax.mail api, however a much more developer manage to send the binary attachment by second option but i couldn't figure out it yet.
Here, we are going to describe all the necessary step to implement the first option describe above.
First of all we will develop a small java library to send the mail. Java class will be contain one static method with all the necessary parameter to send mail, including smtp server address and the receptions address, moreover it will have one byte array parameter (byte[]) to get the binary data. The java class is as follows:
package com.blu.nsi.transport;

import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.DataHandler;
import java.io.*;

import javax.mail.Multipart;

public class MailClient {

/**
* public method that will invoked by business service to send mail
* */
public static void  sendMail(String smtpServer,
String from,
String to,
String subject,
String body,
String fileName,
byte[] zipFile) throws Exception
{
java.util.Properties props = System.getProperties();

props.setProperty("mail.smtp.host", smtpServer);
Session session = Session.getInstance(props);
Message message = new MimeMessage(session);
message.setFrom( new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(subject);
message.setSentDate(new java.util.Date());
message.setHeader("X-Mailer", "MailClient");

//Set content to the mime body
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(body);
MimeBodyPart messageAttach = new MimeBodyPart();
String fName = fileName.substring(fileName.lastIndexOf("\\")+1);

MailDataSource dataSource = new MailClient.MailDataSource();
dataSource.setContentType("application/x-zip-compressed");
dataSource.setData(zipFile);
dataSource.setName(fName);

messageAttach.setDataHandler(new DataHandler(dataSource));
messageAttach.setFileName(fName);
messageAttach.addHeader("charset", "windows-1251");

// add multipart
Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(messagePart);
multiPart.addBodyPart(messageAttach);
// add to message
message.setContent(multiPart);

Transport.send(message);

}
static class MailDataSource implements javax.activation.DataSource{
private byte[] data;
private String contentType;
private String name;


public void setContentType(String contentType) {
this.contentType = contentType;
}

public void setData(byte[] data) {
this.data = data;
}

public void setName(String name) {
this.name = name;
}

public String getContentType() {

return contentType;
}

public InputStream getInputStream() throws IOException {

return new java.io.ByteArrayInputStream(data);
}

public String getName() {

return name;
}

public OutputStream getOutputStream() throws IOException {

return null;
}

}
}

Now we are ready to develop project in OSB to send the attachment. First we will create a wsdl to send the soap message with attachment to proxy service. The wsdl file contain only one port and binding will be the style with rpc/literal. Only rpc style could define multipart message. The wsdl file is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.com.blu/SOAPwithAttachment/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
name="SOAPwithAttachment"
targetNamespace="http://www.com.blu/SOAPwithAttachment/">
<wsdl:types>
<xsd:schema targetNamespace="http://www.com.blu/SOAPwithAttachment/">
<xsd:complexType name="SubmitAttachmentResponseType">
<xsd:sequence>
<xsd:element name="response" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="SubmitAttachmentRequestType">
<xsd:sequence>
<xsd:element name="smtpserver" type="xsd:string" />
<xsd:element name="to" type="xsd:string" />
<xsd:element name="from" type="xsd:string" />
<xsd:element name="subject" type="xsd:string" />
<xsd:element name="body" type="xsd:string" />
<xsd:element name="fileName" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
<xsd:element name="submitAttachmentRequest" type="tns:SubmitAttachmentRequestType" />
<xsd:element name="zipFile" type="xsd:base64Binary" />
</xsd:schema>
</wsdl:types>

<wsdl:message name="submitAttachmentRequest">
<wsdl:part name="submitAttachment" type="tns:SubmitAttachmentRequestType"  />
<wsdl:part name="zipFile" type="xsd:base64Binary" />
</wsdl:message>

<wsdl:message name="submitAttachmentResponse">
<wsdl:part name="submitAttachmentResponse" type="tns:SubmitAttachmentResponseType" />
</wsdl:message>
<wsdl:portType name="SOAPwithAttachmentPort">
<wsdl:operation name="submitAttachment">
<wsdl:input message="tns:submitAttachmentRequest" />
<wsdl:output message="tns:submitAttachmentResponse" />
</wsdl:operation>
</wsdl:portType>

<wsdl:binding name="SOAPwithAttachmentSOAP"
type="tns:SOAPwithAttachmentPort">
<soap:binding style="rpc"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="submitAttachment">
<soap:operation
soapAction="http://www.com.blu/SOAPwithAttachment/submitAttachment" />
<wsdl:input>
<mime:multipartRelated>
<mime:part>
<soap:body parts="submitAttachment" use="literal" namespace="http://www.com.blu/SOAPwithAttachment/"/>
</mime:part>
<mime:part>
<mime:content part="zipFile" type="application/zip"/>
</mime:part>
</mime:multipartRelated>
</wsdl:input>
<wsdl:output>
<soap:body parts="submitAttachmentResponse" use="literal" namespace="http://www.com.blu/SOAPwithAttachment/"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>

<wsdl:service name="SOAPwithAttachment">
<wsdl:port binding="tns:SOAPwithAttachmentSOAP"
name="SOAPwithAttachmentSOAP">
<soap:address location="http://localhost:7001/SOAPwithAttachment_WS/SOAPwithAttachment" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


Now we will create a proxy service based on this wsdl file and import the jar file in the project.
Add one pipeline node on the proxy and add one stage node on the request pipeline.
In the stage node add one assign action and set the following xpath expression
$attachments/ctx:attachment/ctx:body/ctx:binary-content
and set the name for variable for example attachData, where $attachments is the variable of the attachment. binary-content element hold the reference of the binary data to hash table. binary-content element looks like this:
<binary-content ref="ccid:2321f-fa-edf21"/>
XML and text attachments are represented as XML and text, respectively, and can be manipulated directly with XQuery or XSLT. Binary attachment data can be manipulated only by passing the binary data to a Javacallout for processing. Remember you should send the content of the binary-content element not its reference id.
Add a JavaCallout action in the stage node and assaign it to the jar file. Fill up all the expression from the $body variable as follows:
$body/soap:submitAttachment/submitAttachment/subject/text()
also set the variable named attachData to byte[] parameter of the java class method.

Add one stage node on the response pipeline and add one replace and delete action. In the response pipleline we will make up the following response:
<ns0:submitAttachmentResponse xmlns:ns0="http://www.alsb.com/SOAPwithAttachment/">
<submitAttachmentResponse>
<response>OK</response>
</submitAttachmentResponse>
</ns0:submitAttachmentResponse>

In the replace action set the variable to the body element and xpath as ./*, also set the above xml fragment. It means we will replace the content of body element with this fragment of xml.
Now in the properties of the delete action, set the attachment variable to delete.

Here, you have just complete the tutorial and are ready to deploy it on the server. After that you have to generate stab or proxy java class from the proxy service wsdl and test the service.
Resources:
See additional information about OSB message context model.

Apply dynamic routing on Oracle service bus

Last year on one of our project we applied oracle enteprise service (SOA suite 10.1.3.1) to integrate with a few legacy system through IBM MQ. This year we have a plan to migrate this integration on OSB (Oracle service bus) previously known Aqua logic service bus from company BEA. Today i simplify one of our integration by using dynamic routing action.

Usecase: We have a few queue predefined on MQ manager to send messages for different purpose. Messages should route by according to their message content.

By using dynamic routing action and routing table we can easily publish messages to proper destination. The input xml file is as follows
<?xml version="1.0" encoding="UTF-8"?>
<TaxDiclaration id="uuid:62fa5eac-3df4-448d-a576-916dd5b432f2">
<destination>
rtu-chanel-2
</destination>
<gtd>
<id>13421</id>
<person>
<type>physical</type>
<name>Jhone</name>
<lastname>dow</lastname>
<tid>co-321478</tid>
<date>01-11-2009</date>
</person>
</gtd>
</TaxDiclaration>

by the content of the destination tag we will route entire message to his physical destination queue. Whole process is as follows:

1) Create three business service according to the destination of MQ.
2) Define the routing table xml as follows:
<routing>
<row>
<logical>rtu-chanel-2</logical>
<physical>rtu_SB/rtu_2_Biz</physical>
</row>
<row>
<logical>rtu-chanel-3</logical>
<physical>rtu_SB/rtu_3_Biz</physical>
</row>
<row>
<logical>rtu-chanel-4</logical>
<physical>rtu_SB/rtu_4_Biz</physical>
</row>
</routing>

where logical name is the name of the service, which will get from the input xml file. Physical tag contain the qualified name of the business services.
3) Create a proxy service with file transport and configure it with proper system folder to receive file.
3.1) Add one pipeline node and two stage node on request pipeline.
3.2) Add one assign action to collect the file from the inbound transport.
3.3) On 2nd stage node, add one assign action and add the routing table xml to the Expression. Give the variable name as routingTable.
3.4) Add one more assign action which will define the destination content from the xml body as follows:
$body/TaxDiclaration/destination/text()
and give the variable name as like logicalDestination.
3.5)Add one more assign action with following xpath:
<ctx:route>
<ctx:service>{$routingTable/row[logical/text()=$logicalDestination]/physical/text()}</ctx:service>
</ctx:route>

name the variable as physicalService.
3.6) Add a route node on the proxy and put a dynamic routing action on it.
3.7.) On expression properties put the following variable named $physicalService.
Now the service bus is ready for dynamic routing. For testing purpose just put some proper xml file with logical destination, service bus will apply dynamic routing according to the content of the entire xml.