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