Skip to content

Commit

Permalink
[IO-831] Support getInputStream() for https & http in URIOrigin (#630)
Browse files Browse the repository at this point in the history
* IO-831 Support https & http by URIOrigin

* Update test

* Fix typo

* Fix comment

* Remove extra whitespace

* Missed refactoring

---------

Co-authored-by: Gary Gregory <garydgregory@users.noreply.github.com>
  • Loading branch information
thachlp and garydgregory authored Jun 2, 2024
1 parent 1bd12b5 commit b8babec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/apache/commons/io/build/AbstractOrigin.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.spi.FileSystemProvider;
import java.util.Arrays;
import java.util.Objects;

import org.apache.commons.io.IOUtils;
import org.apache.commons.io.RandomAccessFileMode;
import org.apache.commons.io.RandomAccessFiles;
import org.apache.commons.io.file.spi.FileSystemProviders;
import org.apache.commons.io.input.CharSequenceInputStream;
import org.apache.commons.io.input.CharSequenceReader;
import org.apache.commons.io.input.ReaderInputStream;
Expand Down Expand Up @@ -402,6 +404,19 @@ public Path getPath() {
return Paths.get(get());
}

@Override
public InputStream getInputStream(final OpenOption... options) throws IOException {
final URI uri = get();
final String scheme = uri.getScheme();
final FileSystemProvider fileSystemProvider = FileSystemProviders.installed().getFileSystemProvider(scheme);
if (fileSystemProvider != null) {
return Files.newInputStream(fileSystemProvider.getPath(uri), options);
}
if ("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme)) {
return uri.toURL().openStream();
}
return Files.newInputStream(getPath(), options);
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/org/apache/commons/io/build/URIOriginTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
*/
package org.apache.commons.io.build;

import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.io.InputStream;
import java.net.URI;
import java.nio.file.Paths;

import org.apache.commons.io.build.AbstractOrigin.URIOrigin;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Tests {@link URIOrigin}.
Expand All @@ -35,4 +41,23 @@ public void beforeEach() {
setOriginRw(new URIOrigin(Paths.get(FILE_NAME_RW).toUri()));
}

@ParameterizedTest
@ValueSource(strings = {
"http://example.com",
"https://example.com"
})
void testGetInputStream(String uri) throws Exception {
final AbstractOrigin.URIOrigin origin = new AbstractOrigin.URIOrigin(new URI(uri));
try (final InputStream in = origin.getInputStream()) {
assertNotEquals(-1, in.read());
}
}

@Test
void testGetInputStreamFileURI() throws Exception {
final AbstractOrigin.URIOrigin origin = getOriginRo().asThis();
try (final InputStream in = origin.getInputStream()) {
assertNotEquals(-1, in.read());
}
}
}

0 comments on commit b8babec

Please sign in to comment.