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.web;
35  
36  import fr.paris.lutece.plugins.folderlisting.business.portlet.FolderListingPortlet;
37  import fr.paris.lutece.plugins.folderlisting.business.portlet.FolderListingPortletHome;
38  import fr.paris.lutece.portal.business.page.Page;
39  import fr.paris.lutece.portal.business.page.PageHome;
40  import fr.paris.lutece.portal.service.html.EncodingService;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.web.constants.Parameters;
45  
46  import java.io.BufferedInputStream;
47  import java.io.File;
48  import java.io.FileInputStream;
49  import java.io.FileNotFoundException;
50  import java.io.IOException;
51  import java.io.InputStream;
52  
53  import javax.servlet.http.HttpServletRequest;
54  
55  
56  /**
57   * This class provides methods needed to serve the user a folderlisting file
58   */
59  public class FolderListingFileJspBean
60  {
61      //////////////////////////////////////////////////////////////////////////
62      // Constants
63      // Parameters
64      private static final String PARAMETER_FILE_NAME = "file";
65      private static final String PARAMETER_DIRECTORY_NAME = "folder";
66      private static final String PARAMETER_PORTLET_ID = "portlet_id";
67      private static final String PARAMETER_PAGE_ID = "page_id";
68      private static final String PARAMETER_MESSAGE_ERROR = "error";
69  
70      //JSP
71      private static final String PROPERTY_ERROR_NOT_FOUND = "folderlisting.error.NotFound";
72      private static final String PROPERTY_ERROR_NOT_ALLOWED = "folderlisting.error.NotAllowed";
73      public static final int FILE_NOT_FOUND = 0;
74      public static final int FILE_NOT_ALLOWED = 1;
75  
76      /**
77       * Public constructor
78       */
79      public FolderListingFileJspBean(  )
80      {
81      }
82  
83      /**
84       * Get the directory path of the file to display from the request
85       *
86       * @param request the http request
87       *
88       * @return the path of the directory containing the file to display (given from the root folder defined for the
89       *         porltet)
90       */
91      public String getDirectoryPath( HttpServletRequest request )
92      {
93          String strDirPath = request.getParameter( PARAMETER_DIRECTORY_NAME );
94  
95          return strDirPath;
96      }
97  
98      /**
99       * Get the binary content corresponding to the file to display
100      *
101      * @param request the http request
102      *
103      * @return the content to display
104      */
105     public byte[] getFileContent( HttpServletRequest request )
106     {
107         String strIdPortlet = request.getParameter( Parameters.PORTLET_ID );
108 
109         FolderListingPortlet folderListingPortlet = (FolderListingPortlet) FolderListingPortletHome.findByPrimaryKey( Integer.parseInt( 
110                     strIdPortlet ) );
111 
112         //    check that the directory exists. if not, replace by root dir.
113         File file = new File( AppPathService.getAbsolutePathFromRelativePath( folderListingPortlet.getRootFolderPath(  ) +
114                     getDirectoryPath( request ) + "/" + getFilename( request ) ) );
115 
116         if ( file.exists(  ) && file.isFile(  ) )
117         {
118             try
119             {
120                 final InputStream in0 = new FileInputStream( file );
121                 final InputStream in = new BufferedInputStream( in0 );
122 
123                 final byte[] buf = new byte[(int) file.length(  )];
124                 int len = 0;
125                 int pos = 0;
126 
127                 do
128                 {
129                     len = in.read( buf, pos, buf.length - pos );
130                     pos += len;
131                 }
132                 while ( len > 0 );
133 
134                 in.close(  );
135 
136                 return buf;
137             }
138             catch ( FileNotFoundException e )
139             {
140                 AppLogService.error( e.getMessage(  ), e );
141 
142                 return null;
143             }
144             catch ( IOException e )
145             {
146                 AppLogService.error( e.getMessage(  ), e );
147 
148                 return null;
149             }
150         }
151 
152         return null;
153     }
154 
155     public String getFileErrorUrl( HttpServletRequest request, int nErrorType )
156     {
157         String strError = null;
158         String strDirPath = request.getParameter( PARAMETER_DIRECTORY_NAME );
159         String strPortletId = request.getParameter( PARAMETER_PORTLET_ID );
160         String strPageId = request.getParameter( PARAMETER_PAGE_ID );
161 
162         switch ( nErrorType )
163         {
164             case FILE_NOT_FOUND:
165                 strError = I18nService.getLocalizedString( PROPERTY_ERROR_NOT_FOUND, request.getLocale(  ) );
166 
167                 break;
168 
169             case FILE_NOT_ALLOWED:
170                 strError = I18nService.getLocalizedString( PROPERTY_ERROR_NOT_ALLOWED, request.getLocale(  ) );
171 
172                 break;
173 
174             default:
175                 strError = I18nService.getLocalizedString( PROPERTY_ERROR_NOT_FOUND, request.getLocale(  ) );
176 
177                 break;
178         }
179 
180         strError = EncodingService.encodeUrl( strError );
181 
182         String strBaseUrl = AppPathService.getBaseUrl( request );
183         String strParam = PARAMETER_MESSAGE_ERROR + "=" + strError + "&" + PARAMETER_PAGE_ID + "=" + strPageId + "&" +
184             PARAMETER_DIRECTORY_NAME + "_" + strPortletId + "=" + strDirPath + "#" + PARAMETER_PORTLET_ID + "_" +
185             strPortletId;
186         String strUrl = strBaseUrl + AppPathService.getPortalUrl(  ) + "?" + strParam; // TODO Use UrlItem to build this url
187 
188         return strUrl;
189     }
190 
191     /**
192      * Get the name of the file to display from the request
193      *
194      * @param request the http request
195      *
196      * @return the name of the file to display
197      */
198     public String getFilename( HttpServletRequest request )
199     {
200         String strFileName = request.getParameter( PARAMETER_FILE_NAME );
201 
202         return strFileName;
203     }
204 
205     /**
206      * Check that the user can view the file requested
207      *
208      * @param request the http request
209      *
210      * @return true if the user has the right to view the file, false otherwise
211      */
212     public boolean checkRights( HttpServletRequest request )
213     {
214         String strIdPortlet = request.getParameter( Parameters.PORTLET_ID );
215 
216         FolderListingPortlet folderListingPortlet = (FolderListingPortlet) FolderListingPortletHome.findByPrimaryKey( Integer.parseInt( 
217                     strIdPortlet ) );
218 
219         boolean bRights = false;
220 
221         Page page = PageHome.findByPrimaryKey( folderListingPortlet.getPageId(  ) );
222         bRights = page.isVisible( request );
223 
224         return bRights;
225     }
226 }