001: /*
002: * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/naming/resources/DirContextURLConnection.java,v 1.12 2001/06/22 20:18:24 glenn Exp $
003: * $Revision: 1.12 $
004: * $Date: 2001/06/22 20:18:24 $
005: *
006: * ====================================================================
007: *
008: * The Apache Software License, Version 1.1
009: *
010: * Copyright (c) 1999 The Apache Software Foundation. All rights
011: * reserved.
012: *
013: * Redistribution and use in source and binary forms, with or without
014: * modification, are permitted provided that the following conditions
015: * are met:
016: *
017: * 1. Redistributions of source code must retain the above copyright
018: * notice, this list of conditions and the following disclaimer.
019: *
020: * 2. Redistributions in binary form must reproduce the above copyright
021: * notice, this list of conditions and the following disclaimer in
022: * the documentation and/or other materials provided with the
023: * distribution.
024: *
025: * 3. The end-user documentation included with the redistribution, if
026: * any, must include the following acknowlegement:
027: * "This product includes software developed by the
028: * Apache Software Foundation (http://www.apache.org/)."
029: * Alternately, this acknowlegement may appear in the software itself,
030: * if and wherever such third-party acknowlegements normally appear.
031: *
032: * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
033: * Foundation" must not be used to endorse or promote products derived
034: * from this software without prior written permission. For written
035: * permission, please contact apache@apache.org.
036: *
037: * 5. Products derived from this software may not be called "Apache"
038: * nor may "Apache" appear in their names without prior written
039: * permission of the Apache Group.
040: *
041: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
042: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
043: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
044: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
045: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
046: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
047: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
048: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
049: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
050: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
051: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
052: * SUCH DAMAGE.
053: * ====================================================================
054: *
055: * This software consists of voluntary contributions made by many
056: * individuals on behalf of the Apache Software Foundation. For more
057: * information on the Apache Software Foundation, please see
058: * <http://www.apache.org/>.
059: *
060: * [Additional notices, if required by prior licensing conditions]
061: *
062: */
063:
064: package org.apache.naming.resources;
065:
066: import java.net.URL;
067: import java.net.URLConnection;
068: import java.io.IOException;
069: import java.io.InputStream;
070: import java.io.FileNotFoundException;
071: import java.security.Permission;
072: import java.util.Date;
073: import java.util.Enumeration;
074: import java.util.Vector;
075: import javax.naming.NamingException;
076: import javax.naming.NamingEnumeration;
077: import javax.naming.NameClassPair;
078: import javax.naming.directory.DirContext;
079: import javax.naming.directory.Attribute;
080: import javax.naming.directory.Attributes;
081: import org.apache.naming.JndiPermission;
082: import org.apache.naming.resources.Resource;
083: import org.apache.naming.resources.ResourceAttributes;
084:
085: /**
086: * Connection to a JNDI directory context.
087: * <p/>
088: * Note: All the object attribute names are the WebDAV names, not the HTTP
089: * names, so this class overrides some methods from URLConnection to do the
090: * queries using the right names. Content handler is also not used; the
091: * content is directly returned.
092: *
093: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
094: * @version $Revision: 1.12 $
095: */
096: public class DirContextURLConnection extends URLConnection {
097:
098: // ----------------------------------------------------------- Constructors
099:
100: public DirContextURLConnection(DirContext context, URL url) {
101: super (url);
102: if (context == null)
103: throw new IllegalArgumentException(
104: "Directory context can't be null");
105: if (System.getSecurityManager() != null) {
106: this .permission = new JndiPermission(url.toString());
107: }
108: this .context = context;
109: }
110:
111: // ----------------------------------------------------- Instance Variables
112:
113: /**
114: * Directory context.
115: */
116: protected DirContext context;
117:
118: /**
119: * Associated resource.
120: */
121: protected Resource resource;
122:
123: /**
124: * Associated DirContext.
125: */
126: protected DirContext collection;
127:
128: /**
129: * Other unknown object.
130: */
131: protected Object object;
132:
133: /**
134: * Attributes.
135: */
136: protected Attributes attributes;
137:
138: /**
139: * Date.
140: */
141: protected long date;
142:
143: /**
144: * Permission
145: */
146: protected Permission permission;
147:
148: // ------------------------------------------------------------- Properties
149:
150: /**
151: * Connect to the DirContext, and retrive the bound object, as well as
152: * its attributes. If no object is bound with the name specified in the
153: * URL, then an IOException is thrown.
154: *
155: * @throws IOException Object not found
156: */
157: public void connect() throws IOException {
158:
159: if (!connected) {
160:
161: try {
162: date = System.currentTimeMillis();
163: String path = getURL().getFile();
164: if (context instanceof ProxyDirContext) {
165: ProxyDirContext proxyDirContext = (ProxyDirContext) context;
166: String hostName = proxyDirContext.getHostName();
167: String contextName = proxyDirContext
168: .getContextName();
169: if (hostName != null) {
170: if (!path.startsWith("/" + hostName + "/"))
171: return;
172: path = path.substring(hostName.length() + 1);
173: }
174: if (contextName != null) {
175: if (!path.startsWith(contextName + "/")) {
176: return;
177: } else {
178: path = path.substring(contextName.length());
179: }
180: }
181: }
182: object = context.lookup(path);
183: attributes = context.getAttributes(path);
184: if (object instanceof Resource)
185: resource = (Resource) object;
186: if (object instanceof DirContext)
187: collection = (DirContext) object;
188: } catch (NamingException e) {
189: // Object not found
190: }
191:
192: connected = true;
193:
194: }
195:
196: }
197:
198: /**
199: * Return the content length value.
200: */
201: public int getContentLength() {
202: return getHeaderFieldInt(ResourceAttributes.CONTENT_LENGTH, -1);
203: }
204:
205: /**
206: * Return the content type value.
207: */
208: public String getContentType() {
209: return getHeaderField(ResourceAttributes.CONTENT_TYPE);
210: }
211:
212: /**
213: * Return the last modified date.
214: */
215: public long getDate() {
216: return date;
217: }
218:
219: /**
220: * Return the last modified date.
221: */
222: public long getLastModified() {
223:
224: if (!connected) {
225: // Try to connect (silently)
226: try {
227: connect();
228: } catch (IOException e) {
229: }
230: }
231:
232: if (attributes == null)
233: return 0;
234:
235: Attribute lastModified = attributes
236: .get(ResourceAttributes.LAST_MODIFIED);
237: if (lastModified != null) {
238: try {
239: Date lmDate = (Date) lastModified.get();
240: return lmDate.getTime();
241: } catch (Exception e) {
242: }
243: }
244:
245: return 0;
246: }
247:
248: /**
249: * Returns the name of the specified header field.
250: */
251: public String getHeaderField(String name) {
252:
253: if (!connected) {
254: // Try to connect (silently)
255: try {
256: connect();
257: } catch (IOException e) {
258: }
259: }
260:
261: if (attributes == null)
262: return (null);
263:
264: Attribute attribute = attributes.get(name);
265: try {
266: return attribute.get().toString();
267: } catch (Exception e) {
268: // Shouldn't happen, unless the attribute has no value
269: }
270:
271: return (null);
272:
273: }
274:
275: /**
276: * Get object content.
277: */
278: public Object getContent() throws IOException {
279:
280: if (!connected)
281: connect();
282:
283: if (resource != null)
284: return getInputStream();
285: if (collection != null)
286: return collection;
287: if (object != null)
288: return object;
289:
290: throw new FileNotFoundException();
291:
292: }
293:
294: /**
295: * Get object content.
296: */
297: public Object getContent(Class[] classes) throws IOException {
298:
299: Object object = getContent();
300:
301: for (int i = 0; i < classes.length; i++) {
302: if (classes[i].isInstance(object))
303: return object;
304: }
305:
306: return null;
307:
308: }
309:
310: /**
311: * Get input stream.
312: */
313: public InputStream getInputStream() throws IOException {
314:
315: if (!connected)
316: connect();
317:
318: if (resource == null) {
319: throw new FileNotFoundException();
320: } else {
321: // Reopen resource
322: try {
323: resource = (Resource) context
324: .lookup(getURL().getFile());
325: } catch (NamingException e) {
326: }
327: }
328:
329: return (resource.streamContent());
330:
331: }
332:
333: /**
334: * Get the Permission for this URL
335: */
336: public Permission getPermission() {
337:
338: return permission;
339: }
340:
341: // --------------------------------------------------------- Public Methods
342:
343: /**
344: * List children of this collection. The names given are relative to this
345: * URI's path. The full uri of the children is then : path + "/" + name.
346: */
347: public Enumeration list()
348: throws IOException {
349:
350: if (!connected) {
351: connect();
352: }
353:
354: if ((resource == null) && (collection == null)) {
355: throw new FileNotFoundException();
356: }
357:
358: Vector result = new Vector();
359:
360: if (collection != null) {
361: try {
362: NamingEnumeration enum = context.list(getURL().getFile());
363: while (enum.hasMoreElements()) {
364: NameClassPair ncp = (NameClassPair) enum.nextElement();
365: result.addElement(ncp.getName());
366: }
367: } catch (NamingException e) {
368: // Unexpected exception
369: throw new FileNotFoundException();
370: }
371: }
372:
373: return result.elements();
374:
375: }
376:
377: }
|