Ranides Atterwim 3 lat temu
rodzic
commit
bb99a4992f

+ 9 - 3
assira.commons/src/main/java/net/ranides/assira/io/uri/impl/CFTPHandler.java

@@ -43,6 +43,10 @@ import java.util.Set;
  * Please note:
  * As every URIHandler, it should not be constructed directly, but connected with some instance of URIResolver.
  *
+ * Supported query parameters:
+ *      charset = UTF-8
+ *      timeout = 1000
+ *
  * @author Ranides Atterwim {@literal <ranides@gmail.com>}
  */
 public class CFTPHandler implements URIHandler {
@@ -285,7 +289,8 @@ public class CFTPHandler implements URIHandler {
         private final Optional<String> user;
         private final Optional<String> pass;
         private final Optional<Integer> port;
-        
+        private final int timeout;
+
         public FTPHostName(URI uri, UserStore users) {
             host = uri.getHost();
             Optional<GenericMap<String>> uis = users.find(uri).last();
@@ -298,6 +303,7 @@ public class CFTPHandler implements URIHandler {
                 pass = Optional.empty();
             }
             port = URIUtils.getPort(uri);
+            timeout = URIUtils.getParam(uri, "timeout").map(Integer::valueOf).orElse(CONNECT_TIMEOUT);
             hash = HashUtils.hashValues(5, 23, host, user, pass, port);
         }
 
@@ -325,10 +331,10 @@ public class CFTPHandler implements URIHandler {
             ftp.configure(config);
 
             if(port.isPresent()) {
-                ftp.setConnectTimeout(CONNECT_TIMEOUT);
+                ftp.setConnectTimeout(timeout);
                 ftp.connect(host, port.get());
             } else {
-                ftp.setConnectTimeout(CONNECT_TIMEOUT);
+                ftp.setConnectTimeout(timeout);
                 ftp.connect(host);
             }
             ftp.enterLocalPassiveMode();

+ 35 - 0
assira.commons/src/test/java/net/ranides/assira/io/uri/impl/CFTPHandlerTest.java

@@ -10,6 +10,7 @@ import static net.ranides.assira.io.uri.URIFlags.*;
 import static net.ranides.assira.junit.NewAssert.*;
 
 import java.io.IOException;
+import java.net.SocketTimeoutException;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.charset.Charset;
@@ -200,6 +201,40 @@ public class CFTPHandlerTest {
         handle1.close();
         handle2.close();
     }
+
+    @Test
+    public void testTimeout() throws URISyntaxException, IOException {
+
+        URI u1 = new URI("ftp://user@ftp.ranides.net/readme.txt?timeout=750");
+        URI u2 = new URI("ftp://user@ftp.ranides.net/readme.txt?timeout=1");
+        URI u3 = new URI("ftp://user@ftp.ranides.net/readme.txt?timeout=2");
+        URI u4 = new URI("ftp://user@ftp.ranides.net/readme.txt?timeout=800");
+
+
+        URIHandle handle1 = re.resolve(u1);
+        URIHandle handle2 = re.resolve(u2);
+
+        // we will use timeout from "u1"
+        assertEquals(TEXT_1, IOStrings.read(handle1.reader()));
+        // we are already connected
+        // we won't use any timeout
+        assertEquals(TEXT_1, IOStrings.read(handle2.reader()));
+
+        handle1.close();
+        handle2.close();
+
+        URIHandle handle3 = re.resolve(u3);
+        URIHandle handle4 = re.resolve(u4);
+
+        // we will use timeout from "u3"
+        assertThrows(IOException.class, () -> {
+            assertEquals(TEXT_1, IOStrings.read(handle3.reader()));
+        });
+
+        // we are not connected yet
+        // we will use timeout from "u4"
+        assertEquals(TEXT_1, IOStrings.read(handle4.reader()));
+    }
     
     @Test
     public void testCopy() throws URISyntaxException, IOException {