1 22 package org.jboss.net.protocol.file; 23 24 import java.io.File ; 25 import java.io.FileNotFoundException ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.io.FileInputStream ; 30 import java.io.FileOutputStream ; 31 32 import java.net.URLConnection ; 33 import java.net.URL ; 34 import java.net.MalformedURLException ; 35 import java.net.URLDecoder ; 36 37 import java.security.Permission ; 38 import java.io.FilePermission ; 39 import java.io.BufferedInputStream ; 40 41 49 public class FileURLConnection extends URLConnection 50 { 51 static boolean decodeFilePaths = true; 52 static 53 { 54 String flag = System.getProperty("org.jboss.net.protocol.file.decodeFilePaths"); 55 if (flag != null) 56 decodeFilePaths = Boolean.valueOf(flag).booleanValue(); 57 } 58 59 protected File file; 60 61 public FileURLConnection(final URL url) throws MalformedURLException , IOException 62 { 63 super(url); 64 String path = url.getPath(); 65 if (decodeFilePaths) 66 path = URLDecoder.decode(path, "UTF-8"); 67 68 file = new File (path.replace('/', File.separatorChar).replace('|', ':')); 70 71 doOutput = false; 72 } 73 74 77 public File getFile() 78 { 79 return file; 80 } 81 82 87 public void connect() throws IOException 88 { 89 if (connected) 90 return; 91 92 if (!file.exists()) 93 { 94 throw new FileNotFoundException (file.getPath()); 95 } 96 97 connected = true; 98 } 99 100 public InputStream getInputStream() throws IOException 101 { 102 if (!connected) 103 connect(); 104 105 return new FileInputStream (file); 106 } 107 108 public OutputStream getOutputStream() throws IOException 109 { 110 if (!connected) 111 connect(); 112 SecurityManager sm = System.getSecurityManager(); 113 if( sm != null ) 114 { 115 FilePermission p = new FilePermission (file.getPath(), "write"); 117 sm.checkPermission(p); 118 } 119 return new FileOutputStream (file); 120 } 121 122 126 public String getHeaderField(final String name) 127 { 128 String 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 is = getInputStream(); 141 BufferedInputStream bis = new BufferedInputStream (is); 142 headerField = URLConnection.guessContentTypeFromStream(bis); 143 bis.close(); 144 } 145 catch(IOException e) 146 { 147 } 148 } 149 } 150 else if (name.equalsIgnoreCase("date")) 151 headerField = String.valueOf(file.lastModified()); 152 else 153 { 154 headerField = super.getHeaderField(name); 156 } 157 return headerField; 158 } 159 160 163 public Permission getPermission() throws IOException 164 { 165 return new FilePermission (file.getPath(), "read"); 166 } 167 168 171 public long getLastModified() 172 { 173 return file.lastModified(); 174 } 175 } 176 | Popular Tags |