1 18 package org.apache.beehive.controls.system.webservice.jaxrpc; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 23 import javax.xml.namespace.QName ; 24 import javax.xml.rpc.handler.Handler ; 25 import javax.xml.rpc.handler.HandlerInfo ; 26 import javax.xml.rpc.handler.MessageContext ; 27 import javax.xml.rpc.handler.soap.SOAPMessageContext ; 28 import javax.xml.soap.SOAPElement ; 29 import javax.xml.soap.SOAPEnvelope ; 30 import javax.xml.soap.SOAPException ; 31 import javax.xml.soap.SOAPHeader ; 32 import javax.xml.soap.SOAPHeaderElement ; 33 import javax.xml.soap.SOAPMessage ; 34 import javax.xml.soap.SOAPPart ; 35 36 import org.apache.log4j.Logger; 37 import org.w3c.dom.Element ; 38 39 public class HeaderHandler implements Handler { 40 41 static Logger logger = Logger.getLogger(HeaderHandler.class); 42 43 static ThreadLocal localInHeaders = new ThreadLocal (); 44 45 static ThreadLocal localOutHeaders = new ThreadLocal (); 46 47 52 public boolean handleRequest(MessageContext mc) { 53 System.out.println("In HeaderHandler's handleRequest myOutHeaders: " 54 + localOutHeaders.get()); 55 56 try { 57 SOAPMessageContext smc = (SOAPMessageContext ) mc; 58 SOAPMessage msg = smc.getMessage(); 59 logger.debug("Request message body: " 60 + msg.getSOAPBody().toString() + "\n header: " 61 + msg.getSOAPHeader().toString()); 62 SOAPPart part = msg.getSOAPPart(); 63 SOAPEnvelope envelope = part.getEnvelope(); 64 SOAPHeader header = envelope.getHeader(); 65 66 70 Element [] elemsToAdd = (Element []) localOutHeaders.get(); 71 72 if (null == elemsToAdd) { 73 logger.debug("no header to send"); 74 return true; 75 } 76 for (Element nxtElement : elemsToAdd) { 77 header 78 .addChildElement(new org.apache.axis.message.SOAPHeaderElement( 79 nxtElement)); } 81 82 } catch (SOAPException e) { 83 e.printStackTrace(); 84 logger.error("Failed to add header.", e); 85 } 86 87 return true; 88 } 89 90 93 static public void reset() { 94 localInHeaders = new ThreadLocal (); 95 localOutHeaders = new ThreadLocal (); 96 97 } 98 99 102 public boolean handleResponse(MessageContext mc) { 103 System.out.println("In HeaderHandler's handleResponse"); 104 105 try { 106 SOAPMessageContext smc = (SOAPMessageContext ) mc; 107 SOAPMessage msg = smc.getMessage(); 108 logger.debug("Response message body: " 109 + msg.getSOAPBody().toString() + "\n header: " 110 + msg.getSOAPHeader().toString()); 111 SOAPPart part = msg.getSOAPPart(); 112 SOAPEnvelope envelope = part.getEnvelope(); 113 SOAPHeader header = envelope.getHeader(); 114 if (header == null) 115 return true; 116 117 Iterator it = header.extractAllHeaderElements(); 118 ArrayList <Element > list = new ArrayList <Element >(); 119 while (it.hasNext()) { 120 SOAPElement nxtSoapElement = (SOAPElement ) it.next(); 121 list.add((Element ) nxtSoapElement.cloneNode(true)); 122 } 123 124 Element [] elems = (Element []) list 125 .toArray(new Element [list.size()]); localInHeaders.set(elems); 127 128 } catch (SOAPException e) { 129 e.printStackTrace(); 130 logger.error("Error extracting the header.", e); 131 } 132 133 logger.debug("myInHeaders: " + localInHeaders.get()); 134 return true; 135 } 136 137 140 public boolean handleFault(MessageContext mc) { 141 System.out.println("In HeaderHandler's handleFault"); 142 return true; 143 } 144 145 150 public void init(HandlerInfo arg0) { 151 System.out.println("In HeaderHandler's init"); 152 153 } 154 155 160 public void destroy() { 161 logger.debug("In HeaderHandler's destroy"); 162 163 } 164 165 170 public QName [] getHeaders() { 171 return null; 173 } 174 175 178 static public Element [] getInHeaders() { 179 return (Element []) localInHeaders.get(); 180 } 181 182 186 static public void setOutHeaders(Element [] outHeaders) { 187 localOutHeaders.set(outHeaders); 188 } 189 } 190 | Popular Tags |