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.calendar.web;
35  
36  import fr.paris.lutece.plugins.calendar.business.CalendarFilter;
37  import fr.paris.lutece.plugins.calendar.business.stylesheet.CalendarStyleSheetHome;
38  import fr.paris.lutece.plugins.calendar.service.XMLUtils;
39  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
40  import fr.paris.lutece.portal.business.user.AdminUser;
41  import fr.paris.lutece.portal.service.admin.AdminUserService;
42  import fr.paris.lutece.portal.service.i18n.I18nService;
43  import fr.paris.lutece.portal.service.message.SiteMessage;
44  import fr.paris.lutece.portal.service.message.SiteMessageException;
45  import fr.paris.lutece.portal.service.message.SiteMessageService;
46  import fr.paris.lutece.portal.service.plugin.Plugin;
47  import fr.paris.lutece.portal.service.plugin.PluginService;
48  import fr.paris.lutece.portal.service.util.AppException;
49  import fr.paris.lutece.portal.service.util.AppPropertiesService;
50  import fr.paris.lutece.portal.web.constants.Messages;
51  import fr.paris.lutece.portal.web.constants.Parameters;
52  import fr.paris.lutece.portal.web.stylesheet.StyleSheetJspBean;
53  
54  import java.io.ByteArrayInputStream;
55  import java.io.IOException;
56  import java.io.OutputStream;
57  
58  import javax.servlet.ServletContext;
59  import javax.servlet.http.HttpServletRequest;
60  import javax.servlet.http.HttpServletResponse;
61  import javax.servlet.http.HttpSession;
62  
63  
64  /**
65   * This class provides the user interface to manage calendars from the dataBase
66   * features ( manage, create, modify, remove)
67   */
68  public class CalendarDownloadFile
69  {
70      //Properties
71      private static final String PROPERTY_FILE_EXPORT_NAME = "agenda";
72      private static final String PROPERTY_RSS_FILE_NAME = "rss";
73      private static final String PROPERTY_CALENDAR_RSS_FILE_ID = "calendar.rss.xls.id";
74  
75      //Messages
76      private static final String PROPERTY_ERROR_MESSAGE = "calendar.message.errorDownloadFile";
77  
78      /**
79       * Download a calendar into a file
80       * @param request The request
81       * @param response The response
82       * @throws SiteMessageException If a message needs to be displayed to the
83       *             user
84       */
85      public void downloadCalendarToFile( HttpServletRequest request, HttpServletResponse response )
86              throws SiteMessageException
87      {
88          byte[] result = null;
89          String strAgenda = request.getParameter( Constants.PARAM_AGENDA );
90          String strStyleSheetId = request.getParameter( Constants.PARAMETER_STYLESHEET_ID );
91          String strRss = request.getParameter( Constants.PARAMETER_ACTION );
92          String strXml = Constants.EMPTY_STRING;
93          String strEventSort = request.getParameter( Constants.PARAMETER_SORT_EVENTS );
94          String[] arrayCategory = request.getParameterValues( Constants.PARAMETER_CATEGORY );
95          String[] arrayCalendar = request.getParameterValues( Constants.PARAMETER_CALENDAR_ID );
96          int[] arrayCategoryIds = null;
97          int[] arrayCalendarIds = null;
98  
99          // Mandatory field
100         if ( ( strAgenda != null ) && strAgenda.equals( "none" ) )
101         {
102             SiteMessageService.setMessage( request, Messages.MANDATORY_FIELDS, SiteMessage.TYPE_STOP );
103         }
104 
105         //Category ids
106         if ( arrayCategory != null )
107         {
108             if ( arrayCategory.length != 0 )
109             {
110                 arrayCategoryIds = new int[arrayCategory.length];
111                 int i = 0;
112 
113                 for ( String strIdCategory : arrayCategory )
114                 {
115                     if ( strIdCategory != null )
116                     {
117                         arrayCategoryIds[i++] = Integer.parseInt( strIdCategory );
118                     }
119                 }
120             }
121         }
122 
123         //Calendar ids
124         if ( arrayCalendar != null )
125         {
126             if ( !arrayCalendar[0].equals( Constants.PROPERTY_CALENDAR_NONE ) ) //FIXME
127             {
128                 arrayCalendarIds = new int[arrayCalendar.length];
129 
130                 int i = 0;
131 
132                 for ( String strId : arrayCalendar )
133                 {
134                     if ( strId != null )
135                     {
136                         arrayCalendarIds[i++] = Integer.parseInt( strId );
137                     }
138                 }
139             }
140         }
141 
142         CalendarFilter filter = new CalendarFilter( );
143 
144         if ( strRss != null )
145         {
146             filter.setCategoriesId( arrayCategoryIds );
147             filter.setCalendarIds( arrayCalendarIds );
148             filter.setSortEvents( ( strEventSort != null ) ? Integer.parseInt( strEventSort ) : ( -1 ) );
149             strXml = XMLUtils.getRssXml( filter );
150         }
151         else
152         {
153             strXml = XMLUtils.getAgendaXml( strAgenda, request );
154         }
155 
156         try
157         {
158             String strFileName = Constants.EMPTY_STRING;
159             ByteArrayInputStream baSource = null;
160 
161             Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
162 
163             if ( strStyleSheetId != null )
164             {
165                 int nStyleSheetId = Integer.parseInt( strStyleSheetId );
166                 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nStyleSheetId, plugin );
167                 String strAgendaExtension = CalendarStyleSheetHome.getExtension( nStyleSheetId, plugin );
168                 baSource = new ByteArrayInputStream( stylesheet.getSource( ) );
169                 strFileName = PROPERTY_FILE_EXPORT_NAME + "." + strAgendaExtension;
170                 response.setHeader( "Content-disposition", "attachement; filename=\"" + strFileName + "\"" );
171             }
172             else if ( strRss != null )
173             {
174                 int nStyleSheetId = AppPropertiesService.getPropertyInt( PROPERTY_CALENDAR_RSS_FILE_ID, 1 );
175                 StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nStyleSheetId, plugin );
176                 String strAgendaExtension = CalendarStyleSheetHome.getExtension( nStyleSheetId, plugin );
177                 baSource = new ByteArrayInputStream( stylesheet.getSource( ) );
178                 strFileName = PROPERTY_RSS_FILE_NAME + "." + strAgendaExtension;
179                 response.setHeader( "Content-disposition", "inline; filename=\"" + strFileName + "\"" );
180             }
181 
182             if ( baSource != null )
183             {
184                 HttpSession session = request.getSession( );
185                 ServletContext context = session.getServletContext( );
186                 String strMimetype = context.getMimeType( strFileName );
187                 response.setContentType( ( strMimetype != null ) ? strMimetype : "application/octet-stream" );
188                 result = XMLUtils.transformXMLToXSL( strXml, baSource, null, null );
189             }
190 
191             OutputStream os = response.getOutputStream( );
192             os.write( result );
193             os.flush( );
194             os.close( );
195         }
196         catch ( IOException e )
197         {
198             throw new AppException( I18nService.getLocalizedString( PROPERTY_ERROR_MESSAGE, request.getLocale( ) ) );
199         }
200         catch ( Exception e )
201         {
202             throw new AppException( I18nService.getLocalizedString( PROPERTY_ERROR_MESSAGE, request.getLocale( ) ) );
203         }
204     }
205 
206     /**
207      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
208      * methods.
209      * @param request servlet request
210      * @param response servlet response
211      */
212     public void downloadXSLFileFromDatabase( HttpServletRequest request, HttpServletResponse response )
213     {
214         AdminUser user = AdminUserService.getAdminUser( request );
215 
216         if ( ( user != null ) && ( user.checkRight( StyleSheetJspBean.RIGHT_MANAGE_STYLESHEET ) ) )
217         {
218             String strStyleSheetId = request.getParameter( Parameters.STYLESHEET_ID );
219 
220             try
221             {
222                 if ( strStyleSheetId != null )
223                 {
224                     int nStyleSheetId = Integer.parseInt( strStyleSheetId );
225 
226                     Plugin plugin = PluginService.getPlugin( Constants.PLUGIN_NAME );
227 
228                     StyleSheet stylesheet = CalendarStyleSheetHome.findByPrimaryKey( nStyleSheetId, plugin );
229 
230                     HttpSession session = request.getSession( );
231                     ServletContext context = session.getServletContext( );
232                     String strMimetype = context.getMimeType( stylesheet.getFile( ) );
233                     response.setContentType( ( strMimetype != null ) ? strMimetype : "application/octet-stream" );
234                     response.setHeader( "Content-Disposition", "attachement; filename=\"" + stylesheet.getFile( )
235                             + "\"" );
236 
237                     OutputStream out = response.getOutputStream( );
238                     out.write( stylesheet.getSource( ) );
239                     out.flush( );
240                     out.close( );
241                 }
242             }
243             catch ( IOException e )
244             {
245                 throw new AppException( I18nService.getLocalizedString( PROPERTY_ERROR_MESSAGE, request.getLocale( ) ) );
246             }
247         }
248     }
249 }