Example usage for java.net HttpURLConnection getHeaderFieldDate

List of usage examples for java.net HttpURLConnection getHeaderFieldDate

Introduction

In this page you can find the example usage for java.net HttpURLConnection getHeaderFieldDate.

Prototype

@SuppressWarnings("deprecation")
    public long getHeaderFieldDate(String name, long Default) 

Source Link

Usage

From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java

public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    String requestURL = "https://databank.ora.ox.ac.uk/oaipmh?verb=ListRecords&resumptionToken=20121206_TKMGW4A_SWT3NHV";

    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=ListRecords&resumptionToken=1354116062009:ELibM_external:eudml-article2:33753:37054::";
    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=GetRecord&identifier=urn:eudml.eu:ELibM_external:05152756&metadataPrefix=eudml-article2";
    //String requestURL = "C:/Users/Gilberto Pedrosa/Desktop/OAIHandler.xml";
    //FileInputStream fis = new FileInputStream(requestURL);
    //InputStream in = fis;
    logger.debug("requestURL=" + requestURL);
    DocumentBuilderFactory factory;
    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*  ww  w.java  2  s.c  o m*/
    Thread t = Thread.currentThread();
    DocumentBuilder builder = factory.newDocumentBuilder();
    HashMap builderMap = new HashMap();
    builderMap.put(t, builder);

    InputStream in;

    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    String xmlString = new String(inputBytes, "UTF-8");
    xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

    builder.parse(data);

    System.out.println("data = " + data);
}

From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java

/**
 * @param args/*from  ww w  . j a va2  s .  c  o  m*/
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 */
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException {
    String requestURL = "https://databank.ora.ox.ac.uk/oaipmh?verb=ListRecords&resumptionToken=20121206_TKMGW4A_SWT3NHV";

    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=ListRecords&resumptionToken=1354116062009:ELibM_external:eudml-article2:33753:37054::";
    //String requestURL = "http://bd2.inesc-id.pt:8080/repox2Eudml/OAIHandler?verb=GetRecord&identifier=urn:eudml.eu:ELibM_external:05152756&metadataPrefix=eudml-article2";
    //String requestURL = "C:/Users/Gilberto Pedrosa/Desktop/OAIHandler.xml";
    //FileInputStream fis = new FileInputStream(requestURL);
    //InputStream in = fis;
    logger.debug("requestURL=" + requestURL);
    DocumentBuilderFactory factory;
    factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    Thread t = Thread.currentThread();
    DocumentBuilder builder = factory.newDocumentBuilder();
    HashMap<Thread, DocumentBuilder> builderMap = new HashMap<Thread, DocumentBuilder>();
    builderMap.put(t, builder);

    InputStream in;

    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    String xmlString = new String(inputBytes, "UTF-8");
    xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

    builder.parse(data);

    System.out.println("data = " + data);
}

From source file:Main.java

public static boolean isUpdateAvailable(Context paramContext, String paramString) {
    try {/*from w w w.ja v  a2  s  .  c  o  m*/
        HttpURLConnection localHttpURLConnection = (HttpURLConnection) new URL(paramString).openConnection();
        long l1 = localHttpURLConnection.getHeaderFieldDate("Last-Modified", System.currentTimeMillis());
        localHttpURLConnection.disconnect();
        long l2 = PreferenceManager.getDefaultSharedPreferences(paramContext.getApplicationContext())
                .getLong(paramString, 0L);
        boolean toReturn = l1 > l2;

        return toReturn;
    } catch (Throwable localThrowable) {
        localThrowable.printStackTrace();
    }
    return false;
}

From source file:de.uni.stuttgart.informatik.ToureNPlaner.Net.Handler.SyncCoreLoader.java

private long getLastModifiedOnServer() throws IOException {
    HttpURLConnection con = null;
    long result = new Date().getTime();
    try {//from   w  w  w.j  a  va2 s  . c om
        URL url = new URL(coreURL + pathPrefix + corePrefix + coreLevel + coreSuffix);
        con = (HttpURLConnection) url.openConnection();

        con.setRequestMethod("HEAD");
        con.setDoInput(true);
        con.setAllowUserInteraction(false);
        result = con.getHeaderFieldDate("Last-Modified", result);
        Log.d(TAG, "Last modified is parsed " + new Date(result));

    } catch (MalformedURLException e) {
        e.printStackTrace();
        return result;
    } finally {
        if (con != null) {
            con.disconnect();
        }
    }
    return result;
}

From source file:com.googlecode.onevre.utils.ServerClassLoader.java

