1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.provider.http4;
18
19 import java.net.MalformedURLException;
20 import java.net.URL;
21
22 import org.apache.commons.vfs2.FileContent;
23 import org.apache.commons.vfs2.FileObject;
24 import org.apache.commons.vfs2.FileSystemException;
25 import org.apache.commons.vfs2.FileSystemManager;
26 import org.apache.commons.vfs2.FileSystemOptions;
27 import org.apache.commons.vfs2.VFS;
28 import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
29 import org.junit.Assert;
30 import org.junit.Test;
31
32 import junit.framework.TestCase;
33
34
35
36
37 public class Http4GetContentInfoTest extends TestCase {
38
39 FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
40
41 String proxyHost = null;
42 int proxyPort = -1;
43 final String proxyUrl = System.getenv("https_proxy");
44 if (proxyUrl != null) {
45 final URL url = new URL(proxyUrl);
46 proxyHost = url.getHost();
47 proxyPort = url.getPort();
48 }
49
50
51 if (proxyHost == null || proxyPort == -1) {
52 return null;
53 }
54
55
56 final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
57 final FileSystemOptions opts = new FileSystemOptions();
58 builder.setProxyHost(opts, proxyHost);
59 builder.setProxyPort(opts, proxyPort);
60 builder.setProxyScheme(opts, "http");
61 return opts;
62 }
63
64 private FileSystemOptions getOptionsWithProxyAuthentication() throws MalformedURLException {
65
66 String proxyHost = null;
67 int proxyPort = -1;
68 String[] user = null;
69 final String proxyUrl = System.getenv("https_proxy");
70 if (proxyUrl != null) {
71 final URL url = new URL(proxyUrl);
72 proxyHost = url.getHost();
73 proxyPort = url.getPort();
74 final String userInfo = url.getUserInfo();
75 if (userInfo != null) {
76 user = userInfo.split(":");
77
78 }
79 }
80
81
82 if (proxyHost == null || proxyPort == -1) {
83 return null;
84 }
85
86
87 final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
88 final FileSystemOptions opts = new FileSystemOptions();
89 builder.setProxyHost(opts, proxyHost);
90 builder.setProxyPort(opts, proxyPort);
91 if (user != null) {
92 builder.setProxyAuthenticator(opts, new StaticUserAuthenticator(null, user[0], user[1]));
93 }
94 return opts;
95 }
96
97
98
99
100
101
102 @Test
103 public void testGetContentInfo() throws FileSystemException, MalformedURLException {
104 @SuppressWarnings("resource")
105 final FileSystemManager fsManager = VFS.getManager();
106 final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
107 try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy());
108 final FileContent content = fo.getContent()) {
109 Assert.assertNotNull(content);
110
111 content.getContentInfo();
112 }
113 }
114
115
116
117
118
119
120 @Test
121 public void testGetContentWithProxyAuthInfo() throws FileSystemException, MalformedURLException {
122 @SuppressWarnings("resource")
123 final FileSystemManager fsManager = VFS.getManager();
124 final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
125 try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxyAuthentication());
126 final FileContent content = fo.getContent()) {
127 Assert.assertNotNull(content);
128 content.getContentInfo();
129 }
130 }
131 }