001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.provider.webdav4; 018 019import org.apache.commons.vfs2.FileSystemException; 020import org.apache.commons.vfs2.FileType; 021import org.apache.commons.vfs2.provider.GenericURLFileName; 022import org.apache.commons.vfs2.util.URIUtils; 023 024/** 025 * WebDAV 4 file name that represents a URL. 026 * 027 * @since 2.10.0 028 */ 029public class Webdav4FileName extends GenericURLFileName { 030 private static final int BUFFER_SIZE = 250; 031 032 private final boolean appendTrailingSlash; 033 034 /** 035 * Constructs a new instance. 036 * 037 * @param scheme Host scheme. 038 * @param hostName Host name or IP address. 039 * @param port Host port. 040 * @param defaultPort Default host port. 041 * @param userName user name. 042 * @param password user password. 043 * @param path Path on the host. 044 * @param type File type on the host. 045 * @param queryString Query string for the path. 046 */ 047 public Webdav4FileName(final String scheme, final String hostName, final int port, final int defaultPort, 048 final String userName, final String password, final String path, final FileType type, 049 final String queryString) { 050 this(scheme, hostName, port, defaultPort, userName, password, path, type, queryString, false); 051 } 052 053 /** 054 * Constructs a new instance. 055 * 056 * @param scheme Host scheme. 057 * @param hostName Host name or IP address. 058 * @param port Host port. 059 * @param defaultPort Default host port. 060 * @param userName user name. 061 * @param password user password. 062 * @param path Path on the host. 063 * @param type File type on the host. 064 * @param queryString Query string for the path. 065 * @param appendTrailingSlash Append trailing slash to path. 066 */ 067 public Webdav4FileName(final String scheme, final String hostName, final int port, final int defaultPort, 068 final String userName, final String password, final String path, final FileType type, 069 final String queryString, final boolean appendTrailingSlash) { 070 super(scheme, hostName, port, defaultPort, userName, password, path, type, queryString); 071 this.appendTrailingSlash = appendTrailingSlash; 072 } 073 074 /** 075 * Gets the path encoded suitable for url like file system e.g. (http, webdav). 076 * Reappend the trailing slash ( / ) if this FileName is a directory and not ROOT 077 * because many WEBDav-Servers require the trailing slash if the request access a directory 078 * 079 * @param charset the charset used for the path encoding 080 * @return The encoded path. 081 * @throws FileSystemException If some other error occurs. 082 */ 083 @Override 084 public String getPathQueryEncoded(final String charset) throws FileSystemException { 085 String pathDecoded = getPathDecoded(); 086 087 if (appendTrailingSlash && getType() == FileType.FOLDER && getPath().length() > 1) { 088 pathDecoded += SEPARATOR; 089 } 090 091 if (getQueryString() == null || getQueryString().isEmpty()) { 092 if (charset != null) { 093 return URIUtils.encodePath(pathDecoded, charset); 094 } 095 return URIUtils.encodePath(pathDecoded); 096 } 097 098 final StringBuilder sb = new StringBuilder(BUFFER_SIZE); 099 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}