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