View Javadoc
1   /*
2    * Copyright (c) 2002-2022, City of 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.genericattributes.service.entrytype;
35  
36  import fr.paris.lutece.plugins.genericattributes.business.Entry;
37  import fr.paris.lutece.plugins.genericattributes.business.GenAttFileItem;
38  import fr.paris.lutece.plugins.genericattributes.business.GenericAttributeError;
39  import fr.paris.lutece.plugins.genericattributes.business.MandatoryError;
40  import fr.paris.lutece.plugins.genericattributes.business.Response;
41  import fr.paris.lutece.plugins.genericattributes.business.ResponseHome;
42  import fr.paris.lutece.plugins.genericattributes.service.file.GenericAttributeFileService;
43  import fr.paris.lutece.plugins.genericattributes.service.upload.AbstractGenAttUploadHandler;
44  import fr.paris.lutece.portal.business.file.File;
45  import fr.paris.lutece.portal.business.file.FileHome;
46  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
47  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
48  import fr.paris.lutece.util.filesystem.FileSystemUtil;
49  
50  import org.apache.commons.collections.CollectionUtils;
51  import org.apache.commons.fileupload.FileItem;
52  import org.apache.commons.lang3.StringUtils;
53  
54  import java.util.List;
55  import java.util.Locale;
56  
57  import javax.servlet.http.HttpServletRequest;
58  
59  /**
60   * Abstract entries of type files. This abstract entry type extends the abstract entry type upload.
61   */
62  public abstract class AbstractEntryTypeFile extends AbstractEntryTypeUpload
63  {
64      /**
65       * {@inheritDoc}
66       */
67      @Override
68      public abstract AbstractGenAttUploadHandler getAsynchronousUploadHandler( );
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public GenericAttributeError getResponseData( Entry entry, HttpServletRequest request, List<Response> listResponse, Locale locale )
75      {
76          if ( !( request instanceof MultipartHttpServletRequest ) )
77          {
78              return entry.isMandatory( ) ? new MandatoryError( entry, locale ) : null;
79          }
80  
81          String strAttributeName = getAttributeName( entry, request );
82  
83          if ( getAsynchronousUploadHandler( ).hasAddFileFlag( request, strAttributeName ) )
84          {
85              MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
86              List<FileItem> listFileItemsToUpload = multipartRequest.getFileList( strAttributeName );
87              List<FileItem> listUploadedFileItems = getAsynchronousUploadHandler( ).getListUploadedFiles( strAttributeName, request.getSession( ) );
88              GenericAttributeError error = null;
89  
90              // remove when multipartRequest.getFileList( ) will be fixed.
91              if ( listFileItemsToUpload.size( ) == 1 && listFileItemsToUpload.get( 0 ).getName( ).isEmpty( ) )
92              {
93                  listFileItemsToUpload = null;
94              }
95  
96              if ( listFileItemsToUpload != null )
97              {
98                  error = this.canUploadFiles( entry, listUploadedFileItems, listFileItemsToUpload, locale );
99              }
100 
101             if ( error != null )
102             {
103                 for ( FileItem fileItem : listUploadedFileItems )
104                 {
105                     Response response = getResponseFromFile( fileItem, entry, false );
106                     response.setIterationNumber( getResponseIterationValue( request ) );
107 
108                     listResponse.add( response );
109                 }
110                 return error;
111             }
112         }
113 
114         List<FileItem> listFilesSource = getFileSources( request, strAttributeName );
115 
116         GenericAttributeError genAttError = null;
117 
118         if ( getAsynchronousUploadHandler( ).hasRemoveFlag( request, strAttributeName )
119                 || getAsynchronousUploadHandler( ).hasAddFileFlag( request, strAttributeName ) )
120         {
121             if ( CollectionUtils.isNotEmpty( listFilesSource ) )
122             {
123                 for ( FileItem fileItem : listFilesSource )
124                 {
125                     listResponse.add( getResponseFromFile( fileItem, entry, false ) );
126                 }
127             }
128 
129             genAttError = new GenericAttributeError( );
130             genAttError.setErrorMessage( StringUtils.EMPTY );
131             genAttError.setMandatoryError( false );
132             genAttError.setIsDisplayableError( false );
133 
134             return genAttError;
135         }
136 
137         if ( CollectionUtils.isNotEmpty( listFilesSource ) )
138         {
139             genAttError = checkResponseData( entry, listFilesSource, locale, request );
140 
141             for ( FileItem fileItem : listFilesSource )
142             {
143                 Response response = getResponseFromFile( fileItem, entry, genAttError == null );
144                 response.setIterationNumber( getResponseIterationValue( request ) );
145 
146                 listResponse.add( response );
147             }
148 
149             if ( genAttError != null )
150             {
151                 return genAttError;
152             }
153 
154             for ( FileItem fileItem : listFilesSource )
155             {
156                 if ( checkForImages( ) )
157                 {
158                     genAttError = doCheckforImages( fileItem, entry, request.getLocale( ) );
159                 }
160             }
161 
162             return genAttError;
163         }
164 
165         if ( entry.isMandatory( ) )
166         {
167             genAttError = new MandatoryError( entry, locale );
168 
169             Response/genericattributes/business/Response.html#Response">Response response = new Response( );
170             response.setEntry( entry );
171             response.setIterationNumber( getResponseIterationValue( request ) );
172             listResponse.add( response );
173         }
174 
175         return genAttError;
176     }
177 
178     /**
179      * Get a generic attributes response from a file item
180      * 
181      * @param fileItem
182      *            The file item
183      * @param entry
184      *            The entry
185      * @param bCreatePhysicalFile
186      *            True to create the physical file associated with the file of the response, false otherwise. Note that the physical file will never be saved in
187      *            the database by this method, like any other created object.
188      * @return The created response
189      */
190     private Response getResponseFromFile( FileItem fileItem, Entry entry, boolean bCreatePhysicalFile )
191     {
192         if ( fileItem instanceof GenAttFileItem )
193         {
194             GenAttFileItem./../fr/paris/lutece/plugins/genericattributes/business/GenAttFileItem.html#GenAttFileItem">GenAttFileItem genAttFileItem = (GenAttFileItem) fileItem;
195 
196             if ( genAttFileItem.getIdResponse( ) > 0 )
197             {
198                 Response response = ResponseHome.findByPrimaryKey( genAttFileItem.getIdResponse( ) );
199                 response.setEntry( entry );
200                 response.setFile( GenericAttributeFileService.getInstance().load( response.getFile( ).getFileKey( ) ) );
201 
202                 if ( bCreatePhysicalFile )
203                 {
204                     response.getFile( ).getPhysicalFile( ).setValue( fileItem.get( ) );
205                 }
206 
207                 return response;
208             }
209         }
210 
211         Response/genericattributes/business/Response.html#Response">Response response = new Response( );
212         response.setEntry( entry );
213 
214         File file = new File( );
215         file.setTitle( fileItem.getName( ) );
216         file.setSize( ( fileItem.getSize( ) < Integer.MAX_VALUE ) ? (int) fileItem.getSize( ) : Integer.MAX_VALUE );
217 
218         if ( bCreatePhysicalFile )
219         {
220             file.setMimeType( FileSystemUtil.getMIMEType( file.getTitle( ) ) );
221 
222             PhysicalFile physicalFile = new PhysicalFile( );
223             physicalFile.setValue( fileItem.get( ) );
224             file.setPhysicalFile( physicalFile );
225         }
226 
227         response.setFile( file );
228 
229         return response;
230     }
231 }