by Rama Pulavarthi

Handlers are message interceptors that can be easily plugged in to the JAX-WS runtime to do additional processing of the inbound and outbound messages. JAX-WS defines two types of handlers, logical handlers and protocol handlers. Protocol handlers are specific to a protocol and may access or change the protocol specific aspects of a message. Logical handlers are protocol-agnostic and cannot change any protocol-specific parts (like headers) of a message. Logical handlers act only on the payload of the message.

Handlers are invoked with a message context that provides methods to access and modify inbound and outbound messages and to manage a set of properties. As explained in this article, message context properties can be used to communicate information between handlers and client and service implementations. SOAP handlers should extend javax.xml.ws.handler.soap.SOAPHandler, which is defined for SOAP binding by JAX-WS specification. SOAP handlers are invoked with SOAPMessageContext, which provides methods to access SOAPMessage. SOAPMessageContext.getMessage() gives a SOAPMessage. One can use the SAAJ API to manipulate the SOAP Message.

Logical handlers extend javax.xml.ws.handler.LogicalHandler and provide access to message context and message payload. If you are using SOAP over HTTP, the content of the SOAP body forms the payload. If you are using XML over HTTP, the XML content of the primary part of the message becomes the payload. Logical handlers are invoked with LogicalMessageContext. LogicalMessageContext.getMessage() returns a LogicalMessage. The LogicalMessage represents a protocol neutral XML message and contains methods that provide access to the payload of the message.

<*注解:

  1. logicalHandler和SOAPHandler所能控制message的内容不同。SOAPHandler所能控制的范围更大,包括header和body;LogicalHandler只能控制body;

  2. SOAPMessageContext包含更多的信息;LogicalMessageContext包含的信息相对较少;

>

Logical handlers can coexist with SOAP handlers in a handler chain. During runtime, the handler chain is re-ordered such that logical handlers are executed before the SOAP handlers on an outbound message and SOAP handlers are executed before logical handlers on an inbound message. The following figure shows how logical and SOAP handlers are invoked during a request and response.


Writing a Handler in JAX-WS:

Writing a handler in JAX-WS is easy. A basic handler should implement the following three methods.

handleMessage( ): This is called for inbound and outbound messages.

handleFault( ): This is called instead of handleMessage( ), when the message contains a protocol fault.

close( ): This is called after the completion of message processing by all handlers for each web service invocation (after completion of MEP). This can be useful to clean up any resources used during processing the message.

The following section describes how to SOAP handler and logical handler.

Writing a SOAP Handler:

Writing a SOAP handler involves extending javax.xml.ws.handler.soap.SOAPHandler. A sample SOAP logging handler can be accessed here. The following snippet shows how you can access SOAP Message in a SOAP handler.

public boolean handleMessage(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean) smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outboundProperty.booleanValue()) {
out.println("\nOutbound message:");
} else {
out.println("\nInbound message:");
} SOAPMessage message = smc.getMessage();
// Use SAAJ API to manipulate the SOAP Message
try {
message.writeTo(out);
out.println(""); // just to add a newline
} catch (Exception e) {
out.println("Exception in handler: " + e);
} }

Writing a Logical Handler:

Writing a logical handler involves extending javax.xml.ws.handler.LogicalHandler. A sample logical logging handler can be accessed here. The following snippet shows how you can access and manipulate a message payload from message context in a logical handler. LogicalMessage.getPayload() returns the payload as Source . The type of source returned depends on the JAX-WS runtime. If the returned source is DOMSource, then modifications to the encapsulated DOM tree change the message payload in-place, there is no need to subsequently call setPayload(). Other types of Source provide only read access to the message payload and require setPayload() for modifications.

public boolean handleMessage(LogicalMessageContext context) {
Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); if (outboundProperty) {
out.println("\nOutbound message:");
} else {
out.println("\nInbound message:");
}
LogicalMessage lm = context.getMessage();
Source payload = lm.getPayload(); // Process Payload Source
printSource(payload);
// .... // If the payload is modified, Do lm.setPayload(source) to be safe,
// Without it, behavior may vary on the kind of source returned in lm.getPayload().
// See LogicalMessage JavaDoc for more details.
// lm.setPayload(modifiedPayload); return true;
}

You can also pass JAXBContext, to get the payload as JAXB object, as shown below. Note that there is no connection between the returned object and the message payload, changes to the payload require calling setPayload().

LogicalMessage lm = context.getMessage();
Object jaxbObject = lm.getPayload(jaxbContext);
// Modify JAXB Object
lm.setPayload(modifiedJaxbObject,jaxbContext);

Summary:

The following table summarizes the differences between a SOAP handler and logical handler.

Logical handler

SOAP handler

extends

javax.xml.ws.handler.LogicalHandler

javax.xml.ws.handler.soap.SOAPHandler

message context

javax.xml.ws.handler.LogicalMessageContext

javax.xml.ws.handler.soap.SOAPMessageContext

getMessage() of message context gives

javax.xml.ws.LogicalMessage

javax.xml.soap.SOAPMessage

