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.http;
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.junit.jupiter.api.Test;
31  
32  /**
33   * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo().
34   */
35  public class HttpGetContentInfoFunctionalTest {
36  
37      FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
38          // get proxy host and port from env var "https_proxy"
39          String proxyHost = null;
40          int proxyPort = -1;
41          final String proxyUrl = System.getenv("https_proxy");
42          if (proxyUrl != null) {
43              final URL url = new URL(proxyUrl);
44              proxyHost = url.getHost();
45              proxyPort = url.getPort();
46          }
47  
48          // return null if proxy host or port invalid
49          if (proxyHost == null || proxyPort == -1) {
50              return null;
51          }
52  
53          // return options with proxy
54          final HttpFileSystemConfigBuilder builder = HttpFileSystemConfigBuilder.getInstance();
55          final FileSystemOptions opts = new FileSystemOptions();
56          builder.setProxyHost(opts, proxyHost);
57          builder.setProxyPort(opts, proxyPort);
58          return opts;
59      }
60  
61      /**
62       * Tests VFS-427 NPE on HttpFileObject.getContent().getContentInfo().
63       *
64       * @throws FileSystemException thrown when the getContentInfo API fails.
65       */
66      @Test
67      public void testGetContentInfo() throws FileSystemException, MalformedURLException {
68          final FileSystemManager fsManager = VFS.getManager();
69          final String uri = "http://www.apache.org/licenses/LICENSE-2.0.txt";
70          try (FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy());
71                  final FileContent content = fo.getContent()) {
72              assertNotNull(content);
73              // Used to NPE before fix:
74              content.getContentInfo();
75          }
76      }
77  
78  }