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.seo.service;
35  
36  import fr.paris.lutece.plugins.seo.business.FriendlyUrl;
37  import fr.paris.lutece.plugins.seo.business.FriendlyUrlHome;
38  import fr.paris.lutece.plugins.seo.business.UrlRewriterRule;
39  import fr.paris.lutece.plugins.seo.business.UrlRewriterRuleHome;
40  import fr.paris.lutece.portal.service.datastore.DatastoreService;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  
47  import org.apache.commons.io.FileUtils;
48  
49  import java.io.File;
50  import java.io.IOException;
51  
52  import java.text.DateFormat;
53  import java.text.MessageFormat;
54  
55  import java.util.Collection;
56  import java.util.Date;
57  import java.util.HashMap;
58  import java.util.List;
59  import java.util.Locale;
60  
61  /**
62   * RuleFileService
63   */
64  public final class RuleFileService
65  {
66      private static final String TEMPLATE_FILE = "/admin/plugins/seo/urlrewrite.xml";
67      private static final String MARK_RULES_LIST = "rules_list";
68      private static final String MARK_URL_LIST = "url_list";
69      private static final String PROPERTY_FILE = "seo.configFilePath";
70      private static final String PROPERTY_REWRITE_CONFIG_LOG = "seo.config.log";
71  
72      /**
73       * Private constructor
74       */
75      private RuleFileService( )
76      {
77      }
78  
79      /**
80       * Generate the rule file
81       * 
82       * @throws java.io.IOException
83       *             If an error occurs
84       */
85      public static void generateFile( ) throws IOException
86      {
87          String strFilePath = AppPathService.getWebAppPath( ) + AppPropertiesService.getProperty( PROPERTY_FILE );
88          File file = new File( strFilePath );
89          FileUtils.writeStringToFile( file, generateFileContent( ) );
90      }
91  
92      /**
93       * Generate the rule file content
94       * 
95       * @return The file content
96       */
97      private static String generateFileContent( )
98      {
99          HashMap model = new HashMap( );
100         Collection<UrlRewriterRule> listRules = UrlRewriterRuleHome.findAll( );
101         List<FriendlyUrl> listUrl = FriendlyUrlHome.findAll( );
102 
103         model.put( MARK_RULES_LIST, listRules );
104         model.put( MARK_URL_LIST, listUrl );
105 
106         HtmlTemplate t = AppTemplateService.getTemplate( TEMPLATE_FILE, Locale.getDefault( ), model );
107 
108         String strResult = "OK";
109         String strDate = DateFormat.getDateTimeInstance( ).format( new Date( ) );
110         Object [ ] args = {
111                 strDate, listRules.size( ) + listUrl.size( ), strResult
112         };
113         String strLogFormat = I18nService.getLocalizedString( PROPERTY_REWRITE_CONFIG_LOG, Locale.getDefault( ) );
114         String strLog = MessageFormat.format( strLogFormat, args );
115         DatastoreService.setDataValue( SEODataKeys.KEY_REWRITE_CONFIG_UPDATE, strLog );
116         DatastoreService.setDataValue( SEODataKeys.KEY_CONFIG_UPTODATE, DatastoreService.VALUE_TRUE );
117 
118         String strFileContent = t.getHtml( );
119 
120         int nStartXml = strFileContent.indexOf( "<?xml" );
121         if ( nStartXml != 0 )
122         {
123             strFileContent = strFileContent.substring( nStartXml );
124         }
125 
126         return strFileContent;
127     }
128 }