Skip to content

Commit c622f6c

Browse files
committed
Add FtpFileSystemConfigBuilder.getControlEncodingCharset(FileSystemOptions)
and deprecate getControlEncoding(FileSystemOptions) - Add FtpFileSystemConfigBuilder.setControlEncoding(FileSystemOptions, Charset) and deprecate setControlEncoding(FileSystemOptions, String) - Next version will be 2.11.0 - Add FileSystemConfigBuilder.getCharset(FileSystemOptions, String) - Add FileSystemConfigBuilder.getCharset(FileSystemOptions, String, Charset)
1 parent fd37980 commit c622f6c

File tree

6 files changed

+101
-19
lines changed

6 files changed

+101
-19
lines changed

commons-vfs2/src/main/java/org/apache/commons/vfs2/FileSystemConfigBuilder.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.commons.vfs2;
1818

19+
import java.nio.charset.Charset;
1920
import java.time.Duration;
2021
import java.util.Objects;
2122
import java.util.function.Function;
@@ -96,7 +97,7 @@ protected boolean getBoolean(final FileSystemOptions fileSystemOptions, final St
9697
*/
9798
protected Boolean getBoolean(final FileSystemOptions fileSystemOptions, final String name,
9899
final Boolean defaultValue) {
99-
return getParam(fileSystemOptions, name, defaultValue, Boolean::valueOf);
100+
return getParam(fileSystemOptions, name, defaultValue, Boolean::valueOf, Boolean.class);
100101
}
101102

102103
/**
@@ -136,7 +137,7 @@ protected byte getByte(final FileSystemOptions fileSystemOptions, final String n
136137
* @since 2.0
137138
*/
138139
protected Byte getByte(final FileSystemOptions fileSystemOptions, final String name, final Byte defaultValue) {
139-
return getParam(fileSystemOptions, name, defaultValue, Byte::valueOf);
140+
return getParam(fileSystemOptions, name, defaultValue, Byte::valueOf, Byte.class);
140141
}
141142

142143
/**
@@ -188,6 +189,32 @@ protected Character getCharacter(final FileSystemOptions fileSystemOptions, fina
188189
return value;
189190
}
190191

192+
/**
193+
* Gets a named option as a Charset.
194+
*
195+
* @param fileSystemOptions file system options to query, may be null.
196+
* @param name the option name
197+
* @return the option in {@code opts} or system properties, otherwise null
198+
* @see #getCharset(FileSystemOptions, String, Charset)
199+
* @since 2.11.0
200+
*/
201+
protected Charset getCharset(final FileSystemOptions fileSystemOptions, final String name) {
202+
return getCharset(fileSystemOptions, name, null);
203+
}
204+
205+
/**
206+
* Gets a named option as a Charset.
207+
*
208+
* @param fileSystemOptions file system options to query, may be null.
209+
* @param name the option name
210+
* @param defaultValue value to return if option is not present
211+
* @return the option in {@code opts} or system properties, otherwise {@code defaultValue}
212+
* @since 2.11.0
213+
*/
214+
protected Charset getCharset(final FileSystemOptions fileSystemOptions, final String name, final Charset defaultValue) {
215+
return getParam(fileSystemOptions, name, defaultValue, Charset::forName, Charset.class);
216+
}
217+
191218
/**
192219
* Gets the target of this configuration.
193220
*
@@ -235,7 +262,7 @@ protected double getDouble(final FileSystemOptions fileSystemOptions, final Stri
235262
*/
236263
protected Double getDouble(final FileSystemOptions fileSystemOptions, final String name,
237264
final Double defaultValue) {
238-
return getParam(fileSystemOptions, name, defaultValue, Double::valueOf);
265+
return getParam(fileSystemOptions, name, defaultValue, Double::valueOf, Double.class);
239266
}
240267

241268
/**
@@ -262,7 +289,7 @@ protected Duration getDuration(final FileSystemOptions fileSystemOptions, final
262289
*/
263290
protected Duration getDuration(final FileSystemOptions fileSystemOptions, final String name,
264291
final Duration defaultValue) {
265-
return getParam(fileSystemOptions, name, defaultValue, Duration::parse);
292+
return getParam(fileSystemOptions, name, defaultValue, Duration::parse, Duration.class);
266293
}
267294

268295
/**
@@ -289,7 +316,7 @@ protected Integer getDurationInteger(final FileSystemOptions fileSystemOptions,
289316
*/
290317
protected Integer getDurationInteger(final FileSystemOptions fileSystemOptions, final String name,
291318
final Duration defaultValue) {
292-
return DurationUtils.toMillisInt(getParam(fileSystemOptions, name, defaultValue, Duration::parse));
319+
return DurationUtils.toMillisInt(getParam(fileSystemOptions, name, defaultValue, Duration::parse, Duration.class));
293320
}
294321

295322
/**
@@ -375,7 +402,7 @@ protected float getFloat(final FileSystemOptions fileSystemOptions, final String
375402
* @since 2.0
376403
*/
377404
protected Float getFloat(final FileSystemOptions fileSystemOptions, final String name, final Float defaultValue) {
378-
return getParam(fileSystemOptions, name, defaultValue, Float::valueOf);
405+
return getParam(fileSystemOptions, name, defaultValue, Float::valueOf, Float.class);
379406
}
380407

381408
/**
@@ -419,7 +446,7 @@ protected int getInteger(final FileSystemOptions fileSystemOptions, final String
419446
*/
420447
protected Integer getInteger(final FileSystemOptions fileSystemOptions, final String name,
421448
final Integer defaultValue) {
422-
return getParam(fileSystemOptions, name, defaultValue, Integer::valueOf);
449+
return getParam(fileSystemOptions, name, defaultValue, Integer::valueOf, Integer.class);
423450
}
424451

425452
/**
@@ -462,7 +489,7 @@ protected long getLong(final FileSystemOptions fileSystemOptions, final String n
462489
* @since 2.0
463490
*/
464491
protected Long getLong(final FileSystemOptions fileSystemOptions, final String name, final Long defaultValue) {
465-
return getParam(fileSystemOptions, name, defaultValue, Long::valueOf);
492+
return getParam(fileSystemOptions, name, defaultValue, Long::valueOf, Long.class);
466493
}
467494

468495
/**
@@ -486,22 +513,21 @@ protected <T> T getParam(final FileSystemOptions fileSystemOptions, final String
486513
* @param name get option with this name
487514
* @param defaultValue value to use if the system property value is null.
488515
* @param function Builds an instance of T from a system property String value.
516+
* @param returnType TODO
489517
* @return the named option or null
490518
* @since 2.8.0
491519
*/
492-
private <T> T getParam(final FileSystemOptions fileSystemOptions, final String name, final T defaultValue,
493-
final Function<String, T> function) {
520+
private <T> T getParam(final FileSystemOptions fileSystemOptions, final String name, final T defaultValue, final Function<String, T> function,
521+
final Class<T> returnType) {
494522
T value = getParam(fileSystemOptions, name);
495523
if (value == null) {
496524
final String str = getProperty(name);
497525
if (str == null) {
498526
return defaultValue;
499527
}
500-
if (function != null) {
501-
value = function.apply(str);
502-
}
528+
value = function.apply(str);
503529
}
504-
return value;
530+
return returnType == null ? null : returnType.isInstance(value) ? value : function.apply(Objects.toString(value));
505531
}
506532

507533
/**
@@ -580,7 +606,7 @@ protected short getShort(final FileSystemOptions fileSystemOptions, final String
580606
* @since 2.0
581607
*/
582608
protected Short getShort(final FileSystemOptions fileSystemOptions, final String name, final Short defaultValue) {
583-
return getParam(fileSystemOptions, name, defaultValue, Short::valueOf);
609+
return getParam(fileSystemOptions, name, defaultValue, Short::valueOf, Short.class);
584610
}
585611

586612
/**
@@ -607,7 +633,7 @@ protected String getString(final FileSystemOptions fileSystemOptions, final Stri
607633
*/
608634
protected String getString(final FileSystemOptions fileSystemOptions, final String name,
609635
final String defaultValue) {
610-
return getParam(fileSystemOptions, name, defaultValue, String::valueOf);
636+
return getParam(fileSystemOptions, name, defaultValue, String::valueOf, String.class);
611637
}
612638

613639
/**

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FTPClientWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ public InputStream retrieveFileStream(final String relPath, final long restartOf
344344
* causing the server to send FTP reply code 421. This exception may be caught either as an IOException or
345345
* independently as itself.
346346
* @throws IOException If an I/O error occurs while either sending the command or receiving the server reply.
347+
* @since 2.11.0
347348
*/
348349
public int sendOptions(final String commandName, String commandOptions) throws IOException {
349350
// Commons Net 3.12.0

commons-vfs2/src/main/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilder.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.commons.vfs2.provider.ftp;
1818

1919
import java.net.Proxy;
20+
import java.nio.charset.Charset;
2021
import java.time.Duration;
2122
import java.util.ArrayList;
2223
import java.util.Arrays;
@@ -153,11 +154,24 @@ public Duration getConnectTimeoutDuration(final FileSystemOptions options) {
153154
* @param options The FileSystemOptions.
154155
* @return The control encoding.
155156
* @since 2.0
157+
* @deprecated Use {@link #getControlEncodingCharset(FileSystemOptions)}.
156158
*/
159+
@Deprecated
157160
public String getControlEncoding(final FileSystemOptions options) {
158161
return getString(options, ENCODING);
159162
}
160163

164+
/**
165+
* Gets the control encoding.
166+
*
167+
* @param options The FileSystemOptions.
168+
* @return The control encoding.
169+
* @since 2.11.0
170+
*/
171+
public Charset getControlEncodingCharset(final FileSystemOptions options) {
172+
return getCharset(options, ENCODING);
173+
}
174+
161175
/**
162176
* Gets the controlKeepAliveReplyTimeout duration.
163177
*
@@ -435,13 +449,26 @@ public void setConnectTimeout(final FileSystemOptions options, final Integer dur
435449
setConnectTimeout(options, Duration.ofMillis(duration));
436450
}
437451

452+
/**
453+
* See {@link org.apache.commons.net.ftp.FTP#setControlEncoding} for details and examples.
454+
*
455+
* @param options The FileSystemOptions.
456+
* @param encoding the encoding to use
457+
* @since 2.11.0
458+
*/
459+
public void setControlEncoding(final FileSystemOptions options, final Charset encoding) {
460+
setParam(options, ENCODING, encoding);
461+
}
462+
438463
/**
439464
* See {@link org.apache.commons.net.ftp.FTP#setControlEncoding} for details and examples.
440465
*
441466
* @param options The FileSystemOptions.
442467
* @param encoding the encoding to use
443468
* @since 2.0
469+
* @deprecated Use {@link #setControlEncoding(FileSystemOptions, Charset)}.
444470
*/
471+
@Deprecated
445472
public void setControlEncoding(final FileSystemOptions options, final String encoding) {
446473
setParam(options, ENCODING, encoding);
447474
}

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpFileSystemConfigBuilderTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertEquals;
2020

21+
import java.nio.charset.Charset;
22+
import java.nio.charset.StandardCharsets;
2123
import java.time.Duration;
2224

2325
import org.apache.commons.lang3.Range;
@@ -37,14 +39,28 @@ public void testActivePortRange() {
3739
assertEquals(Range.between(2121, 2125), instance.getActivePortRange(options));
3840
}
3941

42+
@Test
43+
public void testControlEncoding() {
44+
final FileSystemOptions options = new FileSystemOptions();
45+
final FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
46+
final Charset charset = StandardCharsets.UTF_8;
47+
final String charsetName = charset.name();
48+
builder.setControlEncoding(options, charsetName);
49+
assertEquals(charset, builder.getControlEncodingCharset(options));
50+
assertEquals(charsetName, builder.getControlEncoding(options));
51+
builder.setControlEncoding(options, charset);
52+
assertEquals(charset, builder.getControlEncodingCharset(options));
53+
assertEquals(charsetName, builder.getControlEncoding(options));
54+
}
55+
4056
@Test
4157
public void testControlKeepAliveReplyTimeout() {
4258
final FtpFileSystemConfigBuilder instance = FtpFileSystemConfigBuilder.getInstance();
4359
final FileSystemOptions options = new FileSystemOptions();
4460
instance.setControlKeepAliveReplyTimeout(options, Duration.ofSeconds(10));
4561
assertEquals(Duration.ofSeconds(10), instance.getControlKeepAliveReplyTimeout(options));
4662
}
47-
63+
4864
@Test
4965
public void testControlKeepAliveTimeout() {
5066
final FtpFileSystemConfigBuilder instance = FtpFileSystemConfigBuilder.getInstance();

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpProviderTestCase.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.IOException;
2222
import java.net.URL;
23+
import java.nio.charset.Charset;
2324
import java.nio.charset.StandardCharsets;
2425
import java.time.Duration;
2526

@@ -252,7 +253,14 @@ protected void init(final FtpFileSystemConfigBuilder builder, final FileSystemOp
252253
// FtpFileType.BINARY is the default
253254
builder.setFileType(options, FtpFileType.BINARY);
254255
builder.setConnectTimeout(options, Duration.ofSeconds(10));
255-
builder.setControlEncoding(options, StandardCharsets.UTF_8.name());
256+
final Charset charset = StandardCharsets.UTF_8;
257+
final String charsetName = charset.name();
258+
builder.setControlEncoding(options, charsetName);
259+
assertEquals(charset, builder.getControlEncodingCharset(options));
260+
assertEquals(charsetName, builder.getControlEncoding(options));
261+
builder.setControlEncoding(options, charset);
262+
assertEquals(charset, builder.getControlEncodingCharset(options));
263+
assertEquals(charsetName, builder.getControlEncoding(options));
256264
builder.setControlKeepAliveReplyTimeout(options, Duration.ofSeconds(35));
257265
builder.setControlKeepAliveTimeout(options, Duration.ofSeconds(30));
258266
builder.setMdtmLastModifiedTime(options, mdtmLastModifiedTime);

src/changes/changes.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ The <action> type attribute can be add,update,fix,remove.
4545
<author email="[email protected]">Apache Commons Developers</author>
4646
</properties>
4747
<body>
48-
<release version="2.10.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
48+
<release version="2.11.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
4949
<!-- FIX -->
5050
<!-- ADD -->
5151
<action dev="ggregory" type="update" due-to="Gary Gregory">Add org.apache.commons.vfs2.provider.ftp.FTPClientWrapper.sendOptions(String, String).</action>
52+
<action dev="ggregory" type="update" due-to="Gary Gregory">Add FtpFileSystemConfigBuilder.getControlEncodingCharset(FileSystemOptions) and deprecate getControlEncoding(FileSystemOptions).</action>
53+
<action dev="ggregory" type="update" due-to="Gary Gregory">Add FtpFileSystemConfigBuilder.setControlEncoding(FileSystemOptions, Charset) and deprecate setControlEncoding(FileSystemOptions, String).</action>
54+
<action dev="ggregory" type="update" due-to="Gary Gregory">Add FileSystemConfigBuilder.getCharset(FileSystemOptions, String).</action>
55+
<action dev="ggregory" type="update" due-to="Gary Gregory">Add FileSystemConfigBuilder.getCharset(FileSystemOptions, String, Charset).</action>
5256
<!-- UPDATE -->
5357
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.ftpserver:ftpserver-core from 1.2.0 to 1.2.1.</action>
5458
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump org.apache.mina:mina-core from 2.1.10 to 2.2.4.</action>

0 commit comments

Comments
 (0)