private URL getResourceURL(String name) throws IOException {
    URL url = cachedFiles.get(name);

    // If the cached jar is not found, find the class in a remote jar file
    if (url == null) {
        return getRemoteResource(name);
    }//w  w w.j ava 2  s  . c o m

    // If the cached jar is found, check if it is updated remotely
    File jar = cachedJars.get(url);
    if (System.currentTimeMillis() - jar.lastModified() > CACHE_TIMEOUT) {

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        addSslConnection(connection);
        connection.setRequestMethod("HEAD");
        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {

            long time = connection.getHeaderFieldDate("Last-Modified", System.currentTimeMillis());

            // If the remote jar has been updated,
            // redownload the jar and load it
            if (jar.lastModified() < time) {
                downloadJar(url);
            } else {
                jar.setLastModified(System.currentTimeMillis());
            }
        } else if (connection.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
            return getRemoteResource(name);
        } else {
            throw new IOException("Connection Error: " + connection.getResponseCode() + " "
                    + connection.getResponseMessage());
        }
    }

    return url;
}

From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java

/**
 * Preforms the OAI request/*from   w w  w  . j a v  a  2s  .  c o m*/
 *
 * @param requestURL
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
public void harvestOldOclcImplementation(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
        con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    InputSource data = new InputSource(in);

    Thread t = Thread.currentThread();
    DocumentBuilder builder = (DocumentBuilder) builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    doc = builder.parse(data);

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
}

From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java

/**
 * Performs the OAI request/*from  w ww. ja  va2s .  c o m*/
 * 
 * @param requestURL
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
public void harvestOldOclcImplementation(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
        con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    InputSource data = new InputSource(in);

    Thread t = Thread.currentThread();
    DocumentBuilder builder = builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    doc = builder.parse(data);

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
}

From source file:org.apache.roller.weblogger.util.Blacklist.java

