Example usage for java.net HttpURLConnection getHeaderFieldInt

List of usage examples for java.net HttpURLConnection getHeaderFieldInt

Introduction

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

Prototype

public int getHeaderFieldInt(String name, int Default) 

Source Link

Document

Returns the value of the named field parsed as a number.

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);/* w  ww . j  a va 2s .  co  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//w  w  w. jav  a  2s.  c om
 * @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:org.exoplatform.utils.image.CookieAwarePicassoDownloader.java

@Override
public Response load(Uri uri, int networkPolicy) throws IOException {
    // TODO use networkPolicy as in com.squareup.picasso.UrlConnectionDownloader
    // https://github.com/square/picasso/blob/picasso-parent-2.5.2/picasso/src/main/java/com/squareup/picasso/UrlConnectionDownloader.java
    HttpURLConnection connection = connection(uri);
    connection.setUseCaches(true);//from  ww w  .  ja  v  a 2s .co m

    int responseCode = connection.getResponseCode();
    if (responseCode >= 300) {
        connection.disconnect();
        throw new ResponseException(responseCode + " " + connection.getResponseMessage(), networkPolicy,
                responseCode);
    }

    long contentLength = connection.getHeaderFieldInt("Content-Length", -1);
    // boolean fromCache =
    // parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE));
    boolean fromCache = false;

    return new Response(connection.getInputStream(), fromCache, contentLength);
}

From source file:tree.love.providers.downloads.DownloadThread.java

private void parseRetryAfterHeaders(State state, HttpURLConnection conn) {
    state.mRetryAfter = conn.getHeaderFieldInt("Retry-After", -1);
    if (state.mRetryAfter < 0) {
        state.mRetryAfter = 0;/*from w  w w.  jav a 2 s. c  o m*/
    } else {
        if (state.mRetryAfter < Constants.MIN_RETRY_AFTER) {
            state.mRetryAfter = Constants.MIN_RETRY_AFTER;
        } else if (state.mRetryAfter > Constants.MAX_RETRY_AFTER) {
            state.mRetryAfter = Constants.MAX_RETRY_AFTER;
        }
        state.mRetryAfter += Helpers.sRandom.nextInt(Constants.MIN_RETRY_AFTER + 1);
        state.mRetryAfter *= 1000;
    }
}

From source file:org.exoplatform.utils.image.ExoPicassoDownloader.java

@Override
public Response load(Uri uri, int networkPolicy) throws IOException {
    // TODO use networkPolicy as in com.squareup.picasso.UrlConnectionDownloader
    // https://github.com/square/picasso/blob/picasso-parent-2.5.2/picasso/src/main/java/com/squareup/picasso/UrlConnectionDownloader.java
    HttpURLConnection connection = connection(uri);
    connection.setInstanceFollowRedirects(true);
    connection.setUseCaches(true);/*from w  w w . java  2 s  .  c om*/

    int responseCode = connection.getResponseCode();
    // Handle HTTP redirections that are not managed by HttpURLConnection
    // automatically, e.g. HTTP -> HTTPS
    // TODO consider using OkHttp instead
    if (responseCode >= 300 && responseCode < 400) {
        String location = connection.getHeaderField("Location");
        connection.disconnect();
        connection = connection(Uri.parse(location));
        connection.setInstanceFollowRedirects(true);
        connection.setUseCaches(true);
        responseCode = connection.getResponseCode();
    }
    // Either the original or the new request have failed -> error
    if (responseCode >= 300) {
        connection.disconnect();
        throw new ResponseException(responseCode + " " + connection.getResponseMessage(), networkPolicy,
                responseCode);
    }

    long contentLength = connection.getHeaderFieldInt("Content-Length", -1);
    // boolean fromCache =
    // parseResponseSourceHeader(connection.getHeaderField(RESPONSE_SOURCE));
    boolean fromCache = false;

    return new Response(connection.getInputStream(), fromCache, contentLength);
}

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

