Wednesday, April 11, 2018

Java SOAP WebService Proxy: SOAPHandler to print SOAP messages

Problem: We all know about creating webservice proxy from jdeveloper. Proxy generates few classes based on WSDL and associated XSD and allow us to invoke webservice as if we are simply calling some java method. Complexity of creating soap message and sending over network and receiving output is hidden from us. One of the problem we face many a time is to view SOAP request and response messages. If we can it would be very helpful for debugging.
In this blog I will be using SOAP handler to view SOAP messages.

Solution: Solution which I am going to show is using SOAP handler feature of web-service proxy.

Using proxy is very simple, we just work with normal java classes and complexity of invoking webservice is completely hidden. SOAP Handler is java class (with some guidelines) that can be injected with proxy and proxy will make sure to call methods of this class before sending a request and after receiving message.

SOAP Handler class must implement SOAPHandler<SOAPMessageContext> class.
A Sample SOAPHandler class can look like

import java.util.Collections;
import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPException;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class CalculatorSOAPHandler implements SOAPHandler<SOAPMessageContext> {
    public Set<QName> getHeaders() {
        return Collections.emptySet();
    }

    public boolean handleMessage(SOAPMessageContext messageContext) {
                Boolean outboundProperty = (Boolean)
            messageContext.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if (outboundProperty.booleanValue()) {
            System.out.println("Request message:");
        } else {
            System.out.println("Response message:");
        }
        
        try {
            System.out.println(messageContext.getMessage().getSOAPPart().getEnvelope().toString());
        } catch (SOAPException e) {
            e.printStackTrace();
        }


        return true;
    }

    public boolean handleFault(SOAPMessageContext messageContext) {
        return true;
    }

    public void close(MessageContext messageContext) {
    }
}


Now we need to inject this class in proxy or in other words we need to configure it with proxy so that it knows about this class. There are couple of ways to do that.
1. Specify SOAP handler while creating proxy in Proxy creating wizard.

In above diagram we are specifying a SOAP handler. We have not selected any port so handler will be registered against all web-service port. Jdev will not create any class. You must create class separately.

When we generate proxy, there is one most important class which has @WebServiceClient annotation. Above step will create a handler entry in that class as

@HandlerChain(file = "MyServiceProxy-HandlerChain.xml")

It will also create a HandlerChain.xml file, which will have entry of handler.
<ns0:handler-chains xmlns:ns0="http://java.sun.com/xml/ns/javaee">
    <ns0:handler-chain>
        <ns0:service-name-pattern xmlns:ns1="http://xmlns.oracle.com/SIH/SIHParentProcess/BPELParentProcess">ns1:bpelparentprocess_client_ep</ns0:service-name-pattern>
        <ns0:handler>
            <ns0:handler-name>CalculatorSOAPHandler</ns0:handler-name>
            <ns0:handler-class>com.san.wsproxy.client.CalculatorSOAPHandler</ns0:handler-class>
        </ns0:handler>
    </ns0:handler-chain>
</ns0:handler-chains>



Now when you run service you will see that handleMessage method of SOAP handler is getting called and its printing SOAP header and body.

Another way to associate SOAP handler class with proxy is simply modify client code.
2. With proxy generation you get a client class. It has a main method, which shows how to run a webservice. You can use this class to add security (WS-Security) header in soap message by setting OWSM policy. Similarly you can use this class also to associate handler with proxy.

Your client method would look something like

    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        CalculatorSoap calculatorSoap = calculator.getCalculatorSoap();
        // Add your code to call the desired methods.
       
        Binding binding = ((BindingProvider)calculatorSoap).getBinding();
        List handlerList = binding.getHandlerChain();
        handlerList.add(new CalculatorSOAPHandler());
        binding.setHandlerChain(handlerList);
             
             
        System.out.println(calculatorSoap.add(5, 6));
       
    }

Thats all, Now if you run service, you will see that SOAP messages are getting printed on console.



Below is the print of messages
Calling handleMessage
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"/>

<S:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <Add xmlns="http://tempuri.org/">
    <intA>5</intA>
    <intB>6</intB>
  </Add>
