2525import java .io .InputStream ;
2626import java .io .InputStreamReader ;
2727import java .io .Reader ;
28+ import java .net .HttpURLConnection ;
2829import java .net .URL ;
2930import java .net .URLConnection ;
3031import java .nio .file .Files ;
5960/**
6061 * Reads ontologies from OWL resources
6162 *
62- * @author paul
63+ * @author paul
6364 * @version $Id$
6465 */
6566public class OntologyLoader {
@@ -70,13 +71,13 @@ public class OntologyLoader {
7071 private static final String TMP_CACHE_SUFFIX = ".tmp" ;
7172
7273 /**
73- * @param url
74- * @param model
74+ * @param url
75+ * @param model
7576 * @return
7677 */
7778 public static Collection <OntologyResource > initialize ( String url , OntModel model ) {
7879
79- Collection <OntologyResource > result = new HashSet <OntologyResource >();
80+ Collection <OntologyResource > result = new HashSet <>();
8081
8182 ExtendedIterator <OntClass > classIt = model .listClasses ();
8283 int count = 0 ;
@@ -139,9 +140,9 @@ public static Collection<OntologyResource> initialize( String url, OntModel mode
139140 /**
140141 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
141142 *
142- * @param is
143- * @param url, used as a key
144- * @param spec
143+ * @param is
144+ * @param url, used as a key
145+ * @param spec
145146 * @return
146147 */
147148 public static OntModel loadMemoryModel ( InputStream is , String url , OntModelSpec spec ) {
@@ -154,7 +155,7 @@ public static OntModel loadMemoryModel( InputStream is, String url, OntModelSpec
154155 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available. Uses
155156 * OWL_MEM_TRANS_INF
156157 *
157- * @param url
158+ * @param url
158159 * @return
159160 */
160161 public static OntModel loadMemoryModel ( String url ) {
@@ -166,7 +167,7 @@ public static OntModel loadMemoryModel( String url ) {
166167 * OWL_MEM_TRANS_INF
167168 * If load from URL fails, attempt to load from disk cache under @cacheName.
168169 *
169- * @param url
170+ * @param url
170171 * @return
171172 */
172173 public static OntModel loadMemoryModel ( String url , String cacheName ) {
@@ -176,7 +177,7 @@ public static OntModel loadMemoryModel( String url, String cacheName ) {
176177 /**
177178 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
178179 *
179- * @param url
180+ * @param url
180181 * @return
181182 */
182183 public static OntModel loadMemoryModel ( String url , OntModelSpec spec ) {
@@ -187,9 +188,9 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec ) {
187188 * Load an ontology into memory. Use this type of model when fast access is critical and memory is available.
188189 * If load from URL fails, attempt to load from disk cache under @cacheName.
189190 *
190- * @param url
191- * @param spec e.g. OWL_MEM_TRANS_INF
192- * @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
191+ * @param url
192+ * @param spec e.g. OWL_MEM_TRANS_INF
193+ * @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
193194 * @return
194195 */
195196 public static OntModel loadMemoryModel ( String url , OntModelSpec spec , String cacheName ) {
@@ -205,15 +206,34 @@ public static OntModel loadMemoryModel( String url, OntModelSpec spec, String ca
205206 // help ensure mis-configured web servers aren't causing trouble.
206207 urlc .setRequestProperty ( "Accept" , "application/rdf+xml" );
207208
209+ try {
210+ HttpURLConnection c = ( HttpURLConnection ) urlc ;
211+ c .setInstanceFollowRedirects ( true );
212+ } catch ( ClassCastException e ) {
213+ // not via http, using a FileURLConnection.
214+ }
215+
208216 if ( tries > 0 ) {
209217 log .info ( "Retrying connecting to " + url + " [" + tries + "/" + MAX_CONNECTION_TRIES
210218 + " of max tries" );
211219 } else {
212- log .info ( "Connecting to ontology from " + url );
220+ log .info ( "Connecting to " + url );
213221 }
214222
215223 urlc .connect (); // Will error here on bad URL
216224
225+ if ( urlc instanceof HttpURLConnection ) {
226+ String newUrl = urlc .getHeaderField ( "Location" );
227+
228+ if ( StringUtils .isNotBlank ( newUrl ) ) {
229+ log .info ( "Redirect to " + newUrl );
230+ urlc = new URL ( newUrl ).openConnection ();
231+ // help ensure mis-configured web servers aren't causing trouble.
232+ urlc .setRequestProperty ( "Accept" , "application/rdf+xml" );
233+ urlc .connect ();
234+ }
235+ }
236+
217237 break ;
218238 } catch ( IOException e ) {
219239 // try to recover.
@@ -343,7 +363,7 @@ public static boolean deleteOldCache( String cacheName ) {
343363 /**
344364 * Get model that is entirely in memory with default OntModelSpec.OWL_MEM_RDFS_INF.
345365 *
346- * @param url
366+ * @param url
347367 * @return
348368 */
349369 static OntModel getMemoryModel ( String url ) {
@@ -353,8 +373,8 @@ static OntModel getMemoryModel( String url ) {
353373 /**
354374 * Get model that is entirely in memory.
355375 *
356- * @param url
357- * @param specification
376+ * @param url
377+ * @param specification
358378 * @return
359379 */
360380 static OntModel getMemoryModel ( String url , OntModelSpec specification ) {
@@ -370,7 +390,7 @@ static OntModel getMemoryModel( String url, OntModelSpec specification ) {
370390 }
371391
372392 /**
373- * @param name
393+ * @param name
374394 * @return
375395 */
376396 public static File getDiskCachePath ( String name ) {
@@ -379,18 +399,16 @@ public static File getDiskCachePath( String name ) {
379399 return null ;
380400 }
381401
382- if (!new File (ontologyDir ).exists ()) {
383- new File (ontologyDir ).mkdirs ();
402+ if ( !new File ( ontologyDir ).exists () ) {
403+ new File ( ontologyDir ).mkdirs ();
384404 }
385-
405+
386406 assert ontologyDir != null ;
387407
388408 String path = ontologyDir + File .separator + "ontology" + File .separator + name ;
389409
390410 File indexFile = new File ( path );
391-
392-
393-
411+
394412 return indexFile ;
395413 }
396414
0 commit comments