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.http4s;
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.provider.http4.Http4FileSystemConfigBuilder;
29  import org.junit.Assert;
30  import org.junit.Test;
31  
32  import junit.framework.TestCase;
33  
34  /**
35   * Tests VFS-427 NPE on Http4FileObject.getContent().getContentInfo()
36   */
37  public class Http4sGetContentInfoTest extends TestCase {
38  
39      FileSystemOptions getOptionsWithProxy() throws MalformedURLException {
40          // get proxy host and port from env var "https_proxy"
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          // return null if proxy host or port invalid
51          if (proxyHost == null || proxyPort == -1) {
52              return null;
53          }
54  
55          // return options with proxy
56          final Http4FileSystemConfigBuilder builder = Http4FileSystemConfigBuilder.getInstance();
57          final FileSystemOptions opts = new FileSystemOptions();
58          builder.setProxyHost(opts, proxyHost);
59          builder.setProxyPort(opts, proxyPort);
60          return opts;
61      }
62  
63      /**
64       * Tests VFS-427 NPE on Http4FileObject.getContent().getContentInfo().
65       *
66       * @throws FileSystemException thrown when the getContentInfo API fails.
67       * @throws MalformedURLException thrown when the System environment contains an invalid URL for an HTTPS proxy.
68       */
69      @Test
70      public void testGetContentInfo() throws FileSystemException, MalformedURLException {
71          final FileSystemManager fsManager = VFS.getManager();
72          final String uri = "http4://www.apache.org/licenses/LICENSE-2.0.txt";
73          try (final FileObject fo = fsManager.resolveFile(uri, getOptionsWithProxy())) {
74              final FileContent content = fo.getContent();
75              Assert.assertNotNull(content);
76              // Used to NPE before fix:
77              content.getContentInfo();
78          }
79      }
80  }