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.formengine.web;
35  
36  import fr.paris.lutece.plugins.formengine.service.FormsRegistrationService;
37  import fr.paris.lutece.plugins.formengine.util.JSONUtils;
38  import fr.paris.lutece.portal.service.i18n.I18nService;
39  import fr.paris.lutece.portal.service.util.AppLogService;
40  import fr.paris.lutece.portal.web.upload.IAsynchronousUploadHandler;
41  
42  import net.sf.json.JSONObject;
43  
44  import org.apache.commons.fileupload.FileItem;
45  import org.apache.commons.lang.StringUtils;
46  
47  import java.util.List;
48  
49  import javax.servlet.http.HttpServletRequest;
50  import javax.servlet.http.HttpServletResponse;
51  
52  
53  /**
54   * Handler for asynchronous uploads.
55   * Files are stored using {@link SubForm#addFileItem(String, String, FileItem)}.
56   * The <code>jessionid</code> parameter should be the <strong>REAL</strong> session id,
57   * not the flash player one.
58   * The uploaded files are deleted by SubForm when filling fields.
59   *
60   */
61  public class FormengineAsynchronousUploadHandler implements IAsynchronousUploadHandler
62  {
63      private static final String PARAMETER_PAGE = "page";
64      private static final String PARAMETER_FORM = "form";
65      private static final String PARAMETER_SUBFORM = "subform";
66      private static final String PARAMETER_FIELD_NAME = "fieldname";
67      private static final String PARAMETER_JSESSION_ID = "jsessionid";
68      private static final String PROPERTY_MESSAGE_ERROR_UPLOAD = "";
69  
70      /**
71       * Invoked if <code>request.getParameter( {@link #PARAMETER_PAGE} ).equals {@link SharedConstants#PLUGIN_NAME}</code>
72       */
73      public boolean isInvoked( HttpServletRequest request )
74      {
75          return SharedConstants.PLUGIN_NAME.equals( request.getParameter( PARAMETER_PAGE ) );
76      }
77  
78      /**
79       * Gets the file(s) and associates them to the user "fake" session.
80       * Checks if form/subform exists.
81       * Adds the uploaded file list to the main object.
82       */
83      public void process( HttpServletRequest request, HttpServletResponse response, JSONObject mainObject,
84          List<FileItem> listFileItems )
85      {
86          String strFormName = request.getParameter( PARAMETER_FORM );
87          String strSubformName = request.getParameter( PARAMETER_SUBFORM );
88          String strFieldName = request.getParameter( PARAMETER_FIELD_NAME );
89          Form form = FormsRegistrationService.getForm( strFormName );
90          String strSessionId = request.getParameter( PARAMETER_JSESSION_ID );
91  
92          // check session, form, subform
93          if ( strSessionId == null )
94          {
95              AppLogService.error( "Session does not exists" );
96              mainObject.accumulate( JSONUtils.JSON_KEY_ERROR,
97                  I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOAD, request.getLocale(  ) ) );
98          }
99          else if ( form == null )
100         {
101             mainObject.accumulate( JSONUtils.JSON_KEY_ERROR,
102                 I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_UPLOAD, request.getLocale(  ) ) );
103         }
104         else
105         {
106             SubForm subForm = null;
107 
108             if ( StringUtils.isNotBlank( strSubformName ) )
109             {
110                 subForm = form.getSubForm( strSubformName );
111             }
112 
113             if ( subForm == null )
114             {
115                 // if upload is in first subform, no subform parameter available
116                 subForm = form.getFirstSubForm(  );
117             }
118 
119             // field name check cannot be performed
120             for ( FileItem fileItem : listFileItems )
121             {
122                 // the uploaded file cannot be bound to the actual session
123                 // due to flash player restrictions
124                 subForm.addFileItem( strSessionId, strFieldName, fileItem );
125             }
126 
127             // get the list of file in session for the given id to display the list
128             List<FileItem> listSessionFileItems = subForm.getFileItems( strSessionId, strFieldName );
129             JSONObject jsonListFileItems = JSONUtils.getUploadedFileJSON( listSessionFileItems );
130             mainObject.accumulateAll( jsonListFileItems );
131 
132             mainObject.element( JSONUtils.JSON_KEY_FIELD, strFieldName );
133         }
134     }
135 }