1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http5s;
18
19 import static org.junit.jupiter.api.Assertions.assertNotNull;
20
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.nio.charset.StandardCharsets;
28 import java.util.stream.Collectors;
29
30 import org.apache.commons.vfs2.FileContent;
31 import org.apache.commons.vfs2.FileObject;
32 import org.apache.commons.vfs2.FileSystemException;
33 import org.apache.commons.vfs2.FileSystemManager;
34 import org.apache.commons.vfs2.FileSystemOptions;
35 import org.apache.commons.vfs2.VFS;
36 import org.apache.commons.vfs2.provider.http5.Http5FileSystemConfigBuilder;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 public class Http5sGetContentInfoTest {
43
44 private static final String SERVER_JCEKS_RES = "org.apache.httpserver/star_apache_cert.ts";
45
46 FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
47
48 String proxyHost = null;
49 int proxyPort = -1;
50 final String proxyUrl = System.getenv("https_proxy");
51 if (proxyUrl != null) {
52 final URL url = new URL(proxyUrl);
53 proxyHost = url.getHost();
54 proxyPort = url.getPort();
55 }
56
57
58 if (proxyHost == null || proxyPort == -1) {
59 return null;
60 }
61
62
63 final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
64 final FileSystemOptions opts = new FileSystemOptions();
65 builder.setProxyHost(opts, proxyHost);
66 builder.setProxyPort(opts, proxyPort);
67 return opts;
68 }
69
70 private FileSystemOptions getOptionsWithSSL() throws MalformedURLException {
71 final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
72 FileSystemOptions opts = getOptionsWithProxy();
73 if (opts == null) {
74 opts = new FileSystemOptions();
75 }
76 final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JCEKS_RES);
77 builder.setKeyStoreFile(opts, serverJksResource.getFile());
78 builder.setKeyStorePass(opts, "Hello_1234");
79 builder.setKeyStoreType(opts, "JCEKS");
80 return opts;
81 }
82
83
84
85
86
87
88 @Test
89 public void testGetContentInfo() throws FileSystemException, MalformedURLException {
90 final FileSystemManager fsManager = VFS.getManager();
91 final String uri = "http5://www.apache.org/licenses/LICENSE-2.0.txt";
92 final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy());
93 final FileContent content = fo.getContent();
94 assertNotNull(content);
95
96 content.getContentInfo();
97 }
98
99
100
101
102
103
104
105 @Test
106 public void testSSLGetContentInfo() throws IOException {
107 final FileSystemManager fsManager = VFS.getManager();
108 final String uri = "http5s://www.apache.org/licenses/LICENSE-2.0.txt";
109 final FileObject fo = fsManager.resolveFile(uri, getOptionsWithSSL());
110 final FileContent content = fo.getContent();
111 try (InputStream is = content.getInputStream()) {
112 final String text = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n"));
113 assertNotNull(text);
114 }
115 }
116
117 }