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.plugins.folderlisting.business.portlet;
35  
36  import fr.paris.lutece.plugins.folderlisting.business.FolderListingDirectory;
37  import fr.paris.lutece.plugins.folderlisting.business.FolderListingFile;
38  import fr.paris.lutece.plugins.folderlisting.business.FolderListingHome;
39  import fr.paris.lutece.plugins.folderlisting.service.Folder;
40  import fr.paris.lutece.plugins.folderlisting.service.FolderService;
41  import fr.paris.lutece.portal.business.portlet.Portlet;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
45  import fr.paris.lutece.util.xml.XmlUtil;
46  
47  import java.io.File;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  
52  /**
53   * This class represents business objects FolderListingPortlet
54   */
55  public class FolderListingPortlet extends Portlet
56  {
57      ///////////////////////////////////////////////////////////////////////
58      // Constants
59  
60      // Xml tags
61      private static final String TAG_ALIAS_ROOT_DIRECTORY = "directory-root-alias";
62      private static final String TAG_PORTLET_FOLDERLISTING = "folderlisting-portlet";
63      private static final String TAG_DIRECTORY_PATH = "directory-path";
64  
65      // Variables declarations
66      private int _nFolderListingFileId;
67      private String _strFolderPath;
68      private String _strRootFolderId;
69  
70      /**
71       * Creates a new FolderListingPortlet object.
72       */
73      public FolderListingPortlet(  )
74      {
75          setPortletTypeId( FolderListingPortletHome.getInstance(  ).getPortletTypeId(  ) );
76      }
77  
78      /**
79       * Sets the Id of the folderlistingfile
80       *
81       * @param nFolderListingFileId the new Id
82       */
83      public void setFolderListingFileId( int nFolderListingFileId )
84      {
85          _nFolderListingFileId = nFolderListingFileId;
86      }
87  
88      /**
89       * Returns the Id of this folderlistingfile
90       *
91       * @return the folderlistingfile Id
92       */
93      public int getFolderListingFileId(  )
94      {
95          return _nFolderListingFileId;
96      }
97  
98      /**
99       * Returns the FolderPath
100      *
101      * @return The FolderPath
102      */
103     public String getFolderPath(  )
104     {
105         return _strFolderPath;
106     }
107 
108     /**
109      * Sets the RootFolderId
110      *
111      * @param strRootFolderId The RootFolderId
112      */
113     public void setRootFolderId( String strRootFolderId )
114     {
115         _strRootFolderId = strRootFolderId;
116     }
117 
118     /**
119      * Returns the RootFolderId
120      *
121      * @return The RootFolderId
122      */
123     public String getRootFolderId(  )
124     {
125         return _strRootFolderId;
126     }
127 
128     /**
129      * Sets the FolderPath
130      *
131      * @param strFolderPath The FolderPath
132      */
133     public void setFolderPath( String strFolderPath )
134     {
135         _strFolderPath = strFolderPath;
136     }
137 
138     /**
139      * Get the path corresponding to Root Folder Id
140      * @return The path of the root folder
141      */
142     public String getRootFolderPath(  )
143     {
144         String strFolderPath = "";
145 
146         Folder folder = FolderService.getInstance(  ).getFolder( getRootFolderId(  ) );
147 
148         if ( folder != null )
149         {
150             strFolderPath = folder.getPath(  );
151         }
152         else
153         {
154             AppLogService.error( "Folderlisting : Invalid folder Id :" + getRootFolderId(  ) );
155         }
156 
157         return strFolderPath;
158     }
159 
160     /**
161      * Get the path corresponding to Root Folder Id
162      * @return The path of the root folder
163      */
164     public String getRootFolderName(  )
165     {
166         String strRootFolderName = "";
167 
168         Folder folder = FolderService.getInstance(  ).getFolder( getRootFolderId(  ) );
169 
170         if ( folder != null )
171         {
172             strRootFolderName = folder.getName(  );
173         }
174         else
175         {
176             AppLogService.error( "Folderlisting : Invalid folder Id :" + getRootFolderId(  ) );
177         }
178 
179         return strRootFolderName;
180     }
181 
182     /**
183      * Returns the Xml code of the FolderListing portlet without XML heading
184      *
185      * @param request The http request
186      * @return the Xml code of the FolderListing portlet content
187      */
188     public String getXml( HttpServletRequest request )
189     {
190         // retrieve the folder path from the request
191         setFolderPathFromRequest( request );
192 
193         StringBuffer sbXml = new StringBuffer(  );
194 
195         XmlUtil.beginElement( sbXml, TAG_PORTLET_FOLDERLISTING );
196 
197         XmlUtil.addElement( sbXml, TAG_ALIAS_ROOT_DIRECTORY, getRootFolderName(  ) );
198 
199         XmlUtil.addElement( sbXml, TAG_DIRECTORY_PATH, getFolderPath(  ) );
200 
201         try
202         {
203             for ( FolderListingDirectory folderlistingDirectory : FolderListingHome.getSubDirectories( getRootFolderPath(  ) +
204                     getFolderPath(  ) ) )
205             {
206                 sbXml.append( folderlistingDirectory.getXml( request ) );
207             }
208 
209             for ( FolderListingFile folderlistingFile : FolderListingHome.getFiles( getRootFolderPath(  ) +
210                     getFolderPath(  ) ) )
211             {
212                 sbXml.append( folderlistingFile.getXml( request ) );
213             }
214 
215             XmlUtil.endElement( sbXml, TAG_PORTLET_FOLDERLISTING );
216         }
217         catch ( DirectoryNotFoundException e )
218         {
219             AppLogService.error( e.getMessage(  ), e );
220             // Clear the buffer
221             sbXml.delete( 0, sbXml.length(  ) );
222         }
223 
224         return addPortletTags( sbXml );
225     }
226 
227     /**
228      * Set the current folder path from the string passed in request.
229      * This string is the path from the webapp folder.
230      * The retrieved path should :
231      * <ul>
232      * <li>match an existing directory on the filesystem</li>
233      * <li>start with the root path defined for the portlet : we only allow to browse folders above the one defined as root for the portlet.</li>
234      * </ul>
235      * If not, it is replaced by the root path.
236      *
237      * @param request the http request
238      */
239     private void setFolderPathFromRequest( HttpServletRequest request )
240     {
241         String strFolderPath = null;
242 
243         if ( request != null )
244         {
245             strFolderPath = request.getParameter( "folder_" + getId(  ) );
246         }
247 
248         if ( ( strFolderPath == null ) || ( strFolderPath.trim(  ).equals( "" ) ) )
249         {
250             setFolderPath( "" );
251         }
252         else
253         {
254             //  check that the directory exists. if not, replace by root dir.
255             File fDirectory = new File( AppPathService.getAbsolutePathFromRelativePath( getRootFolderPath(  ) +
256                         strFolderPath ) );
257 
258             if ( fDirectory.exists(  ) )
259             {
260                 setFolderPath( strFolderPath );
261             }
262             else
263             {
264                 setFolderPath( "" );
265             }
266         }
267     }
268 
269     /**
270      * Returns the Xml code of the FolderListing portlet with XML heading
271      *
272      * @param request the HttpServletRequest
273      * @return the Xml code of the FolderListing portlet
274      */
275     public String getXmlDocument( HttpServletRequest request )
276     {
277         return XmlUtil.getXmlHeader(  ) + getXml( request );
278     }
279 
280     /**
281      * Update the portlet
282      */
283     public void update(  )
284     {
285         FolderListingPortletHome.getInstance(  ).update( this );
286     }
287 
288     /**
289      * Remove portlet
290      */
291     public void remove(  )
292     {
293         FolderListingPortletHome.getInstance(  ).remove( this );
294     }
295 }