1 23 package com.sun.enterprise.jbi.serviceengine.util.soap; 24 25 import java.util.*; 26 import javax.wsdl.*; 27 import javax.wsdl.extensions.*; 28 import javax.wsdl.extensions.soap.*; 29 import java.util.logging.*; 30 import javax.xml.soap.SOAPMessage ; 31 import javax.xml.namespace.QName ; 32 import org.w3c.dom.NodeList ; 33 import org.w3c.dom.Node ; 34 35 import com.sun.logging.LogDomains; 36 37 44 public class EndpointMetaData { 45 46 49 protected static final Logger logger = 50 LogDomains.getLogger(LogDomains.SERVER_LOGGER); 51 52 private Definition def = null; 53 private QName serviceName = null; 54 private String epName = null; 55 56 private OperationMetaData operationMetaData; private OperationMetaData[] opmds = null; 58 59 62 public EndpointMetaData(Definition def, QName serviceName, String epName) { 63 this.def = def; 64 this.serviceName = serviceName; 65 this.epName = epName; 66 } 67 68 73 public EndpointMetaData(Definition def) { 74 this.def = def; 75 } 76 77 86 public void resolve() { 87 if (logger.isLoggable(Level.FINE)) { 88 logger.fine("Resolving the WSDL for : " + serviceName + " : " + epName); 89 } 90 Binding binding = getBinding(def, serviceName, epName); 91 javax.wsdl.Operation[] ops = getOperations(binding); 92 if (ops.length == 1) { 93 operationMetaData = new OperationMetaData(); 94 operationMetaData.setOperationName(ops[0].getName()); 95 operationMetaData.setOneWay(javax.wsdl.OperationType.ONE_WAY.equals(ops[0].getStyle())); 96 } else if (ops.length > 1) { 97 opmds = new OperationMetaData[ops.length]; 98 int i = 0 ; 99 for (javax.wsdl.Operation op : ops) { 100 Message input = null; 101 if (op.getInput() != null) { 102 input = op.getInput().getMessage(); 103 } 104 Set s = getInputPartNames(input); 105 OperationMetaData md = new OperationMetaData(); 106 md.setOperationName(op.getName()); 107 md.setInputParameters(s); 108 md.setOneWay(javax.wsdl.OperationType.ONE_WAY.equals(op.getStyle())); 109 opmds[i++] = md; 110 } 111 } else { 112 throw new RuntimeException ("WSDL operation not resolved"); 114 } 115 } 116 117 public OperationMetaData getOperationMetaData(SOAPMessage soapMsg) { 118 if (this.operationMetaData != null) { 119 return this.operationMetaData; 120 } 121 122 List nodeNames = new ArrayList(); 123 try { 124 javax.xml.soap.SOAPBody soapBody = soapMsg.getSOAPBody(); 125 NodeList nl = soapBody.getChildNodes(); 126 127 for (int i = 0; i < nl.getLength(); i++) { 128 Node n = nl.item(i); 129 130 if (Node.ELEMENT_NODE == n.getNodeType()) { 131 String nodeName = n.getLocalName(); 132 if (nodeName != null) { 133 nodeNames.add(nodeName); 134 } 135 } 136 } 137 } catch (Exception e) { 138 e.printStackTrace(); 139 } 140 141 for (OperationMetaData om : opmds) { 142 if (logger.isLoggable(Level.FINEST)) { 143 logger.finest("Matching for" + om.getOperationName()); 144 } 145 Set inputs = om.getInputParameters(); 146 if (logger.isLoggable(Level.FINEST)) { 147 logger.finest("Inputs" + inputs); 148 logger.finest("Nodes" + nodeNames); 149 } 150 if (inputs.containsAll(nodeNames)) { 151 return om; 152 } 153 } 154 return null; 155 } 156 157 public Definition getDefinition() { 158 return def; 159 } 160 161 private Set getInputPartNames(Message msg) { 162 Set set = new HashSet(); 163 164 if (logger.isLoggable(Level.FINE)) { 165 logger.fine("Getting input parameters for: "+ msg); 166 } 167 if (msg != null) { 168 Iterator bodyIterator = null; 169 Map msgParts = msg.getParts(); 170 if (logger.isLoggable(Level.FINE)) { 171 logger.fine("Message Parts are: "+ msgParts); 172 } 173 if (msgParts != null) { 174 bodyIterator = msgParts.keySet().iterator(); 175 } 176 while (bodyIterator != null && bodyIterator.hasNext()) { 178 String bodyPart = (String ) bodyIterator.next(); 179 Part part = msg.getPart(bodyPart); 180 if (part == null) { 181 throw new IllegalStateException ("WSDL error"); 182 } 183 QName typeQName = part.getTypeName(); 184 QName elemQName = part.getElementName(); 185 186 if (typeQName != null) { 187 set.add(part.getName()); 189 if (logger.isLoggable(Level.FINE)) { 190 logger.fine("Added partName: "+ part.getName()); 191 } 192 } else if (elemQName != null) { 193 if (logger.isLoggable(Level.FINE)) { 195 logger.fine("Added root node: "+ elemQName.getLocalPart()); 196 logger.fine("Part name is : " + part.getName()); 197 } 198 set.add(elemQName.getLocalPart()); 199 } 200 } 201 } 202 203 return set; 204 } 205 206 public static javax.wsdl.Operation[] getOperations(Binding binding) { 207 if (binding != null) { 208 PortType pt = binding.getPortType(); 209 if (pt != null) { 210 List l = pt.getOperations(); 211 if (l != null && l.size() > 0) { 212 return (javax.wsdl.Operation[]) 213 l.toArray(new javax.wsdl.Operation[0]); 214 } 215 } 216 } 217 return null; 218 } 219 220 public static Binding getBinding(Definition def, QName serviceName, String endpointName) { 221 String location = null; 222 Service svc = def.getService(serviceName); 223 if (svc == null) { 224 return null; 225 } 226 Port port = svc.getPort(QName.valueOf(endpointName).getLocalPart()); 227 if (port == null) { 228 return null; 229 } else { 230 return port.getBinding(); 231 } 232 } 233 234 237 class OperationMetaData { 238 private String operationName; 239 private Set inputParams; 240 private boolean oneWay; 241 242 void setOperationName(String name) { 243 this.operationName = name; 244 } 245 246 private void setInputParameters(Set params) { 247 this.inputParams = params; 248 } 249 250 String getOperationName() { 251 return this.operationName; 252 } 253 254 private Set getInputParameters() { 255 return inputParams; 256 } 257 258 boolean isOneWay() { 259 return oneWay; 260 } 261 262 void setOneWay(boolean oneWay) { 263 this.oneWay = oneWay; 264 } 265 } 266 } 267 | Popular Tags |