1/*2 * Copyright 2000-2001,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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.cache.disk;
1819//standard java stuff20import java.io.File;
21import java.net.MalformedURLException;
22import java.net.URL;
23import java.util.Enumeration;
24import java.util.Vector;
2526//turbine27import org.apache.turbine.services.servlet.TurbineServlet;
2829// Jetspeed classes30import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
31import org.apache.jetspeed.services.logging.JetspeedLogger;
32import org.apache.jetspeed.services.resources.JetspeedResources;
33import org.apache.jetspeed.util.URIEncoder;
3435/***36 <p>37 Misc utils for managing the disk cache.38 </p>3940 <p>41 This tries to separate URLs into three categories:42 <ul>43 <li>44 Virtual: URLs which are contructored such as: /test/test.xml45 </li>4647 <li>48 Local: URLs which are contructored such as: http://localhost/test/test.xml49 </li>5051 <li>52 Remote: URLs which are contructored such as: http://REMOTE.SERVER/test/test.xml53 </li>5455 </ul>56 </p>5758 @see DiskCache59 @author <A HREF="mailto:burton@apache.org">Kevin A. Burton</A>60 @version $Id: DiskCacheUtils.java,v 1.20 2004/02/23 02:45:29 jford Exp $61*/62publicclassDiskCacheUtils {
6364/***65 Used to determine if a given URL should be cached. This prevents people 66 from trying to cache documents that aren't supported. http and ftp should67 fit almost any situation.68 */69publicfinalstatic String[] VALID_PROTOCOLS = { "http", "ftp" };
7071/***72 Stores the protocols which sould be recognized as local73 */74privatestatic Vector localprotocols = JetspeedResources.getVector("diskcache.localprotocols");
7576/***77 * Static initialization of the logger for this class78 */79privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(DiskCacheUtils.class.getName());
8081privatestatic String hostName = "localhost";
82static {
83try {
84 hostName = java.net.InetAddress.getLocalHost().getHostName();
85 } catch (Throwable t) {}
86if (localprotocols.size()==0) { // default values, if key is not defined87 localprotocols.add("file");
88 }
89 }
9091/***92 Give an full url: http://www.cnn.com/test9394 just return the virutal portion: /test95 */96publicstatic String getVirtual( String url ) {
9798//strip off the begining of the URL if necessary:99100int begin = 0;
101102if ( url.indexOf(":/") != -1 ) {
103 begin = url.indexOf( ":/" ) + 2;
104 }
105106if ( begin > 0 ) {
107 url = url.substring( begin, url.length() );
108 }
109110 url = url.substring( url.indexOf("/"), url.length() );
111112113return url;
114 }
115116/***117 Given a virtual URL, resolve it to a local URL:118119 Ex: /test.xml -> http://localhost:80/test.xml120 */121publicstatic String getLocalURL( String virtual ) {
122123//if this virtual URL is actually a local, return it directly.124if ( virtual != null &&
125 isLocal( virtual ) &&
126 virtual.indexOf("/") != 0 ) {
127return virtual;
128 }
129130if ( isVirtual( virtual ) == false ) {
131thrownew IllegalArgumentException( "The URL specifies is not a virtual URL: " + virtual );
132 }
133134 String url = TurbineServlet.getResource( virtual ).toString();
135136return url;
137 }
138139/***140 Return true if this URL is virtual.141142 EX: /tmp/test.xml143 */144publicstaticboolean isVirtual( String url ) {
145146if ( url.indexOf( "/" ) == 0 ) {
147returntrue;
148 }
149150return false;
151 }
152153/***154 Return true if this URL is on the local server.155 */156publicstaticboolean isLocal( String url ) {
157158159/*160 If the URL is virtual, return true:161162 EX: /test/test.xml163164 */165166if ( url != null && url.length() > 1 ) {
167168if ( ( url.indexOf( ":/" ) == -1 ) ) {
169//this must be a local URL because it is virtual "/test/test.xml"170returntrue;
171 }
172173174/*175 ok... perform two more tests. if the URL is on the local server.. or on176 localhost then return true177178 EX: http://localhost179http://server.domain180 */181if ( ( url.indexOf( "http://localhost" ) == 0 ) ||
182 ( url.indexOf( "http://127.0.0.1" ) == 0 ) ||
183 ( url.indexOf( "http://" + hostName ) == 0 ) ||
184185// RL: using EngineContext doesn't work because the serverName186// and serverPort is a request-based information.187// We should either fix EngineContext to use a config property188// or remove it altogether and find something that always works !189190/* ( url.indexOf( EngineContext.getInstance().getServerScheme() +191 "://" + 192 EngineContext.getInstance().getServerName() ) == 0 ) */193 ( false )
194 ) {
195returntrue;
196 }
197198/* SH Testing local protocols also */199if (localprotocols!=null) {
200 Enumeration en = localprotocols.elements();
201while(en.hasMoreElements()) {
202 String protocol = (String)en.nextElement()+":";
203if ( url.indexOf(protocol) != -1 )
204 {
205returntrue;
206 }
207 }
208 }
209210 }
211return false;
212 }
213214/***215 Return true if this URL is NOT on the local server.216 */217publicstaticboolean isRemote( String url ) {
218return ! isLocal( url );
219 }
220221/***222 Return true if this url is in the cache.223 @see DiskCache#isCached( String )224 */225publicstaticboolean isCached( DiskCache instance,
226 String url ) {
227228/* if ( isLocal( url ) ) {229 //SGP: I think we should not cache local urls230 return false;231 }*/232233// return DiskCacheUtils.getFile( instance, url ).exists();234return instance.isCached(url);
235 }
236237/***238 @see DiskCacheUtils#isCached( DiskCache, String )239 */240publicstaticboolean isCached( String url ) {
241return isCached( JetspeedDiskCache.getInstance(), url );
242 }
243244/***245 Return true if the given URL should be cached or not.246 */247publicstaticboolean isCacheable( String url ) {
248249for (int i = 0; i < VALID_PROTOCOLS.length;++i) {
250251 String uri = VALID_PROTOCOLS[i] + ":/";
252253if (url.length() >= uri.length() &&
254 url.substring(0, uri.length() ).equals( uri ) ) {
255return isRemote( url ); //SGP was true256 }
257258 }
259return false;
260 }
261262/***263 Given a URL, determine what the filename would be within the cache. Note 264 that this doesn't return a URL just a path to where it would be stored 265 locally.266 */267publicstatic File getFile( DiskCache instance,
268 String url ) {
269270 String file = URIEncoder.encode( url );
271272 file = instance.getRoot() + "/" + file;
273274returnnew File( file );
275276 }
277278/***279 Given a url and an disk cache instance, determine what the correct URL for this280 cache entry for the remote URL would be.281 */282publicstatic String getFileURL( DiskCache instance,
283 String url ) {
284285 URL fileURL = null;
286287try {
288 fileURL = DiskCacheUtils.getFile( instance, url ).toURL();
289 } catch (MalformedURLException e) {
290// what can we do in this case ?291 logger.error("Exception getting URL", e);
292returnnull;
293 }
294295return fileURL.toString();
296297 }
298299 }
300