View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.util.filesystem;
35  
36  import fr.paris.lutece.portal.service.util.AppLogService;
37  import fr.paris.lutece.portal.service.util.AppPathService;
38  
39  // Java IO
40  import java.io.File;
41  import java.io.IOException;
42  
43  // Java Util
44  import java.util.ArrayList;
45  import java.util.List;
46  import java.util.TreeSet;
47  
48  import javax.activation.MimetypesFileTypeMap;
49  
50  
51  /**
52   * This Service is used to manipulate Files and Directories in the File System.
53   */
54  public final class FileSystemUtil
55  {
56      private static final String DEFAULT_MIME_TYPE = "application/octet-stream";
57      private static final String FILE_SEPARATOR = File.separator;
58      private static final String FILE_MIME_TYPE = "WEB-INF" + FILE_SEPARATOR + "conf" + FILE_SEPARATOR + "mime.types";
59  
60      /**
61       * Private constructor
62       */
63      private FileSystemUtil(  )
64      {
65      }
66  
67      /**
68       * Returns the sub-directories of a directory.
69       *
70       * @param strRoot the root directory.
71       * @param strDirectoryRelativePath the parent directory.
72       * @throws DirectoryNotFoundException if the directory does not exist.
73       * @return The list of sub-directories.
74       */
75      public static List<File> getSubDirectories( String strRoot, String strDirectoryRelativePath )
76          throws DirectoryNotFoundException
77      {
78          // Files' list init
79          ArrayList<File> listFiles = new ArrayList<File>(  );
80  
81          // Directory path
82          String strDirectory = strRoot + strDirectoryRelativePath;
83  
84          // Directory
85          File fDirectory = new File( strDirectory );
86  
87          if ( !fDirectory.exists(  ) )
88          {
89              throw new DirectoryNotFoundException(  );
90          }
91  
92          File[] files = fDirectory.listFiles(  );
93  
94          for ( int i = 0; i < files.length; i++ )
95          {
96              File file = files[i];
97  
98              if ( file.isDirectory(  ) )
99              {
100                 listFiles.add( file );
101             }
102         }
103 
104         return listFiles;
105     }
106 
107     /**
108      * Returns the files of a directory, alphabetically ordered.
109      *
110      * @param strRoot The root directory
111      * @param strDirectoryRelativePath The directory's path relative to the root directory.
112      * @throws DirectoryNotFoundException if the directory does not exist.
113      * @return The list of files.
114      */
115     public static List<File> getFiles( String strRoot, String strDirectoryRelativePath )
116         throws DirectoryNotFoundException
117     {
118         // Use a treeset to order files with a comparator
119         TreeSet<File> set = new TreeSet<File>( new FileNameComparator(  ) );
120 
121         // Directory path
122         String strDirectory = strRoot + strDirectoryRelativePath;
123 
124         // Directory
125         File fDirectory = new File( strDirectory );
126 
127         if ( !fDirectory.exists(  ) )
128         {
129             throw new DirectoryNotFoundException(  );
130         }
131 
132         File[] files = fDirectory.listFiles(  );
133 
134         for ( int i = 0; i < files.length; i++ )
135         {
136             File file = files[i];
137 
138             if ( file.isFile(  ) )
139             {
140                 set.add( file );
141             }
142         }
143 
144         // Convert into a list to preserve the order
145         return new ArrayList<File>( set );
146     }
147 
148     /**
149      * Return the mimetype of the file depending of his extension and the mime.types file
150      * @param strFilename the file name
151      * @return the file mime type
152      */
153     public static String getMIMEType( String strFilename )
154     {
155         try
156         {
157             MimetypesFileTypeMap mimeTypeMap = new MimetypesFileTypeMap( AppPathService.getWebAppPath(  ) +
158                     File.separator + FILE_MIME_TYPE );
159 
160             return mimeTypeMap.getContentType( strFilename.toLowerCase(  ) );
161         }
162         catch ( IOException e )
163         {
164             AppLogService.error( e );
165 
166             return DEFAULT_MIME_TYPE;
167         }
168     }
169 }