1616import java .io .IOException ;
1717import java .lang .System .Logger ;
1818import java .lang .System .Logger .Level ;
19+ import java .net .HttpURLConnection ;
1920import java .net .MalformedURLException ;
2021import java .net .URI ;
2122import java .net .URISyntaxException ;
23+ import java .net .URL ;
2224import java .text .MessageFormat ;
2325import java .util .Collection ;
2426import java .util .Hashtable ;
2931import java .util .TreeSet ;
3032import java .util .Vector ;
3133
34+ import javax .net .ssl .HttpsURLConnection ;
3235import javax .xml .parsers .ParserConfigurationException ;
3336
3437import org .xml .sax .InputSource ;
3740
3841public class Catalog implements EntityResolver2 {
3942
43+ Logger logger = System .getLogger (Catalog .class .getName ());
44+
4045 private Map <String , String > systemCatalog ;
4146 private Map <String , String > publicCatalog ;
4247 private Map <String , String > uriCatalog ;
@@ -321,6 +326,9 @@ public InputSource resolveEntity(String name, String publicId, String baseURI, S
321326 try {
322327 URI uri = new URI (baseURI != null ? baseURI : "" ).resolve (systemId ).normalize ();
323328 if (uri .toURL ().getProtocol () != null ) {
329+ if (uri .toURL ().getProtocol ().startsWith ("http" )) {
330+ return resolveHttp (uri );
331+ }
324332 return new InputSource (uri .toURL ().openStream ());
325333 }
326334 return new InputSource (new FileInputStream (uri .toURL ().toString ()));
@@ -344,6 +352,27 @@ public InputSource resolveEntity(String name, String publicId, String baseURI, S
344352 return null ;
345353 }
346354
355+ private InputSource resolveHttp (URI uri ) throws MalformedURLException , IOException {
356+ MessageFormat mf = new MessageFormat (Messages .getString ("Catalog.3" ));
357+ logger .log (Level .WARNING , mf .format (new String [] { uri .toURL ().toString () }));
358+ URL url = uri .toURL ();
359+ try {
360+ HttpURLConnection con = url .getProtocol ().equals ("https" ) ? (HttpsURLConnection ) url .openConnection ()
361+ : (HttpURLConnection ) url .openConnection ();
362+ con .setReadTimeout (5000 );
363+ con .connect ();
364+ con .getResponseCode ();
365+ if (con .getResponseCode () != 200 ) {
366+ mf = new MessageFormat (Messages .getString ("Catalog.2" ));
367+ throw new IOException (mf .format (new String [] { con .getResponseCode () + "" , url .toString () }));
368+ }
369+ return new InputSource (con .getInputStream ());
370+ } catch (IOException e ) {
371+ mf = new MessageFormat (Messages .getString ("Catalog.1" ));
372+ throw new IOException (mf .format (new String [] { url .toString (), e .getMessage () }));
373+ }
374+ }
375+
347376 public String matchPublic (String publicId ) {
348377 if (publicId != null ) {
349378 if (publicId .startsWith ("urn:publicid:" )) {
@@ -478,7 +507,6 @@ public void parseDTD(String publicId) {
478507 }
479508 } catch (IOException | SAXException e ) {
480509 // do nothing
481- Logger logger = System .getLogger (Catalog .class .getName ());
482510 MessageFormat mf = new MessageFormat (Messages .getString ("Catalog.0" ));
483511 logger .log (Level .WARNING , mf .format (new String [] { publicId }));
484512 }
0 commit comments