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.vfs.provider;
    18
    19  import org.apache.commons.httpclient.URIException;
    20  import org.apache.commons.httpclient.util.URIUtil;
    21  import org.apache.commons.vfs.FileName;
    22  import org.apache.commons.vfs.FileSystemException;
    23  import org.apache.commons.vfs.FileType;
    24
    25  public class URLFileName extends GenericFileName
    26  {
    27      private final String queryString;
    28
    29      public URLFileName(final String scheme,
    30                         final String hostName,
    31                         final int port,
    32                         final int defaultPort,
    33                         final String userName,
    34                         final String password,
    35                         final String path,
    36                         final FileType type,
    37                         final String queryString)
    38      {
    39          super(scheme, hostName, port, defaultPort, userName, password, path, type);
    40          this.queryString = queryString;
    41      }
    42
    43      /***
    44       * get the query string
    45       *
    46       * @return the query string part of the filename
    47       */
    48      public String getQueryString()
    49      {
    50          return queryString;
    51      }
    52
    53      /***
    54       * get the path and query string e.g. /path/servlet?param1=true
    55       *
    56       * @return the path and its query string
    57       */
    58      public String getPathQuery()
    59      {
    60          StringBuffer sb = new StringBuffer(250);
    61          sb.append(getPath());
    62          sb.append("?");
    63          sb.append(getQueryString());
    64
    65          return sb.toString();
    66      }
    67
    68      /***
    69       * get the path encoded suitable for url like filesystem e.g. (http, webdav)
    70       *
    71       * @param charset the charset used for the path encoding
    72       */
    73      public String getPathQueryEncoded(String charset) throws URIException, FileSystemException
    74      {
    75          if (getQueryString() == null)
    76          {
    77              if (charset != null)
    78              {
    79                  return URIUtil.encodePath(getPathDecoded(), charset);
    80              }
    81              else
    82              {
    83                  return URIUtil.encodePath(getPathDecoded());
    84              }
    85          }
    86
    87          StringBuffer sb = new StringBuffer(250);
    88          if (charset != null)
    89          {
    90              sb.append(URIUtil.encodePath(getPathDecoded(), charset));
    91          }
    92          else
    93          {
    94              sb.append(URIUtil.encodePath(getPathDecoded()));
    95          }
    96          sb.append("?");
    97          sb.append(getQueryString());
    98          return sb.toString();
    99      }
    100
    101     public FileName createName(final String absPath, FileType type)
    102     {
    103         return new URLFileName(getScheme(),
    104             getHostName(),
    105             getPort(),
    106             getDefaultPort(),
    107             getUserName(),
    108             getPassword(),
    109             absPath,
    110             type,
    111             getQueryString());
    112     }
    113
    114     /***
    115      * append query string to the uri
    116      *
    117      * @return the uri
    118      */
    119     protected String createURI()
    120     {
    121         if (getQueryString() != null)
    122         {
    123             StringBuffer sb = new StringBuffer(250);
    124             sb.append(super.createURI());
    125             sb.append("?");
    126             sb.append(getQueryString());
    127
    128             return sb.toString();
    129         }
    130
    131         return super.createURI();
    132     }
    133
    134     public String getURIEncoded(String charset) throws FileSystemException, URIException
    135     {
    136         StringBuffer sb = new StringBuffer(80);
    137         appendRootUri(sb, true);
    138         sb.append(getPathQueryEncoded(charset));
    139         return sb.toString();
    140     }
    141 }