1 package com.ibm.webdav; 2 3 17 import java.io.*; 18 import java.net.*; 19 import java.rmi.*; 20 import java.rmi.UnknownHostException ; 21 import java.util.*; 22 import java.util.logging.*; 23 import java.util.logging.Logger ; 24 25 import com.ibm.webdav.impl.*; 26 import com.ibm.webdav.protocol.http.*; 27 28 35 public class ResourceFactory { 36 public static String defaultDocRoot = "c:\\www\\html\\dav"; 37 38 41 public static Properties properties = null; 42 43 46 private static Logger m_logger = Logger.getLogger(ResourceFactory.class.getName()); 47 48 static { 50 properties = new Properties(); 51 String resource = "dav4j.properties"; 52 53 try { 54 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 57 InputStream istream = loader.getResourceAsStream(resource); if (istream != null) { 59 properties.load(loader.getResourceAsStream(resource)); 60 } 61 else { 62 String classpath = System.getProperty("java.class.path"); 64 StringTokenizer paths = new StringTokenizer(classpath, File.pathSeparator); 65 File propertiesFile = null; 66 boolean found = false; 67 while (!found && paths.hasMoreTokens()) { 68 String path = paths.nextToken(); 69 propertiesFile = new File(path, resource); 70 found = propertiesFile.exists(); 71 } 72 if (found) { 73 try { 74 properties.load(new FileInputStream(propertiesFile)); 75 } catch (Exception exc) { 76 exc.printStackTrace(); 77 } 78 } 79 } 80 } catch (RuntimeException e) { 81 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 82 } catch (IOException e) { 83 m_logger.log(Level.WARNING, e.getLocalizedMessage(), e); 84 } 85 } 86 108 public static Resource create(String url) throws java.io.IOException { 109 return create(url, null); 110 } 111 133 public static Resource create(String url, TargetSelector targetSelector) throws java.io.IOException { 134 Resource resource = null; 135 try { 136 resource = new Resource(url, targetSelector); 137 if (resource.isCollection()) { 138 resource = null; 139 resource = new Collection(url, targetSelector); 140 } 141 } catch (WebDAVException exc) { 142 throw exc; 143 } 144 return resource; 145 } 146 155 public static IRCollection createCollection(URL url, TargetSelector targetSelector) throws WebDAVException { 156 IRCollection resource = null; 157 String protocol = null; 158 try { 159 protocol = url.getProtocol(); 160 161 if (isLocalHost(url)) { 164 resource = new CollectionImpl(url, getRealPath(url), targetSelector); 165 } else if (protocol.equals("http")) { 166 resource = new CollectionHTTPStub(url, targetSelector); 167 } else if (protocol.equals("rmi")) { 168 String name = url.toString(); 169 name = name.substring(name.indexOf(":") + 1); 170 try { 171 resource = (IRCollection) Naming.lookup(name); 172 } catch (java.rmi.NotBoundException exc) { 173 throw new WebDAVException(WebDAVStatus.SC_NOT_FOUND, "Not bound"); 174 } 175 } else { 176 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol); 177 } 178 } catch (WebDAVException exc) { 179 throw exc; 180 } catch (java.io.IOException exc) { 181 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol); 182 } 183 return resource; 184 } 185 194 public static IRResource createResource(URL url, TargetSelector targetSelector) throws WebDAVException { 195 IRResource resource = null; 196 String protocol = null; 197 try { 198 protocol = url.getProtocol(); 199 200 if (isLocalHost(url)) { 203 resource = new ResourceImpl(url, getRealPath(url), targetSelector); 204 } else if (protocol.equals("http")) { 205 resource = new ResourceHTTPStub(url, targetSelector); 207 } else if (protocol.equals("rmi")) { 208 String name = url.toString(); 209 name = name.substring(name.indexOf(":") + 1); 210 try { 211 resource = (IRResource) Naming.lookup(name); 212 } catch (java.rmi.NotBoundException exc) { 213 throw new WebDAVException(WebDAVStatus.SC_NOT_FOUND, "Not bound"); 214 } 215 } else { 216 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol); 217 } 218 } catch (WebDAVException exc) { 219 throw exc; 220 } catch (java.io.IOException exc) { 221 throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "WebDAV: Invalid communication protocol: " + protocol); 222 } 223 return resource; 224 } 225 232 233 public static String getRealPath(URL url) { 234 String docRoot = properties.getProperty("doc.root", defaultDocRoot); 235 String pathName = docRoot + url.getFile().replace('/', File.separatorChar); 236 return pathName; 237 } 238 243 public static boolean isLocalHost(URL url) throws WebDAVException { 244 return true; 257 } 258 } 259 | Popular Tags |