View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.jasper.service;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  
42  import javax.servlet.http.HttpServletRequest;
43  
44  import org.apache.commons.lang.StringUtils;
45  
46  import fr.paris.lutece.plugins.jasper.business.JasperReport;
47  import fr.paris.lutece.plugins.jasper.service.export.HtmlJasperRender;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import net.sf.jasperreports.engine.JRDataSource;
50  import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
51  
52  public enum JasperFileLinkService
53  {
54      /**
55       * Implementation of a singleton instance
56       */
57      INSTANCE;
58      /**
59       * Initialize the JasperFile link service
60       *
61       */
62  
63      private static final String PARAMETER_REPORT_ID = "report_id";
64      private static final String PARAMETER_REPORT_TYPE = "report_type";
65      private static final String PARAMETER_DBPAGE = "dbpage";
66      private static final String PARAMETER_JASPER_VALUE = "value";
67      private static final String PARAMETER_JASPER_LIST = "list";
68      HashMap<String, ILinkJasperReport> mapStrategies;
69  
70      public static String getLink( String strReportId, String strType )
71      {
72          FileTypeContext context = null;
73          String fileLink = StringUtils.EMPTY;
74          
75          context = getFileTypeContext( strType );
76  		fileLink = context.getFileLink( strReportId );
77          
78          return fileLink;
79      }
80  
81      static FileTypeContext getFileTypeContext( String strType )
82      {
83          Collection<ILinkJasperReport> col = ExportFormatService.INSTANCE.getExportTypes( );
84  
85          for ( ILinkJasperReport renderFormat : col )
86          {
87              if ( renderFormat.getFileType( ).equals( strType ) )
88              {
89                  return new FileTypeContext( renderFormat );
90              }
91          }
92  
93          return new FileTypeContext( new HtmlJasperRender( ) );
94      }
95  
96      /**
97       * Exports the files in a byte array
98       * 
99       * @param request
100      *            The Http request
101      * @return An array of byte which is the content of the archive
102      */
103     public static byte [ ] exportFile( HttpServletRequest request )
104     {
105         String strReportCode = request.getParameter( PARAMETER_REPORT_ID );
106         String strType = request.getParameter( PARAMETER_REPORT_TYPE );        
107 		return exportFile( strReportCode, strType, request );
108     }
109 
110     public static byte [ ] exportFile( HttpServletRequest request, String strReportCode )
111     {
112         String strType = request.getParameter( PARAMETER_REPORT_TYPE );
113         return exportFile( strReportCode, strType, request );
114     }
115 
116     public static byte [ ] exportFile( String strReportCode, String strJasperType, HttpServletRequest request )
117     {
118 
119         byte [ ] buffer = new byte [ 1024];
120 
121 		FileTypeContext context = getFileTypeContext( strJasperType );
122 		buffer = context.getBuffer( strReportCode, request );
123 
124         return buffer;
125     }
126 
127     public static byte [ ] exportFile( String strReportCode, String strJasperType, JRBeanCollectionDataSource dataSource, HttpServletRequest request )
128     {
129         // We override the methods instead of replacing them to ensure binary compatibility
130         return exportFile( strReportCode, strJasperType, (JRDataSource) dataSource, request );
131     }
132 
133     public static byte [ ] exportFile( String strReportCode, String strJasperType, JRDataSource dataSource, HttpServletRequest request )
134     {
135 
136         byte [ ] buffer = new byte [ 1024];
137 
138 		FileTypeContext context = getFileTypeContext( strJasperType );
139 		buffer = context.getBuffer( strReportCode, request );
140 
141         return buffer;
142     }
143 
144     public static byte [ ] exportFile( JasperReport report, String strJasperType, JRBeanCollectionDataSource dataSource, Map<String, Object> parameters,
145             HttpServletRequest request )
146     {
147         // We override the methods instead of replacing them to ensure binary compatibility
148         return exportFile( report, strJasperType, (JRDataSource) dataSource, parameters, request );
149     }
150 
151     public static byte [ ] exportFile( JasperReport report, String strJasperType, JRDataSource dataSource, Map<String, Object> parameters,
152             HttpServletRequest request )
153     {
154 
155         byte [ ] buffer = new byte [ 1024];
156 
157 		FileTypeContext context = getFileTypeContext( strJasperType );
158 		buffer = context.getBuffer( report, dataSource, parameters, request );
159         
160         return buffer;
161     }
162 
163     /**
164      *
165      * @param request
166      * @return
167      */
168     public static String getFileName( HttpServletRequest request )
169     {
170 
171         FileTypeContext context = null;
172         String fileName = StringUtils.EMPTY;;
173 
174 		String strReportCode = request.getParameter( PARAMETER_REPORT_ID );
175 		String strType = request.getParameter( PARAMETER_REPORT_TYPE );			
176 		
177 		context = getFileTypeContext( strType );
178 		fileName = context.getFileName( strReportCode );
179 
180         return fileName;
181     }
182 
183     /**
184      * Extracts values from the Http request
185      * 
186      * @param request
187      *            The Http request
188      * @return A list of values
189      */
190     public List<String> getValues( HttpServletRequest request )
191     {
192         List<String> list = new ArrayList<String>( );
193         int i = 0;
194         String strValue = request.getParameter( PARAMETER_JASPER_VALUE + ( i + 1 ) );
195 
196         while ( strValue != null )
197         {
198             list.add( i, strValue );
199             i++;
200             strValue = request.getParameter( PARAMETER_JASPER_VALUE + ( i + 1 ) );
201         }
202 
203         // Collections.sort( list, String.CASE_INSENSITIVE_ORDER );
204         return list;
205     }
206 
207     public List<String> getValuesElm( HttpServletRequest request )
208     {
209         List<String> list = new ArrayList<String>( );
210         int i = 0;
211         String strValue = request.getParameter( PARAMETER_JASPER_LIST + ( i + 1 ) );
212 
213         while ( strValue != null )
214         {
215             list.add( i, strValue );
216             i++;
217             strValue = request.getParameter( PARAMETER_JASPER_LIST + ( i + 1 ) );
218         }
219 
220         // Collections.sort( list, String.CASE_INSENSITIVE_ORDER );
221         return list;
222     }
223 
224     /**
225      * Gets the cache key
226      * 
227      * @param nMode
228      *            The mode
229      * @param request
230      *            The HTTP request
231      * @return The key
232      */
233     public String getKey( HttpServletRequest request )
234     {
235         String strReportId = request.getParameter( PARAMETER_REPORT_ID );
236         strReportId = ( strReportId == null ) ? "" : strReportId;
237 
238         String strDBPageId = request.getParameter( PARAMETER_DBPAGE ); // Must be externalised
239         strDBPageId = ( strDBPageId == null ) ? "" : strDBPageId;
240 
241         String strParameters = "";
242         List<String> listValues = JasperFileLinkService.INSTANCE.getValues( request );
243 
244         for ( int i = 0; i < listValues.size( ); i++ )
245         {
246             strParameters += ( PARAMETER_JASPER_VALUE + ( i + 1 ) + "_" + listValues.get( i ) );
247         }
248 
249         return strReportId + strDBPageId + "_" + strParameters;
250     }
251 }