View Javadoc
1   /*
2    * Copyright (c) 2002-2016, 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.easyrulesbot.service.response.filters;
35  
36  import fr.paris.lutece.plugins.easyrulesbot.service.response.exceptions.ResponseProcessingException;
37  import fr.paris.lutece.plugins.easyrulesbot.util.FileUtils;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.portal.web.l10n.LocaleService;
40  import java.util.ArrayList;
41  
42  import java.util.HashMap;
43  import java.util.List;
44  import java.util.Locale;
45  import java.util.Map;
46  import java.util.Random;
47  
48  /**
49   * Stop On Word Processor
50   */
51  public class StopOnWordFilter extends AbstractFilter implements ResponseFilter
52  {
53  
54      private List<String> _listStopWords;
55      private Map<String, String> _mapLocaleStopWordsFile;
56      private Map<String, List<String>> _mapLocaleStopWordsList = new HashMap<>( );
57      private Map<String, String> _mapLocaleResponseMessageFile;
58      private Map<String, List<String>> _mapLocaleResponseMessageList = new HashMap<>( );
59  
60      /**
61       * Get the list of stop words
62       *
63       * @return The list
64       */
65      public List<String> getListStopWords( )
66      {
67          return _listStopWords;
68      }
69  
70      /**
71       * Set Map Locale / Stop Words File
72       *
73       * @param map
74       *            The map
75       */
76      public void setMapLocaleStopWordsFile( Map<String, String> map )
77      {
78          _mapLocaleStopWordsFile = map;
79      }
80  
81      /**
82       * Set the list of stop words
83       *
84       * @param list
85       *            The list
86       */
87      public void setListStopWords( List<String> list )
88      {
89          _listStopWords = list;
90      }
91  
92      /**
93       * Set Map Locale / Messages File
94       *
95       * @param map
96       *            The map
97       */
98      public void setMapLocaleResponseMessageFile( Map<String, String> map )
99      {
100         _mapLocaleResponseMessageFile = map;
101     }
102 
103     /**
104      * {@inheritDoc }
105      */
106     @Override
107     public String filterResponse( String strResponse, Locale locale, Map mapData ) throws ResponseProcessingException
108     {
109         String strCheck = strResponse.toLowerCase( );
110 
111         for ( String strWord : getWords( locale ) )
112         {
113             if ( strCheck.contains( strWord ) )
114             {
115                 throw new ResponseProcessingException( getStopResponseMessage( locale ) );
116             }
117         }
118 
119         return strResponse;
120     }
121 
122     /**
123      * Gets the stop message list
124      *
125      * @param locale
126      *            The locale
127      * @return A random message on the list
128      */
129     private String getStopResponseMessage( Locale locale )
130     {
131         String strMessage;
132 
133         if ( locale == null )
134         {
135             AppLogService.error( "Locale is NULL" );
136             locale = LocaleService.getDefault( ); // FIXME
137         }
138 
139         String strLanguage = locale.getLanguage( );
140         List<String> listMessages = _mapLocaleResponseMessageList.get( strLanguage );
141         if ( listMessages == null )
142         {
143             String strMessageFile = _mapLocaleResponseMessageFile.get( strLanguage );
144             if ( strMessageFile != null )
145             {
146                 listMessages = FileUtils.loadTermsFromFile( strMessageFile );
147 
148                 _mapLocaleResponseMessageList.put( strLanguage, listMessages );
149             }
150             else
151             {
152                 AppLogService.error( "EasyRuleBot : StopOnWordFilter : No Response Message file available for the language : " + strLanguage );
153                 return "";
154             }
155         }
156         Random randomizer = new Random( );
157         strMessage = listMessages.get( randomizer.nextInt( listMessages.size( ) ) );
158 
159         return strMessage;
160     }
161 
162     /**
163      * Gets the list of words
164      *
165      * @return The list
166      */
167     private List<String> getWords( Locale locale )
168     {
169         String strLanguage = locale.getLanguage( );
170         List<String> listStopWords = _mapLocaleStopWordsList.get( strLanguage );
171         if ( listStopWords == null )
172         {
173             listStopWords = new ArrayList<String>( );
174             if ( _mapLocaleStopWordsFile != null )
175             {
176                 String strMessageFile = _mapLocaleStopWordsFile.get( strLanguage );
177                 if ( strMessageFile != null )
178                 {
179                     listStopWords.addAll( FileUtils.loadTermsFromFile( strMessageFile ) );
180                 }
181             }
182             if ( _listStopWords != null )
183             {
184                 listStopWords.addAll( _listStopWords );
185             }
186             _mapLocaleStopWordsList.put( strLanguage, listStopWords );
187         }
188 
189         return listStopWords;
190     }
191 }