KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > naming > handler > jndi > DirContextURLConnection


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.naming.handler.jndi;
18
19 import java.io.ByteArrayInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.net.URL;
24 import java.net.URLConnection;
25 import java.security.Permission;
26 import java.util.Enumeration;
27 import java.util.Vector;
28
29 import javax.naming.NameClassPair;
30 import javax.naming.NamingEnumeration;
31 import javax.naming.NamingException;
32 import javax.naming.directory.Attribute;
33 import javax.naming.directory.Attributes;
34 import javax.naming.directory.DirContext;
35
36 import org.apache.naming.core.JndiPermission;
37 import org.apache.naming.util.AttributeHelper;
38 // import org.apache.naming.resources.Resource;
39
// import org.apache.naming.resources.ResourceAttributes;
40

41 /**
42  * Connection to a JNDI directory context.
43  * <p/>
44  * Note: All the object attribute names are the WebDAV names, not the HTTP
45  * names, so this class overrides some methods from URLConnection to do the
46  * queries using the right names. Content handler is also not used; the
47  * content is directly returned.
48  *
49  * @author <a HREF="mailto:remm@apache.org">Remy Maucherat</a>
50  * @author Costin Manolache
51  */

52 public class DirContextURLConnection
53     extends URLConnection
54 {
55     
56     
57     // ----------------------------------------------------------- Constructors
58

59     /**
60      * @param context The base context for the dynamic resources.
61      * For regular webapps, it should be a thread-bound context, probably
62      * a branch under java:
63      * For top-level apps, it can be either the InitialContext or a branch
64      * that is used for all content.
65      *
66      * The choice of the context affects the base of the URLs.
67      */

68     public DirContextURLConnection(DirContext context, URL url) {
69         super(url);
70         if (context == null)
71             throw new IllegalArgumentException
72                 ("Directory context can't be null");
73         if (System.getSecurityManager() != null) {
74             this.permission = new JndiPermission(url.toString());
75     }
76         this.context = context;
77     }
78     
79     
80     // ----------------------------------------------------- Instance Variables
81

82     
83     /**
84      * Directory context.
85      */

86     protected DirContext context;
87     
88     
89     /**
90      * Associated resource.
91      */

92     protected Object resource;
93     
94     
95     /**
96      * Associated DirContext.
97      */

98     protected DirContext collection;
99     
100     
101     /**
102      * Other unknown object.
103      */

104     protected Object object;
105     
106     
107     /**
108      * Attributes.
109      */

110     protected Attributes attributes;
111     
112     
113     /**
114      * Date.
115      */

116     protected long date;
117     
118     
119     /**
120      * Permission
121      */

122     protected Permission permission;
123
124
125     // ------------------------------------------------------------- Properties
126

127     
128     /**
129      * Connect to the DirContext, and retrive the bound object, as well as
130      * its attributes. If no object is bound with the name specified in the
131      * URL, then an IOException is thrown.
132      *
133      * @throws IOException Object not found
134      */

135     public void connect()
136         throws IOException {
137         
138         if (!connected) {
139             
140             try {
141                 // TODO: What is this ??? (costin)
142
date = System.currentTimeMillis();
143                 String path = getURL().getFile();
144
145                 /* This deals with a strange case, where the
146                    name is prefixed by hostname and contextname.
147
148                    A webapp should never use this - all resources
149                    are local ( or a different mean should be used to
150                    locate external res ).
151
152                    The top-level handler must use the hostname + context to
153                    locate files in a particular context.
154                    
155                   if (context instanceof ProxyDirContext) {
156                     ProxyDirContext proxyDirContext =
157                         (ProxyDirContext) context;
158                     String hostName = proxyDirContext.getHostName();
159                     String contextName = proxyDirContext.getContextName();
160                     if (hostName != null) {
161                         if (!path.startsWith("/" + hostName + "/"))
162                             return;
163                         path = path.substring(hostName.length()+ 1);
164                     }
165                     if (contextName != null) {
166                         if (!path.startsWith(contextName + "/")) {
167                             return;
168                         } else {
169                             path = path.substring(contextName.length());
170                         }
171                     }
172                 }
173                 */

174                 object = context.lookup(path);
175                 attributes = context.getAttributes(path);
176                 // if (object instanceof Resource)
177
// resource = (Resource) object;
178
if (object instanceof DirContext)
179                     collection = (DirContext) object;
180                 else
181                     resource=object;
182             } catch (NamingException e) {
183                 // Object not found
184
}
185             
186             connected = true;
187             
188         }
189         
190     }
191     
192     
193     /**
194      * Return the content length value.
195      */

196     public int getContentLength() {
197         if (!connected) {
198             // Try to connect (silently)
199
try {
200                 connect();
201             } catch (IOException e) {
202             }
203         }
204         
205         if (attributes == null)
206             return (-1);
207
208         return (int)AttributeHelper.getContentLength( attributes );
209     }
210
211     /**
212      * Return the content type value.
213      */

214     public String getContentType() {
215         if (!connected) {
216             // Try to connect (silently)
217
try {
218                 connect();
219             } catch (IOException e) {
220             }
221         }
222         
223         if (attributes == null)
224             return null;
225         
226         return AttributeHelper.getContentType(attributes);
227     }
228     
229     
230     /**
231      * Return the last modified date.
232      * TODO: it's broken
233      */

234     public long getDate() {
235         return date;
236     }
237     
238     
239     /**
240      * Return the last modified date.
241      */

242     public long getLastModified() {
243
244         if (!connected) {
245             // Try to connect (silently)
246
try {
247                 connect();
248             } catch (IOException e) {
249             }
250         }
251
252         if (attributes == null)
253             return 0;
254
255         return AttributeHelper.getLastModified( attributes );
256     }
257     
258     
259     /**
260      * Returns the name of the specified header field.
261      */

262     public String getHeaderField(String name) {
263
264         if (!connected) {
265             // Try to connect (silently)
266
try {
267                 connect();
268             } catch (IOException e) {
269             }
270         }
271         
272         if (attributes == null)
273             return (null);
274
275         Attribute attribute = attributes.get(name);
276         try {
277             return attribute.get().toString();
278         } catch (Exception e) {
279             // Shouldn't happen, unless the attribute has no value
280
}
281
282         return (null);
283         
284     }
285     
286     
287     /**
288      * Get object content.
289      */

290     public Object getContent()
291         throws IOException {
292         
293         if (!connected)
294             connect();
295         
296         if (resource != null)
297             return getInputStream();
298         if (collection != null)
299             return collection;
300         if (object != null)
301             return object;
302         
303         throw new FileNotFoundException();
304         
305     }
306     
307     
308     /**
309      * Get object content.
310      */

311     public Object getContent(Class[] classes)
312         throws IOException {
313         
314         Object object = getContent();
315         
316         for (int i = 0; i < classes.length; i++) {
317             if (classes[i].isInstance(object))
318                 return object;
319         }
320         
321         return null;
322         
323     }
324     
325     
326     /**
327      * Get input stream.
328      */

329     public InputStream getInputStream()
330         throws IOException {
331         
332         if (!connected)
333             connect();
334         
335         if (resource == null) {
336             throw new FileNotFoundException();
337         } else {
338             // Reopen resource
339
try {
340                 resource = context.lookup(getURL().getFile());
341             } catch (NamingException e) {
342             }
343         }
344         
345         // return (resource.streamContent());
346
return getInputStream(resource);
347         
348     }
349
350     /** Try to extract content from a resource found in the directory
351      * Code from Resource and ProxyContext.
352      */

353     public static InputStream getInputStream( Object resource ) {
354         if( resource instanceof InputStream )
355             return (InputStream) resource;
356
357         // Found in: ProxyDirContext.lookup ( strange, only in one ).
358
return new ByteArrayInputStream(resource.toString().getBytes());
359     }
360     
361     /**
362      * Get the Permission for this URL
363      */

364     public Permission getPermission() {
365
366         return permission;
367     }
368
369
370     // --------------------------------------------------------- Public Methods
371

372     
373     /**
374      * List children of this collection. The names given are relative to this
375      * URI's path. The full uri of the children is then : path + "/" + name.
376      */

377     public Enumeration list()
378         throws IOException {
379         
380         if (!connected) {
381             connect();
382         }
383         
384         if ((resource == null) && (collection == null)) {
385             throw new FileNotFoundException();
386         }
387         
388         Vector result = new Vector();
389         
390         if (collection != null) {
391             try {
392                 NamingEnumeration enum = context.list(getURL().getFile());
393                 while (enum.hasMoreElements()) {
394                     NameClassPair ncp = (NameClassPair) enum.nextElement();
395                     result.addElement(ncp.getName());
396                 }
397             } catch (NamingException e) {
398                 // Unexpected exception
399
throw new FileNotFoundException();
400             }
401         }
402         
403         return result.elements();
404         
405     }
406     
407     
408 }
409
Popular Tags