Monday, January 6, 2020

12c SOA - Oracle Mediator part10 - Java Callout

  • Java Callout is used when we need to have certain validation or want to manipulate the message. We need to implement the oracle.tip.mediator.common.api.IjavaCallout interface to use below defined function for Java Callout.
  • The Java Callout is invoked by the Mediator on a number of times, prior to and after performing the Routing Rule and each of the cases in it. The Java Callout class can implement a number of methods, one for each specific event or stage in the Mediator process. These methods get access to the input message as well as the transformation result. That means that the callout class can inspect, validate, log, audit and even manipulate these messages, their payloads, headers and properties.
We have below methods in the oracle.tip.mediator.common.api.IjavaCallout interface.

Initialize : This method is invoked when the callout implementation class is instantiated for the first time.
preRouting : This method is called before Mediator starts executing the cases. You can customize this method to include validations and enhancements.
preRoutingRule : This method is called before Mediator starts executing any particular case. You can customize this method to include case-specific validations and enhancements.
preCallbackRouting: This method is called before Mediator finishes executing callback handling. You can customize this method to perform callback auditing and custom fault tracking.
postRouting : This method is called after Mediator finishes executing the cases. You can customize this method to perform response auditing and custom fault tracking.
postRoutingRule: This method is called after Mediator starts executing the cases. You can customize this method to perform response auditing and custom fault tracking.
postCallbackRouting: This method is called after Mediator finishes executing callback handling. You can customize this method to perform callback auditing and custom fault tracking.

UseCase 1: Using a postRouting method, the javacallout will log the response in the soa_server.log/out file.

Implementation:

Create a SOA project and open a mediator component
Select Synchronous interface.

Call a synchronous service


Create a Java class
Add oracle.tip.mediator.common.api.IjavaCallout interface


Modify the postRouting method
    public boolean postRouting(CalloutMediatorMessage calloutMediatorMessage,
                               CalloutMediatorMessage calloutMediatorMessage2,
                               Throwable throwable) throws MediatorCalloutException {
        System.out.println("Mediator has invoked Java Callout; executing postRouting");
        String sPayload = "null";
        for (Iterator msgIt = calloutMediatorMessage2.getPayload().entrySet().iterator();msgIt.hasNext(); ) {
          Map.Entry msgEntry = (Map.Entry)msgIt.next();
          Object msgKey = msgEntry.getKey();
          if (msgKey.equals("reply")) {
              Object msgValue = msgEntry.getValue();
              sPayload = XmlUtils.convertDomNodeToString((Node) msgValue);
              System.out.println("Mediator sends reply: " + sPayload);
          }
        } //for
        return true;
    }

Select the created class from call out section

Map the request and response



Deploy and test




Sample class for modifying the input request:
public boolean preRouting(CalloutMediatorMessage calloutMediatorMessage) throws MediatorCalloutException {
        System.out.println("Pre routing...");
        String sPayload = "null";
        String sPayload_changed = "null";
        for (Iterator msgIt = calloutMediatorMessage.getPayload().entrySet().iterator();          msgIt.hasNext(); ) {
            Map.Entry msgEntry = (Map.Entry)msgIt.next();
            Object msgKey = msgEntry.getKey();
            if (msgKey.equals("request")) {
                Object msgValue = msgEntry.getValue();
                sPayload = XmlUtils.convertDomNodeToString((Node)msgValue);
                try {
                    XMLDocument xmlpayload = XmlUtils.getXmlDocument(sPayload);
                    NodeList node = xmlpayload.getChildNodes();
                    Node item = node.item(0);
                    System.out.println("the value of the request element = " +
                                       item.getTextContent());
                    sPayload_changed = sPayload.replaceAll(item.getTextContent(),
                                                "changed text");
                    XMLDocument xmlpayload_changed =
                        XmlUtils.getXmlDocument(sPayload_changed);
                    String mykey = "request";
                    calloutMediatorMessage.addPayload(mykey,
                                                      xmlpayload_changed);
                    System.out.println("Mediator sends request: " +
                                       sPayload_changed);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
        }
        return true;
    }

No comments:

Post a Comment

Featured Post

OIC - Oracle Integration-Bulk load into Oracle ATP OR Oracle ADW

Oracle Integration enhances its database adapters, including Oracle Autonomous Transaction Processing (ATP) and Autonomous Data Warehouse (A...