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.local;
18  
19  import org.apache.commons.vfs2.FileName;
20  import org.apache.commons.vfs2.FileSystemException;
21  import org.apache.commons.vfs2.FileType;
22  import org.apache.commons.vfs2.provider.AbstractFileName;
23  import org.apache.commons.vfs2.provider.UriParser;
24  
25  /**
26   * A local file URI.
27   */
28  public class LocalFileName extends AbstractFileName
29  {
30      // URI Characters that are possible in local filenames, but must be escaped
31      // for proper URI handling.
32      //
33      // How reserved URI chars were selected:
34      //
35      //  URIs can contain :, /, ?, #, @
36      //      See http://download.oracle.com/javase/6/docs/api/java/net/URI.html
37      //          http://tools.ietf.org/html/rfc3986#section-2.2
38      //
39      //  Since : and / occur before the path, only chars after path are escaped (i.e., # and ?)
40      //  ? is a reserved filesystem character for Windows and Unix, so can't be part of a filename.
41      //  Therefore only # is a reserved char in a URI as part of the path that can be in the filename.
42      private static final char[] RESERVED_URI_CHARS = {'#'};
43  
44  
45      private final String rootFile;
46  
47      protected LocalFileName(final String scheme,
48                              final String rootFile,
49                              final String path,
50                              final FileType type)
51      {
52          super(scheme, path, type);
53          this.rootFile = rootFile;
54      }
55  
56      /**
57       * Returns the root file for this file.
58       * @return The root file name.
59       */
60      public String getRootFile()
61      {
62          return rootFile;
63      }
64  
65      /**
66       * Factory method for creating name instances.
67       * @param path The file path.
68       * @param type The file type.
69       * @return The FileName.
70       */
71      @Override
72      public FileName createName(final String path, final FileType type)
73      {
74          return new LocalFileName(getScheme(), rootFile, path, type);
75      }
76  
77      /**
78       * Returns the absolute URI of the file.
79       * @return The absolute URI of the file.
80       */
81      @Override
82      public String getURI()
83      {
84          String uri = super.getURI();
85  
86          if (uri != null && uri.length() > 0)
87          {
88              try
89              {
90                  // VFS-325: Handle URI special characters in filename
91                  // Decode the base uri and re-encode with URI special characters
92                  uri = UriParser.decode(uri);
93  
94                  uri = UriParser.encode(uri, RESERVED_URI_CHARS);
95              }
96              catch (final FileSystemException e)
97              {
98                  // Default to base uri value
99              }
100         }
101 
102         return uri;
103     }
104 
105     /**
106      * returns a "friendly path", this is a path without a password.
107      * @return The "friendly" URI.
108      */
109     @Override
110     public String getFriendlyURI()
111     {
112         String uri = super.getFriendlyURI();
113 
114         if (uri != null && uri.length() > 0)
115         {
116             try
117             {
118                 // VFS-325: Handle URI special characters in filename
119                 // Decode the base uri and re-encode with URI special characters
120                 uri = UriParser.decode(uri);
121 
122                 uri = UriParser.encode(uri, RESERVED_URI_CHARS);
123             }
124             catch (final FileSystemException e)
125             {
126                 // Default to base uri value
127             }
128         }
129 
130         return uri;
131     }
132 
133     /**
134      * Returns the decoded URI of the file.
135      * @return the FileName as a URI.
136      */
137     @Override
138     public String toString()
139     {
140         try
141         {
142             return UriParser.decode(super.getURI());
143         }
144         catch (final FileSystemException e)
145         {
146             return super.getURI();
147         }
148     }
149 
150 
151 
152     /**
153      * Builds the root URI for this file name.
154      */
155     @Override
156     protected void appendRootUri(final StringBuilder buffer, final boolean addPassword)
157     {
158         buffer.append(getScheme());
159         buffer.append("://");
160         buffer.append(rootFile);
161     }
162 }