001: package com.ibm.webdav;
002:
003: /*
004: * (C) Copyright IBM Corp. 2000 All rights reserved.
005: *
006: * The program is provided "AS IS" without any warranty express or
007: * implied, including the warranty of non-infringement and the implied
008: * warranties of merchantibility and fitness for a particular purpose.
009: * IBM will not be liable for any damages suffered by you as a result
010: * of using the Program. In no event will IBM be liable for any
011: * special, indirect or consequential damages or lost profits even if
012: * IBM has been advised of the possibility of their occurrence. IBM
013: * will not be liable for any third party claims against you.
014: *
015: * Portions Copyright (C) Simulacra Media Ltd, 2004.
016: */
017: import java.io.*;
018: import java.net.*;
019: import java.rmi.*;
020: import java.rmi.UnknownHostException;
021: import java.util.*;
022: import java.util.logging.*;
023: import java.util.logging.Logger;
024:
025: import com.ibm.webdav.impl.*;
026: import com.ibm.webdav.protocol.http.*;
027:
028: /** A ResourceFactory is used to construct Resources or their subclasses
029: * based on the desired communication protocol, http:, rmi:, or iiop: as
030: * specified in the resource URL.
031: * @author Jim Amsden <jamsden@us.ibm.com>
032: * @see com.ibm.webdav.impl.ResourceHTTPStub
033: * @see com.ibm.webdav.impl.ResourceImpl
034: */
035: public class ResourceFactory {
036: public static String defaultDocRoot = "c:\\www\\html\\dav";
037:
038: /** All WebDAV properties from the dav4j.properties file located somewhere
039: * in the classpath. See the dav4j.properties file for details.
040: */
041: public static Properties properties = null;
042:
043: /**
044: * Logger for this class
045: */
046: private static Logger m_logger = Logger
047: .getLogger(ResourceFactory.class.getName());
048:
049: // load the webdav properties
050: static {
051: properties = new Properties();
052: String resource = "dav4j.properties";
053:
054: try {
055: // in the current build environment, dav4j.properties will most likely be in the
056: // jar file for harmonise-dav-server, so look in here first
057: ClassLoader loader = Thread.currentThread()
058: .getContextClassLoader();
059: InputStream istream = loader.getResourceAsStream(resource); // will return null if not found
060: if (istream != null) {
061: properties.load(loader.getResourceAsStream(resource));
062: } else {
063: // If that fails, find the dav4j.properties file in the classpath
064: String classpath = System
065: .getProperty("java.class.path");
066: StringTokenizer paths = new StringTokenizer(classpath,
067: File.pathSeparator);
068: File propertiesFile = null;
069: boolean found = false;
070: while (!found && paths.hasMoreTokens()) {
071: String path = paths.nextToken();
072: propertiesFile = new File(path, resource);
073: found = propertiesFile.exists();
074: }
075: if (found) {
076: try {
077: properties.load(new FileInputStream(
078: propertiesFile));
079: } catch (Exception exc) {
080: exc.printStackTrace();
081: }
082: }
083: }
084: } catch (RuntimeException e) {
085: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
086: } catch (IOException e) {
087: m_logger.log(Level.WARNING, e.getLocalizedMessage(), e);
088: }
089: }
090:
091: /** Create a Resource identified by the given URL. The URL
092: * specifies the protocol to use for remote communication, and
093: * the host name. If the host name is "localhost", the communication
094: * is local (typical fat client).
095: * <p>
096: * This method does attempt to determine
097: * if the url is for a collection or a normal resource. At the
098: * moment it does this by asking the server. If it can not
099: * determine what type the resource is by asking the server
100: * (often because the resource doesn't yet exist at the server),
101: * then an exception is thrown.
102: * <p>
103: * The caller should be aware that just because we've
104: * asked the server if this URL is a collection/resouce, that doesn't
105: * insure that it wasn't deleted a moment or replaced a moment later
106: * by something different. Locking is required to do that and this
107: * method doesn't lock.
108: *
109: * @param url the identifier of the resource
110: * @return a Resource, ResourceCollection, or one of its subclasses.
111: * @exception java.io.IOException
112: */
113: public static Resource create(String url)
114: throws java.io.IOException {
115: return create(url, null);
116: }
117:
118: /** Create a Resource identified by the given URL. The URL
119: * specifies the protocol to use for remote communication, and
120: * the host name. If the host name is "localhost", the communication
121: * is local (typical fat client).
122: * <p>
123: * This method does attempt to determine
124: * if the url is for a collection or a normal resource. At the
125: * moment it does this by asking the server. If it can not
126: * determine what type the resource is by asking the server
127: * (often because the resource doesn't yet exist at the server),
128: * then an exception is thrown.
129: * <p>
130: * The caller should be aware that just because we've
131: * asked the server if this URL is a collection/resouce, that doesn't
132: * insure that it wasn't deleted a moment or replaced a moment later
133: * by something different. Locking is required to do that and this
134: * method doesn't lock.
135: *
136: * @param resourceURL the identifier of the resource
137: * @return a Resource, ResourceCollection, or one of its subclasses.
138: * @exception java.io.IOException
139: */
140: public static Resource create(String url,
141: TargetSelector targetSelector) throws java.io.IOException {
142: Resource resource = null;
143: try {
144: resource = new Resource(url, targetSelector);
145: if (resource.isCollection()) {
146: resource = null;
147: resource = new Collection(url, targetSelector);
148: }
149: } catch (WebDAVException exc) {
150: throw exc;
151: }
152: return resource;
153: }
154:
155: /** Create a Collection identified by the given URL. The URL
156: * specifies the protocol to use for remote communication, and
157: * the host name. If the host name is "localhost", the communication
158: * is local (typical fat client).
159: *
160: * @param resourceURL the identifier of the resource
161: * @return a Collection
162: * @exception com.ibm.WebDAVException
163: */
164: public static IRCollection createCollection(URL url,
165: TargetSelector targetSelector) throws WebDAVException {
166: IRCollection resource = null;
167: String protocol = null;
168: try {
169: protocol = url.getProtocol();
170:
171: // if the URL host is local, and the port was not specified,
172: // do local access
173: if (isLocalHost(url)) {
174: resource = new CollectionImpl(url, getRealPath(url),
175: targetSelector);
176: } else if (protocol.equals("http")) {
177: resource = new CollectionHTTPStub(url, targetSelector);
178: } else if (protocol.equals("rmi")) {
179: String name = url.toString();
180: name = name.substring(name.indexOf(":") + 1);
181: try {
182: resource = (IRCollection) Naming.lookup(name);
183: } catch (java.rmi.NotBoundException exc) {
184: throw new WebDAVException(
185: WebDAVStatus.SC_NOT_FOUND, "Not bound");
186: }
187: } else {
188: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
189: "WebDAV: Invalid communication protocol: "
190: + protocol);
191: }
192: } catch (WebDAVException exc) {
193: throw exc;
194: } catch (java.io.IOException exc) {
195: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
196: "WebDAV: Invalid communication protocol: "
197: + protocol);
198: }
199: return resource;
200: }
201:
202: /** Create a Resource identified by the given URL. The URL
203: * specifies the protocol to use for remote communication, and
204: * the host name. If the host name is "localhost", the communication
205: * is local (typical fat client).
206: *
207: * @param resourceURL the identifier of the resource
208: * @return a Resource
209: * @exception com.ibm.webdav.WebDAVException
210: */
211: public static IRResource createResource(URL url,
212: TargetSelector targetSelector) throws WebDAVException {
213: IRResource resource = null;
214: String protocol = null;
215: try {
216: protocol = url.getProtocol();
217:
218: // if the URL host is local, and the port was not specified,
219: // do local access
220: if (isLocalHost(url)) {
221: resource = new ResourceImpl(url, getRealPath(url),
222: targetSelector);
223: } else if (protocol.equals("http")) {
224: //@todo this ResourceHTTPStub won't work with Tomcat - needs work
225: resource = new ResourceHTTPStub(url, targetSelector);
226: } else if (protocol.equals("rmi")) {
227: String name = url.toString();
228: name = name.substring(name.indexOf(":") + 1);
229: try {
230: resource = (IRResource) Naming.lookup(name);
231: } catch (java.rmi.NotBoundException exc) {
232: throw new WebDAVException(
233: WebDAVStatus.SC_NOT_FOUND, "Not bound");
234: }
235: } else {
236: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
237: "WebDAV: Invalid communication protocol: "
238: + protocol);
239: }
240: } catch (WebDAVException exc) {
241: throw exc;
242: } catch (java.io.IOException exc) {
243: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST,
244: "WebDAV: Invalid communication protocol: "
245: + protocol);
246: }
247: return resource;
248: }
249:
250: /** Translate a URL into a local path name using the doc.root property
251: * from the webdav properties. The doc.root property in the dav4j.properties
252: * file is used for local access without a server.
253: *
254: * @param url the URL of the resource
255: * @return the translated pathname
256: */
257:
258: public static String getRealPath(URL url) {
259: String docRoot = properties.getProperty("doc.root",
260: defaultDocRoot);
261: String pathName = docRoot
262: + url.getFile().replace('/', File.separatorChar);
263: return pathName;
264: }
265:
266: /** Does the given URL refer to the local host? Used to determine if there doesn't
267: * need to be any RPC at all.
268: * @return true if the host in the URL is the local host, and a port was not specified
269: * @exception UnknownHostException
270: */
271: public static boolean isLocalHost(URL url) throws WebDAVException {
272: /*String thisHost = null;
273:
274: System.out.println("RESOURCE FACTORY: isLocalHost check for url " + url.getProtocol() + url.getHost() + url.getPort() + url.getPort() + url.getQuery());
275:
276: try {
277: thisHost = InetAddress.getLocalHost().getHostName();
278:
279: } catch (java.net.UnknownHostException exc) {
280: throw new WebDAVException(WebDAVStatus.SC_BAD_REQUEST, "Unknown Host");
281: }
282: return (url.getHost().equals("localhost") ||
283: url.getHost().equals(thisHost))*//*&& url.getPort() == -1*///;
284: return true;
285: }
286: }
|