1 7 package org.jboss.webservice.server; 8 9 11 import org.jboss.deployment.DeploymentInfo; 12 import org.jboss.logging.Logger; 13 import org.jboss.webservice.metadata.WebserviceDescriptionMetaData; 14 import org.w3c.dom.Attr ; 15 import org.w3c.dom.Document ; 16 import org.w3c.dom.Element ; 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NodeList ; 19 20 import javax.wsdl.Definition; 21 import javax.wsdl.WSDLException; 22 import javax.wsdl.factory.WSDLFactory; 23 import javax.wsdl.xml.WSDLWriter; 24 import javax.xml.parsers.DocumentBuilder ; 25 import javax.xml.parsers.DocumentBuilderFactory ; 26 import java.net.URL ; 27 import java.net.URLClassLoader ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.StringTokenizer ; 32 33 44 public class WSDLRequestHandler 45 { 46 private Logger log = Logger.getLogger(WSDLRequestHandler.class); 48 49 private WebserviceDescriptionMetaData wsdMetaData; 50 private DeploymentInfo di; 51 52 public WSDLRequestHandler(WebserviceDescriptionMetaData wsdMetaData, DeploymentInfo di) 53 { 54 this.wsdMetaData = wsdMetaData; 55 this.di = di; 56 } 57 58 66 public Document getDocumentForPath(String requestURI, String resourcePath) 67 { 68 Document wsdlDoc = null; 69 70 if (resourcePath == null) 72 { 73 wsdlDoc = getWSDLDocument(wsdMetaData.getWsdlDefinition()); 74 } 75 else 76 { 77 String wsdlFile = wsdMetaData.getWsdlFile(); 78 String rootDir = wsdlFile.substring(0, wsdlFile.lastIndexOf("/")); 79 80 URLClassLoader cl = di.localCl; 82 83 String resource = rootDir + "/" + resourcePath; 84 resource = canonicalize(resource); 85 if (resource.startsWith("WEB-INF/wsdl/") == false && resource.startsWith("META-INF/wsdl/") == false) 86 throw new SecurityException ("Cannot access a resource below the wsdl root: " + resource); 87 88 URL resURL = cl.findResource(resource); 89 if (resURL == null) 90 throw new IllegalStateException ("Cannot obtain wsdl resource from: " + resource); 91 92 try 93 { 94 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 95 factory.setNamespaceAware(true); 96 factory.setValidating(false); 97 DocumentBuilder builder = factory.newDocumentBuilder(); 98 wsdlDoc = builder.parse(resURL.openStream()); 99 } 100 catch (Exception e) 101 { 102 throw new IllegalArgumentException ("Cannot parse wsdl resource: " + resURL); 103 } 104 } 105 106 modifyImportLocations(requestURI, resourcePath, wsdlDoc.getDocumentElement()); 107 return wsdlDoc; 108 } 109 110 113 private Document getWSDLDocument(Definition wsdlDefinition) 114 { 115 try 116 { 117 WSDLFactory factory = WSDLFactory.newInstance(); 118 WSDLWriter wsdlWriter = factory.newWSDLWriter(); 119 return wsdlWriter.getDocument(wsdlDefinition); 120 } 121 catch (WSDLException e) 122 { 123 throw new RuntimeException (e); 124 } 125 } 126 127 130 private void modifyImportLocations(String requestURI, String resourcePath, Element element) 131 { 132 NodeList nlist = element.getChildNodes(); 134 for (int i = 0; i < nlist.getLength(); i++) 135 { 136 Node childNode = nlist.item(i); 137 if (childNode.getNodeType() == Node.ELEMENT_NODE) 138 { 139 Element childElement = (Element )childNode; 140 String nodeName = childElement.getLocalName(); 141 if ("import".equals(nodeName) || "include".equals(nodeName)) 142 { 143 Attr locationAttr = childElement.getAttributeNode("schemaLocation"); 144 if (locationAttr == null) 145 locationAttr = childElement.getAttributeNode("location"); 146 147 if (locationAttr != null) 148 { 149 String orgLocation = locationAttr.getNodeValue(); 150 boolean isAbsolute = orgLocation.startsWith("http://") || orgLocation.startsWith("https://"); 151 if (isAbsolute == false && orgLocation.startsWith(requestURI) == false) 152 { 153 String resource = orgLocation; 154 155 if (resourcePath != null && resourcePath.indexOf("/") > 0) 158 { 159 resource = resourcePath.substring(0, resourcePath.lastIndexOf("/") + 1); 160 resource = resource + orgLocation; 161 } 162 163 String newLocation = requestURI + "?wsdl&resource=" + resource; 164 locationAttr.setNodeValue(newLocation); 165 166 log.debug("Mapping import from '" + orgLocation + "' to '" + newLocation + "'"); 167 } 168 } 169 } 170 else 171 { 172 modifyImportLocations(requestURI, resourcePath, childElement); 173 } 174 } 175 } 176 } 177 178 181 private String canonicalize(String path) 182 { 183 StringTokenizer tok = new StringTokenizer (path, "/"); 184 List parts = new ArrayList (); 185 while (tok.hasMoreTokens()) 186 { 187 String t = tok.nextToken(); 188 if (".".equals(t)) 189 { 190 } 192 else if ("..".equals(t) && parts.size() > 0) 193 { 194 parts.remove(parts.size() - 1); 196 } 197 else 198 { 199 parts.add(t); 200 } 201 } 202 203 StringBuffer ret = new StringBuffer (); 204 for (Iterator iter = parts.iterator(); iter.hasNext();) 205 { 206 ret.append((String )iter.next()); 207 if (iter.hasNext()) 208 ret.append('/'); 209 } 210 return ret.toString(); 211 } 212 213 } 214 | Popular Tags |