1 16 17 package org.apache.jetspeed.cache.disk; 18 19 import java.io.File ; 21 import java.net.MalformedURLException ; 22 import java.net.URL ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 26 import org.apache.turbine.services.servlet.TurbineServlet; 28 29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService; 31 import org.apache.jetspeed.services.logging.JetspeedLogger; 32 import org.apache.jetspeed.services.resources.JetspeedResources; 33 import org.apache.jetspeed.util.URIEncoder; 34 35 62 public class DiskCacheUtils { 63 64 69 public final static String [] VALID_PROTOCOLS = { "http", "ftp" }; 70 71 74 private static Vector localprotocols = JetspeedResources.getVector("diskcache.localprotocols"); 75 76 79 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(DiskCacheUtils.class.getName()); 80 81 private static String hostName = "localhost"; 82 static { 83 try { 84 hostName = java.net.InetAddress.getLocalHost().getHostName(); 85 } catch (Throwable t) {} 86 if (localprotocols.size()==0) { localprotocols.add("file"); 88 } 89 } 90 91 96 public static String getVirtual( String url ) { 97 98 100 int begin = 0; 101 102 if ( url.indexOf(":/") != -1 ) { 103 begin = url.indexOf( ":/" ) + 2; 104 } 105 106 if ( begin > 0 ) { 107 url = url.substring( begin, url.length() ); 108 } 109 110 url = url.substring( url.indexOf("/"), url.length() ); 111 112 113 return url; 114 } 115 116 121 public static String getLocalURL( String virtual ) { 122 123 if ( virtual != null && 125 isLocal( virtual ) && 126 virtual.indexOf("/") != 0 ) { 127 return virtual; 128 } 129 130 if ( isVirtual( virtual ) == false ) { 131 throw new IllegalArgumentException ( "The URL specifies is not a virtual URL: " + virtual ); 132 } 133 134 String url = TurbineServlet.getResource( virtual ).toString(); 135 136 return url; 137 } 138 139 144 public static boolean isVirtual( String url ) { 145 146 if ( url.indexOf( "/" ) == 0 ) { 147 return true; 148 } 149 150 return false; 151 } 152 153 156 public static boolean isLocal( String url ) { 157 158 159 165 166 if ( url != null && url.length() > 1 ) { 167 168 if ( ( url.indexOf( ":/" ) == -1 ) ) { 169 return true; 171 } 172 173 174 181 if ( ( url.indexOf( "http://localhost" ) == 0 ) || 182 ( url.indexOf( "http://127.0.0.1" ) == 0 ) || 183 ( url.indexOf( "http://" + hostName ) == 0 ) || 184 185 190 193 ( false ) 194 ) { 195 return true; 196 } 197 198 199 if (localprotocols!=null) { 200 Enumeration en = localprotocols.elements(); 201 while(en.hasMoreElements()) { 202 String protocol = (String )en.nextElement()+":"; 203 if ( url.indexOf(protocol) != -1 ) 204 { 205 return true; 206 } 207 } 208 } 209 210 } 211 return false; 212 } 213 214 217 public static boolean isRemote( String url ) { 218 return ! isLocal( url ); 219 } 220 221 225 public static boolean isCached( DiskCache instance, 226 String url ) { 227 228 232 233 return instance.isCached(url); 235 } 236 237 240 public static boolean isCached( String url ) { 241 return isCached( JetspeedDiskCache.getInstance(), url ); 242 } 243 244 247 public static boolean isCacheable( String url ) { 248 249 for (int i = 0; i < VALID_PROTOCOLS.length;++i) { 250 251 String uri = VALID_PROTOCOLS[i] + ":/"; 252 253 if (url.length() >= uri.length() && 254 url.substring(0, uri.length() ).equals( uri ) ) { 255 return isRemote( url ); } 257 258 } 259 return false; 260 } 261 262 267 public static File getFile( DiskCache instance, 268 String url ) { 269 270 String file = URIEncoder.encode( url ); 271 272 file = instance.getRoot() + "/" + file; 273 274 return new File ( file ); 275 276 } 277 278 282 public static String getFileURL( DiskCache instance, 283 String url ) { 284 285 URL fileURL = null; 286 287 try { 288 fileURL = DiskCacheUtils.getFile( instance, url ).toURL(); 289 } catch (MalformedURLException e) { 290 logger.error("Exception getting URL", e); 292 return null; 293 } 294 295 return fileURL.toString(); 296 297 } 298 299 } 300 301 | Popular Tags |