/**
 * Preforms the OAI request//from w w  w  .  j  a  v a  2 s.  c  om
 *
 * @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 w w. j a v  a2 s .  c om*/
 * 
 * @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.oclc.oai.harvester2.verb.HarvesterVerb.java

/**
 * Preforms the OAI request, recovering from typical XML error
 *
 * @author nfreire Nuno Freire / Gilberto Pedrosa
 * @param requestURL/* w  w  w  .  j  a va2  s.c o 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
 * /*from ww  w  .  j  a  v  a 2  s  . co 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();
}

From source file:com.google.zxing.web.DecodeServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String imageURIString = request.getParameter("u");
    if (imageURIString == null || imageURIString.isEmpty()) {
        log.info("URI was empty");
        response.sendRedirect("badurl.jspx");
        return;//from www.  j a  v a 2  s.  com
    }

    imageURIString = imageURIString.trim();
    for (CharSequence substring : blockedURLSubstrings) {
        if (imageURIString.contains(substring)) {
            log.info("Disallowed URI " + imageURIString);
            response.sendRedirect("badurl.jspx");
            return;
        }
    }

    URI imageURI;
    try {
        imageURI = new URI(imageURIString);
        // Assume http: if not specified
        if (imageURI.getScheme() == null) {
            imageURI = new URI("http://" + imageURIString);
        }
    } catch (URISyntaxException urise) {
        log.info("URI " + imageURIString + " was not valid: " + urise);
        response.sendRedirect("badurl.jspx");
        return;
    }

    // Shortcut for data URI
    if ("data".equals(imageURI.getScheme())) {
        try {
            BufferedImage image = ImageReader.readDataURIImage(imageURI);
            processImage(image, request, response);
        } catch (IOException ioe) {
            log.info(ioe.toString());
            response.sendRedirect("badurl.jspx");
        }
        return;
    }

    URL imageURL;
    try {
        imageURL = imageURI.toURL();
    } catch (MalformedURLException ignored) {
        log.info("URI was not valid: " + imageURIString);
        response.sendRedirect("badurl.jspx");
        return;
    }

    String protocol = imageURL.getProtocol();
    if (!"http".equalsIgnoreCase(protocol) && !"https".equalsIgnoreCase(protocol)) {
        log.info("URI was not valid: " + imageURIString);
        response.sendRedirect("badurl.jspx");
        return;
    }

    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) imageURL.openConnection();
    } catch (IllegalArgumentException ignored) {
        log.info("URI could not be opened: " + imageURL);
        response.sendRedirect("badurl.jspx");
        return;
    }

    connection.setAllowUserInteraction(false);
    connection.setReadTimeout(5000);
    connection.setConnectTimeout(5000);
    connection.setRequestProperty(HttpHeaders.USER_AGENT, "zxing.org");
    connection.setRequestProperty(HttpHeaders.CONNECTION, "close");

    try {
        connection.connect();
    } catch (IOException ioe) {
        // Encompasses lots of stuff, including
        //  java.net.SocketException, java.net.UnknownHostException,
        //  javax.net.ssl.SSLPeerUnverifiedException,
        //  org.apache.http.NoHttpResponseException,
        //  org.apache.http.client.ClientProtocolException,
        log.info(ioe.toString());
        response.sendRedirect("badurl.jspx");
        return;
    }

    try (InputStream is = connection.getInputStream()) {
        try {
            if (connection.getResponseCode() != HttpServletResponse.SC_OK) {
                log.info("Unsuccessful return code: " + connection.getResponseCode());
                response.sendRedirect("badurl.jspx");
                return;
            }
            if (connection.getHeaderFieldInt(HttpHeaders.CONTENT_LENGTH, 0) > MAX_IMAGE_SIZE) {
                log.info("Too large");
                response.sendRedirect("badimage.jspx");
                return;
            }

            log.info("Decoding " + imageURL);
            processStream(is, request, response);
        } finally {
            consumeRemainder(is);
        }
    } catch (IOException ioe) {
        log.info(ioe.toString());
        response.sendRedirect("badurl.jspx");
    } finally {
        connection.disconnect();
    }

}