View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.http5;
18  
19  import static org.junit.jupiter.api.Assertions.assertNotNull;
20  
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  
24  import org.apache.commons.vfs2.FileContent;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.FileSystemException;
27  import org.apache.commons.vfs2.FileSystemManager;
28  import org.apache.commons.vfs2.FileSystemOptions;
29  import org.apache.commons.vfs2.VFS;
30  import org.apache.commons.vfs2.auth.StaticUserAuthenticator;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo().
35   */
36  public class Http5GetContentInfoTest {
37  
38      FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
39          // get proxy host and port from env var "https_proxy"
40          String proxyHost = null;
41          int proxyPort = -1;
42          final String proxyUrl = System.getenv("https_proxy");
43          if (proxyUrl != null) {
44              final URL url = new URL(proxyUrl);
45              proxyHost = url.getHost();
46              proxyPort = url.getPort();
47          }
48  
49          // return null if proxy host or port invalid
50          if (proxyHost == null || proxyPort == -1) {
51              return null;
52          }
53  
54          // return options with proxy
55          final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
56          final FileSystemOptions opts = new FileSystemOptions();
57          builder.setProxyHost(opts, proxyHost);
58          builder.setProxyPort(opts, proxyPort);
59          builder.setProxyScheme(opts, "http");
60          return opts;
61      }
62  
63      private FileSystemOptions getOptionsWithProxyAuthentication() throws MalformedURLException {
64          // get proxy host and port from env var "https_proxy"
65          String proxyHost = null;
66          int proxyPort = -1;
67          String[] user = null;
68          final String proxyUrl = System.getenv("https_proxy");
69          if (proxyUrl != null) {
70              final URL url = new URL(proxyUrl);
71              proxyHost = url.getHost();
72              proxyPort = url.getPort();
73              final String userInfo = url.getUserInfo();
74              if (userInfo != null) {
75                  user = userInfo.split(":");
76  
77              }
78          }
79  
80          // return null if proxy host or port invalid
81          if (proxyHost == null || proxyPort == -1) {
82              return null;
83          }
84  
85          // return options with proxy
86          final Http5FileSystemConfigBuilder builder = Http5FileSystemConfigBuilder.getInstance();
87          final FileSystemOptions opts = new FileSystemOptions();
88          builder.setProxyHost(opts, proxyHost);
89          builder.setProxyPort(opts, proxyPort);
90          if (user != null) {
91              builder.setProxyAuthenticator(opts, new StaticUserAuthenticator(null, user[0], user[1]));
92          }
93          return opts;
94      }
95  
96      /**
97       * Tests VFS-427 NPE on Http5FileObject.getContent().getContentInfo().
98       *
99       * @throws FileSystemException thrown when the getContentInfo API fails.
100      */
101     @Test
102     public void testGetContentInfo() throws FileSystemException, MalformedURLException {
103         final FileSystemManager fsManager = VFS.getManager();
104         final String uri = "http5://www.apache.org/licenses/LICENSE-2.0.txt";
105         final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy());
106         try (FileContent content = fo.getContent()) {
107             assertNotNull(content);
108             // Used to NPE before fix:
109             content.getContentInfo();
110         }
111     }
112 
113     /**
114      * Tests VFS-782 pass correct proxy authentication credentials.
115      *
116      * @throws FileSystemException thrown when the authentication fails.
117      */
118     @Test
119     public void testGetContentWithProxyAuthInfo() throws FileSystemException, MalformedURLException {
120         final FileSystemManager fsManager = VFS.getManager();
121         final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
122         try (FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxyAuthentication())) {
123             final FileContent content = fo.getContent();
124             assertNotNull(content);
125             content.getContentInfo();
126         }
127     }
128 
129 }