You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by im...@apache.org on 2004/05/21 22:43:40 UTC

cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http HttpFileContentInfoFactory.java HttpFileObject.java

imario      2004/05/21 13:43:39

  Modified:    vfs/src/java/org/apache/commons/vfs/provider
                        AbstractFileObject.java DefaultFileContent.java
                        DefaultURLConnection.java
                        DefaultURLStreamHandler.java
               vfs/src/java/org/apache/commons/vfs FileContent.java
                        FileSystem.java FileSystemManager.java
                        GlobalConfiguration.java
               vfs/src/java/org/apache/commons/vfs/provider/http
                        HttpFileObject.java
  Added:       vfs/src/java/org/apache/commons/vfs/impl
                        DefaultFileContentInfo.java
                        FileContentInfoFilenameFactory.java
               vfs/src/java/org/apache/commons/vfs FileContentInfo.java
                        FileContentInfoFactory.java
               vfs/src/java/org/apache/commons/vfs/provider/http
                        HttpFileContentInfoFactory.java
  Log:
  introduced FileContentInfo&FileContentInfoFactory
  
  Now it is possible that a fileprovider can also provide informations about the content of a file.
  Currently: content-type and content-encoding.
  
  Currently only the Http filesystem is able to provide both. The values are the ones retrieved from the web-server.
  
  All other filesystems are using the FileContentInfoFilenameFactory.
  The content-type is determined by the extension of the filename using the URLConnection.getFileNameMap() jdk function.
  The content-encoding is always null.
  
  This default Factory can be override through the GlobalConfiguration.
  
  Revision  Changes    Path
  1.42      +12 -1     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java
  
  Index: AbstractFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java,v
  retrieving revision 1.41
  retrieving revision 1.42
  diff -u -r1.41 -r1.42
  --- AbstractFileObject.java	20 May 2004 19:42:30 -0000	1.41
  +++ AbstractFileObject.java	21 May 2004 20:43:18 -0000	1.42
  @@ -17,6 +17,7 @@
   
   import org.apache.commons.vfs.Capability;
   import org.apache.commons.vfs.FileContent;
  +import org.apache.commons.vfs.FileContentInfoFactory;
   import org.apache.commons.vfs.FileName;
   import org.apache.commons.vfs.FileObject;
   import org.apache.commons.vfs.FileSelector;
  @@ -24,6 +25,7 @@
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.FileType;
   import org.apache.commons.vfs.FileUtil;
  +import org.apache.commons.vfs.GlobalConfiguration;
   import org.apache.commons.vfs.NameScope;
   import org.apache.commons.vfs.Selectors;
   
  @@ -849,7 +851,7 @@
           attach();
           if (content == null)
           {
  -            content = new DefaultFileContent(this);
  +            content = new DefaultFileContent(this, createFileContentInfoFactory());
           }
           return content;
       }
  @@ -1244,5 +1246,14 @@
       public boolean isAttached()
       {
           return attached;
  +    }
  +
  +    /**
  +     * create the filecontentinfo implementation
  +     */
  +    protected FileContentInfoFactory createFileContentInfoFactory()
  +    {
  +        GlobalConfiguration gc = getFileSystem().getFileSystemManager().getGlobalConfiguration();
  +        return gc.getFileContentInfoFactory();
       }
   }
  
  
  
  1.21      +18 -1     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultFileContent.java
  
  Index: DefaultFileContent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultFileContent.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- DefaultFileContent.java	10 May 2004 20:09:42 -0000	1.20
  +++ DefaultFileContent.java	21 May 2004 20:43:19 -0000	1.21
  @@ -16,6 +16,8 @@
   package org.apache.commons.vfs.provider;
   
   import org.apache.commons.vfs.FileContent;
  +import org.apache.commons.vfs.FileContentInfo;
  +import org.apache.commons.vfs.FileContentInfoFactory;
   import org.apache.commons.vfs.FileObject;
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.util.MonitorInputStream;
  @@ -49,10 +51,13 @@
       private FileContentOutputStream outstr;
       private Map attrs;
       private Map roAttrs;
  +    private FileContentInfo fileContentInfo;
  +    private final FileContentInfoFactory fileContentInfoFactory;
   
  -    public DefaultFileContent(final AbstractFileObject file)
  +    public DefaultFileContent(final AbstractFileObject file, final FileContentInfoFactory fileContentInfoFactory)
       {
           this.file = file;
  +        this.fileContentInfoFactory = fileContentInfoFactory;
       }
   
       /**
  @@ -422,4 +427,16 @@
           }
       }
   
  +    /**
  +     * get the content info. e.g. content-type, content-encoding
  +     */
  +    public FileContentInfo getContentInfo() throws FileSystemException
  +    {
  +        if (fileContentInfo == null)
  +        {
  +            fileContentInfo = fileContentInfoFactory.create(this);
  +        }
  +
  +        return fileContentInfo;
  +    }
   }
  
  
  
  1.11      +26 -1     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultURLConnection.java
  
  Index: DefaultURLConnection.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultURLConnection.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- DefaultURLConnection.java	20 May 2004 19:43:22 -0000	1.10
  +++ DefaultURLConnection.java	21 May 2004 20:43:21 -0000	1.11
  @@ -15,7 +15,6 @@
    */
   package org.apache.commons.vfs.provider;
   
  -import org.apache.commons.vfs.Capability;
   import org.apache.commons.vfs.FileContent;
   import org.apache.commons.vfs.FileSystemException;
   
  @@ -73,6 +72,31 @@
           return -1;
       }
   
  +    public String getContentType()
  +    {
  +        try
  +        {
  +            return content.getContentInfo().getContentType();
  +        }
  +        catch (FileSystemException e)
  +        {
  +            throw new RuntimeException(e);
  +        }
  +    }
  +
  +    public String getContentEncoding()
  +    {
  +        try
  +        {
  +            return content.getContentInfo().getContentEncoding();
  +        }
  +        catch (FileSystemException e)
  +        {
  +            throw new RuntimeException(e);
  +        }
  +    }
  +
  +    /*
       public String getHeaderField(String name)
       {
           try
  @@ -93,4 +117,5 @@
               throw new RuntimeException(e);
           }
       }
  +    */
   }
  
  
  
  1.14      +0 -0      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultURLStreamHandler.java
  
  Index: DefaultURLStreamHandler.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultURLStreamHandler.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/DefaultFileContentInfo.java
  
  Index: DefaultFileContentInfo.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs.impl;
  
  import org.apache.commons.vfs.FileContentInfo;
  
  public class DefaultFileContentInfo implements FileContentInfo
  {
      private final String contentType;
      private final String contentEncoding;
  
      public DefaultFileContentInfo(final String contentType, final String contentEncoding)
      {
          this.contentType = contentType;
          this.contentEncoding = contentEncoding;
      }
  
      public String getContentType()
      {
          return contentType;
      }
  
      public String getContentEncoding()
      {
          return contentEncoding;
      }
  }
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/impl/FileContentInfoFilenameFactory.java
  
  Index: FileContentInfoFilenameFactory.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs.impl;
  
  import org.apache.commons.vfs.FileContent;
  import org.apache.commons.vfs.FileContentInfo;
  import org.apache.commons.vfs.FileContentInfoFactory;
  
  import java.net.FileNameMap;
  import java.net.URLConnection;
  
  /**
   * The FileContentInfoFilenameFactory.<br>
   * Uses the filename extension to determine the content-type.<br>
   * The content-encoding is not resolved.
   * 
   * @author <a href="mailto:imario@apache.org">Mario Ivanovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/05/21 20:43:28 $
   */
  public class FileContentInfoFilenameFactory implements FileContentInfoFactory
  {
      public FileContentInfo create(FileContent fileContent)
      {
          String contentType = null;
  
          String name = fileContent.getFile().getName().getBaseName();
          if (name != null)
          {
              FileNameMap fileNameMap = URLConnection.getFileNameMap();
              contentType = fileNameMap.getContentTypeFor(name);
          }
  
          return new DefaultFileContentInfo(contentType, null);
      }
  }
  
  
  
  1.13      +6 -0      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileContent.java
  
  Index: FileContent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileContent.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- FileContent.java	10 May 2004 20:09:45 -0000	1.12
  +++ FileContent.java	21 May 2004 20:43:29 -0000	1.13
  @@ -179,4 +179,10 @@
        * method.
        */
       void close() throws FileSystemException;
  +
  +
  +    /**
  +     * get the content info. e.g. type, encoding, ...
  +     */
  +    public FileContentInfo getContentInfo() throws FileSystemException;
   }
  
  
  
  1.16      +1 -1      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileSystem.java
  
  Index: FileSystem.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileSystem.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  
  
  
  1.19      +0 -0      jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileSystemManager.java
  
  Index: FileSystemManager.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileSystemManager.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  
  
  
  1.3       +19 -1     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/GlobalConfiguration.java
  
  Index: GlobalConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/GlobalConfiguration.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- GlobalConfiguration.java	17 May 2004 20:13:20 -0000	1.2
  +++ GlobalConfiguration.java	21 May 2004 20:43:29 -0000	1.3
  @@ -16,6 +16,7 @@
   package org.apache.commons.vfs;
   
   import org.apache.commons.vfs.cache.DefaultFilesCache;
  +import org.apache.commons.vfs.impl.FileContentInfoFilenameFactory;
   import org.apache.commons.vfs.provider.AbstractVfsComponent;
   import org.apache.commons.vfs.provider.VfsComponent;
   
  @@ -30,6 +31,7 @@
       private boolean inUse = false;
   
       private FilesCache filesCache = new DefaultFilesCache();
  +    private FileContentInfoFactory fileContentInfoFactory = new FileContentInfoFilenameFactory();
   
       public GlobalConfiguration()
       {
  @@ -102,5 +104,21 @@
       public FilesCache getFilesCache()
       {
           return filesCache;
  +    }
  +
  +    /**
  +     * returns the fileContentInfoFactory used to determine the infos of a file content.
  +     */
  +    public FileContentInfoFactory getFileContentInfoFactory()
  +    {
  +        return fileContentInfoFactory;
  +    }
  +
  +    /**
  +     * set the fileContentInfoFactory used to determine the infos of a file content.
  +     */
  +    public void setFileContentInfoFactory(FileContentInfoFactory fileContentInfoFactory)
  +    {
  +        this.fileContentInfoFactory = fileContentInfoFactory;
       }
   }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileContentInfo.java
  
  Index: FileContentInfo.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs;
  
  /**
   * Interface to the content info
   *
   * @author <a href="mailto:imario@apache.org">Mario Ivanovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/05/21 20:43:29 $
   */
  public interface FileContentInfo
  {
      /**
       * the content type
       */
      public String getContentType();
  
      /**
       * the content encoding
       */
      public String getContentEncoding();
  }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileContentInfoFactory.java
  
  Index: FileContentInfoFactory.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs;
  
  /**
   * Create a class which is able to determine the content-info for the given content
   * 
   * @author <a href="mailto:imario@apache.org">Mario Ivanovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/05/21 20:43:29 $
   */
  public interface FileContentInfoFactory
  {
      FileContentInfo create(FileContent fileContent) throws FileSystemException;
  }
  
  
  
  1.7       +15 -4     jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileObject.java
  
  Index: HttpFileObject.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileObject.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpFileObject.java	20 May 2004 19:34:01 -0000	1.6
  +++ HttpFileObject.java	21 May 2004 20:43:30 -0000	1.7
  @@ -16,12 +16,12 @@
   package org.apache.commons.vfs.provider.http;
   
   import org.apache.commons.httpclient.Header;
  -import org.apache.commons.httpclient.HeaderElement;
   import org.apache.commons.httpclient.HttpClient;
   import org.apache.commons.httpclient.HttpMethod;
   import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.methods.HeadMethod;
   import org.apache.commons.httpclient.util.DateParser;
  +import org.apache.commons.vfs.FileContentInfoFactory;
   import org.apache.commons.vfs.FileName;
   import org.apache.commons.vfs.FileSystemException;
   import org.apache.commons.vfs.FileType;
  @@ -31,8 +31,6 @@
   import java.io.IOException;
   import java.io.InputStream;
   import java.net.HttpURLConnection;
  -import java.util.Map;
  -import java.util.TreeMap;
   
   /**
    * A file object backed by commons httpclient.
  @@ -189,6 +187,18 @@
           }
       }
   
  +
  +    protected FileContentInfoFactory createFileContentInfoFactory()
  +    {
  +        return new HttpFileContentInfoFactory();
  +    }
  +
  +    HeadMethod getHeadMethod()
  +    {
  +        return method;
  +    }
  +
  +    /*
       protected Map doGetAttributes() throws Exception
       {
           TreeMap map = new TreeMap();
  @@ -206,4 +216,5 @@
           map.put("content-encoding", method.getResponseCharSet());
           return map;
       }
  +    */
   }
  
  
  
  1.1                  jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileContentInfoFactory.java
  
  Index: HttpFileContentInfoFactory.java
  ===================================================================
  /*
   * Copyright 2002, 2003,2004 The Apache Software Foundation.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  package org.apache.commons.vfs.provider.http;
  
  import org.apache.commons.httpclient.Header;
  import org.apache.commons.httpclient.HeaderElement;
  import org.apache.commons.httpclient.HttpException;
  import org.apache.commons.vfs.FileContent;
  import org.apache.commons.vfs.FileContentInfo;
  import org.apache.commons.vfs.FileContentInfoFactory;
  import org.apache.commons.vfs.FileSystemException;
  import org.apache.commons.vfs.impl.DefaultFileContentInfo;
  
  /**
   * Description
   * 
   * @author <a href="mailto:imario@apache.org">Mario Ivanovits</a>
   * @version $Revision: 1.1 $ $Date: 2004/05/21 20:43:30 $
   */
  public class HttpFileContentInfoFactory implements FileContentInfoFactory
  {
      public FileContentInfo create(FileContent fileContent) throws FileSystemException
      {
          HttpFileObject httpFile = (HttpFileObject) fileContent.getFile();
  
          String contentType = null;
          String contentEncoding = null;
  
          Header header = httpFile.getHeadMethod().getResponseHeader("content-type");
          if (header != null)
          {
              HeaderElement[] element = new org.apache.commons.httpclient.HeaderElement[0];
              try
              {
                  element = header.getValues();
              }
              catch (HttpException e)
              {
                  throw new FileSystemException(e);
              }
              if (element != null && element.length > 0)
              {
                  contentType = element[0].getName();
              }
          }
  
          contentEncoding = httpFile.getHeadMethod().getResponseCharSet();
  
          return new DefaultFileContentInfo(contentType, contentEncoding);
      }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org