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.webdav4;
18  
19  import org.apache.commons.vfs2.FileSystemException;
20  import org.apache.commons.vfs2.FileType;
21  import org.apache.commons.vfs2.provider.GenericURLFileName;
22  import org.apache.commons.vfs2.util.URIUtils;
23  
24  /**
25   * WebDAV 4 file name that represents a URL.
26   *
27   * @since 2.10.0
28   */
29  public class Webdav4FileName extends GenericURLFileName {
30      private static final int BUFFER_SIZE = 250;
31  
32      private final boolean appendTrailingSlash;
33  
34      /**
35       * Constructs a new instance.
36       *
37       * @param scheme Host scheme.
38       * @param hostName Host name or IP address.
39       * @param port Host port.
40       * @param defaultPort Default host port.
41       * @param userName user name.
42       * @param password user password.
43       * @param path Path on the host.
44       * @param type File type on the host.
45       * @param queryString Query string for the path.
46       */
47      public Webdav4FileName(final String scheme, final String hostName, final int port, final int defaultPort,
48                             final String userName, final String password, final String path, final FileType type,
49                             final String queryString) {
50          this(scheme, hostName, port, defaultPort, userName, password, path, type, queryString, false);
51      }
52  
53      /**
54       * Constructs a new instance.
55       *
56       * @param scheme Host scheme.
57       * @param hostName Host name or IP address.
58       * @param port Host port.
59       * @param defaultPort Default host port.
60       * @param userName user name.
61       * @param password user password.
62       * @param path Path on the host.
63       * @param type File type on the host.
64       * @param queryString Query string for the path.
65       * @param appendTrailingSlash Append trailing slash to path.
66       */
67      public Webdav4FileName(final String scheme, final String hostName, final int port, final int defaultPort,
68                             final String userName, final String password, final String path, final FileType type,
69                             final String queryString, final boolean appendTrailingSlash) {
70          super(scheme, hostName, port, defaultPort, userName, password, path, type, queryString);
71          this.appendTrailingSlash = appendTrailingSlash;
72      }
73  
74      /**
75       * Gets the path encoded suitable for url like file system e.g. (http, webdav).
76       * Reappend the trailing slash ( / ) if this FileName is a directory and not ROOT
77       * because many WEBDav-Servers require the trailing slash if the request access a directory
78       *
79       * @param charset the charset used for the path encoding
80       * @return The encoded path.
81       * @throws FileSystemException If some other error occurs.
82       */
83      @Override
84      public String getPathQueryEncoded(final String charset) throws FileSystemException {
85          String pathDecoded = getPathDecoded();
86  
87          if (appendTrailingSlash && getType() == FileType.FOLDER && getPath().length() > 1) {
88              pathDecoded += SEPARATOR;
89          }
90  
91          if (getQueryString() == null || getQueryString().isEmpty()) {
92              if (charset != null) {
93                  return URIUtils.encodePath(pathDecoded, charset);
94              }
95              return URIUtils.encodePath(pathDecoded);
96          }
97  
98          final StringBuilder sb = new StringBuilder(BUFFER_SIZE);
99          if (charset != null) {
100             sb.append(URIUtils.encodePath(pathDecoded, charset));
101         } else {
102             sb.append(URIUtils.encodePath(pathDecoded));
103         }
104         sb.append("?");
105         sb.append(getQueryString());
106         return sb.toString();
107     }
108 
109 }