1 16 17 package org.apache.axis.transport.http; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.ConfigurationException; 23 import org.apache.axis.description.ServiceDesc; 24 import org.apache.axis.server.AxisServer; 25 import org.apache.axis.utils.Messages; 26 import org.apache.axis.utils.XMLUtils; 27 import org.w3c.dom.Document ; 28 import org.w3c.dom.Node ; 29 import org.w3c.dom.NodeList ; 30 import org.w3c.dom.Element ; 31 32 import javax.servlet.http.HttpServletResponse ; 33 import java.io.PrintWriter ; 34 import java.net.HttpURLConnection ; 35 import java.util.Iterator ; 36 import java.util.Set ; 37 import java.util.HashSet ; 38 39 49 public class QSWSDLHandler extends AbstractQueryStringHandler { 50 57 public void invoke(MessageContext msgContext) throws AxisFault { 58 configureFromContext(msgContext); 61 AxisServer engine = (AxisServer) msgContext.getProperty 62 (HTTPConstants.PLUGIN_ENGINE); 63 PrintWriter writer = (PrintWriter ) msgContext.getProperty 64 (HTTPConstants.PLUGIN_WRITER); 65 HttpServletResponse response = (HttpServletResponse ) 66 msgContext.getProperty(HTTPConstants.MC_HTTP_SERVLETRESPONSE); 67 try { 68 engine.generateWSDL(msgContext); 69 Document wsdlDoc = (Document ) msgContext.getProperty("WSDL"); 70 if (wsdlDoc != null) { 71 try { 72 updateSoapAddressLocationURLs(wsdlDoc, msgContext); 73 } catch (RuntimeException re) { 74 log.warn( 75 "Failed to update soap:address location URL(s) in WSDL.", 76 re); 77 } 78 response.setContentType( 79 "text/xml; charset=" + 80 XMLUtils.getEncoding().toLowerCase()); 81 reportWSDL(wsdlDoc, writer); 82 } else { 83 if (log.isDebugEnabled()) { 84 log.debug("processWsdlRequest: failed to create WSDL"); 85 } 86 reportNoWSDL(response, writer, "noWSDL02", null); 87 } 88 } catch (AxisFault axisFault) { 89 if (axisFault.getFaultCode().equals 91 (Constants.QNAME_NO_SERVICE_FAULT_CODE)) { 92 processAxisFault(axisFault); 94 95 response.setStatus(HttpURLConnection.HTTP_NOT_FOUND); 97 reportNoWSDL(response, writer, "noWSDL01", axisFault); 98 } else { 99 throw axisFault; 101 } 102 } 103 } 104 105 111 public void reportWSDL(Document doc, PrintWriter writer) { 112 XMLUtils.PrettyDocumentToWriter(doc, writer); 113 } 114 115 123 public void reportNoWSDL(HttpServletResponse res, PrintWriter writer, 124 String moreDetailCode, AxisFault axisFault) { 125 res.setStatus(HttpURLConnection.HTTP_NOT_FOUND); 126 res.setContentType("text/html"); 127 writer.println("<h2>" + Messages.getMessage("error00") + "</h2>"); 128 writer.println("<p>" + Messages.getMessage("noWSDL00") + "</p>"); 129 if (moreDetailCode != null) { 130 writer.println("<p>" + Messages.getMessage(moreDetailCode) 131 + "</p>"); 132 } 133 if (axisFault != null && isDevelopment()) { 134 writeFault(writer, axisFault); 136 } 137 } 138 139 148 protected void updateSoapAddressLocationURLs(Document wsdlDoc, 149 MessageContext msgContext) 150 throws AxisFault { 151 Set deployedServiceNames; 152 try { 153 deployedServiceNames = getDeployedServiceNames(msgContext); 154 } 155 catch (ConfigurationException ce) { 156 throw new AxisFault("Failed to determine deployed service names.", ce); 157 } 158 NodeList wsdlPorts = wsdlDoc.getDocumentElement().getElementsByTagNameNS(Constants.NS_URI_WSDL11, "port"); 159 if (wsdlPorts != null) { 160 String endpointURL = getEndpointURL(msgContext); 161 String baseEndpointURL = endpointURL.substring(0, endpointURL.lastIndexOf("/") + 1); 162 for (int i = 0; i < wsdlPorts.getLength(); i++) { 163 Element portElem = (Element ) wsdlPorts.item(i); 164 Node portNameAttrib = portElem.getAttributes().getNamedItem("name"); 165 if (portNameAttrib == null) { 166 continue; 167 } 168 String portName = portNameAttrib.getNodeValue(); 169 NodeList soapAddresses = portElem.getElementsByTagNameNS(Constants.URI_WSDL11_SOAP, "address"); 170 if (soapAddresses == null || soapAddresses.getLength() == 0) { 171 soapAddresses = portElem.getElementsByTagNameNS(Constants.URI_WSDL12_SOAP, "address"); 172 } 173 if (soapAddresses != null) { 174 for (int j = 0; j < soapAddresses.getLength(); j++) { 175 Element addressElem = (Element ) soapAddresses.item(j); 176 Node addressLocationAttrib = addressElem.getAttributes().getNamedItem("location"); 177 if ( addressLocationAttrib == null ) 178 { 179 continue; 180 } 181 String addressLocation = addressLocationAttrib.getNodeValue(); 182 String addressServiceName = addressLocation.substring(addressLocation.lastIndexOf("/") + 1); 183 String newServiceName = getNewServiceName(deployedServiceNames, addressServiceName, portName); 184 if (newServiceName != null) { 185 String newAddressLocation = baseEndpointURL + newServiceName; 186 addressLocationAttrib.setNodeValue(newAddressLocation); 187 log.debug("Setting soap:address location values in WSDL for port " + 188 portName + 189 " to: " + 190 newAddressLocation); 191 } 192 else 193 { 194 log.debug("For WSDL port: " + portName + ", unable to match port name or the last component of " + 195 "the SOAP address url with a " + 196 "service name deployed in server-config.wsdd. Leaving SOAP address: " + 197 addressLocation + " unmodified."); 198 } 199 } 200 } 201 } 202 } 203 } 204 205 private String getNewServiceName(Set deployedServiceNames, String currentServiceEndpointName, String portName) { 206 String endpointName = null; 207 if (deployedServiceNames.contains(currentServiceEndpointName)) { 208 endpointName = currentServiceEndpointName; 209 } 210 else if (deployedServiceNames.contains(portName)) { 211 endpointName = portName; 212 } 213 return endpointName; 214 } 215 216 private Set getDeployedServiceNames(MessageContext msgContext) throws ConfigurationException { 217 Set serviceNames = new HashSet (); 218 Iterator deployedServicesIter = msgContext.getAxisEngine().getConfig().getDeployedServices(); 219 while (deployedServicesIter.hasNext()) { 220 ServiceDesc serviceDesc = (ServiceDesc) deployedServicesIter.next(); 221 serviceNames.add(serviceDesc.getName()); 222 } 223 return serviceNames; 224 } 225 226 233 protected String getEndpointURL(MessageContext msgContext) 234 throws AxisFault { 235 String locationUrl = msgContext.getStrProp( 237 MessageContext.WSDLGEN_SERV_LOC_URL); 238 if (locationUrl == null) { 239 locationUrl = 241 msgContext.getService().getInitializedServiceDesc( 242 msgContext) 243 .getEndpointURL(); 244 } 245 if (locationUrl == null) { 246 locationUrl = msgContext.getStrProp(MessageContext.TRANS_URL); 248 } 249 return locationUrl; 250 } 251 } 252 | Popular Tags |