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; 018 019import org.apache.commons.vfs2.FileName; 020import org.apache.commons.vfs2.FileSystemException; 021import org.apache.commons.vfs2.FileType; 022import org.apache.commons.vfs2.util.URIUtils; 023 024/** 025 * Generic file name that represents a URL. 026 */ 027public class GenericURLFileName extends GenericFileName { 028 029 private static final int BUFFER_SIZE = 250; 030 031 private final String queryString; 032 033 /** 034 * Constructs a new instance. 035 * 036 * @param scheme Host scheme. 037 * @param hostName Host name or IP address. 038 * @param port Host port. 039 * @param defaultPort Default host port. 040 * @param userName user name. 041 * @param password user password. 042 * @param path Path on the host. 043 * @param type File type on the host. 044 * @param queryString Query string for the path. 045 */ 046 public GenericURLFileName(final String scheme, final String hostName, final int port, final int defaultPort, 047 final String userName, final String password, final String path, final FileType type, 048 final String queryString) { 049 super(scheme, hostName, port, defaultPort, userName, password, path, type); 050 this.queryString = queryString; 051 } 052 053 /** 054 * Creates a FileName. 055 * 056 * @param absPath The absolute path. 057 * @param type The FileType. 058 * @return The FileName 059 */ 060 @Override 061 public FileName createName(final String absPath, final FileType type) { 062 return new GenericURLFileName(getScheme(), getHostName(), getPort(), getDefaultPort(), getUserName(), getPassword(), 063 absPath, type, getQueryString()); 064 } 065 066 /** 067 * Appends query string to the uri. 068 * 069 * @return the uri 070 */ 071 @Override 072 protected String createURI() { 073 if (getQueryString() != null) { 074 final StringBuilder sb = new StringBuilder(BUFFER_SIZE); 075 sb.append(super.createURI()); 076 sb.append("?"); 077 sb.append(getQueryString()); 078 079 return sb.toString(); 080 } 081 082 return super.createURI(); 083 } 084 085 /** 086 * Gets the path and query string e.g. /path/servlet?param1=true. 087 * 088 * @return the path and its query string 089 */ 090 public String getPathQuery() { 091 final StringBuilder sb = new StringBuilder(BUFFER_SIZE); 092 sb.append(getPath()); 093 sb.append("?"); 094 sb.append(getQueryString()); 095 096 return sb.toString(); 097 } 098 099 /** 100 * Gets the path encoded suitable for url like file system e.g. (http, webdav). 101 * 102 * @param charset the charset used for the path encoding 103 * @return The encoded path. 104 * @throws FileSystemException If some other error occurs. 105 */ 106 public String getPathQueryEncoded(final String charset) throws FileSystemException { 107 if (getQueryString() == null) { 108 if (charset != null) { 109 return URIUtils.encodePath(getPathDecoded(), charset); 110 } 111 return URIUtils.encodePath(getPathDecoded()); 112 } 113 114 final StringBuilder sb = new StringBuilder(BUFFER_SIZE); 115 if (charset != null) { 116 sb.append(URIUtils.encodePath(getPathDecoded(), charset)); 117 } else { 118 sb.append(URIUtils.encodePath(getPathDecoded())); 119 } 120 sb.append("?"); 121 sb.append(getQueryString()); 122 return sb.toString(); 123 } 124 125 /** 126 * Gets the query string. 127 * 128 * @return the query string part of the file name 129 */ 130 public String getQueryString() { 131 return queryString; 132 } 133 134 /** 135 * Encodes a URI. 136 * 137 * @param charset The character set. 138 * @return The encoded URI 139 * @throws FileSystemException if some other exception occurs. 140 */ 141 public String getURIEncoded(final String charset) throws FileSystemException { 142 final StringBuilder sb = new StringBuilder(BUFFER_SIZE); 143 appendRootUri(sb, true); 144 sb.append(getPathQueryEncoded(charset)); 145 return sb.toString(); 146 } 147}