{"id":149,"date":"2008-10-21T14:11:01","date_gmt":"2008-10-21T18:11:01","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=149"},"modified":"2012-05-31T12:55:11","modified_gmt":"2012-05-31T17:55:11","slug":"jax-ws-handlers","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=149","title":{"rendered":"JAX-WS Server Handlers"},"content":{"rendered":"<p><strong>Create a Handler class implementing SOAPHandler<\/strong><\/p>\n<pre lang=\"java\">\r\npackage hello.handler;\r\n\r\nimport java.util.Set;\r\n\r\nimport javax.xml.ws.handler.MessageContext;\r\nimport javax.xml.ws.handler.soap.SOAPHandler;\r\n\r\npublic class LoggingHandler implements SOAPHandler {\r\n\r\n  public Set getHeaders() {\r\n    \/\/ TODO Auto-generated method stub\r\n    return null;\r\n  }\r\n\r\n  public boolean handleMessage(MessageContext arg0) {\r\n    System.out.printf(\"Start handle message...\");\r\n    log(arg0);\r\n    return true;\r\n  }\r\n\r\n  public boolean handleFault(MessageContext arg0) {\r\n    System.out.printf(\"Start handle fault...\");\r\n    log(arg0);\r\n    return true;\r\n  }\r\n\r\n  public void close(MessageContext arg0) {\r\n    \/\/ TODO Auto-generated method stub\r\n    \r\n  }\r\n\r\n  private void log(MessageContext ctx){\r\n    Boolean outbound = (Boolean)ctx.get(\r\n    MessageContext.MESSAGE_OUTBOUND_PROPERTY);\r\n    if (outbound.booleanValue()){\r\n      System.out.println(\"Out bound message\");\r\n    }else{\r\n      System.out.println(\"In bound message\");\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p><strong>If from Java<\/strong><br \/>\n<strong>Create a handler configuration file<\/strong><br \/>\nand add the handler class to handler-chain.<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<handler-chains xmlns=\"http:\/\/java.sun.com\/xml\/ns\/javaee\">\r\n  <handler-chain>\r\n    <handler>\r\n      <handler-class>hello.handler.LoggingHandler<\/handler-class>\r\n    <\/handler>\r\n  <\/handler-chain>\r\n<\/handler-chains>\r\n<\/pre>\n<p><strong>Annotate server implementation class<\/strong><br \/>\nand point to handler config file. Use either absolute path or relative path to the server class.<\/p>\n<pre lang=\"java\">\r\nimport javax.jws.HandlerChain;\r\nimport javax.jws.WebService;\r\n\r\n@WebService\r\n@HandlerChain(file=\"handlers.xml\")\r\npublic class HelloworldImpl {\r\n<\/pre>\n<p><strong>Include handler class and config xml files in the Ant generate-server script<\/strong><\/p>\n<pre lang=\"xml\">\r\n<target name=\"generate-server\" depends=\"setup\">\r\n  <apt\r\n    fork=\"true\"\r\n    debug=\"true\"\r\n    verbose=\"true\"\r\n    destdir=\"${build.classes.home}\"\r\n    sourcedestdir=\"${build.generated.home}\"\r\n    sourcepath=\"${basedir}\/src\">\r\n    <classpath>\r\n        <path refid=\"jaxws.classpath\"\/>\r\n        <pathelement location=\"${basedir}\/src\"\/>\r\n    <\/classpath>\r\n    <option key=\"r\" value=\"${build.home}\"\/>\r\n    <source dir=\"${basedir}\/src\">\r\n        <include name=\"**\/server\/*.java\"\/>\r\n        <include name=\"**\/handler\/*.java\"\/>\r\n    <\/source>\r\n  <\/apt>\r\n  <!-- copy handlers descriptor file -->\r\n  <copy todir=\"${build.classes.home}\">\r\n    <fileset dir=\"${basedir}\/src\">\r\n      <include name=\"**\/server\/**\/*.xml\"\/>\r\n    <\/fileset>\r\n  <\/copy>\r\n<\/target>\r\n<\/pre>\n<p><strong>If from WSDL<\/strong><br \/>\nCreate a custom-server.xml<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n<bindings\r\n  xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\"\r\n  xmlns:jaxb=\"http:\/\/java.sun.com\/xml\/ns\/jaxb\"\r\n  xmlns:wsdl=\"http:\/\/schemas.xmlsoap.org\/wsdl\/\"\r\n  wsdlLocation=\"wedms.wsdl\"\r\n  xmlns=\"http:\/\/java.sun.com\/xml\/ns\/jaxws\">    \r\n  <bindings node=\"wsdl:definitions\" xmlns:jws=\"http:\/\/java.sun.com\/xml\/ns\/javaee\">\r\n    <jws:handler-chains>\r\n      <jws:handler-chain>\r\n        <jws:handler>\r\n          <jws:handler-class>hello.handler.LoggingHandler<\/jws:handler-class>\r\n        <\/jws:handler>\r\n      <\/jws:handler-chain>\r\n    <\/jws:handler-chains>\r\n  <\/bindings>\r\n<\/bindings>\r\n<\/pre>\n<p>Add the binding attribute to wsimport task<\/p>\n<pre lang=\"xml\">\r\n<wsimport\r\n  binding=\"${basedir}\/etc\/custom-server.xml\"\r\n  wsdl=\"${basedir}\/etc\/AddNumbers.wsdl\">\r\n<\/wsimport>\r\n<\/xml>\r\nInclude the generated handler xml files in compiled classes\r\n<pre lang=\"xml\">\r\n<copy todir=\"${build.classes.home}\">\r\n  <fileset dir=\"${gen.class.dir}\">\r\n    <include name=\"**\/*.xml\"\/>\r\n  <\/fileset>\r\n<\/copy>\r\n<\/pre>\n<p><strong>Build and Deploy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create a Handler class implementing SOAPHandler package hello.handler; import java.util.Set; import javax.xml.ws.handler.MessageContext; import javax.xml.ws.handler.soap.SOAPHandler; public class LoggingHandler implements SOAPHandler { public Set getHeaders() { \/\/ TODO Auto-generated method stub return null; } public boolean handleMessage(MessageContext arg0) { System.out.printf(&#8220;Start handle message&#8230;&#8221;); &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=149\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[41,22],"tags":[],"class_list":["post-149","post","type-post","status-publish","format-standard","hentry","category-jax-ws","category-soa"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2p","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=149"}],"version-history":[{"count":6,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":4729,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions\/4729"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}