SOAP handlers are generally used to process SOAP-specific information like SOAP headers. For example, a SOAP Handler can process security headers in a message and pass the request to the endpoint if the message has the required credentials. Logical handlers are commonly used, if the processing does not need access to SOAP headers, for validation of the payload, and with REST style Web Services etc. If you have the JAXBContext and want to alter something in the message, it's very simple with a logical handler as you can get the jaxb objects and call Java methods on them rather than dealing with the Source or the SOAPMessage using SAAJ API in a SOAP handler.

In conclusion, Handlers are flexible to plug-in and can be a powerful add-on to your application. Check out the latest JAX-WS 2.0 Reference Implementation and try it yourself. JAX-WS 2.0 sources are available on java.net project and binaries available on GlassFish.


Rama Pulavarthi is a Member of Technical Staff in the Java Web Services group at Sun Microsystems. He currently works on the development of JAX-WS Reference Implementation. He has previously lead the Software Quality Engineering effort of JAX-RPC.

参考资料:https://jax-ws.java.net/articles/handlers_introduction.html

A little bit about Handlers in JAX-WS的更多相关文章

  1. Spring 4 集成Apache CXF开发JAX-RS Web Service

    什么是JAX-RS 在JSR-311规范中定义,即Java API for RESTful Web Services,一套Java API,用于开发 RESTful风格的Webservice. 工程概 ...

  2. iOS开发之 几本书

    <object_c 编程之道书> <iOS 7 UI Transition Guide> iOS开发指南:从零基础到App Store上架[国内第一本iOS架构设计图书,涵盖i ...

  3. java7 API详解

    Java™ Platform, Standard Edition 7API Specification This document is the API specification for the J ...

  4. Centos7.3之K8S安装初体验

    容器是发展趋势,所以是时候从虚拟机中脱离出来,投入到容器化的怀抱中了. 曾经试过安装k8s,都没有成功,各种乱七八糟的报错,于是一拖再拖,这次总算发现一个可以快速部署的工具,终于安装成功了. 这个k8 ...

  5. Java 5 、6、 7中新特性

    JDK5新特性(与1.4相比)[转] 1 循环 for (type variable : array){ body} for (type variable : arrayList){body} 而1. ...

  6. weblogic项目转为tomcat之后出现的问题

    解决java - JAX-WS和版本冲突 itPublisher分享于2017-03-19 推荐:JWS,JAX-WS,JAX-RS,REST,Restlet,SOAP(JAVA Web Servic ...

  7. [Java - 调用WebService]{http://schemas.microsoft.com/ws/2005/05/addressing/none}ActionNotSupported

    - Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipar ...

  8. The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files

    看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...

  9. com.sun.xml.internal.ws.server.ServerRtException: Server Runtime Error: java.net.BindException: Cannot assign requested address: bind

    在发布 web service 时报错: Endpoint.publish(publishAddress, hl7MessageReveiver); com.sun.xml.internal.ws.s ...

随机推荐

  1. Word2013中制作按钮控件

    1.由于“开发工具”不经常用,所以在功能选项面板中没有“开发工具”这一栏.所以我们需要设置.在功能选项面板中选择“文件”,在跳转出来的版面中选择“选项”.

  2. 索尼Sony ATI显卡驱动 Win7 Win8 Win8.1 视频黑屏 解决方法

    索尼ATI显卡驱动 Win7  Win8  Win8.1 视频 黑屏 完美解决方法: 下载这个补丁 安装 即可 解决  ! baidu pan:  http://pan.baidu.com/s/1gd ...

  3. iOS当中一些常见的面试题

    转自各方面..... 一.前言部分 文中的问题多收集整理自网络,不保证100%准确,还望斟酌采纳. 1.iOS9有哪些新特性? 答案: 1)改进了 Siri 基于日期.位置和相簿名称来搜索个人照片和视 ...

  4. Java线程:线程的同步与锁

    一.同步问题提出 线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏. 例如:两个线程ThreadA.ThreadB都操作同一个对象Foo对象,并修改Foo对象上的数据. public ...

  5. C++文件流类与文件流对象

    文件流是以外存文件为输入输出对象的数据流.输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据.每一个文件流都有一个内存缓冲区与之对应. 请区分文件流与文件的概念,不用误以为文件 ...

  6. https基础流程

    背景: https基于SSL,目的是保护http通信的过程,防止中间人篡改信息,或假冒服务端的问题.   要解决的问题: 1. 客户端如何证明是与正确的服务端进行通信 2. 客户端如何确认收到服务端的 ...

  7. 轻松三步教你配置Oracle—windows环境

    最近笔者在学习Oracle的时候,虽然度过了大家所说的安装难题,但是又遇到了一系列的问题,经过多方求教才知道原来是自己仅仅是安装了Oracle,却没有在环境变量中进行相应的配置.笔者也像大家遇到问题时 ...

  8. express框架路由配置及congtroller自动加载

    express框架在node官方推荐的一个框架,关于如何入门的文章,已经很多了,我就不在累赘了,本文的核心是如何修改文件使得更接近一个MVC的框架 express原生是通过require的方式实现了模 ...

  9. CentOS 6.5 安装HDFS集群(Hadoop-2.7.3)

    安装真实集群,而不是但节点或者伪分布式,以3个节点为例,node1为NameNode和SecondNameNode,node2和node3为DataNode. 1.3台机器的配置必须要一模一样,只需要 ...

  10. 搜索引擎LuceneNet

    http://www.cnblogs.com/edisonchou/p/5348625.html