/** Download the MT blacklist from the web to our uploads directory. */
private boolean downloadBlacklist() {

    boolean blacklist_updated = false;
    try {/*from w w w .ja  v a 2s  . com*/
        mLogger.debug("Attempting to download MT blacklist");

        URL url = new URL(blacklistURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // after spending way too much time debugging i've discovered
        // that the blacklist server is selective based on the User-Agent
        // header.  without this header set i always get a 403 response :(
        connection.setRequestProperty("User-Agent", "Mozilla/5.0");

        if (this.lastModified != null) {
            connection.setRequestProperty("If-Modified-Since", DateUtil.formatRfc822(this.lastModified));
        }

        int responseCode = connection.getResponseCode();

        mLogger.debug("HttpConnection response = " + responseCode);

        // did the connection return NotModified? If so, no need to parse
        if (responseCode == HttpURLConnection.HTTP_NOT_MODIFIED) {
            mLogger.debug("MT blacklist site says we are current");
            return false;
        }

        // did the connection return a LastModified header?
        long lastModifiedLong = connection.getHeaderFieldDate("Last-Modified", -1);

        // if the file is newer than our current then we need do update it
        if (responseCode == HttpURLConnection.HTTP_OK
                && (this.lastModified == null || this.lastModified.getTime() < lastModifiedLong)) {

            mLogger.debug("my last modified = " + this.lastModified.getTime());
            mLogger.debug("MT last modified = " + lastModifiedLong);

            // save the new blacklist
            InputStream instream = connection.getInputStream();

            String uploadDir = WebloggerConfig.getProperty("uploads.dir");
            String path = uploadDir + File.separator + blacklistFile;
            FileOutputStream outstream = new FileOutputStream(path);

            mLogger.debug("writing updated MT blacklist to " + path);

            // read from url and write to file
            byte[] buf = new byte[4096];
            int length = 0;
            while ((length = instream.read(buf)) > 0)
                outstream.write(buf, 0, length);

            outstream.close();
            instream.close();

            blacklist_updated = true;

            mLogger.debug("MT blacklist download completed.");

        } else {
            mLogger.debug("blacklist *NOT* saved, assuming we are current");
        }

    } catch (Exception e) {
        mLogger.error("error downloading blacklist", e);
    }

    return blacklist_updated;
}

From source file:org.oclc.oai.harvester2.verb.HarvesterVerb.java

/**
 * Preforms the OAI request, recovering from typical XML error
 *
 * @author nfreire Nuno Freire / Gilberto Pedrosa
 * @param requestURL//from w w w. ja  va 2 s.  co m
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
private void harvest(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    Thread t = Thread.currentThread();
    DocumentBuilder builder = (DocumentBuilder) builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    try {
        doc = builder.parse(data);
    } catch (SAXException e) {
        try {
            //Here we can try to recover the xml from known typical problems

            //Recover from invalid characters
            //we assume this is UTF-8...
            String xmlString = new String(inputBytes, "UTF-8");
            xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

            data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
            doc = builder.parse(data);
        } catch (Exception e2) {
            //the recovered version did not work either. Throw the original exception
            throw e;
        }
    } catch (IOException e3) {
        System.out.println("e = " + e3.getMessage());
    } catch (Exception e4) {
        System.out.println("e = " + e4.getMessage());
    }

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
    this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI();
}

From source file:org.oclc.oai.harvester.verb.HarvesterVerb.java

/**
 * Performs the OAI request, recovering from typical XML error
 * /*  w w  w  .  j a  va  2  s  .c o m*/
 * @author nfreire Nuno Freire / Gilberto Pedrosa
 * @param requestURL
 * @throws IOException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws TransformerException
 */
private void harvest(String requestURL)
        throws IOException, ParserConfigurationException, SAXException, TransformerException {
    this.requestURL = requestURL;
    logger.debug("requestURL=" + requestURL);
    InputStream in;
    URL url = new URL(requestURL);
    HttpURLConnection con;
    int responseCode;
    do {
        con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(30000);
        con.setReadTimeout(600000);

        if (con.getAllowUserInteraction()) {
            con.setRequestProperty("User-Agent", "OAIHarvester/2.0");
            con.setRequestProperty("Accept-Encoding", "compress, gzip, identify");
        }
        try {
            responseCode = con.getResponseCode();
            logger.debug("responseCode=" + responseCode);
        } catch (FileNotFoundException e) {
            // assume it's a 503 response
            logger.error(requestURL, e);
            responseCode = HttpURLConnection.HTTP_UNAVAILABLE;
        }

        if (responseCode == HttpURLConnection.HTTP_UNAVAILABLE) {
            long retrySeconds = con.getHeaderFieldInt("Retry-After", -1);
            if (retrySeconds == -1) {
                long now = (new Date()).getTime();
                long retryDate = con.getHeaderFieldDate("Retry-After", now);
                retrySeconds = retryDate - now;
            }
            if (retrySeconds == 0) { // Apparently, it's a bad URL
                throw new FileNotFoundException("Bad URL?");
            }
            logger.warn("Server response: Retry-After=" + retrySeconds);
            if (retrySeconds > 0) {
                try {
                    Thread.sleep(retrySeconds * 1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }
        }
    } while (responseCode == HttpURLConnection.HTTP_UNAVAILABLE);
    String contentEncoding = con.getHeaderField("Content-Encoding");
    logger.debug("contentEncoding=" + contentEncoding);
    if ("compress".equals(contentEncoding)) {
        ZipInputStream zis = new ZipInputStream(con.getInputStream());
        zis.getNextEntry();
        in = zis;
    } else if ("gzip".equals(contentEncoding)) {
        in = new GZIPInputStream(con.getInputStream());
    } else if ("deflate".equals(contentEncoding)) {
        in = new InflaterInputStream(con.getInputStream());
    } else {
        in = con.getInputStream();
    }

    byte[] inputBytes = IOUtils.toByteArray(in);
    InputSource data = new InputSource(new ByteArrayInputStream(inputBytes));

    Thread t = Thread.currentThread();
    DocumentBuilder builder = builderMap.get(t);
    if (builder == null) {
        builder = factory.newDocumentBuilder();
        builderMap.put(t, builder);
    }
    try {
        doc = builder.parse(data);
    } catch (SAXException e) {
        try {
            //Here we can try to recover the xml from known typical problems

            //Recover from invalid characters
            //we assume this is UTF-8...
            String xmlString = new String(inputBytes, "UTF-8");
            xmlString = XmlUtil.removeInvalidXMLCharacters(xmlString);

            data = new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8")));
            doc = builder.parse(data);
        } catch (Exception e2) {
            //the recovered version did not work either. Throw the original exception
            throw e;
        }
    } catch (IOException e3) {
        System.out.println("e = " + e3.getMessage());
    } catch (Exception e4) {
        System.out.println("e = " + e4.getMessage());
    }

    StringTokenizer tokenizer = new StringTokenizer(getSingleString("/*/@xsi:schemaLocation"), " ");
    StringBuffer sb = new StringBuffer();
    while (tokenizer.hasMoreTokens()) {
        if (sb.length() > 0)
            sb.append(" ");
        sb.append(tokenizer.nextToken());
    }
    this.schemaLocation = sb.toString();
    this.defaultNamespace = getDocument().getDocumentElement().getNamespaceURI();
}