001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.naming.resources;
019:
020: import java.net.URL;
021: import java.net.URLConnection;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.FileNotFoundException;
025: import java.security.Permission;
026: import java.util.Date;
027: import java.util.Enumeration;
028: import java.util.Vector;
029: import javax.naming.NamingException;
030: import javax.naming.NamingEnumeration;
031: import javax.naming.NameClassPair;
032: import javax.naming.directory.DirContext;
033: import javax.naming.directory.Attribute;
034: import javax.naming.directory.Attributes;
035: import org.apache.naming.JndiPermission;
036: import org.apache.naming.resources.Resource;
037: import org.apache.naming.resources.ResourceAttributes;
038:
039: /**
040: * Connection to a JNDI directory context.
041: * <p/>
042: * Note: All the object attribute names are the WebDAV names, not the HTTP
043: * names, so this class overrides some methods from URLConnection to do the
044: * queries using the right names. Content handler is also not used; the
045: * content is directly returned.
046: *
047: * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
048: * @version $Revision: 505593 $
049: */
050: public class DirContextURLConnection extends URLConnection {
051:
052: // ----------------------------------------------------------- Constructors
053:
054: public DirContextURLConnection(DirContext context, URL url) {
055: super (url);
056: if (context == null)
057: throw new IllegalArgumentException(
058: "Directory context can't be null");
059: if (org.apache.naming.Constants.IS_SECURITY_ENABLED) {
060: this .permission = new JndiPermission(url.toString());
061: }
062: this .context = context;
063: }
064:
065: // ----------------------------------------------------- Instance Variables
066:
067: /**
068: * Directory context.
069: */
070: protected DirContext context;
071:
072: /**
073: * Associated resource.
074: */
075: protected Resource resource;
076:
077: /**
078: * Associated DirContext.
079: */
080: protected DirContext collection;
081:
082: /**
083: * Other unknown object.
084: */
085: protected Object object;
086:
087: /**
088: * Attributes.
089: */
090: protected Attributes attributes;
091:
092: /**
093: * Date.
094: */
095: protected long date;
096:
097: /**
098: * Permission
099: */
100: protected Permission permission;
101:
102: // ------------------------------------------------------------- Properties
103:
104: /**
105: * Connect to the DirContext, and retrive the bound object, as well as
106: * its attributes. If no object is bound with the name specified in the
107: * URL, then an IOException is thrown.
108: *
109: * @throws IOException Object not found
110: */
111: public void connect() throws IOException {
112:
113: if (!connected) {
114:
115: try {
116: date = System.currentTimeMillis();
117: String path = getURL().getFile();
118: if (context instanceof ProxyDirContext) {
119: ProxyDirContext proxyDirContext = (ProxyDirContext) context;
120: String hostName = proxyDirContext.getHostName();
121: String contextName = proxyDirContext
122: .getContextName();
123: if (hostName != null) {
124: if (!path.startsWith("/" + hostName + "/"))
125: return;
126: path = path.substring(hostName.length() + 1);
127: }
128: if (contextName != null) {
129: if (!path.startsWith(contextName + "/")) {
130: return;
131: } else {
132: path = path.substring(contextName.length());
133: }
134: }
135: }
136: object = context.lookup(path);
137: attributes = context.getAttributes(path);
138: if (object instanceof Resource)
139: resource = (Resource) object;
140: if (object instanceof DirContext)
141: collection = (DirContext) object;
142: } catch (NamingException e) {
143: // Object not found
144: }
145:
146: connected = true;
147:
148: }
149:
150: }
151:
152: /**
153: * Return the content length value.
154: */
155: public int getContentLength() {
156: return getHeaderFieldInt(ResourceAttributes.CONTENT_LENGTH, -1);
157: }
158:
159: /**
160: * Return the content type value.
161: */
162: public String getContentType() {
163: return getHeaderField(ResourceAttributes.CONTENT_TYPE);
164: }
165:
166: /**
167: * Return the last modified date.
168: */
169: public long getDate() {
170: return date;
171: }
172:
173: /**
174: * Return the last modified date.
175: */
176: public long getLastModified() {
177:
178: if (!connected) {
179: // Try to connect (silently)
180: try {
181: connect();
182: } catch (IOException e) {
183: }
184: }
185:
186: if (attributes == null)
187: return 0;
188:
189: Attribute lastModified = attributes
190: .get(ResourceAttributes.LAST_MODIFIED);
191: if (lastModified != null) {
192: try {
193: Date lmDate = (Date) lastModified.get();
194: return lmDate.getTime();
195: } catch (Exception e) {
196: }
197: }
198:
199: return 0;
200: }
201:
202: /**
203: * Returns the name of the specified header field.
204: */
205: public String getHeaderField(String name) {
206:
207: if (!connected) {
208: // Try to connect (silently)
209: try {
210: connect();
211: } catch (IOException e) {
212: }
213: }
214:
215: if (attributes == null)
216: return (null);
217:
218: Attribute attribute = attributes.get(name);
219: try {
220: return attribute.get().toString();
221: } catch (Exception e) {
222: // Shouldn't happen, unless the attribute has no value
223: }
224:
225: return (null);
226:
227: }
228:
229: /**
230: * Get object content.
231: */
232: public Object getContent() throws IOException {
233:
234: if (!connected)
235: connect();
236:
237: if (resource != null)
238: return getInputStream();
239: if (collection != null)
240: return collection;
241: if (object != null)
242: return object;
243:
244: throw new FileNotFoundException();
245:
246: }
247:
248: /**
249: * Get object content.
250: */
251: public Object getContent(Class[] classes) throws IOException {
252:
253: Object object = getContent();
254:
255: for (int i = 0; i < classes.length; i++) {
256: if (classes[i].isInstance(object))
257: return object;
258: }
259:
260: return null;
261:
262: }
263:
264: /**
265: * Get input stream.
266: */
267: public InputStream getInputStream() throws IOException {
268:
269: if (!connected)
270: connect();
271:
272: if (resource == null) {
273: throw new FileNotFoundException();
274: } else {
275: // Reopen resource
276: try {
277: resource = (Resource) context
278: .lookup(getURL().getFile());
279: } catch (NamingException e) {
280: }
281: }
282:
283: return (resource.streamContent());
284:
285: }
286:
287: /**
288: * Get the Permission for this URL
289: */
290: public Permission getPermission() {
291:
292: return permission;
293: }
294:
295: // --------------------------------------------------------- Public Methods
296:
297: /**
298: * List children of this collection. The names given are relative to this
299: * URI's path. The full uri of the children is then : path + "/" + name.
300: */
301: public Enumeration list() throws IOException {
302:
303: if (!connected) {
304: connect();
305: }
306:
307: if ((resource == null) && (collection == null)) {
308: throw new FileNotFoundException();
309: }
310:
311: Vector result = new Vector();
312:
313: if (collection != null) {
314: try {
315: NamingEnumeration enumeration = context.list(getURL()
316: .getFile());
317: while (enumeration.hasMoreElements()) {
318: NameClassPair ncp = (NameClassPair) enumeration
319: .nextElement();
320: result.addElement(ncp.getName());
321: }
322: } catch (NamingException e) {
323: // Unexpected exception
324: throw new FileNotFoundException();
325: }
326: }
327:
328: return result.elements();
329:
330: }
331:
332: }
|