</S:Body>

Calling handleMessage
<env:Header xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"/>

<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <AddResponse xmlns="http://tempuri.org/">
    <AddResult>11</AddResult>
  </AddResponse>
</soap:Body>


Instead system.out.println, you can add ADF logger also to print messages. In that case your messages would appear in weblogic log as well.

21 comments:

Blogger said...

As claimed by Stanford Medical, It's indeed the SINGLE reason women in this country live 10 years more and weigh on average 42 lbs lighter than we do.

(And really, it has NOTHING to do with genetics or some secret diet and really, EVERYTHING to do with "HOW" they eat.)

P.S, I said "HOW", not "WHAT"...

TAP on this link to determine if this short quiz can help you discover your true weight loss potential

Muhammad Azwar said...

Nice Article, thanks for this information
Also check this out
Laptops, Mobiles, Games, Tv, Smartwatches etc Daily Tech News Updates.

High Technologies Solutions said...

High Technologies Solutions provides Online Core and Advance Java Training Institute in DELHI NCR. We have a team of experienced Java professionals who help our students learn Java based on live Projects. Contact Details Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/java/advanced-java-training-institute-in-delhi

John smith said...

HERE YOU HAVE GIVEN ABOUT Rich Text Editor IS VERY UNIQUE AND INFORMATIVE

bhanu sreee said...

This information is really awesome thanks for sharing the most valuable information.
Best Mulesoft Online Training
Best Mulesoft Training

bhanu sreee said...


This information is really awesome thanks for sharing most valuable information.
Mulesoft Online Training
Mulesoft Online Course

MD Josim Uddin said...

This information is really very excellent thanks for sharing valuable information
https://www.hiddentech.club

Anonymous said...

thank you this website is really awesome . my self thank you

Anonymous said...

Thanks for sharing this useful Information. It is much helpful for those who are unaware about it.

If anyone is looking to build apps with the latest trends & technologies, reach Way2Smile Solutions App Developers in Chennai

smartpos said...

obviously its a great post keep sharing this kind of post!Asset management software

NM-GALLERY said...

RABBIT-NOW FOR FUN AND ENTERTAINMENT

Ekwikclasses said...

Thanks for sharing such a nice post.Advance SEO Training Course in Laxmi Nagar

antonio said...

Great post.
https://www.multichain.com/qa/user/RufusGentry

high technologies solutions said...

Thank you for sharing this content in your post thank you apart form that if anyone look for Python training institute in Delhi Contact Here-+91-9311002620 Or Visit Website- https://www.htsindia.com/Courses/python/python-training-institute-in-delhi

Dreamsoft Consultants said...

Have to work? need of money but have no experience certificate. Get in touch with us we provide experience certificate in Gurgaon 100% genuine certificate in Gurgaon. It will help it your courier. So don’t be late. Get your experience letter now. For experience letter in Delhi contact at 9599119376 or can visit our website at https://experiencecertificates.com/experience-certificate-provider-in-Gurgaon.html

Anonymous said...

Know every aspect of Artificial Intelligence with Tecdecod. Here, I will share with you some unknown and secret hacks and tips of AI. There are many things you need to know about AI, and this guide will help you know everything. So, what are you waiting for? Subscribe my blog and understand the whole AI concept.

CIC-Chromatographyinst said...

gas chromatography products

Ampcus Inc said...

RMF
Risk Management Framework - The management of organizational risk is essential for an organizations information security program. The Risk Management Framework provides an effective framework for selection of appropriate security controls for a system. RMF protects the individuals and assets of organization.

Anonymous said...

With high-quality and cost-effective services from our react agency, your web & mobile app development vision can become even more innovative.

Hire Bike On Rent said...

Nice blog! The blog is full of information, while reading my interest build up by its own to me. We are bike rental company that provides you wee-maintained bikes with required documents.Read our blogs all about Manali.
Bike on rent in Manali
Bike Rentals in Manali

hjgjhghjg said...

https://www.techhealth.live/2022/04/top-5-dogs-breeds-that-can-survive.html