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.processors;
35  
36  import fr.paris.lutece.plugins.easyrulesbot.service.response.exceptions.InvalidResponseException;
37  import fr.paris.lutece.portal.service.i18n.I18nService;
38  
39  import java.util.Locale;
40  import java.util.Map;
41  import java.util.regex.Matcher;
42  import java.util.regex.Pattern;
43  
44  /**
45   * InputStringResponseProcessor
46   */
47  public class InputStringResponseProcessor extends AbstractProcessor implements ResponseProcessor
48  {
49      private static final String PATTERN_EMAIL_NAME = "email";
50      private static final Pattern PATTERN_EMAIL_VALUE = Pattern.compile( "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE );
51      private String _strStandardPattern;
52      private String _strCustomPattern;
53      private String _strPatternErrorMessage;
54      private String _strPatternErrorMessageI18nKey;
55  
56      /**
57       * Returns the StandardPattern
58       * 
59       * @return The StandardPattern
60       */
61      public String getStandardPattern( )
62      {
63          return _strStandardPattern;
64      }
65  
66      /**
67       * Sets the StandardPattern
68       * 
69       * @param strStandardPattern
70       *            The StandardPattern
71       */
72      public void setStandardPattern( String strStandardPattern )
73      {
74          _strStandardPattern = strStandardPattern;
75      }
76  
77      /**
78       * Returns the CustomPattern
79       * 
80       * @return The CustomPattern
81       */
82      public String getCustomPattern( )
83      {
84          return _strCustomPattern;
85      }
86  
87      /**
88       * Sets the CustomPattern
89       * 
90       * @param strCustomPattern
91       *            The CustomPattern
92       */
93      public void setCustomPattern( String strCustomPattern )
94      {
95          _strCustomPattern = strCustomPattern;
96      }
97  
98      /**
99       * Returns the PatternErrorMessage
100      * 
101      * @return The PatternErrorMessage
102      */
103     public String getPatternErrorMessage( )
104     {
105         return _strPatternErrorMessage;
106     }
107 
108     /**
109      * Sets the PatternErrorMessage
110      * 
111      * @param strPatternErrorMessage
112      *            The PatternErrorMessage
113      */
114     public void setPatternErrorMessage( String strPatternErrorMessage )
115     {
116         _strPatternErrorMessage = strPatternErrorMessage;
117     }
118 
119     /**
120      * Returns the PatternErrorMessageI18nKey
121      * 
122      * @return The PatternErrorMessageI18nKey
123      */
124     public String getPatternErrorMessageI18nKey( )
125     {
126         return _strPatternErrorMessageI18nKey;
127     }
128 
129     /**
130      * Sets the PatternErrorMessageI18nKey
131      * 
132      * @param strPatternErrorMessageI18nKey
133      *            The PatternErrorMessageI18nKey
134      */
135     public void setPatternErrorMessageI18nKey( String strPatternErrorMessageI18nKey )
136     {
137         _strPatternErrorMessageI18nKey = strPatternErrorMessageI18nKey;
138     }
139 
140     /**
141      * {@inheritDoc }
142      */
143     @Override
144     public String processResponse( String strResponse, Locale locale, Map mapData ) throws InvalidResponseException
145     {
146         if ( _strStandardPattern != null )
147         {
148             checkStandardPattern( strResponse, locale );
149         }
150 
151         if ( _strCustomPattern != null )
152         {
153             checkCustomPattern( strResponse, locale );
154         }
155 
156         return strResponse;
157     }
158 
159     /**
160      * Check if the response matches a standard pattern
161      * 
162      * @param strResponse
163      *            The response
164      * @param locale
165      *            The locale
166      * @throws InvalidResponseException
167      *             if doesn't match the pattern
168      */
169     private void checkStandardPattern( String strResponse, Locale locale ) throws InvalidResponseException
170     {
171         if ( _strStandardPattern.equalsIgnoreCase( PATTERN_EMAIL_NAME ) )
172         {
173             Matcher matcher = PATTERN_EMAIL_VALUE.matcher( strResponse );
174 
175             if ( !matcher.find( ) )
176             {
177                 processInvalidResponse( locale );
178             }
179         }
180     }
181 
182     /**
183      * Check if the response matches the custom pattern
184      * 
185      * @param strResponse
186      *            The response
187      * @param locale
188      *            The locale
189      * @throws InvalidResponseException
190      *             if doesn't match the pattern
191      */
192     private void checkCustomPattern( String strResponse, Locale locale ) throws InvalidResponseException
193     {
194         Pattern pattern = Pattern.compile( _strCustomPattern, Pattern.CASE_INSENSITIVE );
195         Matcher matcher = pattern.matcher( strResponse );
196 
197         if ( !matcher.find( ) )
198         {
199             processInvalidResponse( locale );
200         }
201     }
202 
203     /**
204      * Process an invalid response
205      * 
206      * @param locale
207      *            The locale
208      * @throws InvalidResponseException
209      *             with the
210      */
211     private void processInvalidResponse( Locale locale ) throws InvalidResponseException
212     {
213         String strMessage;
214 
215         if ( _strPatternErrorMessageI18nKey != null )
216         {
217             strMessage = I18nService.getLocalizedString( _strPatternErrorMessageI18nKey, locale );
218         }
219         else
220         {
221             strMessage = _strPatternErrorMessage;
222         }
223 
224         throw new InvalidResponseException( strMessage );
225     }
226 }