001 package com.croftsoft.core.util.cache; 002 003 import java.io.*; 004 import java.net.*; 005 006 /********************************************************************* 007 * A ContentAccessor that accesses the content via a URL. 008 * 009 * @version 010 * 1999-04-24 011 * @author 012 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 013 *********************************************************************/ 014 015 public class URLContentAccessor 016 implements ContentAccessor, Serializable 017 ////////////////////////////////////////////////////////////////////// 018 ////////////////////////////////////////////////////////////////////// 019 { 020 021 private static final long serialVersionUID = 1L; 022 023 protected String urlName; 024 025 ////////////////////////////////////////////////////////////////////// 026 ////////////////////////////////////////////////////////////////////// 027 028 public URLContentAccessor ( String urlName ) 029 throws MalformedURLException 030 ////////////////////////////////////////////////////////////////////// 031 { 032 this.urlName = urlName; 033 034 035 // Test to see if valid URL. 036 new URL ( urlName ); 037 } 038 039 ////////////////////////////////////////////////////////////////////// 040 ////////////////////////////////////////////////////////////////////// 041 042 public String getURLName ( ) { return urlName; } 043 044 ////////////////////////////////////////////////////////////////////// 045 ////////////////////////////////////////////////////////////////////// 046 047 public InputStream getInputStream ( ) throws IOException 048 ////////////////////////////////////////////////////////////////////// 049 { 050 URL url = null; 051 052 try 053 { 054 url = new URL ( urlName ); 055 } 056 catch ( MalformedURLException ex ) 057 { 058 return null; 059 } 060 061 URLConnection urlConnection = url.openConnection ( ); 062 063 if ( urlConnection instanceof HttpURLConnection ) 064 { 065 HttpURLConnection httpURLConnection 066 = ( HttpURLConnection ) urlConnection; 067 068 if ( httpURLConnection.getResponseCode ( ) 069 != HttpURLConnection.HTTP_OK ) 070 { 071 return null; 072 } 073 } 074 075 return urlConnection.getInputStream ( ); 076 } 077 078 ////////////////////////////////////////////////////////////////////// 079 ////////////////////////////////////////////////////////////////////// 080 }