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.rss.web;
35  
36  import fr.paris.lutece.plugins.rss.business.RssFeed;
37  import fr.paris.lutece.plugins.rss.business.RssFeedHome;
38  import fr.paris.lutece.plugins.rss.service.RssContent;
39  import fr.paris.lutece.plugins.rss.service.RssContentService;
40  import fr.paris.lutece.plugins.rss.service.RssPlugin;
41  import fr.paris.lutece.portal.business.style.ModeHome;
42  import fr.paris.lutece.portal.business.style.Style;
43  import fr.paris.lutece.portal.business.style.StyleHome;
44  import fr.paris.lutece.portal.business.stylesheet.StyleSheet;
45  import fr.paris.lutece.portal.service.content.PageData;
46  import fr.paris.lutece.portal.service.html.XmlTransformerService;
47  import fr.paris.lutece.portal.service.i18n.I18nService;
48  import fr.paris.lutece.portal.service.includes.PageInclude;
49  import fr.paris.lutece.portal.service.plugin.Plugin;
50  import fr.paris.lutece.portal.service.plugin.PluginService;
51  import fr.paris.lutece.portal.service.template.AppTemplateService;
52  import fr.paris.lutece.portal.service.util.AppLogService;
53  import fr.paris.lutece.portal.service.util.AppPathService;
54  import fr.paris.lutece.portal.service.util.AppPropertiesService;
55  import fr.paris.lutece.util.html.HtmlTemplate;
56  
57  import java.io.File;
58  import java.io.StringReader;
59  
60  import java.util.Collection;
61  import java.util.HashMap;
62  import java.util.List;
63  import java.util.Locale;
64  import java.util.Map;
65  
66  import javax.servlet.http.HttpServletRequest;
67  
68  import javax.xml.transform.stream.StreamSource;
69  
70  
71  /**
72   * This class provides the user interface to manage rss features
73   */
74  public class RssFeedInclude implements PageInclude
75  {
76      /**
77       * The bookmark in the page frameset
78       */
79      private static final String MARK_RSS = "rss";
80      private static final String MARK_PLUGIN = "plugin";
81      private static final String MARK_LOCALE = "locale";
82  
83      /**
84       * The list of the rss files
85       */
86      private static final String MARK_RSS_FILES_LIST = "rss_list";
87  
88      /**
89       * The template used to generate the rss links in the html code
90       */
91      private static final String TEMPLATE_RSS_INCLUDE = "skin/plugins/rss/rss_feed_include.html";
92  
93      // Properties
94      private static final String PROPERTY_RSS_MARKER_PREFIX = "rss.include.marker.prefix";
95      private static final String PROPERTY_PATH_XSL = "path.stylesheet";
96  
97      // Default Marker prefix if nothing defined in properties file
98      private static final String MARK_DEFAULT_RSS_MARKER_PREFIX = "rss_";
99  
100     //Constantes
101     private static final int CONSTANTE_RSS_STATUT_OK = 0;
102 
103     /**
104      * Substitue specific Freemarker markers in the page template.
105      * @param rootModel the HashMap containing markers to substitute
106      * @param data A PageData object containing applications data
107      * @param nMode The current mode
108      * @param request The HTTP request
109      */
110     public void fillTemplate( Map<String, Object> rootModel, PageData data, int nMode, HttpServletRequest request )
111     {
112         Plugin plugin = PluginService.getPlugin( RssPlugin.PLUGIN_NAME );
113         Locale locale = getLocale( request );
114 
115         HashMap<String, Collection<RssFeed>> model = new HashMap<String, Collection<RssFeed>>(  );
116         model.put( MARK_RSS_FILES_LIST, RssFeedHome.getRssFeeds(  ) );
117 
118         List<RssFeed> listRss = RssFeedHome.getRssFeeds(  );
119 
120         for ( RssFeed rssFeed : listRss )
121         {
122             if ( rssFeed.getLastFetchStatus(  ) == CONSTANTE_RSS_STATUT_OK )
123             {
124                 rootModel.put( getRssMarkerPrefix(  ) + String.valueOf( rssFeed.getId(  ) ),
125                     getTemplateHtmlForRss( rssFeed, plugin, locale ) );
126             }
127         }
128     }
129 
130     public static String getRssMarkerPrefix(  )
131     {
132         return AppPropertiesService.getProperty( PROPERTY_RSS_MARKER_PREFIX, MARK_DEFAULT_RSS_MARKER_PREFIX );
133     }
134 
135     /**
136      * Generate the HTML code for the specified {@link RssFeed}
137      * @param quicklinks The {@link RssFeed}
138      * @param plugin The {@link Plugin}
139      * @param locale The {@link Locale}
140      * @return The HTML code
141      */
142     private String getTemplateHtmlForRss( RssFeed rss, Plugin plugin, Locale locale )
143     {
144         HashMap<String, Object> model = new HashMap<String, Object>(  );
145 
146         RssContent rssContent = RssContentService.getInstance(  ).getRssContent( String.valueOf( rss.getId(  ) ) );
147 
148         String strHtml = "";
149 
150         if ( rss.getIdIncludeStyle(  ) != -1 )
151         {
152             Style rssStyle = StyleHome.findByPrimaryKey( rss.getIdIncludeStyle(  ) );
153             Collection<StyleSheet> rssStyleSheetList = StyleHome.getStyleSheetList( rssStyle.getId(  ) );
154             String strXslDirectory = null;
155             String strXslFileName = null;
156 
157             for ( StyleSheet rssStyleSheet : rssStyleSheetList )
158             {
159                 strXslDirectory = AppPathService.getPath( PROPERTY_PATH_XSL ) +
160                     ModeHome.findByPrimaryKey( rssStyleSheet.getModeId(  ) ).getPath(  );
161                 strXslFileName = rssStyleSheet.getFile(  );
162             }
163 
164             String strXslPath = strXslDirectory + strXslFileName;
165 
166             File fileXsl = new File( strXslPath );
167             StreamSource sourceStyleSheet = new StreamSource( fileXsl );
168             StringReader srInput = new StringReader( rssContent.getContent(  ) );
169             StreamSource sourceDocument = new StreamSource( srInput );
170 
171             try
172             {
173                 XmlTransformerService xmlTransformerService = new XmlTransformerService(  );
174                 strHtml = xmlTransformerService.transformBySourceWithXslCache( sourceDocument, sourceStyleSheet,
175                         strXslPath, null, null );
176             }
177             catch ( Exception e )
178             {
179                 AppLogService.error( e );
180             }
181         }
182         else
183         {
184             strHtml = rssContent.getContent(  );
185         }
186 
187         model.put( MARK_RSS, strHtml );
188 
189         model.put( MARK_PLUGIN, plugin );
190         model.put( MARK_LOCALE, locale );
191 
192         HtmlTemplate templateList = AppTemplateService.getTemplate( TEMPLATE_RSS_INCLUDE, Locale.getDefault(  ), model );
193 
194         return templateList.getHtml(  );
195     }
196 
197     /**
198      * Get the locale from request of the default locale if request is null
199      * @param request The {@link HttpServletRequest}
200      * @return The locale
201      */
202     private Locale getLocale( HttpServletRequest request )
203     {
204         Locale locale = null;
205 
206         if ( request != null )
207         {
208             locale = request.getLocale(  );
209         }
210         else
211         {
212             locale = I18nService.getDefaultLocale(  );
213         }
214 
215         return locale;
216     }
217 }