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: */
017: package org.apache.commons.vfs.provider;
018:
019: import org.apache.commons.httpclient.URIException;
020: import org.apache.commons.httpclient.util.URIUtil;
021: import org.apache.commons.vfs.FileName;
022: import org.apache.commons.vfs.FileSystemException;
023: import org.apache.commons.vfs.FileType;
024:
025: public class URLFileName extends GenericFileName {
026: private final String queryString;
027:
028: public URLFileName(final String scheme, final String hostName,
029: final int port, final int defaultPort,
030: final String userName, final String password,
031: final String path, final FileType type,
032: final String queryString) {
033: super (scheme, hostName, port, defaultPort, userName, password,
034: path, type);
035: this .queryString = queryString;
036: }
037:
038: /**
039: * get the query string
040: *
041: * @return the query string part of the filename
042: */
043: public String getQueryString() {
044: return queryString;
045: }
046:
047: /**
048: * get the path and query string e.g. /path/servlet?param1=true
049: *
050: * @return the path and its query string
051: */
052: public String getPathQuery() {
053: StringBuffer sb = new StringBuffer(250);
054: sb.append(getPath());
055: sb.append("?");
056: sb.append(getQueryString());
057:
058: return sb.toString();
059: }
060:
061: /**
062: * get the path encoded suitable for url like filesystem e.g. (http, webdav)
063: *
064: * @param charset the charset used for the path encoding
065: */
066: public String getPathQueryEncoded(String charset)
067: throws URIException, FileSystemException {
068: if (getQueryString() == null) {
069: if (charset != null) {
070: return URIUtil.encodePath(getPathDecoded(), charset);
071: } else {
072: return URIUtil.encodePath(getPathDecoded());
073: }
074: }
075:
076: StringBuffer sb = new StringBuffer(250);
077: if (charset != null) {
078: sb.append(URIUtil.encodePath(getPathDecoded(), charset));
079: } else {
080: sb.append(URIUtil.encodePath(getPathDecoded()));
081: }
082: sb.append("?");
083: sb.append(getQueryString());
084: return sb.toString();
085: }
086:
087: public FileName createName(final String absPath, FileType type) {
088: return new URLFileName(getScheme(), getHostName(), getPort(),
089: getDefaultPort(), getUserName(), getPassword(),
090: absPath, type, getQueryString());
091: }
092:
093: /**
094: * append query string to the uri
095: *
096: * @return the uri
097: */
098: protected String createURI() {
099: if (getQueryString() != null) {
100: StringBuffer sb = new StringBuffer(250);
101: sb.append(super .createURI());
102: sb.append("?");
103: sb.append(getQueryString());
104:
105: return sb.toString();
106: }
107:
108: return super .createURI();
109: }
110:
111: public String getURIEncoded(String charset)
112: throws FileSystemException, URIException {
113: StringBuffer sb = new StringBuffer(80);
114: appendRootUri(sb, true);
115: sb.append(getPathQueryEncoded(charset));
116: return sb.toString();
117: }
118: }
|