1 16 package org.apache.axis.handlers; 17 18 import org.apache.axis.components.logger.LogFactory; 19 import org.apache.axis.utils.ClassUtils; 20 import org.apache.axis.utils.Messages; 21 import org.apache.commons.logging.Log; 22 23 import javax.xml.rpc.JAXRPCException ; 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.rpc.soap.SOAPFaultException ; 29 import javax.xml.soap.SOAPBody ; 30 import javax.xml.soap.SOAPElement ; 31 import javax.xml.soap.SOAPEnvelope ; 32 import javax.xml.soap.SOAPException ; 33 import javax.xml.soap.SOAPMessage ; 34 import java.util.ArrayList ; 35 import java.util.Arrays ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 43 public class HandlerChainImpl extends ArrayList implements javax.xml.rpc.handler 44 .HandlerChain { 45 protected static Log log = 46 LogFactory.getLog(HandlerChainImpl.class.getName()); 47 48 public static final String JAXRPC_METHOD_INFO = "jaxrpc.method.info"; 49 50 private String [] _roles; 51 52 private int falseIndex = -1; 53 54 public String [] getRoles() { 55 return _roles; 56 } 57 58 public void setRoles(String [] roles) { 59 if (roles != null) { 60 _roles = (String [])roles.clone(); 62 } 63 } 64 65 public void init(Map map) { 66 } 68 69 protected List handlerInfos = new ArrayList (); 70 71 public HandlerChainImpl() { 72 } 73 74 public HandlerChainImpl(List handlerInfos) { 75 this.handlerInfos = handlerInfos; 76 for (int i = 0; i < handlerInfos.size(); i++) { 77 add(newHandler(getHandlerInfo(i))); 78 } 79 } 80 81 public void addNewHandler(String className, Map config) { 82 try { 83 HandlerInfo handlerInfo = 84 new HandlerInfo (ClassUtils.forName(className), config, null); 85 handlerInfos.add(handlerInfo); 86 add(newHandler(handlerInfo)); 87 } catch (Exception ex) { 88 String messageText = 89 Messages.getMessage("NoJAXRPCHandler00", className); 90 throw new JAXRPCException (messageText, ex); 91 } 92 } 93 94 public boolean handleFault(MessageContext _context) { 95 SOAPMessageContext context = (SOAPMessageContext )_context; 96 preInvoke(context); 97 try { 98 int endIdx = size() - 1; 99 if (falseIndex != -1) { 100 endIdx = falseIndex; 101 } 102 for (int i = endIdx; i >= 0; i--) { 103 if (getHandlerInstance(i).handleFault(context) == false) { 104 return false; 105 } 106 } 107 return true; 108 } finally { 109 postInvoke(context); 110 } 111 } 112 113 public ArrayList getMessageInfo(SOAPMessage message) { 114 ArrayList list = new ArrayList (); 115 try { 116 if(message == null || message.getSOAPPart() == null) 117 return list; 118 SOAPEnvelope env = message.getSOAPPart().getEnvelope(); 119 SOAPBody body = env.getBody(); 120 Iterator it = body.getChildElements(); 121 SOAPElement operation = (SOAPElement )it.next(); 122 list.add(operation.getElementName().toString()); 123 for (Iterator i = operation.getChildElements(); i.hasNext();) { 124 SOAPElement elt = (SOAPElement )i.next(); 125 list.add(elt.getElementName().toString()); 126 } 127 } catch (Exception e) { 128 log.debug("Exception in getMessageInfo : ", e); 129 } 130 return list; 131 } 132 133 public boolean handleRequest(MessageContext _context) { 134 org.apache.axis.MessageContext actx = 135 (org.apache.axis.MessageContext)_context; 136 actx.setRoles(getRoles()); 137 SOAPMessageContext context = (SOAPMessageContext )_context; 138 preInvoke(context); 139 try { 140 for (int i = 0; i < size(); i++) { 141 Handler currentHandler = getHandlerInstance(i); 142 try { 143 if (currentHandler.handleRequest(context) == false) { 144 falseIndex = i; 145 return false; 146 } 147 } catch (SOAPFaultException sfe) { 148 falseIndex = i; 149 throw sfe; 150 } 151 } 152 return true; 153 } finally { 154 postInvoke(context); 155 } 156 } 157 158 public boolean handleResponse(MessageContext context) { 159 SOAPMessageContext scontext = (SOAPMessageContext )context; 160 preInvoke(scontext); 161 try { 162 int endIdx = size() - 1; 163 if (falseIndex != -1) { 164 endIdx = falseIndex; 165 } 166 for (int i = endIdx; i >= 0; i--) { 167 if (getHandlerInstance(i).handleResponse(context) == false) { 168 return false; 169 } 170 } 171 return true; 172 } finally { 173 postInvoke(scontext); 174 } 175 } 176 177 private void preInvoke(SOAPMessageContext msgContext) { 178 try { 179 SOAPMessage message = msgContext.getMessage(); 180 if(message != null && message.getSOAPPart() != null) 182 message.getSOAPPart().getEnvelope(); 183 msgContext.setProperty(org.apache.axis.SOAPPart.ALLOW_FORM_OPTIMIZATION, 184 Boolean.FALSE); 185 msgContext.setProperty(JAXRPC_METHOD_INFO, getMessageInfo(message)); 186 } catch (Exception e) { 187 log.debug("Exception in preInvoke : ", e); 188 throw new RuntimeException ("Exception in preInvoke : " + e.toString()); 189 } 190 } 191 192 private void postInvoke(SOAPMessageContext msgContext) { 193 Boolean propFormOptimization = (Boolean )msgContext.getProperty(org.apache.axis.SOAPPart.ALLOW_FORM_OPTIMIZATION); 194 if (propFormOptimization != null && !propFormOptimization.booleanValue()) { 195 msgContext.setProperty(org.apache.axis.SOAPPart.ALLOW_FORM_OPTIMIZATION, 196 Boolean.TRUE); 197 SOAPMessage message = msgContext.getMessage(); 198 ArrayList oldList = 199 (ArrayList )msgContext.getProperty(JAXRPC_METHOD_INFO); 200 if (oldList != null) { 201 if (!Arrays.equals(oldList.toArray(), getMessageInfo(message) 202 .toArray())) { 203 throw new RuntimeException (Messages.getMessage("invocationArgumentsModified00")); 204 } 205 } 206 try { 207 if (message != null) { 208 message.saveChanges(); 209 } 210 } catch (SOAPException e) { 211 log.debug("Exception in postInvoke : ", e); 212 throw new RuntimeException ("Exception in postInvoke : " + e.toString()); 213 } 214 } 215 } 216 217 public void destroy() { 218 int endIdx = size() - 1; 219 if (falseIndex != -1) { 220 endIdx = falseIndex; 221 } 222 for (int i = endIdx; i >= 0; i--) { 223 getHandlerInstance(i).destroy(); 224 } 225 falseIndex = -1; 226 clear(); 227 } 228 229 private Handler getHandlerInstance(int index) { 230 return (Handler )get(index); 231 } 232 233 private HandlerInfo getHandlerInfo(int index) { 234 return (HandlerInfo )handlerInfos.get(index); 235 } 236 237 private Handler newHandler(HandlerInfo handlerInfo) { 238 try { 239 Handler handler = (Handler )handlerInfo.getHandlerClass() 240 .newInstance(); 241 handler.init(handlerInfo); 242 return handler; 243 } catch (Exception ex) { 244 String messageText = 245 Messages.getMessage("NoJAXRPCHandler00", 246 handlerInfo.getHandlerClass().toString()); 247 throw new JAXRPCException (messageText, ex); 248 } 249 } 250 } | Popular Tags |