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