/*
 * Copyright (c) 2016 Network New Technologies Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.networknt.schema.uri;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.util.Collections;
import java.util.Set;

/**
 * A URIfetcher that uses {@link URL#openStream()} for fetching and assumes given {@link URI}s
 * are actualy {@link URL}s.
 */
public final class URLFetcher implements URIFetcher {
  // These supported schemes are defined in {@link #URL(String, String, int, String)}.
  // This fetcher also supports the {@link URL}s created with the {@link ClasspathURIFactory}.
  public static final Set<String> SUPPORTED_SCHEMES = Collections.unmodifiableSet(URLFactory.SUPPORTED_SCHEMES);

  /**
   * {@inheritDoc}
   */
  @Override
  public InputStream fetch(final URI uri) throws IOException {
    return uri.toURL().openStream();
  }
}

@@ -703,16 +703,51 @@
 Stream;%0A
+import java.net.HttpURLConnection;%0A
 import j
@@ -780,16 +780,47 @@
 et.URL;%0A
+import java.net.URLConnection;%0A
 import j
@@ -962,19 +962,16 @@
 nk URI%7Ds
-%0A *
  are act
@@ -1047,16 +1047,19 @@
 tcher %7B%0A
+%0A
+  
   // The
@@ -1137,16 +1137,18 @@
 ring)%7D.%0A
+  
   // Thi
@@ -1234,16 +1234,18 @@
 ctory%7D.%0A
+  
   public
@@ -1354,14 +1354,18 @@
 );%0A%0A
+  
   /**%0A
+  
    *
@@ -1383,14 +1383,18 @@
 oc%7D%0A
+  
    */%0A
+  
   @O
@@ -1401,16 +1401,18 @@
 verride%0A
+  
   public
@@ -1474,41 +1474,2028 @@
     
-return uri.toURL().openStream();%0A
+    URLConnection conn = uri.toURL().openConnection();%0A        return this.openConnectionCheckRedirects(conn);%0A    %7D%0A%0A    // https://www.cs.mun.ca/java-api-1.5/guide/deployment/deployment-guide/upgrade-guide/article-17.html%0A    private InputStream openConnectionCheckRedirects(URLConnection c) throws IOException %7B%0A        boolean redir;%0A        int redirects = 0;%0A        InputStream in = null;%0A        do %7B%0A            if (c instanceof HttpURLConnection) %7B%0A                ((HttpURLConnection) c).setInstanceFollowRedirects(false);%0A            %7D%0A            // We want to open the input stream before getting headers%0A            // because getHeaderField() et al swallow IOExceptions.%0A            in = c.getInputStream();%0A            redir = false;%0A            if (c instanceof HttpURLConnection) %7B%0A                HttpURLConnection http = (HttpURLConnection) c;%0A                int stat = http.getResponseCode();%0A                if (stat %3E= 300 && stat %3C= 307 && stat != 306%0A                    && stat != HttpURLConnection.HTTP_NOT_MODIFIED) %7B%0A                    URL base = http.getURL();%0A                    String loc = http.getHeaderField(%22Location%22);%0A                    URL target = null;%0A                    if (loc != null) %7B%0A                        target = new URL(base, loc);%0A                    %7D%0A                    http.disconnect();%0A                    // Redirection should be allowed only for HTTP and HTTPS%0A                    // and should be limited to 5 redirections at most.%0A                    if (target == null%0A                        %7C%7C !(target.getProtocol().equals(%22http%22)%0A                        %7C%7C target.getProtocol().equals(%22https%22))%0A                        %7C%7C redirects %3E= 5) %7B%0A                            throw new SecurityException(%22illegal URL redirect%22);%0A                    %7D%0A                    redir = true;%0A                    c = target.openConnection();%0A                    redirects++;%0A                %7D%0A            %7D%0A        %7D%0A        while (redir);%0A        return in;%0A  
   %7D%0A

Fix remote ref to follow redirects