1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
58
59
60
61 public String getStandardPattern( )
62 {
63 return _strStandardPattern;
64 }
65
66
67
68
69
70
71
72 public void setStandardPattern( String strStandardPattern )
73 {
74 _strStandardPattern = strStandardPattern;
75 }
76
77
78
79
80
81
82 public String getCustomPattern( )
83 {
84 return _strCustomPattern;
85 }
86
87
88
89
90
91
92
93 public void setCustomPattern( String strCustomPattern )
94 {
95 _strCustomPattern = strCustomPattern;
96 }
97
98
99
100
101
102
103 public String getPatternErrorMessage( )
104 {
105 return _strPatternErrorMessage;
106 }
107
108
109
110
111
112
113
114 public void setPatternErrorMessage( String strPatternErrorMessage )
115 {
116 _strPatternErrorMessage = strPatternErrorMessage;
117 }
118
119
120
121
122
123
124 public String getPatternErrorMessageI18nKey( )
125 {
126 return _strPatternErrorMessageI18nKey;
127 }
128
129
130
131
132
133
134
135 public void setPatternErrorMessageI18nKey( String strPatternErrorMessageI18nKey )
136 {
137 _strPatternErrorMessageI18nKey = strPatternErrorMessageI18nKey;
138 }
139
140
141
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
161
162
163
164
165
166
167
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
184
185
186
187
188
189
190
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
205
206
207
208
209
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 }