问题
I've created an Extension of GenericHandler called SOAPHeaderHandler. I placed log4j statements in the handler and can see the constructor being built. When I generate a SOAP message, however, I don't see the message related to the handleRequest method. I've registered the Handler in the stub as follows:
if (service == null) {
super.service = new com.ibm.ws.webservices.engine.client.Service();
}
else {
super.service = service;
}
List handlerInfoList = new ArrayList();
QName[] headersArr = null;
HandlerInfo handlerInfo = new HandlerInfo(com.xxxxxx.hdhp.business.debitcard.cardservices.CardServiceSOAPHeaderHandler.class,
null, headersArr);
handlerInfoList.add(handlerInfo);
service.getHandlerRegistry().setHandlerChain(new QName("MetavanteDebitCard"), handlerInfoList);
The Handler is:
public class AccountManagementSOAPHeaderHandler extends GenericHandler {
private static Logger logger = Logger.getLogger (AccountManagementSOAPHeaderHandler.class);
private HandlerInfo handlerInfo = null;
public AccountManagementSOAPHeaderHandler () {
logger.info("Constructing AccountManagementSOAPHeaderHandler");
}
/* (non-Javadoc)
* @see javax.xml.rpc.handler.GenericHandler#getHeaders()
*/
public QName[] getHeaders() {
logger.info("calling getHeaders()");
return null;
}
public boolean handleFault(MessageContext arg0) {
logger.info("Fault in AccountManagementSOAPHeaderHandler");
return true;
}
public boolean handleResponse(MessageContext arg0) {
logger.info("Response in AccountManagementSOAPHeaderHandler");
return true;
}
public void init(HandlerInfo arg0) {
logger.info("init in AccountManagementSOAPHeaderHandler");
handlerInfo = arg0;
super.init(arg0);
}
public void destroy() {
logger.info("--- In AccountManagementSOAPHeaderHandler.destroy ()");
}
public boolean handleRequest(MessageContext ctx) {
logger.debug("BEGIN handleRequest()");
if (ctx instanceof SOAPMessageContext) {
SOAPMessageContext context = (SOAPMessageContext) ctx;
logger.debug("instance of SOAPMessageContext");
try {
SOAPHeader header = context.getMessage().getSOAPPart()
.getEnvelope().getHeader();
logger.debug("SOAP Header is " + ((header==null)?"NULL":"NOT NULL"));
Iterator headers = header
.extractHeaderElements("http://schemas.xmlsoap.org/soap/actor/next");
while (headers.hasNext()) {
SOAPHeaderElement he = (SOAPHeaderElement) headers.next();
logger.info("HEADER Qn " + he.getElementName().getQualifiedName());
}
} catch (SOAPException x) {
logger.error("SOAPException while handlingRequest for SOAP: '" + x.getMessage() + "'");
}
}
return true;
}
and I've changed the web.xml as follows:
<service-ref>
<description>WSDL Service AccountManagerService</description>
<service-ref-name>service/AccountManagerService</service-ref-name>
<service-interface>com.medibank.www.AccountManagerService</service-interface>
<!-- <wsdl-file>WEB-INF/wsdl/AccountManagerService.asmx.wsdl</wsdl-file>-->
<jaxrpc-mapping-file>WEB-INF/AccountManagerService.asmx_mapping.xml</jaxrpc-mapping-file>
<service-qname xmlns:pfx="http://www.medibank.com/MBIWebServices/Access/Services/AccountManager/2004/06/">pfx:AccountManagerService</service-qname>
<port-component-ref>
<service-endpoint-interface>com.medibank.www.AccountManagerServiceSoap</service-endpoint-interface>
</port-component-ref>
<port-component-ref>
<service-endpoint-interface>com.medibank.www.AccountManagerServiceSoap</service-endpoint-interface>
</port-component-ref>
<handler>
<description>
</description>
<display-name></display-name>
<handler-name>AccountManagementSOAPHeaderHandler</handler-name>
<handler-class>com.xxxxx.hdhp.business.debitcard.accountmanagement.AccountManagementSOAPHeaderHandler</handler-class>
</handler>
</service-ref>
This is deployed on Websphere Application Server v6.0.2.35. Any ideas what the problem may be? Why do the logger statements in the handler never get executed? Have I failed to register the handler correctly? Do I need to specify which service methods get handled?
回答1:
I wrote a few JAX-RPC SOAP clients that run on WAS 6.0, and require GenericHandlers that add a custom SOAP header to request messages. I also had the requirement of not using a service-ref (for ease-of-use reasons in the client codebase), so my client class sets up the handler in a programmatic way. This may not exactly apply to how your configuration works, but maybe something will be of use.
I started with the client code generated by RAD 7.5's WSDL2Java tool in Ant, but the "Web Service Client" wizard uses it as well. It created all the business objects, serializer/deserializers, locator and SOAP binding classes, etc, etc. I then created a custom GenericHandler similar to the one you have.
Since I didn't have a service-ref available, I couldn't bind it to the client that way. So I used the following code in the client class itself, to programmatically add the handler:
private AccountManager createAccountManagerStub() throws Exception {
AccountManagerServiceLocator locator = new AccountManagerServiceLocator();
// Set the JMS endpoint address
AccountManagerSOAPBindingStub accountManagerStub = (AccountManagerSOAPBindingStub) locator
.getAccountManagerSOAPPort(new URL(generateJMSEndpointAddress()));
// Set the Client Handler
HandlerRegistry registry = locator.getHandlerRegistry();
List chain = registry
.getHandlerChain((QName) locator.getPorts().next());
HandlerInfo handlerInfo = new HandlerInfo();
handlerInfo.setHandlerClass(AccountManagerClientHandler.class);
chain.add(handlerInfo);
return (AccountManager) accountManagerStub;
}
The object returned by that method is completely set up, and calling any of the client methods on that class work correctly. The AccountManagerClientHandler.handleRequest(MessageContext msgContext) method is invoked, the messageContext updated, and then the message is sent on its merry way.
来源:https://stackoverflow.com/questions/1806412/jax-rpc-generichandler-fails-on-websphere-application-server-v6-0-2-35