Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Web service handler could not registered/called in client web service

 
saravanan periasamy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have two web service ServiceA & ServiceB and both implemented in weblogic.

The ServiceA is SSL enable and protocol is https which is not published by me.
The ServieB is my web service(wls8.1) and act as client for ServiceA.

My problem is when i hit my service, its not able set the handler when it call ServiceA but it is invoking the service and giving application exception like authentication error.

My service file:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.HandlerRegistry;
import javax.xml.rpc.handler.soap.SOAPMessageContext;

import weblogic.webservice.client.SSLAdapterFactory;
import weblogic.webservice.client.WLSSLAdapter;

public class HelloService {

String wsdl = "https://188.122.123.23/RemoetService?WSDL";

static {
SSLAdapterFactory factory = SSLAdapterFactory.getDefaultFactory();
WLSSLAdapter adapter = (WLSSLAdapter) factory.getSSLAdapter();
adapter.setTrustedCertificatesFile("D:\\lib\\certs
cacerts");
factory.setDefaultAdapter(adapter);
System.setProperty("weblogic.xml.encryption.verbose","true");
System.setProperty("weblogic.xml.signature.verbose","true");
System.setProperty("weblogic.webservice.verbose","true");
}

public String sayHello(String user) {

RemoteService_Impl service = new RemoteService_Impl(wsdl);
RemotePortType port = service.getRemoteServicePort1();

String namespace = service.getServiceName()
.getNamespaceURI();

QName portName = new QName(namespace,
"RemoteServicePortType");

HandlerRegistry reg = service.getHandlerRegistry();

List handlerList = new ArrayList();

Map map = new HashMap();
map.put("Username", "user1");
map.put("Password", "pwd1");

HandlerInfo info = new HandlerInfo();
info.setHandlerClass(WSClientHandler .class);
info.setHandlerConfig(map);

handlerList.add(info);

reg.setHandlerChain(portName,(List)handlerList);
RemoteServiceResponse = port.callMe(name);

}

}

My Handler Class:

package com.test;

import java.util.Map;

import javax.xml.namespace.QName;
import javax.xml.rpc.handler.Handler;
import javax.xml.rpc.handler.HandlerInfo;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.handler.soap.SOAPMessageContext;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;

public class WSClientHandler implements Handler {

private HandlerInfo handlerInfo;

public WSClientAuthenticateHandler(){}

public void init(HandlerInfo hi) {
System.out.println("Handler init");
handlerInfo = hi;
}

public void destroy() {
System.out.println("Handler destroy method called");
handlerInfo = null;
}

public QName[] getHeaders() {
System.out.println("Handler Header method called");
try {
Map map = handlerInfo.getHandlerConfig();
QName[] headers = handlerInfo.getHeaders();
System.out.println(" Config :"+map);
for(int i=0;i<headers.length;i++) {
System.out.println(headers.getLocalPart()+" "+
headers.toString()+" "+headers.getNamespaceURI());
}
}catch(Exception e) {
e.printStackTrace();
}

return handlerInfo.getHeaders();
}

public boolean handleRequest(MessageContext mc) {

SOAPMessageContext smc = (SOAPMessageContext) mc;

System.out.println("Calling handler class.....................");

try {
SOAPEnvelope se = smc.getMessage().getSOAPPart().getEnvelope();

System.out.println("Calling handler class.....................");
SOAPHeader soapHeader = se.getHeader();
Name headerName = se.createName("Security","wsse","http://schemas.xmlsoap.org/ws/2002/07/secext");
SOAPHeaderElement headerElement = soapHeader.addHeaderElement(headerName);
SOAPElement element = headerElement.addChildElement(se.createName("UsernameToken", "wsse", "http://schemas.xmlsoap.org/ws/2002/07/secext"));
element.addChildElement(se.createName("Username", "wsse","http://schemas.xmlsoap.org/ws/2002/07/secext")).addTextNode("testuser");
element.addChildElement(se.createName("Password", "wsse","http://schemas.xmlsoap.org/ws/2002/07/secext")).addTextNode("testpwd");

System.out.println("Calling handler class.....................");
System.out.println("** Request: \n "se.toString()"\n");
}catch(SOAPException e) {
e.printStackTrace();
}

return true;
}

/** * Specifies that the SOAP response message be logged to a
* log file before the
* * message is sent back to the client application
* that invoked the Web service.
* */

public boolean handleResponse(MessageContext mc) {
System.out.println("Handler Response method called");
SOAPMessageContext messageContext = (SOAPMessageContext) mc;
System.out.println("** Response: \n"messageContext.getMessage().toString()"\n");
return true;
}

/** * Specifies that a message be logged to the log file if a SOAP fault is
* * thrown by the Handler instance.
* */

public boolean handleFault(MessageContext mc) {
SOAPMessageContext messageContext = (SOAPMessageContext) mc;
System.out.println("** Fault: \n"messageContext.getMessage().toString()"\n");
return true;
}

}


Please need help here.

Thanks in Advance,
pps>
 
saravanan periasamy
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tested static client calling using handler simple above service and found the issues.


QName portName = new QName(namespace,

"RemoteServicePortType");


The above line code has created the issues,becuase in wsdl file ( given similar wsdl file).


<?xml version="1.0"; encoding="UTF-8"?>

<definitions name="HelloService"

targetNamespace="http://www.ecerami.com/wsdl/HelloService.wsdl"

xmlns="http://schemas.xmlsoap.org/wsdl/"

xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"

xmlns:tns="http://www.ecerami.com/wsdl/HelloService.wsdl"

xmlns:xsd="http://www.w3.org/2001/XMLSchema">

<message name="SayHelloRequest">

<part name="firstName" type="xsd:string"/>

</message>

<message name="SayHelloResponse">

<part name="greeting" type="xsd:string"/>

</message>

<portType name="RemoteServicePortType">

<operation name="sayHello">

<input message="tns:SayHelloRequest"/>

<output message="tns:SayHelloResponse"/>

</operation>

</portType>

<binding name="Hello_Binding" type="tns:RemoteServicePortType">

<soap:binding style="rpc"

transport="http://schemas.xmlsoap.org/soap/http"/>

<operation name="sayHello">

<soapperation soapAction="sayHello"/>

<input>

<soap:body

encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

namespace="urn:examples:helloservice"

use="encoded"/>

</input>

<output>

<soap:body

encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

namespace="urn:examples:helloservice"

use="encoded"/>

</output>

</operation>

</binding>

<service name="Hello_Service">

<port binding="tns:Hello_Binding" name="RemoteServicePortType1">

<soap:address

location="http://host1:8080/soap/servlet/rpcrouter"/>

</port>

<port binding="tns:Hello_Binding" name="RemoteServicePortType2">

<soap:address

location="http://host2:8080/soap/servlet/rpcrouter"/>

</port>

<port binding="tns:Hello_Binding" name="RemoteServicePortType3">

<soap:address

location="http://host3:8080/soap/servlet/rpcrouter"/>

</port>

<port binding="tns:Hello_Binding" name="RemoteServicePortType4">

<soap:address

location="http://host4:8080/soap/servlet/rpcrouter"/>

</port>


</service>

</definitions>


From the above WSDL, I have four port name (port binding="tns:Hello_Binding" name="RemoteServicePortType1) which is not matching with PortType (portType name="RemoteServicePortType")

even i have iterated from getPorts() method and used to invoke the service.But handler was not calling when i invoke.

Please suggest me how i specify correct portname which can call Handler class also.

Thanks in advance,
pps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic