This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-vfs.git
commit 7b16ef7d4e50ee6552bffd5e301d644cdf704887 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Fri Feb 19 09:58:07 2021 -0500 Sort members. --- .../vfs2/provider/http/HttpClientFactory.java | 20 +- .../vfs2/provider/http/HttpFileNameParser.java | 8 +- .../commons/vfs2/provider/http/HttpFileObject.java | 8 +- .../vfs2/provider/http/HttpFileProvider.java | 8 +- .../commons/vfs2/provider/http/HttpFileSystem.java | 8 +- .../provider/http/HttpFileSystemConfigBuilder.java | 300 ++++++++-------- .../provider/http/HttpRandomAccessContent.java | 46 +-- .../vfs2/provider/http4/Http4FileNameParser.java | 8 +- .../vfs2/provider/http4/Http4FileObject.java | 96 ++--- .../vfs2/provider/http4/Http4FileProvider.java | 218 +++++------ .../vfs2/provider/http4/Http4FileSystem.java | 8 +- .../http4/Http4FileSystemConfigBuilder.java | 388 ++++++++++---------- .../provider/http4/Http4RandomAccessContent.java | 48 +-- .../vfs2/provider/http4s/Http4sFileNameParser.java | 8 +- .../vfs2/provider/http5/Http5FileNameParser.java | 8 +- .../vfs2/provider/http5/Http5FileObject.java | 92 ++--- .../vfs2/provider/http5/Http5FileProvider.java | 236 ++++++------ .../vfs2/provider/http5/Http5FileSystem.java | 8 +- .../http5/Http5FileSystemConfigBuilder.java | 398 ++++++++++----------- .../provider/http5/Http5RandomAccessContent.java | 48 +-- .../vfs2/provider/http5s/Http5sFileNameParser.java | 8 +- .../vfs2/provider/https/HttpsFileNameParser.java | 8 +- .../http/GetContentInfoFunctionalTest.java | 34 +- .../vfs2/provider/http/HttpProviderTestCase.java | 70 ++-- .../vfs2/provider/http4/Http4ProviderTestCase.java | 58 +-- .../provider/http4s/Http4sGetContentInfoTest.java | 36 +- .../provider/http5s/Http5sGetContentInfoTest.java | 88 ++--- .../https/GetContentInfoFunctionalTest.java | 40 +-- 28 files changed, 1152 insertions(+), 1152 deletions(-) diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpClientFactory.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpClientFactory.java index 8ad023e..fabe2ae 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpClientFactory.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpClientFactory.java @@ -39,16 +39,6 @@ import org.apache.commons.vfs2.util.UserAuthenticatorUtils; */ public final class HttpClientFactory { - private HttpClientFactory() { - } - - public static HttpClient createConnection(final String scheme, final String hostname, final int port, - final String username, final String password, final FileSystemOptions fileSystemOptions) - throws FileSystemException { - return createConnection(HttpFileSystemConfigBuilder.getInstance(), scheme, hostname, port, username, password, - fileSystemOptions); - } - /** * Creates a new connection to the server. * @@ -140,4 +130,14 @@ public final class HttpClientFactory { return client; } + public static HttpClient createConnection(final String scheme, final String hostname, final int port, + final String username, final String password, final FileSystemOptions fileSystemOptions) + throws FileSystemException { + return createConnection(HttpFileSystemConfigBuilder.getInstance(), scheme, hostname, port, username, password, + fileSystemOptions); + } + + private HttpClientFactory() { + } + } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileNameParser.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileNameParser.java index 779243b..27111c2 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileNameParser.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileNameParser.java @@ -28,11 +28,11 @@ public class HttpFileNameParser extends URLFileNameParser { private static final HttpFileNameParser INSTANCE = new HttpFileNameParser(); - public HttpFileNameParser() { - super(DEFAULT_PORT); - } - public static FileNameParser getInstance() { return INSTANCE; } + + public HttpFileNameParser() { + super(DEFAULT_PORT); + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileObject.java index d4800a4..890b6e2 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileObject.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileObject.java @@ -202,10 +202,6 @@ public class HttpFileObject<FS extends HttpFileSystem> extends AbstractFileObjec return followRedirect; } - protected String getUserAgent() { - return userAgent; - } - HeadMethod getHeadMethod() throws IOException { if (method != null) { return method; @@ -225,6 +221,10 @@ public class HttpFileObject<FS extends HttpFileSystem> extends AbstractFileObjec return urlCharset; } + protected String getUserAgent() { + return userAgent; + } + /** * Prepares a HttpMethod object. * diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileProvider.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileProvider.java index a4c3f52..470508b 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileProvider.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileProvider.java @@ -85,12 +85,12 @@ public class HttpFileProvider extends AbstractOriginatingFileProvider { } @Override - public FileSystemConfigBuilder getConfigBuilder() { - return HttpFileSystemConfigBuilder.getInstance(); + public Collection<Capability> getCapabilities() { + return capabilities; } @Override - public Collection<Capability> getCapabilities() { - return capabilities; + public FileSystemConfigBuilder getConfigBuilder() { + return HttpFileSystemConfigBuilder.getInstance(); } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystem.java index b053f85..509b4b4 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystem.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystem.java @@ -49,10 +49,6 @@ public class HttpFileSystem extends AbstractFileSystem { caps.addAll(HttpFileProvider.capabilities); } - protected HttpClient getClient() { - return client; - } - /** @since 2.0 */ @Override public void closeCommunicationLink() { @@ -71,4 +67,8 @@ public class HttpFileSystem extends AbstractFileSystem { protected FileObject createFile(final AbstractFileName name) throws Exception { return new HttpFileObject<>(name, this); } + + protected HttpClient getClient() { + return client; + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystemConfigBuilder.java index 02ac875..68dbe11 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystemConfigBuilder.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpFileSystemConfigBuilder.java @@ -52,6 +52,19 @@ public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder { private static final String KEY_PREEMPTIVE_AUTHENTICATION = "preemptiveAuth"; /** + * Gets the singleton builder. + * + * @return the singleton builder. + */ + public static HttpFileSystemConfigBuilder getInstance() { + return BUILDER; + } + + private HttpFileSystemConfigBuilder() { + super("http."); + } + + /** * Creates new config builder. * * @param prefix String for properties of this file system. @@ -61,63 +74,87 @@ public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder { super(prefix); } - private HttpFileSystemConfigBuilder() { - super("http."); + @Override + protected Class<? extends FileSystem> getConfigClass() { + return HttpFileSystem.class; } /** - * Gets the singleton builder. + * Gets the connection timeout. * - * @return the singleton builder. + * @param opts The FileSystem options. + * @return The connection timeout. + * @since 2.1 + * @deprecated Use {@link #getConnectionTimeoutDuration(FileSystemOptions)}. */ - public static HttpFileSystemConfigBuilder getInstance() { - return BUILDER; + @Deprecated + public int getConnectionTimeout(final FileSystemOptions opts) { + return getDurationInteger(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); } /** - * Sets the charset used for url encoding. + * Gets the connection timeout. * * @param opts The FileSystem options. - * @param chaset the chaset + * @return The connection timeout. + * @since 2.8.0 */ - public void setUrlCharset(final FileSystemOptions opts, final String chaset) { - setParam(opts, "urlCharset", chaset); + public Duration getConnectionTimeoutDuration(final FileSystemOptions opts) { + return getDuration(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); } /** - * Sets the charset used for url encoding. + * Gets the cookies to add to the request. * * @param opts The FileSystem options. - * @return the chaset + * @return the Cookie array. */ - public String getUrlCharset(final FileSystemOptions opts) { - return getString(opts, "urlCharset"); + public Cookie[] getCookies(final FileSystemOptions opts) { + return getParam(opts, "cookies"); } /** - * Sets the proxy to use for http connection. - * <p> - * You have to set the ProxyPort too if you would like to have the proxy really used. - * </p> + * Gets whether to follow redirects for the connection. * * @param opts The FileSystem options. - * @param proxyHost the host - * @see #setProxyPort + * @return {@code true} to follow redirects, {@code false} not to. + * @see #setFollowRedirect + * @since 2.1 */ - public void setProxyHost(final FileSystemOptions opts, final String proxyHost) { - setParam(opts, "proxyHost", proxyHost); + public boolean getFollowRedirect(final FileSystemOptions opts) { + return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT); } /** - * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the - * proxy really used. + * Gets the maximum number of connections allowed per host. + * + * @param opts The FileSystemOptions. + * @return The maximum number of connections allowed per host. + * @since 2.0 + */ + public int getMaxConnectionsPerHost(final FileSystemOptions opts) { + return getInteger(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS); + } + + /** + * Gets the maximum number of connections allowed. + * + * @param opts The FileSystemOptions. + * @return The maximum number of connections allowed. + * @since 2.0 + */ + public int getMaxTotalConnections(final FileSystemOptions opts) { + return getInteger(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS); + } + + /** + * Gets the proxy authenticator where the system should get the credentials from. * * @param opts The FileSystem options. - * @param proxyPort the port - * @see #setProxyHost + * @return The UserAuthenticator. */ - public void setProxyPort(final FileSystemOptions opts, final int proxyPort) { - setParam(opts, "proxyPort", Integer.valueOf(proxyPort)); + public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) { + return getParam(opts, "proxyAuthenticator"); } /** @@ -145,89 +182,105 @@ public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Sets the proxy authenticator where the system should get the credentials from. + * Gets the socket timeout. * - * @param opts The FileSystem options. - * @param authenticator The UserAuthenticator. + * @param opts The FileSystemOptions. + * @return The socket timeout. + * @since 2.1 + * @deprecated Use {@link #getSoTimeoutDuration(FileSystemOptions)}. */ - public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) { - setParam(opts, "proxyAuthenticator", authenticator); + @Deprecated + public int getSoTimeout(final FileSystemOptions opts) { + return getDurationInteger(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT); } /** - * Gets the proxy authenticator where the system should get the credentials from. + * Gets the socket timeout. * - * @param opts The FileSystem options. - * @return The UserAuthenticator. + * @param opts The FileSystemOptions. + * @return The socket timeout. + * @since 2.8.0 */ - public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) { - return getParam(opts, "proxyAuthenticator"); + public Duration getSoTimeoutDuration(final FileSystemOptions opts) { + return getDuration(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT); } /** - * The cookies to add to the request. + * Sets the charset used for url encoding. * * @param opts The FileSystem options. - * @param cookies An array of Cookies. + * @return the chaset */ - public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) { - setParam(opts, "cookies", cookies); + public String getUrlCharset(final FileSystemOptions opts) { + return getString(opts, "urlCharset"); } /** - * Sets whether to follow redirects for the connection. + * Gets the user agent string * - * @param opts The FileSystem options. - * @param redirect {@code true} to follow redirects, {@code false} not to. - * @see #setFollowRedirect - * @since 2.1 + * @param opts the file system options to modify + * @return User provided User-Agent string, otherwise default of: Commons-VFS */ - public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) { - setParam(opts, KEY_FOLLOW_REDIRECT, redirect); + public String getUserAgent(final FileSystemOptions opts) { + final String userAgent = getParam(opts, KEY_USER_AGENT); + return userAgent != null ? userAgent : DEFAULT_USER_AGENT; } /** - * Gets the cookies to add to the request. + * Determines if the FileSystemOptions indicate that preemptive authentication is requested. + * + * @param opts The FileSystemOptions. + * @return true if preemptiveAuth is requested. + * @since 2.0 + */ + public boolean isPreemptiveAuth(final FileSystemOptions opts) { + return getBoolean(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.FALSE).booleanValue(); + } + + /** + * The connection timeout. * * @param opts The FileSystem options. - * @return the Cookie array. + * @param timeout The connection timeout. + * @since 2.8.0 */ - public Cookie[] getCookies(final FileSystemOptions opts) { - return getParam(opts, "cookies"); + public void setConnectionTimeout(final FileSystemOptions opts, final Duration timeout) { + setParam(opts, HttpConnectionParams.CONNECTION_TIMEOUT, timeout); } /** - * Gets whether to follow redirects for the connection. + * The connection timeout. * * @param opts The FileSystem options. - * @return {@code true} to follow redirects, {@code false} not to. - * @see #setFollowRedirect + * @param timeout The connection timeout. * @since 2.1 + * @deprecated Use {@link #setConnectionTimeout(FileSystemOptions, Duration)}. */ - public boolean getFollowRedirect(final FileSystemOptions opts) { - return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT); + @Deprecated + public void setConnectionTimeout(final FileSystemOptions opts, final int timeout) { + setConnectionTimeout(opts, Duration.ofMillis(timeout)); } /** - * Sets the maximum number of connections allowed. + * The cookies to add to the request. * * @param opts The FileSystem options. - * @param maxTotalConnections The maximum number of connections. - * @since 2.0 + * @param cookies An array of Cookies. */ - public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) { - setParam(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections)); + public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) { + setParam(opts, "cookies", cookies); } /** - * Gets the maximum number of connections allowed. + * Sets whether to follow redirects for the connection. * - * @param opts The FileSystemOptions. - * @return The maximum number of connections allowed. - * @since 2.0 + * @param opts The FileSystem options. + * @param redirect {@code true} to follow redirects, {@code false} not to. + * @see #setFollowRedirect + * @since 2.1 */ - public int getMaxTotalConnections(final FileSystemOptions opts) { - return getInteger(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS); + public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) { + setParam(opts, KEY_FOLLOW_REDIRECT, redirect); } /** @@ -242,25 +295,14 @@ public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Gets the maximum number of connections allowed per host. - * - * @param opts The FileSystemOptions. - * @return The maximum number of connections allowed per host. - * @since 2.0 - */ - public int getMaxConnectionsPerHost(final FileSystemOptions opts) { - return getInteger(opts, HttpConnectionManagerParams.MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS); - } - - /** - * Determines if the FileSystemOptions indicate that preemptive authentication is requested. + * Sets the maximum number of connections allowed. * - * @param opts The FileSystemOptions. - * @return true if preemptiveAuth is requested. + * @param opts The FileSystem options. + * @param maxTotalConnections The maximum number of connections. * @since 2.0 */ - public boolean isPreemptiveAuth(final FileSystemOptions opts) { - return getBoolean(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.FALSE).booleanValue(); + public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) { + setParam(opts, HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections)); } /** @@ -276,51 +318,39 @@ public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * The connection timeout. - * - * @param opts The FileSystem options. - * @param timeout The connection timeout. - * @since 2.8.0 - */ - public void setConnectionTimeout(final FileSystemOptions opts, final Duration timeout) { - setParam(opts, HttpConnectionParams.CONNECTION_TIMEOUT, timeout); - } - - /** - * The connection timeout. + * Sets the proxy authenticator where the system should get the credentials from. * * @param opts The FileSystem options. - * @param timeout The connection timeout. - * @since 2.1 - * @deprecated Use {@link #setConnectionTimeout(FileSystemOptions, Duration)}. + * @param authenticator The UserAuthenticator. */ - @Deprecated - public void setConnectionTimeout(final FileSystemOptions opts, final int timeout) { - setConnectionTimeout(opts, Duration.ofMillis(timeout)); + public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) { + setParam(opts, "proxyAuthenticator", authenticator); } /** - * Gets the connection timeout. + * Sets the proxy to use for http connection. + * <p> + * You have to set the ProxyPort too if you would like to have the proxy really used. + * </p> * * @param opts The FileSystem options. - * @return The connection timeout. - * @since 2.1 - * @deprecated Use {@link #getConnectionTimeoutDuration(FileSystemOptions)}. + * @param proxyHost the host + * @see #setProxyPort */ - @Deprecated - public int getConnectionTimeout(final FileSystemOptions opts) { - return getDurationInteger(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + public void setProxyHost(final FileSystemOptions opts, final String proxyHost) { + setParam(opts, "proxyHost", proxyHost); } /** - * Gets the connection timeout. + * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the + * proxy really used. * * @param opts The FileSystem options. - * @return The connection timeout. - * @since 2.8.0 + * @param proxyPort the port + * @see #setProxyHost */ - public Duration getConnectionTimeoutDuration(final FileSystemOptions opts) { - return getDuration(opts, HttpConnectionParams.CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + public void setProxyPort(final FileSystemOptions opts, final int proxyPort) { + setParam(opts, "proxyPort", Integer.valueOf(proxyPort)); } /** @@ -348,27 +378,13 @@ public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Gets the socket timeout. - * - * @param opts The FileSystemOptions. - * @return The socket timeout. - * @since 2.1 - * @deprecated Use {@link #getSoTimeoutDuration(FileSystemOptions)}. - */ - @Deprecated - public int getSoTimeout(final FileSystemOptions opts) { - return getDurationInteger(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT); - } - - /** - * Gets the socket timeout. + * Sets the charset used for url encoding. * - * @param opts The FileSystemOptions. - * @return The socket timeout. - * @since 2.8.0 + * @param opts The FileSystem options. + * @param chaset the chaset */ - public Duration getSoTimeoutDuration(final FileSystemOptions opts) { - return getDuration(opts, HttpConnectionParams.SO_TIMEOUT, DEFAULT_SO_TIMEOUT); + public void setUrlCharset(final FileSystemOptions opts, final String chaset) { + setParam(opts, "urlCharset", chaset); } /** @@ -380,20 +396,4 @@ public class HttpFileSystemConfigBuilder extends FileSystemConfigBuilder { public void setUserAgent(final FileSystemOptions opts, final String userAgent) { setParam(opts, "userAgent", userAgent); } - - /** - * Gets the user agent string - * - * @param opts the file system options to modify - * @return User provided User-Agent string, otherwise default of: Commons-VFS - */ - public String getUserAgent(final FileSystemOptions opts) { - final String userAgent = getParam(opts, KEY_USER_AGENT); - return userAgent != null ? userAgent : DEFAULT_USER_AGENT; - } - - @Override - protected Class<? extends FileSystem> getConfigClass() { - return HttpFileSystem.class; - } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpRandomAccessContent.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpRandomAccessContent.java index 4d62376..d3a222f 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpRandomAccessContent.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http/HttpRandomAccessContent.java @@ -48,25 +48,12 @@ final class HttpRandomAccessContent<FS extends HttpFileSystem> extends AbstractR } @Override - public long getFilePointer() throws IOException { - return filePointer; - } - - @Override - public void seek(final long pos) throws IOException { - if (pos == filePointer) { - // no change - return; - } - - if (pos < 0) { - throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); - } + public void close() throws IOException { if (dis != null) { - close(); + dis.close(); + dis = null; + mis = null; } - - filePointer = pos; } @Override @@ -126,16 +113,29 @@ final class HttpRandomAccessContent<FS extends HttpFileSystem> extends AbstractR } @Override - public void close() throws IOException { - if (dis != null) { - dis.close(); - dis = null; - mis = null; - } + public long getFilePointer() throws IOException { + return filePointer; } @Override public long length() throws IOException { return fileObject.getContent().getSize(); } + + @Override + public void seek(final long pos) throws IOException { + if (pos == filePointer) { + // no change + return; + } + + if (pos < 0) { + throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); + } + if (dis != null) { + close(); + } + + filePointer = pos; + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileNameParser.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileNameParser.java index 1eca6f3..a2d5c69 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileNameParser.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileNameParser.java @@ -30,11 +30,11 @@ public class Http4FileNameParser extends GenericURLFileNameParser { private static final Http4FileNameParser INSTANCE = new Http4FileNameParser(); - public Http4FileNameParser() { - super(DEFAULT_PORT); - } - public static FileNameParser getInstance() { return INSTANCE; } + + public Http4FileNameParser() { + super(DEFAULT_PORT); + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileObject.java index 441497b..b152a4f 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileObject.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileObject.java @@ -97,18 +97,8 @@ public class Http4FileObject<FS extends Http4FileSystem> extends AbstractFileObj } @Override - protected FileType doGetType() throws Exception { - lastHeadResponse = executeHttpUriRequest(new HttpHead(getInternalURI())); - final int status = lastHeadResponse.getStatusLine().getStatusCode(); - - if (status == HttpStatus.SC_OK - || status == HttpStatus.SC_METHOD_NOT_ALLOWED /* method is not allowed, but resource exist */) { - return FileType.FILE; - } else if (status == HttpStatus.SC_NOT_FOUND || status == HttpStatus.SC_GONE) { - return FileType.IMAGINARY; - } else { - throw new FileSystemException("vfs.provider.http/head.error", getName(), Integer.valueOf(status)); - } + protected void doDetach() throws Exception { + lastHeadResponse = null; } @Override @@ -128,18 +118,6 @@ public class Http4FileObject<FS extends Http4FileSystem> extends AbstractFileObj } @Override - protected long doGetLastModifiedTime() throws Exception { - FileSystemException.requireNonNull(lastHeadResponse, "vfs.provider.http/last-modified.error", getName()); - - final Header header = lastHeadResponse.getFirstHeader("Last-Modified"); - - FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName()); - - return DateUtils.parseDate(header.getValue()).getTime(); - } - - - @Override protected InputStream doGetInputStream(final int bufferSize) throws Exception { final HttpGet getRequest = new HttpGet(getInternalURI()); final HttpResponse httpResponse = executeHttpUriRequest(getRequest); @@ -156,14 +134,36 @@ public class Http4FileObject<FS extends Http4FileSystem> extends AbstractFileObj return new MonitoredHttpResponseContentInputStream(httpResponse, bufferSize); } + + @Override + protected long doGetLastModifiedTime() throws Exception { + FileSystemException.requireNonNull(lastHeadResponse, "vfs.provider.http/last-modified.error", getName()); + + final Header header = lastHeadResponse.getFirstHeader("Last-Modified"); + + FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName()); + + return DateUtils.parseDate(header.getValue()).getTime(); + } + @Override protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception { return new Http4RandomAccessContent<>(this, mode); } @Override - protected String[] doListChildren() throws Exception { - throw new UnsupportedOperationException("Not implemented."); + protected FileType doGetType() throws Exception { + lastHeadResponse = executeHttpUriRequest(new HttpHead(getInternalURI())); + final int status = lastHeadResponse.getStatusLine().getStatusCode(); + + if (status == HttpStatus.SC_OK + || status == HttpStatus.SC_METHOD_NOT_ALLOWED /* method is not allowed, but resource exist */) { + return FileType.FILE; + } else if (status == HttpStatus.SC_NOT_FOUND || status == HttpStatus.SC_GONE) { + return FileType.IMAGINARY; + } else { + throw new FileSystemException("vfs.provider.http/head.error", getName(), Integer.valueOf(status)); + } } @Override @@ -172,21 +172,28 @@ public class Http4FileObject<FS extends Http4FileSystem> extends AbstractFileObj } @Override - protected FileContentInfoFactory getFileContentInfoFactory() { - return new Http4FileContentInfoFactory(); - } - - @Override - protected void doDetach() throws Exception { - lastHeadResponse = null; + protected String[] doListChildren() throws Exception { + throw new UnsupportedOperationException("Not implemented."); } /** - * Return URL charset string. - * @return URL charset string + * Execute the request using the given {@code httpRequest} and return a {@code HttpResponse} from the execution. + * + * @param httpRequest {@code HttpUriRequest} object + * @return {@code HttpResponse} from the execution + * @throws IOException if IO error occurs + * + * @since 2.5.0 */ - protected String getUrlCharset() { - return urlCharset; + protected HttpResponse executeHttpUriRequest(final HttpUriRequest httpRequest) throws IOException { + final HttpClient httpClient = getAbstractFileSystem().getHttpClient(); + final HttpClientContext httpClientContext = getAbstractFileSystem().getHttpClientContext(); + return httpClient.execute(httpRequest, httpClientContext); + } + + @Override + protected FileContentInfoFactory getFileContentInfoFactory() { + return new Http4FileContentInfoFactory(); } /** @@ -214,18 +221,11 @@ public class Http4FileObject<FS extends Http4FileSystem> extends AbstractFileObj } /** - * Execute the request using the given {@code httpRequest} and return a {@code HttpResponse} from the execution. - * - * @param httpRequest {@code HttpUriRequest} object - * @return {@code HttpResponse} from the execution - * @throws IOException if IO error occurs - * - * @since 2.5.0 + * Return URL charset string. + * @return URL charset string */ - protected HttpResponse executeHttpUriRequest(final HttpUriRequest httpRequest) throws IOException { - final HttpClient httpClient = getAbstractFileSystem().getHttpClient(); - final HttpClientContext httpClientContext = getAbstractFileSystem().getHttpClientContext(); - return httpClient.execute(httpRequest, httpClientContext); + protected String getUrlCharset() { + return urlCharset; } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java index 968938f..91e3a38 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileProvider.java @@ -111,35 +111,49 @@ public class Http4FileProvider extends AbstractOriginatingFileProvider { setFileNameParser(Http4FileNameParser.getInstance()); } - @Override - public FileSystemConfigBuilder getConfigBuilder() { - return Http4FileSystemConfigBuilder.getInstance(); - } + private HttpClientConnectionManager createConnectionManager(final Http4FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) throws FileSystemException { + final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); + connManager.setMaxTotal(builder.getMaxTotalConnections(fileSystemOptions)); + connManager.setDefaultMaxPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions)); - @Override - public Collection<Capability> getCapabilities() { - return capabilities; + final SocketConfig socketConfig = + SocketConfig + .custom() + .setSoTimeout(builder.getSoTimeout(fileSystemOptions)) + .build(); + + connManager.setDefaultSocketConfig(socketConfig); + + return connManager; } - @Override - protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) - throws FileSystemException { - final GenericFileName rootName = (GenericFileName) name; + private CookieStore createDefaultCookieStore(final Http4FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) { + final CookieStore cookieStore = new BasicCookieStore(); + final Cookie[] cookies = builder.getCookies(fileSystemOptions); - UserAuthenticationData authData = null; - HttpClient httpClient; - HttpClientContext httpClientContext; + if (cookies != null) { + Arrays.stream(cookies).forEach(cookieStore::addCookie); + } - try { - final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance(); - authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES); - httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData); - httpClient = createHttpClient(builder, rootName, fileSystemOptions); - } finally { - UserAuthenticatorUtils.cleanup(authData); + return cookieStore; + } + + private RequestConfig createDefaultRequestConfig(final Http4FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) { + return RequestConfig.custom() + .setConnectTimeout(builder.getConnectionTimeout(fileSystemOptions)) + .build(); + } + + private HostnameVerifier createHostnameVerifier(final Http4FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) throws FileSystemException { + if (!builder.isHostnameVerificationEnabled(fileSystemOptions)) { + return NoopHostnameVerifier.INSTANCE; } - return new Http4FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext); + return new DefaultHostnameVerifier(); } /** @@ -193,49 +207,6 @@ public class Http4FileProvider extends AbstractOriginatingFileProvider { } /** - * Create {@link SSLContext} for HttpClient. Invoked by {@link #createHttpClientBuilder(Http4FileSystemConfigBuilder, GenericFileName, FileSystemOptions)}. - * - * @param builder Configuration options builder for HTTP4 provider - * @param fileSystemOptions The FileSystem options - * @return a {@link SSLContext} for HttpClient - * @throws FileSystemException if an error occurs - */ - protected SSLContext createSSLContext(final Http4FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) throws FileSystemException { - try { - final SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - sslContextBuilder.setKeyStoreType(builder.getKeyStoreType(fileSystemOptions)); - - File keystoreFileObject = null; - final String keystoreFile = builder.getKeyStoreFile(fileSystemOptions); - - if (keystoreFile != null && !keystoreFile.isEmpty()) { - keystoreFileObject = new File(keystoreFile); - } - - if (keystoreFileObject != null && keystoreFileObject.exists()) { - final String keystorePass = builder.getKeyStorePass(fileSystemOptions); - final char[] keystorePassChars = (keystorePass != null) ? keystorePass.toCharArray() : null; - sslContextBuilder.loadTrustMaterial(keystoreFileObject, keystorePassChars, TrustAllStrategy.INSTANCE); - } else { - sslContextBuilder.loadTrustMaterial(TrustAllStrategy.INSTANCE); - } - - return sslContextBuilder.build(); - } catch (final KeyStoreException e) { - throw new FileSystemException("Keystore error. " + e.getMessage(), e); - } catch (final KeyManagementException e) { - throw new FileSystemException("Cannot retrieve keys. " + e.getMessage(), e); - } catch (final NoSuchAlgorithmException e) { - throw new FileSystemException("Algorithm error. " + e.getMessage(), e); - } catch (final CertificateException e) { - throw new FileSystemException("Certificate error. " + e.getMessage(), e); - } catch (final IOException e) { - throw new FileSystemException("Cannot open key file. " + e.getMessage(), e); - } - } - - /** * Create an {@link HttpClientContext} object for an http4 file system. * * @param builder Configuration options builder for http4 provider @@ -296,39 +267,89 @@ public class Http4FileProvider extends AbstractOriginatingFileProvider { return clientContext; } - private HttpClientConnectionManager createConnectionManager(final Http4FileSystemConfigBuilder builder, + private HttpRoutePlanner createHttpRoutePlanner(final Http4FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) { + final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions); + + if (proxyHost != null) { + return new DefaultProxyRoutePlanner(proxyHost); + } + + return new SystemDefaultRoutePlanner(ProxySelector.getDefault()); + } + + /** + * Create {@link SSLContext} for HttpClient. Invoked by {@link #createHttpClientBuilder(Http4FileSystemConfigBuilder, GenericFileName, FileSystemOptions)}. + * + * @param builder Configuration options builder for HTTP4 provider + * @param fileSystemOptions The FileSystem options + * @return a {@link SSLContext} for HttpClient + * @throws FileSystemException if an error occurs + */ + protected SSLContext createSSLContext(final Http4FileSystemConfigBuilder builder, final FileSystemOptions fileSystemOptions) throws FileSystemException { - final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(); - connManager.setMaxTotal(builder.getMaxTotalConnections(fileSystemOptions)); - connManager.setDefaultMaxPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions)); + try { + final SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); + sslContextBuilder.setKeyStoreType(builder.getKeyStoreType(fileSystemOptions)); - final SocketConfig socketConfig = - SocketConfig - .custom() - .setSoTimeout(builder.getSoTimeout(fileSystemOptions)) - .build(); + File keystoreFileObject = null; + final String keystoreFile = builder.getKeyStoreFile(fileSystemOptions); - connManager.setDefaultSocketConfig(socketConfig); + if (keystoreFile != null && !keystoreFile.isEmpty()) { + keystoreFileObject = new File(keystoreFile); + } - return connManager; - } + if (keystoreFileObject != null && keystoreFileObject.exists()) { + final String keystorePass = builder.getKeyStorePass(fileSystemOptions); + final char[] keystorePassChars = (keystorePass != null) ? keystorePass.toCharArray() : null; + sslContextBuilder.loadTrustMaterial(keystoreFileObject, keystorePassChars, TrustAllStrategy.INSTANCE); + } else { + sslContextBuilder.loadTrustMaterial(TrustAllStrategy.INSTANCE); + } - private RequestConfig createDefaultRequestConfig(final Http4FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) { - return RequestConfig.custom() - .setConnectTimeout(builder.getConnectionTimeout(fileSystemOptions)) - .build(); + return sslContextBuilder.build(); + } catch (final KeyStoreException e) { + throw new FileSystemException("Keystore error. " + e.getMessage(), e); + } catch (final KeyManagementException e) { + throw new FileSystemException("Cannot retrieve keys. " + e.getMessage(), e); + } catch (final NoSuchAlgorithmException e) { + throw new FileSystemException("Algorithm error. " + e.getMessage(), e); + } catch (final CertificateException e) { + throw new FileSystemException("Certificate error. " + e.getMessage(), e); + } catch (final IOException e) { + throw new FileSystemException("Cannot open key file. " + e.getMessage(), e); + } } - private HttpRoutePlanner createHttpRoutePlanner(final Http4FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) { - final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions); + @Override + protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) + throws FileSystemException { + final GenericFileName rootName = (GenericFileName) name; - if (proxyHost != null) { - return new DefaultProxyRoutePlanner(proxyHost); + UserAuthenticationData authData = null; + HttpClient httpClient; + HttpClientContext httpClientContext; + + try { + final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance(); + authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES); + httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData); + httpClient = createHttpClient(builder, rootName, fileSystemOptions); + } finally { + UserAuthenticatorUtils.cleanup(authData); } - return new SystemDefaultRoutePlanner(ProxySelector.getDefault()); + return new Http4FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext); + } + + @Override + public Collection<Capability> getCapabilities() { + return capabilities; + } + + @Override + public FileSystemConfigBuilder getConfigBuilder() { + return Http4FileSystemConfigBuilder.getInstance(); } private HttpHost getProxyHttpHost(final Http4FileSystemConfigBuilder builder, @@ -344,25 +365,4 @@ public class Http4FileProvider extends AbstractOriginatingFileProvider { return null; } - private CookieStore createDefaultCookieStore(final Http4FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) { - final CookieStore cookieStore = new BasicCookieStore(); - final Cookie[] cookies = builder.getCookies(fileSystemOptions); - - if (cookies != null) { - Arrays.stream(cookies).forEach(cookieStore::addCookie); - } - - return cookieStore; - } - - private HostnameVerifier createHostnameVerifier(final Http4FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) throws FileSystemException { - if (!builder.isHostnameVerificationEnabled(fileSystemOptions)) { - return NoopHostnameVerifier.INSTANCE; - } - - return new DefaultHostnameVerifier(); - } - } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java index b3e1c3e..b73b543 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystem.java @@ -80,13 +80,13 @@ public class Http4FileSystem extends AbstractFileSystem { } @Override - protected FileObject createFile(final AbstractFileName name) throws Exception { - return new Http4FileObject<>(name, this); + protected void addCapabilities(final Collection<Capability> caps) { + caps.addAll(Http4FileProvider.capabilities); } @Override - protected void addCapabilities(final Collection<Capability> caps) { - caps.addAll(Http4FileProvider.capabilities); + protected FileObject createFile(final AbstractFileName name) throws Exception { + return new Http4FileObject<>(name, this); } @Override diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java index 7e35a19..9506a9d 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4FileSystemConfigBuilder.java @@ -176,12 +176,12 @@ public class Http4FileSystemConfigBuilder extends FileSystemConfigBuilder { private static final String PROXY_SCHEME = "proxyScheme"; /** - * Construct an {@code Http4FileSystemConfigBuilder}. + * Gets the singleton builder. * - * @param prefix String for properties of this file system. + * @return the singleton builder. */ - protected Http4FileSystemConfigBuilder(final String prefix) { - super(prefix); + public static Http4FileSystemConfigBuilder getInstance() { + return BUILDER; } private Http4FileSystemConfigBuilder() { @@ -189,116 +189,94 @@ public class Http4FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Gets the singleton builder. + * Construct an {@code Http4FileSystemConfigBuilder}. * - * @return the singleton builder. + * @param prefix String for properties of this file system. */ - public static Http4FileSystemConfigBuilder getInstance() { - return BUILDER; + protected Http4FileSystemConfigBuilder(final String prefix) { + super(prefix); } - /** - * Sets the charset used for url encoding. - * - * @param opts The FileSystem options. - * @param chaset the chaset - */ - public void setUrlCharset(final FileSystemOptions opts, final String chaset) { - setParam(opts, "urlCharset", chaset); + @Override + protected Class<? extends FileSystem> getConfigClass() { + return Http4FileSystem.class; } /** - * Sets the charset used for url encoding. + * Gets the connection timeout. * * @param opts The FileSystem options. - * @return the chaset + * @return The connection timeout. */ - public String getUrlCharset(final FileSystemOptions opts) { - return getString(opts, "urlCharset"); + public int getConnectionTimeout(final FileSystemOptions opts) { + return getInteger(opts, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); } /** - * Sets the proxy to use for http connection. - * <p> - * You have to set the ProxyPort too if you would like to have the proxy really used. - * </p> + * Gets the cookies to add to the request. * * @param opts The FileSystem options. - * @param proxyHost the host - * @see #setProxyPort + * @return the Cookie array. */ - public void setProxyHost(final FileSystemOptions opts, final String proxyHost) { - setParam(opts, "proxyHost", proxyHost); + public Cookie[] getCookies(final FileSystemOptions opts) { + return getParam(opts, "cookies"); } /** - * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the - * proxy really used. + * Gets whether to follow redirects for the connection. * * @param opts The FileSystem options. - * @param proxyPort the port - * @see #setProxyHost + * @return {@code true} to follow redirects, {@code false} not to. + * @see #setFollowRedirect */ - public void setProxyPort(final FileSystemOptions opts, final int proxyPort) { - setParam(opts, "proxyPort", Integer.valueOf(proxyPort)); + public boolean getFollowRedirect(final FileSystemOptions opts) { + return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT); } /** - * Sets the proxy-scheme to use for http connection. You have to set the ProxyHost too if you would like to have the - * proxy really used. - * - * @param opts The FileSystem options. - * @param proxyScheme the protocol scheme - * @see #setProxyHost - * @since 2.7.0 + * Return keystore file path to be used in SSL connections. + * @param opts the file system options to modify + * @return keystore file path to be used in SSL connections */ - public void setProxyScheme(final FileSystemOptions opts, final String proxyScheme) { - setParam(opts, PROXY_SCHEME, proxyScheme); + public String getKeyStoreFile(final FileSystemOptions opts) { + return getParam(opts, KEYSTORE_FILE); } /** - * Gets the proxy to use for http connection. You have to set the ProxyPort too if you would like to have the proxy - * really used. - * - * @param opts The FileSystem options. - * @return proxyHost - * @see #setProxyPort + * Return keystore pass phrase for SSL connections. + * @param opts the file system options to modify + * @return keystore pass phrase for SSL connections */ - public String getProxyHost(final FileSystemOptions opts) { - return getString(opts, "proxyHost"); + String getKeyStorePass(final FileSystemOptions opts) { + return getParam(opts, KEYSTORE_PASS); } /** - * Gets the proxy-port to use for http the connection. You have to set the ProxyHost too if you would like to have - * the proxy really used. - * - * @param opts The FileSystem options. - * @return proxyPort: the port number or 0 if it is not set - * @see #setProxyHost + * Get keystore type for SSL connections. + * @param opts the file system options to modify + * @return keystore type for SSL connections + * @since 2.7.0 */ - public int getProxyPort(final FileSystemOptions opts) { - return getInteger(opts, "proxyPort", 0); + public String getKeyStoreType(final FileSystemOptions opts) { + return getString(opts, KEYSTORE_TYPE, KeyStore.getDefaultType()); } /** - * Gets the proxy-scheme to use for http the connection. You have to set the ProxyHost too if you would like to have - * the proxy really used. + * Gets the maximum number of connections allowed per host. * - * @param opts The FileSystem options. - * @return proxyScheme: the http/https scheme of proxy server - * @see #setProxyHost - * @since 2.7.0 + * @param opts The FileSystemOptions. + * @return The maximum number of connections allowed per host. */ - public String getProxyScheme(final FileSystemOptions opts) { - return getString(opts, PROXY_SCHEME, HttpHost.DEFAULT_SCHEME_NAME); + public int getMaxConnectionsPerHost(final FileSystemOptions opts) { + return getInteger(opts, MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS); } /** - * Sets the proxy authenticator where the system should get the credentials from. + * Gets the maximum number of connections allowed. * - * @param opts The FileSystem options. - * @param authenticator The UserAuthenticator. + * @param opts The FileSystemOptions. + * @return The maximum number of connections allowed. */ - public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) { - setParam(opts, "proxyAuthenticator", authenticator); + public int getMaxTotalConnections(final FileSystemOptions opts) { + return getInteger(opts, MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS); } /** @@ -312,85 +290,91 @@ public class Http4FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * The cookies to add to the request. + * Gets the proxy to use for http connection. You have to set the ProxyPort too if you would like to have the proxy + * really used. * * @param opts The FileSystem options. - * @param cookies An array of Cookies. + * @return proxyHost + * @see #setProxyPort */ - public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) { - setParam(opts, "cookies", cookies); + public String getProxyHost(final FileSystemOptions opts) { + return getString(opts, "proxyHost"); } /** - * Sets whether to follow redirects for the connection. + * Gets the proxy-port to use for http the connection. You have to set the ProxyHost too if you would like to have + * the proxy really used. * * @param opts The FileSystem options. - * @param redirect {@code true} to follow redirects, {@code false} not to. - * @see #setFollowRedirect + * @return proxyPort: the port number or 0 if it is not set + * @see #setProxyHost */ - public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) { - setParam(opts, KEY_FOLLOW_REDIRECT, redirect); + public int getProxyPort(final FileSystemOptions opts) { + return getInteger(opts, "proxyPort", 0); } /** - * Gets the cookies to add to the request. + * Gets the proxy-scheme to use for http the connection. You have to set the ProxyHost too if you would like to have + * the proxy really used. * * @param opts The FileSystem options. - * @return the Cookie array. + * @return proxyScheme: the http/https scheme of proxy server + * @see #setProxyHost + * @since 2.7.0 */ - public Cookie[] getCookies(final FileSystemOptions opts) { - return getParam(opts, "cookies"); + public String getProxyScheme(final FileSystemOptions opts) { + return getString(opts, PROXY_SCHEME, HttpHost.DEFAULT_SCHEME_NAME); } /** - * Gets whether to follow redirects for the connection. + * Gets the socket timeout. * - * @param opts The FileSystem options. - * @return {@code true} to follow redirects, {@code false} not to. - * @see #setFollowRedirect + * @param opts The FileSystemOptions. + * @return The socket timeout. */ - public boolean getFollowRedirect(final FileSystemOptions opts) { - return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT); + public int getSoTimeout(final FileSystemOptions opts) { + return getInteger(opts, SO_TIMEOUT, DEFAULT_SO_TIMEOUT); } /** - * Sets the maximum number of connections allowed. + * Sets the charset used for url encoding. * * @param opts The FileSystem options. - * @param maxTotalConnections The maximum number of connections. + * @return the chaset */ - public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) { - setParam(opts, MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections)); + public String getUrlCharset(final FileSystemOptions opts) { + return getString(opts, "urlCharset"); } /** - * Gets the maximum number of connections allowed. + * Gets the user agent string * - * @param opts The FileSystemOptions. - * @return The maximum number of connections allowed. + * @param opts the file system options to modify + * @return User provided User-Agent string, otherwise default of: Commons-VFS */ - public int getMaxTotalConnections(final FileSystemOptions opts) { - return getInteger(opts, MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS); + public String getUserAgent(final FileSystemOptions opts) { + final String userAgent = getParam(opts, KEY_USER_AGENT); + return userAgent != null ? userAgent : DEFAULT_USER_AGENT; } /** - * Sets the maximum number of connections allowed to any host. + * Determines if the hostname should be verified in SSL context. * - * @param opts The FileSystem options. - * @param maxHostConnections The maximum number of connections to a host. + * @param opts The FileSystemOptions. + * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. */ - public void setMaxConnectionsPerHost(final FileSystemOptions opts, final int maxHostConnections) { - setParam(opts, MAX_HOST_CONNECTIONS, Integer.valueOf(maxHostConnections)); + public boolean isHostnameVerificationEnabled(final FileSystemOptions opts) { + return getBoolean(opts, HOSTNAME_VERIFICATION_ENABLED, DEFAULT_HOSTNAME_VERIFICATION_ENABLED); } /** - * Gets the maximum number of connections allowed per host. + * Determines if the FileSystemOptions indicate that HTTP Keep-Alive is respected. * * @param opts The FileSystemOptions. - * @return The maximum number of connections allowed per host. + * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. */ - public int getMaxConnectionsPerHost(final FileSystemOptions opts) { - return getInteger(opts, MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS); + public boolean isKeepAlive(final FileSystemOptions opts) { + return getBoolean(opts, KEEP_ALIVE, DEFAULT_KEEP_ALIVE); } /** @@ -404,18 +388,6 @@ public class Http4FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Sets the given value for preemptive HTTP authentication (using BASIC) on the given FileSystemOptions object. - * Defaults to false if not set. It may be appropriate to set to true in cases when the resulting chattiness of the - * conversation outweighs any architectural desire to use a stronger authentication scheme than basic/preemptive. - * - * @param opts The FileSystemOptions. - * @param preemptiveAuth the desired setting; true=enabled and false=disabled. - */ - public void setPreemptiveAuth(final FileSystemOptions opts, final boolean preemptiveAuth) { - setParam(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.valueOf(preemptiveAuth)); - } - - /** * The connection timeout. * * @param opts The FileSystem options. @@ -426,33 +398,34 @@ public class Http4FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Gets the connection timeout. + * The cookies to add to the request. * * @param opts The FileSystem options. - * @return The connection timeout. + * @param cookies An array of Cookies. */ - public int getConnectionTimeout(final FileSystemOptions opts) { - return getInteger(opts, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) { + setParam(opts, "cookies", cookies); } /** - * The socket timeout. + * Sets whether to follow redirects for the connection. * * @param opts The FileSystem options. - * @param soTimeout socket timeout. + * @param redirect {@code true} to follow redirects, {@code false} not to. + * @see #setFollowRedirect */ - public void setSoTimeout(final FileSystemOptions opts, final int soTimeout) { - setParam(opts, SO_TIMEOUT, Integer.valueOf(soTimeout)); + public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) { + setParam(opts, KEY_FOLLOW_REDIRECT, redirect); } /** - * Gets the socket timeout. + * Sets if the hostname should be verified in SSL context. * * @param opts The FileSystemOptions. - * @return The socket timeout. + * @param hostnameVerificationEnabled whether hostname should be verified */ - public int getSoTimeout(final FileSystemOptions opts) { - return getInteger(opts, SO_TIMEOUT, DEFAULT_SO_TIMEOUT); + public void setHostnameVerificationEnabled(final FileSystemOptions opts, final boolean hostnameVerificationEnabled) { + setParam(opts, HOSTNAME_VERIFICATION_ENABLED, Boolean.valueOf(hostnameVerificationEnabled)); } /** @@ -466,114 +439,141 @@ public class Http4FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Determines if the FileSystemOptions indicate that HTTP Keep-Alive is respected. - * - * @param opts The FileSystemOptions. - * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. + * Set keystore file path for SSL connections. + * @param opts the file system options to modify + * @param keyStoreFile keystore file path */ - public boolean isKeepAlive(final FileSystemOptions opts) { - return getBoolean(opts, KEEP_ALIVE, DEFAULT_KEEP_ALIVE); + public void setKeyStoreFile(final FileSystemOptions opts, final String keyStoreFile) { + setParam(opts, KEYSTORE_FILE, keyStoreFile); } /** - * Sets the user agent to attach to the outgoing http methods - * + * Set keystore pass phrase for SSL connecdtions. * @param opts the file system options to modify - * @param userAgent User Agent String + * @param keyStorePass keystore pass phrase for SSL connecdtions */ - public void setUserAgent(final FileSystemOptions opts, final String userAgent) { - setParam(opts, "userAgent", userAgent); + public void setKeyStorePass(final FileSystemOptions opts, final String keyStorePass) { + setParam(opts, KEYSTORE_PASS, keyStorePass); } /** - * Gets the user agent string - * + * Set keystore type for SSL connections. * @param opts the file system options to modify - * @return User provided User-Agent string, otherwise default of: Commons-VFS + * @param keyStoreType keystore type for SSL connections + * @since 2.7.0 */ - public String getUserAgent(final FileSystemOptions opts) { - final String userAgent = getParam(opts, KEY_USER_AGENT); - return userAgent != null ? userAgent : DEFAULT_USER_AGENT; + public void setKeyStoreType(final FileSystemOptions opts, final String keyStoreType) { + setParam(opts, KEYSTORE_TYPE, keyStoreType); } /** - * Set keystore file path for SSL connections. - * @param opts the file system options to modify - * @param keyStoreFile keystore file path + * Sets the maximum number of connections allowed to any host. + * + * @param opts The FileSystem options. + * @param maxHostConnections The maximum number of connections to a host. */ - public void setKeyStoreFile(final FileSystemOptions opts, final String keyStoreFile) { - setParam(opts, KEYSTORE_FILE, keyStoreFile); + public void setMaxConnectionsPerHost(final FileSystemOptions opts, final int maxHostConnections) { + setParam(opts, MAX_HOST_CONNECTIONS, Integer.valueOf(maxHostConnections)); } /** - * Return keystore file path to be used in SSL connections. - * @param opts the file system options to modify - * @return keystore file path to be used in SSL connections + * Sets the maximum number of connections allowed. + * + * @param opts The FileSystem options. + * @param maxTotalConnections The maximum number of connections. */ - public String getKeyStoreFile(final FileSystemOptions opts) { - return getParam(opts, KEYSTORE_FILE); + public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) { + setParam(opts, MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections)); } /** - * Set keystore pass phrase for SSL connecdtions. - * @param opts the file system options to modify - * @param keyStorePass keystore pass phrase for SSL connecdtions + * Sets the given value for preemptive HTTP authentication (using BASIC) on the given FileSystemOptions object. + * Defaults to false if not set. It may be appropriate to set to true in cases when the resulting chattiness of the + * conversation outweighs any architectural desire to use a stronger authentication scheme than basic/preemptive. + * + * @param opts The FileSystemOptions. + * @param preemptiveAuth the desired setting; true=enabled and false=disabled. */ - public void setKeyStorePass(final FileSystemOptions opts, final String keyStorePass) { - setParam(opts, KEYSTORE_PASS, keyStorePass); + public void setPreemptiveAuth(final FileSystemOptions opts, final boolean preemptiveAuth) { + setParam(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.valueOf(preemptiveAuth)); } /** - * Return keystore pass phrase for SSL connections. - * @param opts the file system options to modify - * @return keystore pass phrase for SSL connections + * Sets the proxy authenticator where the system should get the credentials from. + * + * @param opts The FileSystem options. + * @param authenticator The UserAuthenticator. */ - String getKeyStorePass(final FileSystemOptions opts) { - return getParam(opts, KEYSTORE_PASS); + public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) { + setParam(opts, "proxyAuthenticator", authenticator); } /** - * Set keystore type for SSL connections. - * @param opts the file system options to modify - * @param keyStoreType keystore type for SSL connections - * @since 2.7.0 + * Sets the proxy to use for http connection. + * <p> + * You have to set the ProxyPort too if you would like to have the proxy really used. + * </p> + * + * @param opts The FileSystem options. + * @param proxyHost the host + * @see #setProxyPort */ - public void setKeyStoreType(final FileSystemOptions opts, final String keyStoreType) { - setParam(opts, KEYSTORE_TYPE, keyStoreType); + public void setProxyHost(final FileSystemOptions opts, final String proxyHost) { + setParam(opts, "proxyHost", proxyHost); } /** - * Get keystore type for SSL connections. - * @param opts the file system options to modify - * @return keystore type for SSL connections + * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the + * proxy really used. + * + * @param opts The FileSystem options. + * @param proxyPort the port + * @see #setProxyHost + */ + public void setProxyPort(final FileSystemOptions opts, final int proxyPort) { + setParam(opts, "proxyPort", Integer.valueOf(proxyPort)); + } + + /** + * Sets the proxy-scheme to use for http connection. You have to set the ProxyHost too if you would like to have the + * proxy really used. + * + * @param opts The FileSystem options. + * @param proxyScheme the protocol scheme + * @see #setProxyHost * @since 2.7.0 */ - public String getKeyStoreType(final FileSystemOptions opts) { - return getString(opts, KEYSTORE_TYPE, KeyStore.getDefaultType()); + public void setProxyScheme(final FileSystemOptions opts, final String proxyScheme) { + setParam(opts, PROXY_SCHEME, proxyScheme); } /** - * Sets if the hostname should be verified in SSL context. + * The socket timeout. * - * @param opts The FileSystemOptions. - * @param hostnameVerificationEnabled whether hostname should be verified + * @param opts The FileSystem options. + * @param soTimeout socket timeout. */ - public void setHostnameVerificationEnabled(final FileSystemOptions opts, final boolean hostnameVerificationEnabled) { - setParam(opts, HOSTNAME_VERIFICATION_ENABLED, Boolean.valueOf(hostnameVerificationEnabled)); + public void setSoTimeout(final FileSystemOptions opts, final int soTimeout) { + setParam(opts, SO_TIMEOUT, Integer.valueOf(soTimeout)); } /** - * Determines if the hostname should be verified in SSL context. + * Sets the charset used for url encoding. * - * @param opts The FileSystemOptions. - * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. + * @param opts The FileSystem options. + * @param chaset the chaset */ - public boolean isHostnameVerificationEnabled(final FileSystemOptions opts) { - return getBoolean(opts, HOSTNAME_VERIFICATION_ENABLED, DEFAULT_HOSTNAME_VERIFICATION_ENABLED); + public void setUrlCharset(final FileSystemOptions opts, final String chaset) { + setParam(opts, "urlCharset", chaset); } - @Override - protected Class<? extends FileSystem> getConfigClass() { - return Http4FileSystem.class; + /** + * Sets the user agent to attach to the outgoing http methods + * + * @param opts the file system options to modify + * @param userAgent User Agent String + */ + public void setUserAgent(final FileSystemOptions opts, final String userAgent) { + setParam(opts, "userAgent", userAgent); } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4RandomAccessContent.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4RandomAccessContent.java index 2442c5c..912d450 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4RandomAccessContent.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4/Http4RandomAccessContent.java @@ -46,26 +46,12 @@ final class Http4RandomAccessContent<FS extends Http4FileSystem> extends Abstrac } @Override - public long getFilePointer() throws IOException { - return filePointer; - } - - @Override - public void seek(final long pos) throws IOException { - if (pos == filePointer) { - // no change - return; - } - - if (pos < 0) { - throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); - } - + public void close() throws IOException { if (dis != null) { - close(); + dis.close(); + dis = null; + mis = null; } - - filePointer = pos; } @Override @@ -128,16 +114,30 @@ final class Http4RandomAccessContent<FS extends Http4FileSystem> extends Abstrac } @Override - public void close() throws IOException { - if (dis != null) { - dis.close(); - dis = null; - mis = null; - } + public long getFilePointer() throws IOException { + return filePointer; } @Override public long length() throws IOException { return fileObject.getContent().getSize(); } + + @Override + public void seek(final long pos) throws IOException { + if (pos == filePointer) { + // no change + return; + } + + if (pos < 0) { + throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); + } + + if (dis != null) { + close(); + } + + filePointer = pos; + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4s/Http4sFileNameParser.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4s/Http4sFileNameParser.java index 48de477..0291ade 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4s/Http4sFileNameParser.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http4s/Http4sFileNameParser.java @@ -30,11 +30,11 @@ public class Http4sFileNameParser extends GenericURLFileNameParser { private static final Http4sFileNameParser INSTANCE = new Http4sFileNameParser(); - public Http4sFileNameParser() { - super(DEFAULT_PORT); - } - public static FileNameParser getInstance() { return INSTANCE; } + + public Http4sFileNameParser() { + super(DEFAULT_PORT); + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileNameParser.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileNameParser.java index 4eeb8ba..9d89597 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileNameParser.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileNameParser.java @@ -30,11 +30,11 @@ public class Http5FileNameParser extends GenericURLFileNameParser { private static final Http5FileNameParser INSTANCE = new Http5FileNameParser(); - public Http5FileNameParser() { - super(DEFAULT_PORT); - } - public static FileNameParser getInstance() { return INSTANCE; } + + public Http5FileNameParser() { + super(DEFAULT_PORT); + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileObject.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileObject.java index 44ef679..f78dbc3 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileObject.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileObject.java @@ -98,18 +98,8 @@ public class Http5FileObject<FS extends Http5FileSystem> extends AbstractFileObj } @Override - protected FileType doGetType() throws Exception { - lastHeadResponse = executeHttpUriRequest(new HttpHead(getInternalURI())); - final int status = lastHeadResponse.getCode(); - - if (status == HttpStatus.SC_OK - || status == HttpStatus.SC_METHOD_NOT_ALLOWED /* method is not allowed, but resource exist */) { - return FileType.FILE; - } else if (status == HttpStatus.SC_NOT_FOUND || status == HttpStatus.SC_GONE) { - return FileType.IMAGINARY; - } else { - throw new FileSystemException("vfs.provider.http/head.error", getName(), Integer.valueOf(status)); - } + protected void doDetach() throws Exception { + lastHeadResponse = null; } @Override @@ -129,18 +119,6 @@ public class Http5FileObject<FS extends Http5FileSystem> extends AbstractFileObj } @Override - protected long doGetLastModifiedTime() throws Exception { - FileSystemException.requireNonNull(lastHeadResponse, "vfs.provider.http/last-modified.error", getName()); - - final Header header = lastHeadResponse.getFirstHeader("Last-Modified"); - - FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName()); - - return DateUtils.parseDate(header.getValue()).getTime(); - } - - - @Override protected InputStream doGetInputStream(final int bufferSize) throws Exception { final HttpGet getRequest = new HttpGet(getInternalURI()); final ClassicHttpResponse httpResponse = executeHttpUriRequest(getRequest); @@ -157,14 +135,36 @@ public class Http5FileObject<FS extends Http5FileSystem> extends AbstractFileObj return new MonitoredHttpResponseContentInputStream(httpResponse, bufferSize); } + + @Override + protected long doGetLastModifiedTime() throws Exception { + FileSystemException.requireNonNull(lastHeadResponse, "vfs.provider.http/last-modified.error", getName()); + + final Header header = lastHeadResponse.getFirstHeader("Last-Modified"); + + FileSystemException.requireNonNull(header, "vfs.provider.http/last-modified.error", getName()); + + return DateUtils.parseDate(header.getValue()).getTime(); + } + @Override protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception { return new Http5RandomAccessContent<>(this, mode); } @Override - protected String[] doListChildren() throws Exception { - throw new UnsupportedOperationException("Not implemented."); + protected FileType doGetType() throws Exception { + lastHeadResponse = executeHttpUriRequest(new HttpHead(getInternalURI())); + final int status = lastHeadResponse.getCode(); + + if (status == HttpStatus.SC_OK + || status == HttpStatus.SC_METHOD_NOT_ALLOWED /* method is not allowed, but resource exist */) { + return FileType.FILE; + } else if (status == HttpStatus.SC_NOT_FOUND || status == HttpStatus.SC_GONE) { + return FileType.IMAGINARY; + } else { + throw new FileSystemException("vfs.provider.http/head.error", getName(), Integer.valueOf(status)); + } } @Override @@ -173,21 +173,26 @@ public class Http5FileObject<FS extends Http5FileSystem> extends AbstractFileObj } @Override - protected FileContentInfoFactory getFileContentInfoFactory() { - return new Http5FileContentInfoFactory(); - } - - @Override - protected void doDetach() throws Exception { - lastHeadResponse = null; + protected String[] doListChildren() throws Exception { + throw new UnsupportedOperationException("Not implemented."); } /** - * Return URL charset string. - * @return URL charset string + * Execute the request using the given {@code httpRequest} and return a {@code ClassicHttpResponse} from the execution. + * + * @param httpRequest {@code HttpUriRequest} object + * @return {@code ClassicHttpResponse} from the execution + * @throws IOException if IO error occurs */ - protected String getUrlCharset() { - return urlCharset; + protected ClassicHttpResponse executeHttpUriRequest(final HttpUriRequest httpRequest) throws IOException { + final CloseableHttpClient httpClient = (CloseableHttpClient) getAbstractFileSystem().getHttpClient(); + final HttpClientContext httpClientContext = getAbstractFileSystem().getHttpClientContext(); + return httpClient.execute(httpRequest, httpClientContext); + } + + @Override + protected FileContentInfoFactory getFileContentInfoFactory() { + return new Http5FileContentInfoFactory(); } /** @@ -215,16 +220,11 @@ public class Http5FileObject<FS extends Http5FileSystem> extends AbstractFileObj } /** - * Execute the request using the given {@code httpRequest} and return a {@code ClassicHttpResponse} from the execution. - * - * @param httpRequest {@code HttpUriRequest} object - * @return {@code ClassicHttpResponse} from the execution - * @throws IOException if IO error occurs + * Return URL charset string. + * @return URL charset string */ - protected ClassicHttpResponse executeHttpUriRequest(final HttpUriRequest httpRequest) throws IOException { - final CloseableHttpClient httpClient = (CloseableHttpClient) getAbstractFileSystem().getHttpClient(); - final HttpClientContext httpClientContext = getAbstractFileSystem().getHttpClientContext(); - return httpClient.execute(httpRequest, httpClientContext); + protected String getUrlCharset() { + return urlCharset; } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java index 87f129d..2f01431 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileProvider.java @@ -113,35 +113,59 @@ public class Http5FileProvider extends AbstractOriginatingFileProvider { setFileNameParser(Http5FileNameParser.getInstance()); } - @Override - public FileSystemConfigBuilder getConfigBuilder() { - return Http5FileSystemConfigBuilder.getInstance(); - } + private HttpClientConnectionManager createConnectionManager(final Http5FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) throws FileSystemException { - @Override - public Collection<Capability> getCapabilities() { - return capabilities; + final SocketConfig socketConfig = + SocketConfig + .custom() + .setSoTimeout(builder.getSoTimeout(fileSystemOptions), TimeUnit.MILLISECONDS) + .build(); + + final String[] tlsVersions = builder.getTlsVersions(fileSystemOptions).split("\\s*,\\s*"); + + final TLS[] tlsArray = Arrays.stream(tlsVersions).map(TLS::valueOf).toArray(TLS[]::new); + + final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create() + .setSslContext(createSSLContext(builder, fileSystemOptions)) + .setHostnameVerifier(createHostnameVerifier(builder, fileSystemOptions)) + .setTlsVersions(tlsArray) + .build(); + + return PoolingHttpClientConnectionManagerBuilder.create() + .setSSLSocketFactory(sslSocketFactory) + .setMaxConnTotal(builder.getMaxTotalConnections(fileSystemOptions)) + .setMaxConnPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions)) + .setDefaultSocketConfig(socketConfig) + .build(); } - @Override - protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) - throws FileSystemException { - final GenericFileName rootName = (GenericFileName) name; + private CookieStore createDefaultCookieStore(final Http5FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) { + final CookieStore cookieStore = new BasicCookieStore(); + final Cookie[] cookies = builder.getCookies(fileSystemOptions); - UserAuthenticationData authData = null; - HttpClient httpClient; - HttpClientContext httpClientContext; + if (cookies != null) { + Arrays.stream(cookies).forEach(cookieStore::addCookie); + } - try { - final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance(); - authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES); - httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData); - httpClient = createHttpClient(builder, rootName, fileSystemOptions); - } finally { - UserAuthenticatorUtils.cleanup(authData); + return cookieStore; + } + + private RequestConfig createDefaultRequestConfig(final Http5FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) { + return RequestConfig.custom() + .setConnectTimeout(builder.getConnectionTimeout(fileSystemOptions), TimeUnit.MILLISECONDS) + .build(); + } + + private HostnameVerifier createHostnameVerifier(final Http5FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) throws FileSystemException { + if (!builder.isHostnameVerificationEnabled(fileSystemOptions)) { + return NoopHostnameVerifier.INSTANCE; } - return new Http5FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext); + return new DefaultHostnameVerifier(); } /** @@ -193,49 +217,6 @@ public class Http5FileProvider extends AbstractOriginatingFileProvider { } /** - * Create {@link SSLContext} for HttpClient. Invoked by {@link #createHttpClientBuilder(Http5FileSystemConfigBuilder, GenericFileName, FileSystemOptions)}. - * - * @param builder Configuration options builder for HTTP4 provider - * @param fileSystemOptions The FileSystem options - * @return a {@link SSLContext} for HttpClient - * @throws FileSystemException if an error occurs - */ - protected SSLContext createSSLContext(final Http5FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) throws FileSystemException { - try { - final SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); - sslContextBuilder.setKeyStoreType(builder.getKeyStoreType(fileSystemOptions)); - - File keystoreFileObject = null; - final String keystoreFile = builder.getKeyStoreFile(fileSystemOptions); - - if (keystoreFile != null && !keystoreFile.isEmpty()) { - keystoreFileObject = new File(keystoreFile); - } - - if (keystoreFileObject != null && keystoreFileObject.exists()) { - final String keystorePass = builder.getKeyStorePass(fileSystemOptions); - final char[] keystorePassChars = (keystorePass != null) ? keystorePass.toCharArray() : null; - sslContextBuilder.loadTrustMaterial(keystoreFileObject, keystorePassChars, TrustAllStrategy.INSTANCE); - } else { - sslContextBuilder.loadTrustMaterial(TrustAllStrategy.INSTANCE); - } - - return sslContextBuilder.build(); - } catch (final KeyStoreException e) { - throw new FileSystemException("Keystore error. " + e.getMessage(), e); - } catch (final KeyManagementException e) { - throw new FileSystemException("Cannot retrieve keys. " + e.getMessage(), e); - } catch (final NoSuchAlgorithmException e) { - throw new FileSystemException("Algorithm error. " + e.getMessage(), e); - } catch (final CertificateException e) { - throw new FileSystemException("Certificate error. " + e.getMessage(), e); - } catch (final IOException e) { - throw new FileSystemException("Cannot open key file. " + e.getMessage(), e); - } - } - - /** * Create an {@link HttpClientContext} object for an http4 file system. * * @param builder Configuration options builder for http4 provider @@ -297,49 +278,89 @@ public class Http5FileProvider extends AbstractOriginatingFileProvider { return clientContext; } - private HttpClientConnectionManager createConnectionManager(final Http5FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) throws FileSystemException { + private HttpRoutePlanner createHttpRoutePlanner(final Http5FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) { + final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions); - final SocketConfig socketConfig = - SocketConfig - .custom() - .setSoTimeout(builder.getSoTimeout(fileSystemOptions), TimeUnit.MILLISECONDS) - .build(); + if (proxyHost != null) { + return new DefaultProxyRoutePlanner(proxyHost); + } - final String[] tlsVersions = builder.getTlsVersions(fileSystemOptions).split("\\s*,\\s*"); + return new SystemDefaultRoutePlanner(ProxySelector.getDefault()); + } - final TLS[] tlsArray = Arrays.stream(tlsVersions).map(TLS::valueOf).toArray(TLS[]::new); + /** + * Create {@link SSLContext} for HttpClient. Invoked by {@link #createHttpClientBuilder(Http5FileSystemConfigBuilder, GenericFileName, FileSystemOptions)}. + * + * @param builder Configuration options builder for HTTP4 provider + * @param fileSystemOptions The FileSystem options + * @return a {@link SSLContext} for HttpClient + * @throws FileSystemException if an error occurs + */ + protected SSLContext createSSLContext(final Http5FileSystemConfigBuilder builder, + final FileSystemOptions fileSystemOptions) throws FileSystemException { + try { + final SSLContextBuilder sslContextBuilder = new SSLContextBuilder(); + sslContextBuilder.setKeyStoreType(builder.getKeyStoreType(fileSystemOptions)); - final SSLConnectionSocketFactory sslSocketFactory = SSLConnectionSocketFactoryBuilder.create() - .setSslContext(createSSLContext(builder, fileSystemOptions)) - .setHostnameVerifier(createHostnameVerifier(builder, fileSystemOptions)) - .setTlsVersions(tlsArray) - .build(); + File keystoreFileObject = null; + final String keystoreFile = builder.getKeyStoreFile(fileSystemOptions); - return PoolingHttpClientConnectionManagerBuilder.create() - .setSSLSocketFactory(sslSocketFactory) - .setMaxConnTotal(builder.getMaxTotalConnections(fileSystemOptions)) - .setMaxConnPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions)) - .setDefaultSocketConfig(socketConfig) - .build(); - } + if (keystoreFile != null && !keystoreFile.isEmpty()) { + keystoreFileObject = new File(keystoreFile); + } - private RequestConfig createDefaultRequestConfig(final Http5FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) { - return RequestConfig.custom() - .setConnectTimeout(builder.getConnectionTimeout(fileSystemOptions), TimeUnit.MILLISECONDS) - .build(); + if (keystoreFileObject != null && keystoreFileObject.exists()) { + final String keystorePass = builder.getKeyStorePass(fileSystemOptions); + final char[] keystorePassChars = (keystorePass != null) ? keystorePass.toCharArray() : null; + sslContextBuilder.loadTrustMaterial(keystoreFileObject, keystorePassChars, TrustAllStrategy.INSTANCE); + } else { + sslContextBuilder.loadTrustMaterial(TrustAllStrategy.INSTANCE); + } + + return sslContextBuilder.build(); + } catch (final KeyStoreException e) { + throw new FileSystemException("Keystore error. " + e.getMessage(), e); + } catch (final KeyManagementException e) { + throw new FileSystemException("Cannot retrieve keys. " + e.getMessage(), e); + } catch (final NoSuchAlgorithmException e) { + throw new FileSystemException("Algorithm error. " + e.getMessage(), e); + } catch (final CertificateException e) { + throw new FileSystemException("Certificate error. " + e.getMessage(), e); + } catch (final IOException e) { + throw new FileSystemException("Cannot open key file. " + e.getMessage(), e); + } } - private HttpRoutePlanner createHttpRoutePlanner(final Http5FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) { - final HttpHost proxyHost = getProxyHttpHost(builder, fileSystemOptions); + @Override + protected FileSystem doCreateFileSystem(final FileName name, final FileSystemOptions fileSystemOptions) + throws FileSystemException { + final GenericFileName rootName = (GenericFileName) name; - if (proxyHost != null) { - return new DefaultProxyRoutePlanner(proxyHost); + UserAuthenticationData authData = null; + HttpClient httpClient; + HttpClientContext httpClientContext; + + try { + final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance(); + authData = UserAuthenticatorUtils.authenticate(fileSystemOptions, AUTHENTICATOR_TYPES); + httpClientContext = createHttpClientContext(builder, rootName, fileSystemOptions, authData); + httpClient = createHttpClient(builder, rootName, fileSystemOptions); + } finally { + UserAuthenticatorUtils.cleanup(authData); } - return new SystemDefaultRoutePlanner(ProxySelector.getDefault()); + return new Http5FileSystem(rootName, fileSystemOptions, httpClient, httpClientContext); + } + + @Override + public Collection<Capability> getCapabilities() { + return capabilities; + } + + @Override + public FileSystemConfigBuilder getConfigBuilder() { + return Http5FileSystemConfigBuilder.getInstance(); } private HttpHost getProxyHttpHost(final Http5FileSystemConfigBuilder builder, @@ -355,25 +376,4 @@ public class Http5FileProvider extends AbstractOriginatingFileProvider { return null; } - private CookieStore createDefaultCookieStore(final Http5FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) { - final CookieStore cookieStore = new BasicCookieStore(); - final Cookie[] cookies = builder.getCookies(fileSystemOptions); - - if (cookies != null) { - Arrays.stream(cookies).forEach(cookieStore::addCookie); - } - - return cookieStore; - } - - private HostnameVerifier createHostnameVerifier(final Http5FileSystemConfigBuilder builder, - final FileSystemOptions fileSystemOptions) throws FileSystemException { - if (!builder.isHostnameVerificationEnabled(fileSystemOptions)) { - return NoopHostnameVerifier.INSTANCE; - } - - return new DefaultHostnameVerifier(); - } - } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java index 5c5aabd..b269377 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystem.java @@ -80,13 +80,13 @@ public class Http5FileSystem extends AbstractFileSystem { } @Override - protected FileObject createFile(final AbstractFileName name) throws Exception { - return new Http5FileObject<>(name, this); + protected void addCapabilities(final Collection<Capability> caps) { + caps.addAll(Http5FileProvider.capabilities); } @Override - protected void addCapabilities(final Collection<Capability> caps) { - caps.addAll(Http5FileProvider.capabilities); + protected FileObject createFile(final AbstractFileName name) throws Exception { + return new Http5FileObject<>(name, this); } @Override diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystemConfigBuilder.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystemConfigBuilder.java index 53b327b..657cefc 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystemConfigBuilder.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5FileSystemConfigBuilder.java @@ -191,6 +191,19 @@ public class Http5FileSystemConfigBuilder extends FileSystemConfigBuilder { private static final String DEFAULT_TLS_VERSIONS = "V_1_2"; /** + * Gets the singleton builder. + * + * @return the singleton builder. + */ + public static Http5FileSystemConfigBuilder getInstance() { + return BUILDER; + } + + private Http5FileSystemConfigBuilder() { + super("http."); + } + + /** * Construct an {@code Http4FileSystemConfigBuilder}. * * @param prefix String for properties of this file system. @@ -199,76 +212,98 @@ public class Http5FileSystemConfigBuilder extends FileSystemConfigBuilder { super(prefix); } - private Http5FileSystemConfigBuilder() { - super("http."); + @Override + protected Class<? extends FileSystem> getConfigClass() { + return Http5FileSystem.class; } /** - * Gets the singleton builder. + * Gets the connection timeout. * - * @return the singleton builder. + * @param opts The FileSystem options. + * @return The connection timeout. */ - public static Http5FileSystemConfigBuilder getInstance() { - return BUILDER; + public int getConnectionTimeout(final FileSystemOptions opts) { + return getInteger(opts, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); } /** - * Sets the charset used for url encoding. + * Gets the cookies to add to the request. * * @param opts The FileSystem options. - * @param chaset the chaset + * @return the Cookie array. */ - public void setUrlCharset(final FileSystemOptions opts, final String chaset) { - setParam(opts, "urlCharset", chaset); + public Cookie[] getCookies(final FileSystemOptions opts) { + return getParam(opts, "cookies"); } /** - * Sets the charset used for url encoding. + * Gets whether to follow redirects for the connection. * * @param opts The FileSystem options. - * @return the chaset + * @return {@code true} to follow redirects, {@code false} not to. + * @see #setFollowRedirect */ - public String getUrlCharset(final FileSystemOptions opts) { - return getString(opts, "urlCharset"); + public boolean getFollowRedirect(final FileSystemOptions opts) { + return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT); } /** - * Sets the proxy to use for http connection. - * <p> - * You have to set the ProxyPort too if you would like to have the proxy really used. - * </p> + * Return keystore file path to be used in SSL connections. + * @param opts the file system options to modify + * @return keystore file path to be used in SSL connections + */ + public String getKeyStoreFile(final FileSystemOptions opts) { + return getParam(opts, KEYSTORE_FILE); + } + + /** + * Return keystore pass phrase for SSL connections. + * @param opts the file system options to modify + * @return keystore pass phrase for SSL connections + */ + String getKeyStorePass(final FileSystemOptions opts) { + return getParam(opts, KEYSTORE_PASS); + } + + /** + * Get keystore type for SSL connections. + * @param opts the file system options to modify + * @return keystore type for SSL connections + * @since 2.7.0 + */ + public String getKeyStoreType(final FileSystemOptions opts) { + return getString(opts, KEYSTORE_TYPE, KeyStore.getDefaultType()); + } + + /** + * Gets the maximum number of connections allowed per host. * - * @param opts The FileSystem options. - * @param proxyHost the host - * @see #setProxyPort + * @param opts The FileSystemOptions. + * @return The maximum number of connections allowed per host. */ - public void setProxyHost(final FileSystemOptions opts, final String proxyHost) { - setParam(opts, "proxyHost", proxyHost); + public int getMaxConnectionsPerHost(final FileSystemOptions opts) { + return getInteger(opts, MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS); } /** - * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the - * proxy really used. + * Gets the maximum number of connections allowed. * - * @param opts The FileSystem options. - * @param proxyPort the port - * @see #setProxyHost + * @param opts The FileSystemOptions. + * @return The maximum number of connections allowed. */ - public void setProxyPort(final FileSystemOptions opts, final int proxyPort) { - setParam(opts, "proxyPort", Integer.valueOf(proxyPort)); + public int getMaxTotalConnections(final FileSystemOptions opts) { + return getInteger(opts, MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS); } /** - * Sets the proxy-scheme to use for http connection. You have to set the ProxyHost too if you would like to have the - * proxy really used. + * Gets the proxy authenticator where the system should get the credentials from. * * @param opts The FileSystem options. - * @param proxyScheme the protocol scheme - * @see #setProxyHost - * @since 2.7.0 + * @return The UserAuthenticator. */ - public void setProxyScheme(final FileSystemOptions opts, final String proxyScheme) { - setParam(opts, PROXY_SCHEME, proxyScheme); + public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) { + return getParam(opts, "proxyAuthenticator"); } /** @@ -309,105 +344,66 @@ public class Http5FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Sets the proxy authenticator where the system should get the credentials from. - * - * @param opts The FileSystem options. - * @param authenticator The UserAuthenticator. - */ - public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) { - setParam(opts, "proxyAuthenticator", authenticator); - } - - /** - * Gets the proxy authenticator where the system should get the credentials from. - * - * @param opts The FileSystem options. - * @return The UserAuthenticator. - */ - public UserAuthenticator getProxyAuthenticator(final FileSystemOptions opts) { - return getParam(opts, "proxyAuthenticator"); - } - - /** - * The cookies to add to the request. - * - * @param opts The FileSystem options. - * @param cookies An array of Cookies. - */ - public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) { - setParam(opts, "cookies", cookies); - } - - /** - * Sets whether to follow redirects for the connection. + * Gets the socket timeout. * - * @param opts The FileSystem options. - * @param redirect {@code true} to follow redirects, {@code false} not to. - * @see #setFollowRedirect + * @param opts The FileSystemOptions. + * @return The socket timeout. */ - public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) { - setParam(opts, KEY_FOLLOW_REDIRECT, redirect); + public int getSoTimeout(final FileSystemOptions opts) { + return getInteger(opts, SO_TIMEOUT, DEFAULT_SO_TIMEOUT); } /** - * Gets the cookies to add to the request. + * Gets the enabled TLS versions as a comma separated string, each token of which is the name of + * {@code org.apache.hc.core5.http.ssl.TLS} enum. e.g, "V_1_2, V_1_3". * - * @param opts The FileSystem options. - * @return the Cookie array. + * @param opts the file system options to modify + * @return enabled TLS versions */ - public Cookie[] getCookies(final FileSystemOptions opts) { - return getParam(opts, "cookies"); + public String getTlsVersions(final FileSystemOptions opts) { + final String tlsVersions = getParam(opts, KEY_TLS_VERSIONS); + return tlsVersions != null ? tlsVersions : DEFAULT_TLS_VERSIONS; } /** - * Gets whether to follow redirects for the connection. + * Sets the charset used for url encoding. * * @param opts The FileSystem options. - * @return {@code true} to follow redirects, {@code false} not to. - * @see #setFollowRedirect + * @return the chaset */ - public boolean getFollowRedirect(final FileSystemOptions opts) { - return getBoolean(opts, KEY_FOLLOW_REDIRECT, DEFAULT_FOLLOW_REDIRECT); + public String getUrlCharset(final FileSystemOptions opts) { + return getString(opts, "urlCharset"); } /** - * Sets the maximum number of connections allowed. + * Gets the user agent string * - * @param opts The FileSystem options. - * @param maxTotalConnections The maximum number of connections. + * @param opts the file system options to modify + * @return User provided User-Agent string, otherwise default of: Commons-VFS */ - public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) { - setParam(opts, MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections)); + public String getUserAgent(final FileSystemOptions opts) { + final String userAgent = getParam(opts, KEY_USER_AGENT); + return userAgent != null ? userAgent : DEFAULT_USER_AGENT; } /** - * Gets the maximum number of connections allowed. + * Determines if the hostname should be verified in SSL context. * * @param opts The FileSystemOptions. - * @return The maximum number of connections allowed. - */ - public int getMaxTotalConnections(final FileSystemOptions opts) { - return getInteger(opts, MAX_TOTAL_CONNECTIONS, DEFAULT_MAX_CONNECTIONS); - } - - /** - * Sets the maximum number of connections allowed to any host. - * - * @param opts The FileSystem options. - * @param maxHostConnections The maximum number of connections to a host. + * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. */ - public void setMaxConnectionsPerHost(final FileSystemOptions opts, final int maxHostConnections) { - setParam(opts, MAX_HOST_CONNECTIONS, Integer.valueOf(maxHostConnections)); + public boolean isHostnameVerificationEnabled(final FileSystemOptions opts) { + return getBoolean(opts, HOSTNAME_VERIFICATION_ENABLED, DEFAULT_HOSTNAME_VERIFICATION_ENABLED); } /** - * Gets the maximum number of connections allowed per host. + * Determines if the FileSystemOptions indicate that HTTP Keep-Alive is respected. * * @param opts The FileSystemOptions. - * @return The maximum number of connections allowed per host. + * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. */ - public int getMaxConnectionsPerHost(final FileSystemOptions opts) { - return getInteger(opts, MAX_HOST_CONNECTIONS, DEFAULT_MAX_HOST_CONNECTIONS); + public boolean isKeepAlive(final FileSystemOptions opts) { + return getBoolean(opts, KEEP_ALIVE, DEFAULT_KEEP_ALIVE); } /** @@ -421,18 +417,6 @@ public class Http5FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Sets the given value for preemptive HTTP authentication (using BASIC) on the given FileSystemOptions object. - * Defaults to false if not set. It may be appropriate to set to true in cases when the resulting chattiness of the - * conversation outweighs any architectural desire to use a stronger authentication scheme than basic/preemptive. - * - * @param opts The FileSystemOptions. - * @param preemptiveAuth the desired setting; true=enabled and false=disabled. - */ - public void setPreemptiveAuth(final FileSystemOptions opts, final boolean preemptiveAuth) { - setParam(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.valueOf(preemptiveAuth)); - } - - /** * The connection timeout. * * @param opts The FileSystem options. @@ -443,33 +427,34 @@ public class Http5FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Gets the connection timeout. + * The cookies to add to the request. * * @param opts The FileSystem options. - * @return The connection timeout. + * @param cookies An array of Cookies. */ - public int getConnectionTimeout(final FileSystemOptions opts) { - return getInteger(opts, CONNECTION_TIMEOUT, DEFAULT_CONNECTION_TIMEOUT); + public void setCookies(final FileSystemOptions opts, final Cookie[] cookies) { + setParam(opts, "cookies", cookies); } /** - * The socket timeout. + * Sets whether to follow redirects for the connection. * * @param opts The FileSystem options. - * @param soTimeout socket timeout. + * @param redirect {@code true} to follow redirects, {@code false} not to. + * @see #setFollowRedirect */ - public void setSoTimeout(final FileSystemOptions opts, final int soTimeout) { - setParam(opts, SO_TIMEOUT, Integer.valueOf(soTimeout)); + public void setFollowRedirect(final FileSystemOptions opts, final boolean redirect) { + setParam(opts, KEY_FOLLOW_REDIRECT, redirect); } /** - * Gets the socket timeout. + * Sets if the hostname should be verified in SSL context. * * @param opts The FileSystemOptions. - * @return The socket timeout. + * @param hostnameVerificationEnabled whether hostname should be verified */ - public int getSoTimeout(final FileSystemOptions opts) { - return getInteger(opts, SO_TIMEOUT, DEFAULT_SO_TIMEOUT); + public void setHostnameVerificationEnabled(final FileSystemOptions opts, final boolean hostnameVerificationEnabled) { + setParam(opts, HOSTNAME_VERIFICATION_ENABLED, Boolean.valueOf(hostnameVerificationEnabled)); } /** @@ -483,109 +468,121 @@ public class Http5FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Determines if the FileSystemOptions indicate that HTTP Keep-Alive is respected. - * - * @param opts The FileSystemOptions. - * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. + * Set keystore file path for SSL connections. + * @param opts the file system options to modify + * @param keyStoreFile keystore file path */ - public boolean isKeepAlive(final FileSystemOptions opts) { - return getBoolean(opts, KEEP_ALIVE, DEFAULT_KEEP_ALIVE); + public void setKeyStoreFile(final FileSystemOptions opts, final String keyStoreFile) { + setParam(opts, KEYSTORE_FILE, keyStoreFile); } /** - * Sets the user agent to attach to the outgoing http methods - * + * Set keystore pass phrase for SSL connecdtions. * @param opts the file system options to modify - * @param userAgent User Agent String + * @param keyStorePass keystore pass phrase for SSL connecdtions */ - public void setUserAgent(final FileSystemOptions opts, final String userAgent) { - setParam(opts, "userAgent", userAgent); + public void setKeyStorePass(final FileSystemOptions opts, final String keyStorePass) { + setParam(opts, KEYSTORE_PASS, keyStorePass); } /** - * Gets the user agent string - * + * Set keystore type for SSL connections. * @param opts the file system options to modify - * @return User provided User-Agent string, otherwise default of: Commons-VFS + * @param keyStoreType keystore type for SSL connections + * @since 2.7.0 */ - public String getUserAgent(final FileSystemOptions opts) { - final String userAgent = getParam(opts, KEY_USER_AGENT); - return userAgent != null ? userAgent : DEFAULT_USER_AGENT; + public void setKeyStoreType(final FileSystemOptions opts, final String keyStoreType) { + setParam(opts, KEYSTORE_TYPE, keyStoreType); } /** - * Set keystore file path for SSL connections. - * @param opts the file system options to modify - * @param keyStoreFile keystore file path + * Sets the maximum number of connections allowed to any host. + * + * @param opts The FileSystem options. + * @param maxHostConnections The maximum number of connections to a host. */ - public void setKeyStoreFile(final FileSystemOptions opts, final String keyStoreFile) { - setParam(opts, KEYSTORE_FILE, keyStoreFile); + public void setMaxConnectionsPerHost(final FileSystemOptions opts, final int maxHostConnections) { + setParam(opts, MAX_HOST_CONNECTIONS, Integer.valueOf(maxHostConnections)); } /** - * Return keystore file path to be used in SSL connections. - * @param opts the file system options to modify - * @return keystore file path to be used in SSL connections + * Sets the maximum number of connections allowed. + * + * @param opts The FileSystem options. + * @param maxTotalConnections The maximum number of connections. */ - public String getKeyStoreFile(final FileSystemOptions opts) { - return getParam(opts, KEYSTORE_FILE); + public void setMaxTotalConnections(final FileSystemOptions opts, final int maxTotalConnections) { + setParam(opts, MAX_TOTAL_CONNECTIONS, Integer.valueOf(maxTotalConnections)); } /** - * Set keystore pass phrase for SSL connecdtions. - * @param opts the file system options to modify - * @param keyStorePass keystore pass phrase for SSL connecdtions + * Sets the given value for preemptive HTTP authentication (using BASIC) on the given FileSystemOptions object. + * Defaults to false if not set. It may be appropriate to set to true in cases when the resulting chattiness of the + * conversation outweighs any architectural desire to use a stronger authentication scheme than basic/preemptive. + * + * @param opts The FileSystemOptions. + * @param preemptiveAuth the desired setting; true=enabled and false=disabled. */ - public void setKeyStorePass(final FileSystemOptions opts, final String keyStorePass) { - setParam(opts, KEYSTORE_PASS, keyStorePass); + public void setPreemptiveAuth(final FileSystemOptions opts, final boolean preemptiveAuth) { + setParam(opts, KEY_PREEMPTIVE_AUTHENTICATION, Boolean.valueOf(preemptiveAuth)); } /** - * Return keystore pass phrase for SSL connections. - * @param opts the file system options to modify - * @return keystore pass phrase for SSL connections + * Sets the proxy authenticator where the system should get the credentials from. + * + * @param opts The FileSystem options. + * @param authenticator The UserAuthenticator. */ - String getKeyStorePass(final FileSystemOptions opts) { - return getParam(opts, KEYSTORE_PASS); + public void setProxyAuthenticator(final FileSystemOptions opts, final UserAuthenticator authenticator) { + setParam(opts, "proxyAuthenticator", authenticator); } /** - * Set keystore type for SSL connections. - * @param opts the file system options to modify - * @param keyStoreType keystore type for SSL connections - * @since 2.7.0 + * Sets the proxy to use for http connection. + * <p> + * You have to set the ProxyPort too if you would like to have the proxy really used. + * </p> + * + * @param opts The FileSystem options. + * @param proxyHost the host + * @see #setProxyPort */ - public void setKeyStoreType(final FileSystemOptions opts, final String keyStoreType) { - setParam(opts, KEYSTORE_TYPE, keyStoreType); + public void setProxyHost(final FileSystemOptions opts, final String proxyHost) { + setParam(opts, "proxyHost", proxyHost); } /** - * Get keystore type for SSL connections. - * @param opts the file system options to modify - * @return keystore type for SSL connections - * @since 2.7.0 + * Sets the proxy-port to use for http connection. You have to set the ProxyHost too if you would like to have the + * proxy really used. + * + * @param opts The FileSystem options. + * @param proxyPort the port + * @see #setProxyHost */ - public String getKeyStoreType(final FileSystemOptions opts) { - return getString(opts, KEYSTORE_TYPE, KeyStore.getDefaultType()); + public void setProxyPort(final FileSystemOptions opts, final int proxyPort) { + setParam(opts, "proxyPort", Integer.valueOf(proxyPort)); } /** - * Sets if the hostname should be verified in SSL context. + * Sets the proxy-scheme to use for http connection. You have to set the ProxyHost too if you would like to have the + * proxy really used. * - * @param opts The FileSystemOptions. - * @param hostnameVerificationEnabled whether hostname should be verified + * @param opts The FileSystem options. + * @param proxyScheme the protocol scheme + * @see #setProxyHost + * @since 2.7.0 */ - public void setHostnameVerificationEnabled(final FileSystemOptions opts, final boolean hostnameVerificationEnabled) { - setParam(opts, HOSTNAME_VERIFICATION_ENABLED, Boolean.valueOf(hostnameVerificationEnabled)); + public void setProxyScheme(final FileSystemOptions opts, final String proxyScheme) { + setParam(opts, PROXY_SCHEME, proxyScheme); } /** - * Determines if the hostname should be verified in SSL context. + * The socket timeout. * - * @param opts The FileSystemOptions. - * @return true if if the FileSystemOptions indicate that HTTP Keep-Alive is respected. + * @param opts The FileSystem options. + * @param soTimeout socket timeout. */ - public boolean isHostnameVerificationEnabled(final FileSystemOptions opts) { - return getBoolean(opts, HOSTNAME_VERIFICATION_ENABLED, DEFAULT_HOSTNAME_VERIFICATION_ENABLED); + public void setSoTimeout(final FileSystemOptions opts, final int soTimeout) { + setParam(opts, SO_TIMEOUT, Integer.valueOf(soTimeout)); } /** @@ -600,19 +597,22 @@ public class Http5FileSystemConfigBuilder extends FileSystemConfigBuilder { } /** - * Gets the enabled TLS versions as a comma separated string, each token of which is the name of - * {@code org.apache.hc.core5.http.ssl.TLS} enum. e.g, "V_1_2, V_1_3". + * Sets the charset used for url encoding. * - * @param opts the file system options to modify - * @return enabled TLS versions + * @param opts The FileSystem options. + * @param chaset the chaset */ - public String getTlsVersions(final FileSystemOptions opts) { - final String tlsVersions = getParam(opts, KEY_TLS_VERSIONS); - return tlsVersions != null ? tlsVersions : DEFAULT_TLS_VERSIONS; + public void setUrlCharset(final FileSystemOptions opts, final String chaset) { + setParam(opts, "urlCharset", chaset); } - @Override - protected Class<? extends FileSystem> getConfigClass() { - return Http5FileSystem.class; + /** + * Sets the user agent to attach to the outgoing http methods + * + * @param opts the file system options to modify + * @param userAgent User Agent String + */ + public void setUserAgent(final FileSystemOptions opts, final String userAgent) { + setParam(opts, "userAgent", userAgent); } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5RandomAccessContent.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5RandomAccessContent.java index 7adcaac..ec8e345 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5RandomAccessContent.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5/Http5RandomAccessContent.java @@ -46,26 +46,12 @@ final class Http5RandomAccessContent<FS extends Http5FileSystem> extends Abstrac } @Override - public long getFilePointer() throws IOException { - return filePointer; - } - - @Override - public void seek(final long pos) throws IOException { - if (pos == filePointer) { - // no change - return; - } - - if (pos < 0) { - throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); - } - + public void close() throws IOException { if (dis != null) { - close(); + dis.close(); + dis = null; + mis = null; } - - filePointer = pos; } @Override @@ -128,16 +114,30 @@ final class Http5RandomAccessContent<FS extends Http5FileSystem> extends Abstrac } @Override - public void close() throws IOException { - if (dis != null) { - dis.close(); - dis = null; - mis = null; - } + public long getFilePointer() throws IOException { + return filePointer; } @Override public long length() throws IOException { return fileObject.getContent().getSize(); } + + @Override + public void seek(final long pos) throws IOException { + if (pos == filePointer) { + // no change + return; + } + + if (pos < 0) { + throw new FileSystemException("vfs.provider/random-access-invalid-position.error", Long.valueOf(pos)); + } + + if (dis != null) { + close(); + } + + filePointer = pos; + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5s/Http5sFileNameParser.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5s/Http5sFileNameParser.java index 13a5f44..9d4c7d8 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5s/Http5sFileNameParser.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/http5s/Http5sFileNameParser.java @@ -30,11 +30,11 @@ public class Http5sFileNameParser extends GenericURLFileNameParser { private static final Http5sFileNameParser INSTANCE = new Http5sFileNameParser(); - public Http5sFileNameParser() { - super(DEFAULT_PORT); - } - public static FileNameParser getInstance() { return INSTANCE; } + + public Http5sFileNameParser() { + super(DEFAULT_PORT); + } } diff --git a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/https/HttpsFileNameParser.java b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/https/HttpsFileNameParser.java index a7b292b..11fb874 100644 --- a/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/https/HttpsFileNameParser.java +++ b/commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/https/HttpsFileNameParser.java @@ -27,11 +27,11 @@ public class HttpsFileNameParser extends URLFileNameParser { private static final HttpsFileNameParser INSTANCE = new HttpsFileNameParser(); - public HttpsFileNameParser() { - super(DEFAULT_PORT); - } - public static FileNameParser getInstance() { return INSTANCE; } + + public HttpsFileNameParser() { + super(DEFAULT_PORT); + } } diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/GetContentInfoFunctionalTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/GetContentInfoFunctionalTest.java index 10a3e7f..fc5efec 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/GetContentInfoFunctionalTest.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/GetContentInfoFunctionalTest.java @@ -35,23 +35,6 @@ import org.junit.Test; */ public class GetContentInfoFunctionalTest { - /** - * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo(). - * - * @throws FileSystemException thrown when the getContentInfo API fails. - */ - @Test - public void testGetContentInfo() throws FileSystemException, MalformedURLException { - final FileSystemManager fsManager = VFS.getManager(); - final String uri = "http://www.apache.org/licenses/LICENSE-2.0.txt"; - try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy()); - final FileContent content = fo.getContent()) { - Assert.assertNotNull(content); - // Used to NPE before fix: - content.getContentInfo(); - } - } - FileSystemOptions getOptionsWithProxy() throws MalformedURLException { // get proxy host and port from env var "https_proxy" String proxyHost = null; @@ -75,4 +58,21 @@ public class GetContentInfoFunctionalTest { builder.setProxyPort(opts, proxyPort); return opts; } + + /** + * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo(). + * + * @throws FileSystemException thrown when the getContentInfo API fails. + */ + @Test + public void testGetContentInfo() throws FileSystemException, MalformedURLException { + final FileSystemManager fsManager = VFS.getManager(); + final String uri = "http://www.apache.org/licenses/LICENSE-2.0.txt"; + try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy()); + final FileContent content = fo.getContent()) { + Assert.assertNotNull(content); + // Used to NPE before fix: + content.getContentInfo(); + } + } } diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/HttpProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/HttpProviderTestCase.java index f701f96..f24bbd6 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/HttpProviderTestCase.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http/HttpProviderTestCase.java @@ -126,41 +126,6 @@ public class HttpProviderTestCase extends AbstractProviderTestConfig { return manager.resolveFile(uri); } - /** - * Prepares the file system manager. - */ - @Override - public void prepare(final DefaultFileSystemManager manager) throws Exception { - manager.addProvider("http", new HttpFileProvider()); - } - - private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException { - VFS.getManager().getFilesCache().close(); - final FileSystemOptions opts = new FileSystemOptions(); - HttpFileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect); - try (final FileObject file = VFS.getManager().resolveFile(uri, opts)) { - checkReadTestsFolder(file); - } catch (final FileNotFolderException e) { - // Expected: VFS HTTP does not support listing children yet. - } - } - - public void testResloveFolderSlashNoRedirectOff() throws FileSystemException { - testResloveFolderSlash(ConnectionUri + "/read-tests", false); - } - - public void testResloveFolderSlashNoRedirectOn() throws FileSystemException { - testResloveFolderSlash(ConnectionUri + "/read-tests", true); - } - - public void testResloveFolderSlashYesRedirectOff() throws FileSystemException { - testResloveFolderSlash(ConnectionUri + "/read-tests/", false); - } - - public void testResloveFolderSlashYesRedirectOn() throws FileSystemException { - testResloveFolderSlash(ConnectionUri + "/read-tests/", true); - } - // Test no longer passing 2016/04/28 public void ignoreTestHttp405() throws FileSystemException { try (final FileObject fileObject = VFS.getManager() @@ -171,6 +136,14 @@ public class HttpProviderTestCase extends AbstractProviderTestConfig { } } + /** + * Prepares the file system manager. + */ + @Override + public void prepare(final DefaultFileSystemManager manager) throws Exception { + manager.addProvider("http", new HttpFileProvider()); + } + /** Ensure VFS-453 options are present. */ public void testHttpTimeoutConfig() { final FileSystemOptions options = new FileSystemOptions(); @@ -206,4 +179,31 @@ public class HttpProviderTestCase extends AbstractProviderTestConfig { // TODO: should also check the created HTTPClient } + + private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException { + VFS.getManager().getFilesCache().close(); + final FileSystemOptions opts = new FileSystemOptions(); + HttpFileSystemConfigBuilder.getInstance().setFollowRedirect(opts, followRedirect); + try (final FileObject file = VFS.getManager().resolveFile(uri, opts)) { + checkReadTestsFolder(file); + } catch (final FileNotFolderException e) { + // Expected: VFS HTTP does not support listing children yet. + } + } + + public void testResloveFolderSlashNoRedirectOff() throws FileSystemException { + testResloveFolderSlash(ConnectionUri + "/read-tests", false); + } + + public void testResloveFolderSlashNoRedirectOn() throws FileSystemException { + testResloveFolderSlash(ConnectionUri + "/read-tests", true); + } + + public void testResloveFolderSlashYesRedirectOff() throws FileSystemException { + testResloveFolderSlash(ConnectionUri + "/read-tests/", false); + } + + public void testResloveFolderSlashYesRedirectOn() throws FileSystemException { + testResloveFolderSlash(ConnectionUri + "/read-tests/", true); + } } diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java index ea65b56..89a7c86 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4/Http4ProviderTestCase.java @@ -124,6 +124,14 @@ public class Http4ProviderTestCase extends AbstractProviderTestConfig { return manager.resolveFile(uri); } + // Test no longer passing 2016/04/28 + public void ignoreTestHttp405() throws FileSystemException { + @SuppressWarnings("resource") // getManager() returns a global. + final FileObject fileObject = VFS.getManager() + .resolveFile("http4://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL"); + assert !fileObject.getContent().isEmpty(); + } + /** * Prepares the file system manager. */ @@ -134,6 +142,27 @@ public class Http4ProviderTestCase extends AbstractProviderTestConfig { } } + /** Ensure VFS-453 options are present. */ + public void testHttpTimeoutConfig() { + final FileSystemOptions opts = new FileSystemOptions(); + final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance(); + + // ensure defaults are 0 + assertEquals(0, builder.getConnectionTimeout(opts)); + assertEquals(0, builder.getSoTimeout(opts)); + assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts)); + + builder.setConnectionTimeout(opts, 60000); + builder.setSoTimeout(opts, 60000); + builder.setUserAgent(opts, "foo/bar"); + + // ensure changes are visible + assertEquals(60000, builder.getConnectionTimeout(opts)); + assertEquals(60000, builder.getSoTimeout(opts)); + assertEquals("foo/bar", builder.getUserAgent(opts)); + + } + private void testResloveFolderSlash(final String uri, final boolean followRedirect) throws FileSystemException { VFS.getManager().getFilesCache().close(); final FileSystemOptions opts = new FileSystemOptions(); @@ -162,33 +191,4 @@ public class Http4ProviderTestCase extends AbstractProviderTestConfig { public void testResloveFolderSlashYesRedirectOn() throws FileSystemException { testResloveFolderSlash(ConnectionUri + "/read-tests/", true); } - - // Test no longer passing 2016/04/28 - public void ignoreTestHttp405() throws FileSystemException { - @SuppressWarnings("resource") // getManager() returns a global. - final FileObject fileObject = VFS.getManager() - .resolveFile("http4://www.w3schools.com/webservices/tempconvert.asmx?action=WSDL"); - assert !fileObject.getContent().isEmpty(); - } - - /** Ensure VFS-453 options are present. */ - public void testHttpTimeoutConfig() { - final FileSystemOptions opts = new FileSystemOptions(); - final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance(); - - // ensure defaults are 0 - assertEquals(0, builder.getConnectionTimeout(opts)); - assertEquals(0, builder.getSoTimeout(opts)); - assertEquals("Jakarta-Commons-VFS", builder.getUserAgent(opts)); - - builder.setConnectionTimeout(opts, 60000); - builder.setSoTimeout(opts, 60000); - builder.setUserAgent(opts, "foo/bar"); - - // ensure changes are visible - assertEquals(60000, builder.getConnectionTimeout(opts)); - assertEquals(60000, builder.getSoTimeout(opts)); - assertEquals("foo/bar", builder.getUserAgent(opts)); - - } } diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4s/Http4sGetContentInfoTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4s/Http4sGetContentInfoTest.java index f717ad7..c98b47b 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4s/Http4sGetContentInfoTest.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http4s/Http4sGetContentInfoTest.java @@ -36,24 +36,6 @@ import junit.framework.TestCase; */ public class Http4sGetContentInfoTest extends TestCase { - /** - * Tests VFS-427 NPE on Http4FileObject.getContent().getContentInfo(). - * - * @throws FileSystemException thrown when the getContentInfo API fails. - * @throws MalformedURLException thrown when the System environment contains an invalid URL for an HTTPS proxy. - */ - @Test - public void testGetContentInfo() throws FileSystemException, MalformedURLException { - final FileSystemManager fsManager = VFS.getManager(); - final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt"; - try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy())) { - final FileContent content = fo.getContent(); - Assert.assertNotNull(content); - // Used to NPE before fix: - content.getContentInfo(); - } - } - FileSystemOptions getOptionsWithProxy() throws MalformedURLException { // get proxy host and port from env var "https_proxy" String proxyHost = null; @@ -77,4 +59,22 @@ public class Http4sGetContentInfoTest extends TestCase { builder.setProxyPort(opts, proxyPort); return opts; } + + /** + * Tests VFS-427 NPE on Http4FileObject.getContent().getContentInfo(). + * + * @throws FileSystemException thrown when the getContentInfo API fails. + * @throws MalformedURLException thrown when the System environment contains an invalid URL for an HTTPS proxy. + */ + @Test + public void testGetContentInfo() throws FileSystemException, MalformedURLException { + final FileSystemManager fsManager = VFS.getManager(); + final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt"; + try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy())) { + final FileContent content = fo.getContent(); + Assert.assertNotNull(content); + // Used to NPE before fix: + content.getContentInfo(); + } + } } diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5s/Http5sGetContentInfoTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5s/Http5sGetContentInfoTest.java index c5106dd..e90f498 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5s/Http5sGetContentInfoTest.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/http5s/Http5sGetContentInfoTest.java @@ -44,40 +44,6 @@ public class Http5sGetContentInfoTest extends TestCase { private static final String SERVER_JCEKS_RES = "org.apache.httpserver/star_apache_cert.ts"; - /** - * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo(). - * - * @throws FileSystemException thrown when the getContentInfo API fails. - * @throws MalformedURLException thrown when the System environment contains an invalid URL for an HTTPS proxy. - */ - @Test - public void testGetContentInfo() throws FileSystemException, MalformedURLException { - final FileSystemManager fsManager = VFS.getManager(); - final String uri = "http5://www.apache.org/licenses/LICENSE-2.0.txt"; - final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy()); - final FileContent content = fo.getContent(); - Assert.assertNotNull(content); - // Used to NPE before fix: - content.getContentInfo(); - } - - /** - * Tests VFS-786 set keystore type. - * - * @throws FileSystemException thrown when the getContentInfo API fails. - * @throws MalformedURLException thrown when the System environment contains an invalid URL for an HTTPS proxy. - */ - @Test - public void testSSLGetContentInfo() throws IOException { - final FileSystemManager fsManager = VFS.getManager(); - final String uri = "http5s://www.apache.org/licenses/LICENSE-2.0.txt"; - final FileObject fo = fsManager.resolveFile(uri, getOptionsWithSSL()); - final FileContent content = fo.getContent(); - try(InputStream is = content.getInputStream()){ - final String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); - assertNotNull(text); - } - } FileSystemOptions getOptionsWithProxy() throws MalformedURLException { // get proxy host and port from env var "https_proxy" String proxyHost = null; @@ -103,17 +69,51 @@ public class Http5sGetContentInfoTest extends TestCase { return opts; } - private FileSystemOptions getOptionsWithSSL() throws MalformedURLException { - final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance(); - FileSystemOptions opts = getOptionsWithProxy(); - if (opts == null) { - opts = new FileSystemOptions(); + private FileSystemOptions getOptionsWithSSL() throws MalformedURLException { + final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance(); + FileSystemOptions opts = getOptionsWithProxy(); + if (opts == null) { + opts = new FileSystemOptions(); + } + final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JCEKS_RES); + builder.setKeyStoreFile(opts, serverJksResource.getFile()); + builder.setKeyStorePass(opts, "Hello_1234"); + builder.setKeyStoreType(opts, "JCEKS"); + return opts; } - final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JCEKS_RES); - builder.setKeyStoreFile(opts, serverJksResource.getFile()); - builder.setKeyStorePass(opts, "Hello_1234"); - builder.setKeyStoreType(opts, "JCEKS"); - return opts; + /** + * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo(). + * + * @throws FileSystemException thrown when the getContentInfo API fails. + * @throws MalformedURLException thrown when the System environment contains an invalid URL for an HTTPS proxy. + */ + @Test + public void testGetContentInfo() throws FileSystemException, MalformedURLException { + final FileSystemManager fsManager = VFS.getManager(); + final String uri = "http5://www.apache.org/licenses/LICENSE-2.0.txt"; + final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy()); + final FileContent content = fo.getContent(); + Assert.assertNotNull(content); + // Used to NPE before fix: + content.getContentInfo(); + } + + /** + * Tests VFS-786 set keystore type. + * + * @throws FileSystemException thrown when the getContentInfo API fails. + * @throws MalformedURLException thrown when the System environment contains an invalid URL for an HTTPS proxy. + */ +@Test +public void testSSLGetContentInfo() throws IOException { + final FileSystemManager fsManager = VFS.getManager(); + final String uri = "http5s://www.apache.org/licenses/LICENSE-2.0.txt"; + final FileObject fo = fsManager.resolveFile(uri, getOptionsWithSSL()); + final FileContent content = fo.getContent(); + try(InputStream is = content.getInputStream()){ + final String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")); + assertNotNull(text); } +} } diff --git a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/https/GetContentInfoFunctionalTest.java b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/https/GetContentInfoFunctionalTest.java index a6ad6dc..51f7050 100644 --- a/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/https/GetContentInfoFunctionalTest.java +++ b/commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/https/GetContentInfoFunctionalTest.java @@ -36,26 +36,6 @@ import org.junit.Test; */ public class GetContentInfoFunctionalTest { - /** - * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo(). - * - * @throws FileSystemException thrown when the getContentInfo API fails. - * @throws MalformedURLException thrown when the System environment contains an - * invalid URL for an HTTPS proxy. - */ - @Test - public void testGetContentInfo() throws FileSystemException, MalformedURLException { - @SuppressWarnings("resource") // getManager() returns a global. - final FileSystemManager fsManager = VFS.getManager(); - final String uri = "http://www.apache.org/licenses/LICENSE-2.0.txt"; - try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy()); - final FileContent content = fo.getContent()) { - Assert.assertNotNull(content); - // Used to NPE before fix: - content.getContentInfo(); - } - } - FileSystemOptions getOptionsWithProxy() throws MalformedURLException { // get proxy host and port from env var "https_proxy" String proxyHost = null; @@ -79,4 +59,24 @@ public class GetContentInfoFunctionalTest { builder.setProxyPort(opts, proxyPort); return opts; } + + /** + * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo(). + * + * @throws FileSystemException thrown when the getContentInfo API fails. + * @throws MalformedURLException thrown when the System environment contains an + * invalid URL for an HTTPS proxy. + */ + @Test + public void testGetContentInfo() throws FileSystemException, MalformedURLException { + @SuppressWarnings("resource") // getManager() returns a global. + final FileSystemManager fsManager = VFS.getManager(); + final String uri = "http://www.apache.org/licenses/LICENSE-2.0.txt"; + try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy()); + final FileContent content = fo.getContent()) { + Assert.assertNotNull(content); + // Used to NPE before fix: + content.getContentInfo(); + } + } }