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;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  import java.net.URL;
23  import java.net.URLConnection;
24  
25  import org.apache.commons.io.function.Uncheck;
26  import org.apache.commons.vfs2.FileContent;
27  import org.apache.commons.vfs2.FileSystemException;
28  
29  /**
30   * A default URL connection that will work for most file systems.
31   */
32  public final class DefaultURLConnection extends URLConnection {
33  
34      private final FileContent fileContent;
35  
36      /**
37       * Constructs a new instance.
38       *
39       * @param url The URL to connect.
40       * @param fileContent The URL fileContent.
41       */
42      public DefaultURLConnection(final URL url, final FileContent fileContent) {
43          super(url);
44          this.fileContent = fileContent;
45      }
46  
47      @Override
48      public void connect() {
49          connected = true;
50      }
51  
52      @Override
53      public String getContentEncoding() {
54          return Uncheck.get(() -> fileContent.getContentInfo().getContentEncoding());
55      }
56  
57      @Override
58      public int getContentLength() {
59          try {
60              return (int) fileContent.getSize();
61          } catch (final FileSystemException fse) {
62              return -1; // TODO: report?
63          }
64      }
65  
66      @Override
67      public String getContentType() {
68          return Uncheck.get(() -> fileContent.getContentInfo().getContentType());
69      }
70  
71      @Override
72      public InputStream getInputStream() throws IOException {
73          return fileContent.getInputStream();
74      }
75  
76      @Override
77      public long getLastModified() {
78          try {
79              return fileContent.getLastModifiedTime();
80          } catch (final FileSystemException ignored) {
81              return -1; // TODO: report?
82          }
83      }
84  
85      @Override
86      public OutputStream getOutputStream() throws IOException {
87          return fileContent.getOutputStream();
88      }
89  
90      /*
91       * public String getHeaderField(String name) { try { if
92       * (content.getFile().getFileSystem().hasCapability(Capability.ATTRIBUTES)) { String value = (String)
93       * content.getAttribute(name); if (value != null) { return value; } }
94       *
95       * return null; } catch (FileSystemException e) { throw new UncheckedIOException(e); } }
96       */
97  }