1 57 58 package org.apache.wsif.wsdl; 59 60 import java.io.IOException ; 61 import java.io.InputStream ; 62 import java.io.InputStreamReader ; 63 import java.io.Reader ; 64 import java.net.URL ; 65 66 import org.apache.wsif.logging.Trc; 67 68 import com.ibm.wsdl.util.StringUtils; 69 70 77 public class WSIFWSDLLocatorImpl implements javax.wsdl.xml.WSDLLocator { 78 79 Reader baseReader = null; 80 Reader importReader = null; 81 String contextURI = null; 82 String wsdlLocation = null; 83 String documentBase = null; 84 String importBase = null; 85 ClassLoader loader = null; 86 87 93 public WSIFWSDLLocatorImpl(String ctxt, String wsdlURI, ClassLoader cl) { 94 Trc.entry(this,ctxt,wsdlURI,cl); 95 contextURI = ctxt; 96 wsdlLocation = wsdlURI; 97 loader = cl; 98 Trc.exit(); 99 } 100 101 107 public WSIFWSDLLocatorImpl(String docBase, Reader reader, ClassLoader cl) { 108 Trc.entry(this,docBase,cl); 109 documentBase = docBase; 110 baseReader = reader; 111 loader = cl; 112 Trc.exit(); 113 } 114 115 120 public Reader getBaseReader() { 121 Trc.entry(this); 122 if (baseReader == null) { 123 try { 124 URL url = null; 125 URL contextURL = 126 (contextURI != null) ? StringUtils.getURL(null, contextURI) : null; 127 if (loader != null) { 128 InputStream in = null; 129 try { 130 if (contextURL != null) 131 url = new URL (contextURL, wsdlLocation); 132 else { 133 if (wsdlLocation.indexOf(":") == -1) 134 url = new URL ("file", null, wsdlLocation); 135 else 136 url = new URL (wsdlLocation); 137 } 138 String wsdlRelativeLocation = url.getPath(); 139 if (wsdlRelativeLocation.startsWith("/")) 140 wsdlRelativeLocation = wsdlRelativeLocation.substring(1); 141 in = loader.getResourceAsStream(wsdlRelativeLocation); 142 baseReader = new InputStreamReader (in); 143 } catch (Exception exc) { 144 Trc.ignoredException(exc); 145 } 146 } 147 if (baseReader == null) { 148 url = StringUtils.getURL(contextURL, wsdlLocation); 149 baseReader = StringUtils.getContentAsReader(url); 150 } 151 if (url != null) 152 documentBase = url.toString(); 153 } catch (Exception e) { 154 Trc.exception(e); 155 documentBase = wsdlLocation; 156 } 157 } 158 Trc.exit(); 159 return baseReader; 160 } 161 162 169 public Reader getImportReader(String base, String relativeLocation) { 170 Trc.entry(this,base,relativeLocation); 171 172 importReader = null; 174 boolean triedSU = false; 175 try { 176 URL url = null; 179 if (loader != null) { 180 if (relativeLocation.startsWith("/") || relativeLocation.startsWith("\\")) { 181 relativeLocation = relativeLocation.substring(1, relativeLocation.length()); 184 InputStream in = loader.getResourceAsStream(relativeLocation); 185 importReader = new InputStreamReader (in); 186 } else if (relativeLocation.indexOf("://") != -1) { 187 triedSU = true; 190 url = StringUtils.getURL(null, relativeLocation); 191 importReader = StringUtils.getContentAsReader(url); 192 } else { 193 if (base != null) { 196 int i = base.lastIndexOf("/"); 197 if (i == -1) 198 i = base.lastIndexOf("\\"); 199 if (i != -1) { 200 String path = base.substring(0, i + 1); 201 String resolvedPath = path + relativeLocation; 202 if (relativeLocation.startsWith("..")) { 203 resolvedPath = resolvePath(path, relativeLocation); 204 } 205 if (resolvedPath == null) { 206 throw new Exception ("Invalid Path"); 207 } 208 209 if (resolvedPath.startsWith("file:")) { 211 url = new URL (null, resolvedPath); 212 } else { 213 url = new URL (null, "file:" + resolvedPath); 214 } 215 } else { 216 url = new URL (null, "file:" + relativeLocation); 217 } 218 InputStream in = loader.getResourceAsStream(url.getPath()); 219 importReader = new InputStreamReader (in); 220 } else { 221 url = new URL (null, "file:" + relativeLocation); 222 InputStream in = loader.getResourceAsStream(url.getPath()); 223 importReader = new InputStreamReader (in); 224 } 225 } 226 } else { 227 triedSU = true; 228 URL contextURL = (base != null) ? StringUtils.getURL(null, base) : null; 229 url = StringUtils.getURL(contextURL, relativeLocation); 230 importReader = StringUtils.getContentAsReader(url); 231 } 232 importBase = (url == null) ? relativeLocation : url.toString(); 233 } catch (Exception e) { 234 Trc.exception(e); 235 if (!triedSU) { 238 try { 239 URL contextURL = (base != null) ? StringUtils.getURL(null, base) : null; 240 URL url = StringUtils.getURL(contextURL, relativeLocation); 241 importReader = StringUtils.getContentAsReader(url); 242 importBase = (url == null) ? relativeLocation : url.toString(); 243 } catch (Exception e2) { 244 Trc.exception(e2); 245 importBase = "unknownImportURI"; 248 } 249 } else { 250 importBase = "unknownImportURI"; 253 } 254 } 255 Trc.exit(); 256 return importReader; 257 } 258 259 263 public String getBaseURI() { 264 Trc.entry(this); 265 Trc.exit(documentBase); 266 return documentBase; 267 } 268 269 274 public String getLatestImportURI() { 275 Trc.entry(this); 276 Trc.exit(importBase); 277 return importBase; 278 } 279 280 283 private String resolvePath(String ba, String rel) { 284 StringBuffer sb = new StringBuffer (rel); 285 int dd = 0; 286 while(sb.length() > 0) { 287 if(sb.length() > 3 && sb.charAt(0) == '.' && sb.charAt(1) == '.' 288 && (sb.charAt(2) == '/' || sb.charAt(2) == '\\')) { 289 dd++; 290 sb.delete(0,3); 291 } else { 292 break; 293 } 294 } 295 StringBuffer sb2 = new StringBuffer (ba); 296 int j = sb2.length()-1; 297 int found = 0; 298 for (int k = j; k>=0; k--) { 299 if (k!=j && (sb2.charAt(k) == '/' || sb2.charAt(k) == '\\')) { 300 found++; 301 } 302 if (found < dd) { 303 sb2.deleteCharAt(k); 304 } else { 305 break; 306 } 307 } 308 if (found+1 < dd) return null; 309 return sb2.toString() + sb.toString(); 310 } 311 312 316 public void close() throws IOException { 317 if (baseReader != null) baseReader.close(); 318 if (importReader != null) importReader.close(); 319 } 320 } 321 322 323 | Popular Tags |