View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.directory.service;
35  
36  import javax.servlet.http.HttpServletRequest;
37  
38  import fr.paris.lutece.plugins.directory.business.EntryTypeUrl;
39  import fr.paris.lutece.plugins.directory.business.File;
40  import fr.paris.lutece.plugins.directory.business.FileHome;
41  import fr.paris.lutece.plugins.directory.business.PhysicalFile;
42  import fr.paris.lutece.plugins.directory.business.PhysicalFileHome;
43  import fr.paris.lutece.plugins.directory.service.record.IRecordService;
44  import fr.paris.lutece.plugins.directory.service.record.RecordService;
45  import fr.paris.lutece.portal.service.image.ImageResource;
46  import fr.paris.lutece.portal.service.image.ImageResourceManager;
47  import fr.paris.lutece.portal.service.image.ImageResourceProvider;
48  import fr.paris.lutece.portal.service.plugin.Plugin;
49  import fr.paris.lutece.portal.service.plugin.PluginService;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.portal.service.util.AppLogService;
52  import fr.paris.lutece.portal.web.LocalVariables;
53  import fr.paris.lutece.portal.web.constants.Parameters;
54  import fr.paris.lutece.util.file.FileUtil;
55  import fr.paris.lutece.util.url.UrlItem;
56  
57  /**
58   * Service for Url entry types. Provide ImageResource managemenent
59   *
60   */
61  public class FileImgService implements ImageResourceProvider
62  {
63      private static FileImgService _singleton = new FileImgService( );
64      private static final String IMAGE_RESOURCE_TYPE_ID = "directory_entry_img";
65  
66      /**
67       * Creates a new instance of FileImgService
68       */
69      FileImgService( )
70      {
71      }
72  
73      /**
74       * Initializes the service
75       */
76      public void register( )
77      {
78          ImageResourceManager.registerProvider( this );
79      }
80  
81      /**
82       * Get the unique instance of the service
83       *
84       * @return The unique instance
85       */
86      public static FileImgService getInstance( )
87      {
88          return _singleton;
89      }
90  
91      /**
92       * Return the Resource id
93       * 
94       * @param nIdResource
95       *            The resource identifier
96       * @return The Resource Image
97       */
98      @Override
99      public ImageResource getImageResource( int nIdResource )
100     {
101         // When using an older core version (before 5.1.5), the local variables will not
102         // have been set by the image servlet. So we can get null or a request from another thread.
103         // We could try to detect this by checking request.getServletPath( ) (or maybe other things?)
104         // but it would break if we decide to expose this provider through another entrypoint.
105         // Also, on tomcat (tested 8.5.5), it seems like the request object is reused just like
106         // the thread, so that even if the local variables were set in another request,
107         // the object we get here is the correct one (with the corect LuteceUser or AdminUser etc).
108         // Also, Portal.jsp, the main entry point of the webapp, does clean up the local variables.
109         // Note that the other request could even have run code from another webapp (not even a lutece webapp)
110         // Also, we could log a warning here when request is null, but then it would prevent from using
111         // this function from code not associated with a request. So no warnings.
112         HttpServletRequest request = LocalVariables.getRequest( );
113 
114         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
115         File file = FileHome.findByPrimaryKey( nIdResource, plugin );
116         if ( ( file != null ) && ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
117         {
118             IRecordService recordService = SpringContextService.getBean( RecordService.BEAN_SERVICE );
119             if ( request == null || recordService.isFileAuthorized( nIdResource, request, plugin ) )
120             {
121                 PhysicalFile physicalFile = PhysicalFileHome.findByPrimaryKey( file.getPhysicalFile( ).getIdPhysicalFile( ), plugin );
122                 if ( physicalFile != null )
123                 {
124                     ImageResource imageResource = new ImageResource( );
125                     imageResource.setImage( physicalFile.getValue( ) );
126                     imageResource.setMimeType( file.getMimeType( ) );
127 
128                     return imageResource;
129                 }
130             }
131         }
132 
133         return null;
134     }
135 
136     /**
137      * Return the Resource Type id
138      * 
139      * @return The Resource Type Id
140      */
141     @Override
142     public String getResourceTypeId( )
143     {
144         return IMAGE_RESOURCE_TYPE_ID;
145     }
146 
147     /**
148      * Management of the image associated to the {@link EntryTypeUrl}
149      * 
150      * @param nEntryUrl
151      *            The {@link EntryTypeUrl} identifier
152      * @return The url of the resource without HTML escape characters
153      */
154     public static String getResourceImageEntryUrlWhitoutEntities( int nEntryUrl )
155     {
156         return getResourceImageEntryUrl( nEntryUrl, false );
157     }
158 
159     /**
160      * Management of the image associated to the {@link EntryUrl}
161      * 
162      * @param nEntryUrl
163      *            The {@link EntryUrl} identifier
164      * @param bWithEntities
165      *            True to get the URL with HTML escape characters, false otherwise
166      * @return The url of the resource
167      */
168     private static String getResourceImageEntryUrl( int nEntryUrl, boolean bWithEntities )
169     {
170         String strResourceType = FileImgService.getInstance( ).getResourceTypeId( );
171         UrlItem url = new UrlItem( Parameters.IMAGE_SERVLET );
172         url.addParameter( Parameters.RESOURCE_TYPE, strResourceType );
173         url.addParameter( Parameters.RESOURCE_ID, Integer.toString( nEntryUrl ) );
174 
175         return bWithEntities ? url.getUrlWithEntity( ) : url.getUrl( );
176     }
177 }