View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.asynchronousupload.util;
35  
36  import java.util.List;
37  
38  import javax.servlet.http.HttpServletRequest;
39  import javax.xml.bind.DatatypeConverter;
40  
41  import org.apache.commons.fileupload.FileItem;
42  import org.apache.commons.lang3.StringUtils;
43  
44  import fr.paris.lutece.portal.service.i18n.I18nService;
45  import fr.paris.lutece.util.file.FileUtil;
46  import net.sf.json.JSONObject;
47  
48  /**
49   * Provides json utility methods for forms
50   *
51   */
52  public final class JSONUtils
53  {
54      /**
55       * JSON key for field name
56       */
57      public static final String JSON_KEY_FIELD_NAME = "field_name";
58  
59      /**
60       * JSON key to describe a success
61       */
62      public static final String JSON_KEY_SUCCESS = "success";
63      private static final String JSON_KEY_FILE_NAME = "name";
64      private static final String JSON_KEY_FILE_SIZE = "size";
65      private static final String JSON_KEY_FILE_PREVIEW = "preview";
66      private static final String JSON_KEY_FORM_ERROR = "form_error";
67      private static final String JSON_KEY_UPLOADED_FILES = "files";
68      private static final String JSON_KEY_FILE_COUNT = "fileCount";
69  
70      // PROPERTIES
71      private static final String PROPERTY_MESSAGE_ERROR_REMOVING_FILE = "form.message.error.removingFile";
72  
73      /**
74       * Empty constructor
75       */
76      private JSONUtils( )
77      {
78          // nothing
79      }
80  
81      /**
82       * Builds a json object for the file item list. Key is {@link #JSON_KEY_UPLOADED_FILES}, value is the array of uploaded file.
83       * 
84       * @param listFileItem
85       *            the fileItem list
86       * @return the json
87       */
88      public static JSONObject getUploadedFileJSON( List<FileItem> listFileItem )
89      {
90          JSONObject json = new JSONObject( );
91  
92          if ( listFileItem != null )
93          {
94              for ( FileItem fileItem : listFileItem )
95              {
96                  JSONObject jsonObject = new JSONObject( );
97                  jsonObject.element( JSON_KEY_FILE_NAME, fileItem.getName( ) );
98                  jsonObject.element( JSON_KEY_FILE_PREVIEW, getPreviewImage( fileItem ) );
99                  jsonObject.element( JSON_KEY_FILE_SIZE, fileItem.getSize( ) );
100                 json.accumulate( JSON_KEY_UPLOADED_FILES, jsonObject );
101             }
102 
103             json.element( JSON_KEY_FILE_COUNT, listFileItem.size( ) );
104         }
105         else
106         {
107             // no file
108             json.element( JSON_KEY_FILE_COUNT, 0 );
109         }
110 
111         return json;
112     }
113 
114     /**
115      * Builds a json object with the error message.
116      * 
117      * @param request
118      *            the request
119      * @return the json object.
120      */
121     public static JSONObject buildJsonErrorRemovingFile( HttpServletRequest request )
122     {
123         JSONObject json = new JSONObject( );
124 
125         json.element( JSON_KEY_FORM_ERROR, I18nService.getLocalizedString( PROPERTY_MESSAGE_ERROR_REMOVING_FILE, request.getLocale( ) ) );
126 
127         return json;
128     }
129 
130     /**
131      * Builds a json object with the error message.
132      * 
133      * @param json
134      *            the JSON
135      * @param strMessage
136      *            the error message
137      */
138     public static void buildJsonError( JSONObject json, String strMessage )
139     {
140         if ( json != null )
141         {
142             json.accumulate( JSON_KEY_FORM_ERROR, strMessage );
143         }
144     }
145 
146     private static String getPreviewImage( FileItem fileItem )
147     {
148 
149         if ( FileUtil.hasImageExtension( fileItem.getName( ) ) )
150         {
151 
152             return "data:image/png;base64," + DatatypeConverter.printBase64Binary( fileItem.get( ) );
153         }
154 
155         return StringUtils.EMPTY;
156     }
157 }