KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > protocol > file > FileURLConnection


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.net.protocol.file;
23
24 import java.io.File JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31
32 import java.net.URLConnection JavaDoc;
33 import java.net.URL JavaDoc;
34 import java.net.MalformedURLException JavaDoc;
35 import java.net.URLDecoder JavaDoc;
36
37 import java.security.Permission JavaDoc;
38 import java.io.FilePermission JavaDoc;
39 import java.io.BufferedInputStream JavaDoc;
40
41 /**
42  * Provides local file access via URL semantics, correctly returning
43  * the last modified time of the underlying file.
44  *
45  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
46  * @author <a HREF="mailto:scott.stark@jboss.org">Scott.Stark</a>
47  * @version $Revision: 1958 $
48  */

49 public class FileURLConnection extends URLConnection JavaDoc
50 {
51    static boolean decodeFilePaths = true;
52    static
53    {
54       String JavaDoc flag = System.getProperty("org.jboss.net.protocol.file.decodeFilePaths");
55       if (flag != null)
56          decodeFilePaths = Boolean.valueOf(flag).booleanValue();
57    }
58    
59    protected File JavaDoc file;
60
61    public FileURLConnection(final URL JavaDoc url) throws MalformedURLException JavaDoc, IOException JavaDoc
62    {
63       super(url);
64       String JavaDoc path = url.getPath();
65       if (decodeFilePaths)
66          path = URLDecoder.decode(path, "UTF-8");
67       
68       // Convert the url '/' to the os file separator
69
file = new File JavaDoc(path.replace('/', File.separatorChar).replace('|', ':'));
70
71       doOutput = false;
72    }
73
74    /**
75     * Returns the underlying file for this connection.
76     */

77    public File JavaDoc getFile()
78    {
79       return file;
80    }
81
82    /**
83     * Checks if the underlying file for this connection exists.
84     *
85     * @throws FileNotFoundException
86     */

87    public void connect() throws IOException JavaDoc
88    {
89       if (connected)
90          return;
91
92       if (!file.exists())
93       {
94          throw new FileNotFoundException JavaDoc(file.getPath());
95       }
96       
97       connected = true;
98    }
99
100    public InputStream JavaDoc getInputStream() throws IOException JavaDoc
101    {
102       if (!connected)
103          connect();
104
105       return new FileInputStream JavaDoc(file);
106    }
107
108    public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
109    {
110       if (!connected)
111          connect();
112       SecurityManager JavaDoc sm = System.getSecurityManager();
113       if( sm != null )
114       {
115          // Check for write access
116
FilePermission JavaDoc p = new FilePermission JavaDoc(file.getPath(), "write");
117          sm.checkPermission(p);
118       }
119       return new FileOutputStream JavaDoc(file);
120    }
121
122    /**
123     * Provides support for returning the value for the
124     * <tt>last-modified</tt> header.
125     */

126    public String JavaDoc getHeaderField(final String JavaDoc name)
127    {
128       String JavaDoc headerField = null;
129       if (name.equalsIgnoreCase("last-modified"))
130          headerField = String.valueOf(getLastModified());
131       else if (name.equalsIgnoreCase("content-length"))
132          headerField = String.valueOf(file.length());
133       else if (name.equalsIgnoreCase("content-type"))
134       {
135          headerField = getFileNameMap().getContentTypeFor(file.getName());
136          if( headerField == null )
137          {
138             try
139             {
140                InputStream JavaDoc is = getInputStream();
141                BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(is);
142                headerField = URLConnection.guessContentTypeFromStream(bis);
143                bis.close();
144             }
145             catch(IOException JavaDoc e)
146             {
147             }
148          }
149       }
150       else if (name.equalsIgnoreCase("date"))
151          headerField = String.valueOf(file.lastModified());
152       else
153       {
154          // This always returns null currently
155
headerField = super.getHeaderField(name);
156       }
157       return headerField;
158    }
159
160    /**
161     * Return a permission for reading of the file
162     */

163    public Permission JavaDoc getPermission() throws IOException JavaDoc
164    {
165       return new FilePermission JavaDoc(file.getPath(), "read");
166    }
167
168    /**
169     * Returns the last modified time of the underlying file.
170     */

171    public long getLastModified()
172    {
173       return file.lastModified();
174    }
175 }
176
Popular Tags