/** * Call #wrapwrapInputAsReader(OWLOntologyLoaderConfiguration, String) with UTF-* as default * encoding. * * @param source ontology source * @param configuration loader configuration to use of the reader must be built form the input * IRI * @return A Reader wrapped in an Optional; if no Reader can be obtained, the result is * Optional.empty. @throws OWLOntologyInputSourceException if an IO related exception is * thrown. * @throws OWLOntologyInputSourceException if an IO related exception is thrown. */ public static Reader wrapInputAsReader(OWLOntologyDocumentSource source, OWLOntologyLoaderConfiguration configuration) throws OWLOntologyInputSourceException { return wrapInputAsReader(source, configuration, Charsets.UTF_8); }
/** * Select the available input source and, if it is not already a Reader, wrap it in a Reader. * This method removes the duplication of code required for each caller to figure out if a * reader or an inputstream is available. The returned Reader will be buffered. * * @param source ontology source * @param configuration loader configuration to use of the reader must be built form the input * IRI * @param encoding character encoding if a new Reader needs to be created. * @return A Reader for the input; if no Reader can be obtained, an * OWLOntologyInputSourceException is thrown. * @throws OWLOntologyInputSourceException if an IO related exception is thrown. */ public static Reader wrapInputAsReader(OWLOntologyDocumentSource source, OWLOntologyLoaderConfiguration configuration, Charset encoding) throws OWLOntologyInputSourceException { Optional<Reader> reader = source.getReader(); if (reader.isPresent()) { return new BufferedReader(reader.get()); } return new BufferedReader( new InputStreamReader(wrap(wrapInput(source, configuration)), encoding)); }
protected static InputStream handleZips(IRI documentIRI, URLConnection conn, InputStream is) throws IOException { if (!isZipName(documentIRI, conn)) { return is; } ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = null; ZipEntry nextEntry = zis.getNextEntry(); // XXX is this a bug? while (entry != null && nextEntry != null) { if (couldBeOntology(nextEntry)) { entry = nextEntry; } nextEntry = zis.getNextEntry(); } return zis; }
private static boolean isZipName(IRI documentIRI, URLConnection connection) { if (isZipFileName(documentIRI.toString())) { return true; } else { String fileName = getFileNameFromContentDisposition(connection); return fileName != null && isZipFileName(fileName); } }
private static InputStream getInputStreamFromContentEncoding(URLConnection conn, @Nullable String contentEncoding) throws IOException { InputStream in = conn.getInputStream(); if (contentEncoding != null) { InputStream toReturn = handleKnownContentEncodings(contentEncoding, in); if (toReturn != null) { return toReturn; } } String fileName = getFileNameFromContentDisposition(conn); if (fileName == null && conn.getURL() != null) { fileName = conn.getURL().toString(); } if (fileName != null) { if (fileName.endsWith(".gz")) { LOGGER.info("URL connection has no content encoding but name ends with .gz"); return new BufferedInputStream(new GZIPInputStream(in)); } if (fileName.endsWith(".xz")) { LOGGER.info("URL connection has no content encoding but name ends with .xz"); return new BufferedInputStream(new XZInputStream(in)); } } return wrap(in); }
conn = connect(config, originalProtocol, conn, connectionTimeout, actualAcceptHeaders); String contentEncoding = conn.getContentEncoding(); InputStream is = connectWithFiveRetries(documentIRI, config, conn, connectionTimeout, contentEncoding); if (is == null) { return emptyOptional(); is = handleZips(documentIRI, conn, is); return optional(is); } catch (IOException e) {
try { if (owlFormatFactory.isTextual()) { createParser.parse(wrapInputAsReader(source, config), baseUri); return; createParser.parse(DocumentSources.wrapInput(source, config), baseUri); } catch (OWLOntologyInputSourceException e) { throw new OWLParserException(e.getMessage(), e);
if (source.getDocumentIRI().getNamespace().startsWith("jar:")) { try { return streamFromJar(source.getDocumentIRI()).getInputStream(); } catch (IOException e) { source.setIRIResolutionFailed(true); input = getInputStream(source.getDocumentIRI(), configuration, headers.get()); } else { input = getInputStream(source.getDocumentIRI(), configuration, DEFAULT_REQUEST);
/** * A convenience method that obtains an input stream from a URI. This method sets up the correct * request type and wraps the input stream within a buffered input stream. * * @param documentIRI The URI from which the input stream should be returned * @param config the load configuration * @return The input stream obtained from the URI * @throws OWLOntologyInputSourceException if there was an {@code IOException} in obtaining the * input stream from the URI. * @deprecated use {@link #getInputStream(IRI, OWLOntologyLoaderConfiguration, String)} instead */ @Deprecated public static Optional<InputStream> getInputStream(IRI documentIRI, OWLOntologyLoaderConfiguration config) throws OWLOntologyInputSourceException { return getInputStream(documentIRI, config, DEFAULT_REQUEST); }
@Nullable protected static InputStream connectWithFiveRetries(IRI documentIRI, OWLOntologyLoaderConfiguration config, URLConnection conn, int connectionTimeout, String contentEncoding) throws IOException, OWLOntologyInputSourceException { InputStream is = null; int count = 0; while (count < config.getRetriesToAttempt() && is == null) { try { is = getInputStreamFromContentEncoding(conn, contentEncoding); } catch (SocketTimeoutException e) { count++; if (count == 5) { throw new OWLOntologyInputSourceException( "cannot connect to " + documentIRI + "; retry limit exhausted", e); } conn.setConnectTimeout(connectionTimeout + connectionTimeout * count); } } return is; }
private static InputStream getInputStreamFromContentEncoding(URLConnection conn, @Nullable String contentEncoding) throws IOException { InputStream in = conn.getInputStream(); if (contentEncoding != null) { InputStream toReturn = handleKnownContentEncodings(contentEncoding, in); if (toReturn != null) { return toReturn; } } String fileName = getFileNameFromContentDisposition(conn); if (fileName == null && conn.getURL() != null) { fileName = conn.getURL().toString(); } if (fileName != null) { if (fileName.endsWith(".gz")) { LOGGER.info("URL connection has no content encoding but name ends with .gz"); return new BufferedInputStream(new GZIPInputStream(in)); } if (fileName.endsWith(".xz")) { LOGGER.info("URL connection has no content encoding but name ends with .xz"); return new BufferedInputStream(new XZInputStream(in)); } } return wrap(in); }
conn = connect(config, originalProtocol, conn, connectionTimeout, actualAcceptHeaders); String contentEncoding = conn.getContentEncoding(); InputStream is = connectWithFiveRetries(documentIRI, config, conn, connectionTimeout, contentEncoding); if (is == null) { return emptyOptional(); is = handleZips(documentIRI, conn, is); return optional(is); } catch (IOException e) {
try { if (owlFormatFactory.isTextual()) { createParser.parse(wrapInputAsReader(source, config), baseUri); return; createParser.parse(DocumentSources.wrapInput(source, config), baseUri); } catch (OWLOntologyInputSourceException e) { throw new OWLParserException(e.getMessage(), e);
private static boolean isZipName(IRI documentIRI, URLConnection connection) { if (isZipFileName(documentIRI.toString())) { return true; } else { String fileName = getFileNameFromContentDisposition(connection); return fileName != null && isZipFileName(fileName); } }
if (source.getDocumentIRI().getNamespace().startsWith("jar:")) { try { return streamFromJar(source.getDocumentIRI()).getInputStream(); } catch (IOException e) { source.setIRIResolutionFailed(true); input = getInputStream(source.getDocumentIRI(), configuration, headers.get()); } else { input = getInputStream(source.getDocumentIRI(), configuration, DEFAULT_REQUEST);
/** * A convenience method that obtains an input stream from a URI. This method sets up the correct * request type and wraps the input stream within a buffered input stream. * * @param documentIRI The URI from which the input stream should be returned * @param config the load configuration * @return The input stream obtained from the URI * @throws OWLOntologyInputSourceException if there was an {@code IOException} in obtaining the * input stream from the URI. * @deprecated use {@link #getInputStream(IRI, OWLOntologyLoaderConfiguration, String)} instead */ @Deprecated public static Optional<InputStream> getInputStream(IRI documentIRI, OWLOntologyLoaderConfiguration config) throws OWLOntologyInputSourceException { return getInputStream(documentIRI, config, DEFAULT_REQUEST); }
@Nullable protected static InputStream connectWithFiveRetries(IRI documentIRI, OWLOntologyLoaderConfiguration config, URLConnection conn, int connectionTimeout, String contentEncoding) throws IOException, OWLOntologyInputSourceException { InputStream is = null; int count = 0; while (count < config.getRetriesToAttempt() && is == null) { try { is = getInputStreamFromContentEncoding(conn, contentEncoding); } catch (SocketTimeoutException e) { count++; if (count == 5) { throw new OWLOntologyInputSourceException( "cannot connect to " + documentIRI + "; retry limit exhausted", e); } conn.setConnectTimeout(connectionTimeout + connectionTimeout * count); } } return is; }
/** * Call #wrapwrapInputAsReader(OWLOntologyLoaderConfiguration, String) with UTF-* as default * encoding. * * @param source ontology source * @param configuration loader configuration to use of the reader must be built form the input * IRI * @return A Reader wrapped in an Optional; if no Reader can be obtained, the result is * Optional.empty. @throws OWLOntologyInputSourceException if an IO related exception is * thrown. * @throws OWLOntologyInputSourceException if an IO related exception is thrown. */ public static Reader wrapInputAsReader(OWLOntologyDocumentSource source, OWLOntologyLoaderConfiguration configuration) throws OWLOntologyInputSourceException { return wrapInputAsReader(source, configuration, Charsets.UTF_8); }
private static InputStream getInputStreamFromContentEncoding(URLConnection conn, @Nullable String contentEncoding) throws IOException { InputStream in = conn.getInputStream(); if (contentEncoding != null) { InputStream toReturn = handleKnownContentEncodings(contentEncoding, in); if (toReturn != null) { return toReturn; } } String fileName = getFileNameFromContentDisposition(conn); if (fileName == null && conn.getURL() != null) { fileName = conn.getURL().toString(); } if (fileName != null) { if (fileName.endsWith(".gz")) { LOGGER.info("URL connection has no content encoding but name ends with .gz"); return new BufferedInputStream(new GZIPInputStream(in)); } if (fileName.endsWith(".xz")) { LOGGER.info("URL connection has no content encoding but name ends with .xz"); return new BufferedInputStream(new XZInputStream(in)); } } return wrap(in); }
conn = connect(config, originalProtocol, conn, connectionTimeout, actualAcceptHeaders); String contentEncoding = conn.getContentEncoding(); InputStream is = connectWithFiveRetries(documentIRI, config, conn, connectionTimeout, contentEncoding); if (is == null) { return emptyOptional(); is = handleZips(documentIRI, conn, is); return optional(is); } catch (IOException e) {