JAX – WS – 15 – SOAPHandler Client Side

Merhaba arkadaslar
Bu bolumde SOAPHandler konusuna devam edecegiz ve bir onceki bolumde Server Side’ini yaptigimiz uygulamanin Client Side tarafini tamamlayacagiz.

wsimport

Onceki bolumde yaptigimiz projeyi kullanacagiz (injavawetrust.jaxws.soaphandler.service) ve Web Service i publish edelim. Sonrasinda command line da , wsimport komutu ile Client dosyalarimizi olusturalim.

 wsimport -keep http://localhost:8888/injavawetrust/hello.jaxws?wsdl

Client Handler

Simdi de Client Side’ta calisacak SOAPHandler implementasyon sinifimizi yazalim.
Bir onceki bolumde oldugu gibi handleMessage metodunu kullanacagiz.
SOAPMessage -> SOAPEnvelope -> SOAPHeader objesini elde ediyoruz.
Sonrasinda addHeaderElement metodunu kullaniyoruz.
Yeni bir element ekliyoruz , degerini addTextNode ile ekledik.
Sonrasinda saveChanges metodu ile islemi tamamliyoruz.

TokenClientHandler.java

package ws.handler;

import java.util.Set;

import javax.xml.namespace.QName;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class TokenClientHandler implements SOAPHandler<SOAPMessageContext> {

	@Override
	public boolean handleMessage(SOAPMessageContext context) {
		
		// Standard property: message direction, true for outbound messages,
		// false for inbound.
		Boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
		
		//If it is an outgoing message
		if (outbound) {
			System.out.println("TokentClientHandler # handleMessage...");
			try {
				SOAPMessage soapMsg = context.getMessage();
				SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
				SOAPHeader soapHeader = soapEnv.getHeader();

				// add the property to the header
				QName qname = new QName("http://service.ws.injavawetrust/", "TOKEN");

				// Create a new HeaderElement in order to place the new property
				SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);

				soapHeaderElement.setActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
				soapHeaderElement.addTextNode("token12345");
				soapMsg.saveChanges();

			} catch (SOAPException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	@Override
	public boolean handleFault(SOAPMessageContext context) {
		return false;
	}

	@Override
	public void close(MessageContext context) {

	}

	@Override
	public Set<QName> getHeaders() {
		return null;
	}

}

XML Configuration

handler-client-config.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<javaee:handler-chain>
		<javaee:handler>
			<javaee:handler-class>ws.handler.TokenClientHandler</javaee:handler-class>
		</javaee:handler>
	</javaee:handler-chain>
</javaee:handler-chains>

@HandlerChain

@HandlerChain annotation’ini wsimport ile olusturdugumuz MessageWriterImplService sinifina ekleyelim.

MessageWriterImplService.java

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.2.9-b130926.1035
 * Generated source version: 2.2
 * 
 */
@WebServiceClient(name = "MessageWriterImplService", targetNamespace = "http://service.ws/", wsdlLocation = "http://localhost:8888/injavawetrust/hello.jaxws?wsdl")
@HandlerChain(file="ws/config/handler-client-config.xml")
public class MessageWriterImplService
    extends Service
{
///
/..

}

Client And Run Application

Client.java

package ws.client;

import java.net.MalformedURLException;

import ws.service.MessageWriterImplService;

public class Client {
	
	public static void main(String[] args) throws MalformedURLException {

		MessageWriterImplService s = new MessageWriterImplService();
		String msg = s.getMessageWriterImplPort().getWelcomeMessage("levent", "erguder");
		System.out.println(msg);

	}
}

Ornegi calistirdigimizda;

TokentClientHandler # handleMessage...
Welcome , levent erguder. This message is sent by MessageWriterImpl#getWelcomeMessage.
TokenHandler#handleMessage working...
token12345
Valid Token
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header>
      <TOKEN xmlns="http://service.ws.injavawetrust/" SOAP-ENV:actor="http://schemas.xmlsoap.org/soap/actor/next">token12345</TOKEN>
   </SOAP-ENV:Header>
   <S:Body>
      <ns2:getWelcomeMessage xmlns:ns2="http://service.ws/">
         <name>levent</name>
         <surname>erguder</surname>
      </ns2:getWelcomeMessage>
   </S:Body>
</S:Envelope>

Note : Eclipse Console’da menude uygulamalar arasindaki output icin degisiklik yapmamiz gerekli. (Display Selected Console)

Github kaynak kodlar / source folder
injavawetrust.jaxws.soaphandler.client

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published.