Commit 84af5f973ad4ecc25cf5b2cb5e9449281d742463
1 parent
b36ea6a1
Exists in
master
Correção adição das mensagens de erros(estava adicionando 3x).
Showing
1 changed file
with
220 additions
and
192 deletions
Show diff stats
src/main/java/br/com/ases/model/utilities/Validate.java
... | ... | @@ -18,195 +18,223 @@ import br.com.caelum.vraptor.interceptor.multipart.UploadedFile; |
18 | 18 | import br.com.caelum.vraptor.validator.ValidationMessage; |
19 | 19 | |
20 | 20 | public class Validate { |
21 | - | |
22 | - private final Validator validator; | |
23 | - | |
24 | - public Validate(Validator validator){ | |
25 | - this.validator = validator; | |
26 | - } | |
27 | - | |
28 | - public boolean contato(Contato contato){ | |
29 | - boolean isValido = true; | |
30 | - | |
31 | - if(contato.getNome() == null){ | |
32 | - this.validator.add(new ValidationMessage("Favor preencher o campo NOME!", "error")); | |
33 | - isValido = false; | |
34 | - }else | |
35 | - if(contato.getNome().length() > 200){ | |
36 | - this.validator.add(new ValidationMessage("O campo NOME aceita até 200 caracteres.", "error")); | |
37 | - isValido = false; | |
38 | - } | |
39 | - | |
40 | - if(contato.getEmail() == null){ | |
41 | - this.validator.add(new ValidationMessage("Favor preencher o campo E-MAIL!", "error")); | |
42 | - isValido = false; | |
43 | - }else{ | |
44 | - Pattern p = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-zA-Z]{2,7}$"); | |
45 | - Matcher m = p.matcher(contato.getEmail()); | |
46 | - if (!m.find()){ | |
47 | - //this.validator.add(new ValidationMessage("Não foi possível enviar a mensagem! E-MAIL '"+contato.getEmail()+"' considerado inválido!", "error")); | |
48 | - this.validator.add(new ValidationMessage("E-MAIL considerado inválido!", "error")); | |
49 | - isValido = false; | |
50 | - } | |
51 | - } | |
52 | - | |
53 | - if(contato.getAssunto() == null){ | |
54 | - this.validator.add(new ValidationMessage("Favor escolher uma das opções no campo ASSUNTO!", "error")); | |
55 | - isValido = false; | |
56 | - } | |
57 | - | |
58 | - if(contato.getMensagem() == null){ | |
59 | - this.validator.add(new ValidationMessage("Favor preencher o campo MENSAGEM!", "error")); | |
60 | - isValido = false; | |
61 | - }else | |
62 | - if(contato.getMensagem().length() > 500){ | |
63 | - this.validator.add(new ValidationMessage("O campo MENSAGEM aceita até 500 caracteres.", "error")); | |
64 | - isValido = false; | |
65 | - } | |
66 | - | |
67 | - return isValido; | |
68 | - } | |
69 | - | |
70 | - | |
71 | - | |
72 | - | |
73 | - public boolean url(String campo){ | |
74 | - | |
75 | - boolean isValido = true; | |
76 | - int codResponse; | |
77 | - | |
78 | - if(campo == null || campo.length() <= 10 ){ | |
79 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor preencher o campo URL.", "warning")); | |
80 | - isValido = false; | |
81 | - }else{ | |
82 | - | |
83 | - codResponse = verificarConexao(campo ,"user-agent", "NewUseAgent/1.0"); | |
84 | - | |
85 | - if(codResponse != 200) | |
86 | - { | |
87 | - codResponse = verificarConexao(campo ,"http.agent", "Jakarta Commons-HttpClient/3.1"); | |
88 | - if(codResponse != 200) | |
89 | - { | |
90 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); | |
91 | - isValido = false; | |
92 | - } | |
93 | - else | |
94 | - { | |
95 | - isValido = true; | |
96 | - } | |
97 | - } | |
98 | - else | |
99 | - { | |
100 | - isValido = true; | |
101 | - } | |
102 | - | |
103 | - } | |
104 | - | |
105 | - return isValido; | |
106 | - } | |
107 | - | |
108 | - private Integer verificarConexao(String campo, String usuario, String agente) | |
109 | - { | |
110 | - System.setProperty("jsse.enableSNIExtension", "false"); | |
111 | - int codResponse = -1; | |
112 | - HttpMethod metodoRequisicaoGET = null; | |
113 | - HttpClient clienteHTTPJakartaCommons; | |
114 | - URL UrlConvertida; | |
115 | - | |
116 | - try { | |
117 | - | |
118 | - UrlConvertida = new URL(campo); | |
119 | - | |
120 | - clienteHTTPJakartaCommons = new HttpClient(); | |
121 | - clienteHTTPJakartaCommons.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false)); | |
122 | - clienteHTTPJakartaCommons.getParams().setParameter("http.protocol.allow-circular-redirects", true); | |
123 | - | |
124 | - metodoRequisicaoGET = new GetMethod(UrlConvertida.toExternalForm()); | |
125 | - | |
126 | - metodoRequisicaoGET.setRequestHeader(usuario, agente); | |
127 | - metodoRequisicaoGET.setFollowRedirects(true); | |
128 | - | |
129 | - codResponse = clienteHTTPJakartaCommons.executeMethod(metodoRequisicaoGET); | |
130 | - | |
131 | - return codResponse; | |
132 | - | |
133 | - } catch (MalformedURLException e) { | |
134 | - e.printStackTrace(); | |
135 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); | |
136 | - return -1; | |
137 | - } catch (IOException e) { | |
138 | - e.printStackTrace(); | |
139 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); | |
140 | - return -1; | |
141 | - } | |
142 | - | |
143 | - } | |
144 | - | |
145 | - public boolean condigoFonte(String campo){ | |
146 | - | |
147 | - if(campo == null || campo.length() <= 10 ){ | |
148 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor preencher o campo Código a analisar.", "warning")); | |
149 | - return false; | |
150 | - } | |
151 | - | |
152 | - String reg = "<html.*?>(.*)<\\/html>"; | |
153 | - | |
154 | - Pattern p = Pattern.compile(reg,Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE ); | |
155 | - Matcher m = p.matcher(campo); | |
156 | - | |
157 | - if(!m.find()){ | |
158 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! O código fonte não é do tipo HTML ou XHTML.", "warning")); | |
159 | - return false; | |
160 | - } | |
161 | - | |
162 | - if(campo.length() > 1048576){ | |
163 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Tamanho máximo permitido para código fonte é até 1024KB.", "warning")); | |
164 | - return false; | |
165 | - } | |
166 | - | |
167 | - return true; | |
168 | - | |
169 | - } | |
170 | - | |
171 | - public boolean uploadForm(UploadedFile file){ | |
172 | - | |
173 | - if(file == null ){ | |
174 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor realizar o upload do arquivo.", "warning")); | |
175 | - return false; | |
176 | - } | |
177 | - | |
178 | - String fileType = file.getContentType(); | |
179 | - if(!(fileType.equals("text/html") || fileType.equals("application/xhtml+xml") || fileType.equals("application/xhtml") || fileType.equals("application/xml"))){ | |
180 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! As extensões permitidas para o arquivo são: .xht, .htm, .html ou .xhtml.", "warning")); | |
181 | - return false; | |
182 | - } | |
183 | - | |
184 | - if(file.getSize() > 1048576){ | |
185 | - this.validator.add(new ValidationMessage("Não foi possével realizar o upload do arquivo! Tamanho máximo permitido é de 1024KB.", "warning")); | |
186 | - return false; | |
187 | - } | |
188 | - | |
189 | - return true; | |
190 | - | |
191 | - } | |
192 | - | |
193 | - public boolean conteudoUploadForm(String html){ | |
194 | - | |
195 | - String reg = "<html.*?>(.*)<\\/html>"; | |
196 | - | |
197 | - Pattern p = Pattern.compile(reg,Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); | |
198 | - Matcher m = p.matcher(html); | |
199 | - | |
200 | - if(!m.find()){ | |
201 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! O conteédo do arquivo não é do tipo HTML ou XHTML.", "warning")); | |
202 | - return false; | |
203 | - } | |
204 | - | |
205 | - return true; | |
206 | - } | |
207 | - | |
208 | - public Validator getMessage(){ | |
209 | - return this.validator; | |
210 | - } | |
211 | - | |
212 | -} | |
213 | 21 | \ No newline at end of file |
22 | + | |
23 | + private final Validator validator; | |
24 | + | |
25 | + public Validate(Validator validator) { | |
26 | + this.validator = validator; | |
27 | + } | |
28 | + | |
29 | + public boolean contato(Contato contato) { | |
30 | + boolean isValido = true; | |
31 | + | |
32 | + if (contato.getNome() == null) { | |
33 | + this.validator.add(new ValidationMessage("Favor preencher o campo NOME!", "error")); | |
34 | + isValido = false; | |
35 | + } else if (contato.getNome().length() > 200) { | |
36 | + this.validator.add(new ValidationMessage("O campo NOME aceita até 200 caracteres.", | |
37 | + "error")); | |
38 | + isValido = false; | |
39 | + } | |
40 | + | |
41 | + if (contato.getEmail() == null) { | |
42 | + this.validator.add(new ValidationMessage("Favor preencher o campo E-MAIL!", "error")); | |
43 | + isValido = false; | |
44 | + } else { | |
45 | + Pattern p = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-zA-Z]{2,7}$"); | |
46 | + Matcher m = p.matcher(contato.getEmail()); | |
47 | + if (!m.find()) { | |
48 | + // this.validator.add(new | |
49 | + // ValidationMessage("Não foi possível enviar a mensagem! E-MAIL '"+contato.getEmail()+"' considerado inválido!", | |
50 | + // "error")); | |
51 | + this.validator.add(new ValidationMessage("E-MAIL considerado inválido!", "error")); | |
52 | + isValido = false; | |
53 | + } | |
54 | + } | |
55 | + | |
56 | + if (contato.getAssunto() == null) { | |
57 | + this.validator.add(new ValidationMessage( | |
58 | + "Favor escolher uma das opções no campo ASSUNTO!", "error")); | |
59 | + isValido = false; | |
60 | + } | |
61 | + | |
62 | + if (contato.getMensagem() == null) { | |
63 | + this.validator.add(new ValidationMessage("Favor preencher o campo MENSAGEM!", "error")); | |
64 | + isValido = false; | |
65 | + } else if (contato.getMensagem().length() > 500) { | |
66 | + this.validator.add(new ValidationMessage( | |
67 | + "O campo MENSAGEM aceita até 500 caracteres.", "error")); | |
68 | + isValido = false; | |
69 | + } | |
70 | + | |
71 | + return isValido; | |
72 | + } | |
73 | + | |
74 | + | |
75 | + | |
76 | + public boolean url(String campo) { | |
77 | + | |
78 | + boolean isValido = true; | |
79 | + int codResponse; | |
80 | + | |
81 | + if (campo == null || campo.length() <= 10) { | |
82 | + this.validator | |
83 | + .add(new ValidationMessage( | |
84 | + "Não foi possível realizar a avaliação! Favor preencher o campo URL.", | |
85 | + "warning")); | |
86 | + isValido = false; | |
87 | + } else { | |
88 | + | |
89 | + codResponse = verificarConexao(campo, "user-agent", "NewUseAgent/1.0"); | |
90 | + | |
91 | + if (codResponse != 200) { | |
92 | + codResponse = verificarConexao(campo, "http.agent", "Jakarta Commons-HttpClient/3.1"); | |
93 | + if (codResponse != 200) { | |
94 | + this.validator.add(new ValidationMessage( | |
95 | + "Não foi possível realizar a avaliação! URL " + campo | |
96 | + + " é considerada inválida.", "warning")); | |
97 | + isValido = false; | |
98 | + } else { | |
99 | + isValido = true; | |
100 | + } | |
101 | + } else { | |
102 | + isValido = true; | |
103 | + } | |
104 | + | |
105 | + } | |
106 | + | |
107 | + return isValido; | |
108 | + } | |
109 | + | |
110 | + private Integer verificarConexao(String campo, String usuario, String agente) { | |
111 | + System.setProperty("jsse.enableSNIExtension", "false"); | |
112 | + int codResponse = -1; | |
113 | + HttpMethod metodoRequisicaoGET = null; | |
114 | + HttpClient clienteHTTPJakartaCommons; | |
115 | + URL UrlConvertida; | |
116 | + | |
117 | + try { | |
118 | + | |
119 | + UrlConvertida = new URL(campo); | |
120 | + | |
121 | + clienteHTTPJakartaCommons = new HttpClient(); | |
122 | + clienteHTTPJakartaCommons.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, | |
123 | + new DefaultHttpMethodRetryHandler(3, false)); | |
124 | + clienteHTTPJakartaCommons.getParams().setParameter("http.protocol.allow-circular-redirects", | |
125 | + true); | |
126 | + | |
127 | + metodoRequisicaoGET = new GetMethod(UrlConvertida.toExternalForm()); | |
128 | + | |
129 | + metodoRequisicaoGET.setRequestHeader(usuario, agente); | |
130 | + metodoRequisicaoGET.setFollowRedirects(true); | |
131 | + | |
132 | + codResponse = clienteHTTPJakartaCommons.executeMethod(metodoRequisicaoGET); | |
133 | + | |
134 | + return codResponse; | |
135 | + | |
136 | + } catch (MalformedURLException e) { | |
137 | + e.printStackTrace(); | |
138 | + // this.validator.add(new ValidationMessage( | |
139 | + // "Não foi possível realizar a avaliação! URL " + campo | |
140 | + // + " é considerada inválida.", "warning")); | |
141 | + return -1; | |
142 | + } catch (IOException e) { | |
143 | + e.printStackTrace(); | |
144 | + //this.validator.add(new ValidationMessage( | |
145 | + // "Não foi possível realizar a avaliação! URL " + campo | |
146 | + // + " é considerada inválida.", "warning")); | |
147 | + return -1; | |
148 | + } | |
149 | + | |
150 | + } | |
151 | + | |
152 | + public boolean condigoFonte(String campo) { | |
153 | + | |
154 | + if (campo == null || campo.length() <= 10) { | |
155 | + this.validator | |
156 | + .add(new ValidationMessage( | |
157 | + "Não foi possível realizar a avaliação! Favor preencher o campo Código a analisar.", | |
158 | + "warning")); | |
159 | + return false; | |
160 | + } | |
161 | + | |
162 | + String reg = "<html.*?>(.*)<\\/html>"; | |
163 | + | |
164 | + Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); | |
165 | + Matcher m = p.matcher(campo); | |
166 | + | |
167 | + if (!m.find()) { | |
168 | + this.validator | |
169 | + .add(new ValidationMessage( | |
170 | + "Não foi possível realizar a avaliação! O código fonte não é do tipo HTML ou XHTML.", | |
171 | + "warning")); | |
172 | + return false; | |
173 | + } | |
174 | + | |
175 | + if (campo.length() > 1048576) { | |
176 | + this.validator | |
177 | + .add(new ValidationMessage( | |
178 | + "Não foi possível realizar a avaliação! Tamanho máximo permitido para código fonte é até 1024KB.", | |
179 | + "warning")); | |
180 | + return false; | |
181 | + } | |
182 | + | |
183 | + return true; | |
184 | + | |
185 | + } | |
186 | + | |
187 | + public boolean uploadForm(UploadedFile file) { | |
188 | + | |
189 | + if (file == null) { | |
190 | + this.validator | |
191 | + .add(new ValidationMessage( | |
192 | + "Não foi possível realizar a avaliação! Favor realizar o upload do arquivo.", | |
193 | + "warning")); | |
194 | + return false; | |
195 | + } | |
196 | + | |
197 | + String fileType = file.getContentType(); | |
198 | + if (!(fileType.equals("text/html") || fileType.equals("application/xhtml+xml") | |
199 | + || fileType.equals("application/xhtml") || fileType.equals("application/xml"))) { | |
200 | + this.validator | |
201 | + .add(new ValidationMessage( | |
202 | + "Não foi possível realizar a avaliação! As extensões permitidas para o arquivo são: .xht, .htm, .html ou .xhtml.", | |
203 | + "warning")); | |
204 | + return false; | |
205 | + } | |
206 | + | |
207 | + if (file.getSize() > 1048576) { | |
208 | + this.validator | |
209 | + .add(new ValidationMessage( | |
210 | + "Não foi possével realizar o upload do arquivo! Tamanho máximo permitido é de 1024KB.", | |
211 | + "warning")); | |
212 | + return false; | |
213 | + } | |
214 | + | |
215 | + return true; | |
216 | + | |
217 | + } | |
218 | + | |
219 | + public boolean conteudoUploadForm(String html) { | |
220 | + | |
221 | + String reg = "<html.*?>(.*)<\\/html>"; | |
222 | + | |
223 | + Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); | |
224 | + Matcher m = p.matcher(html); | |
225 | + | |
226 | + if (!m.find()) { | |
227 | + this.validator | |
228 | + .add(new ValidationMessage( | |
229 | + "Não foi possível realizar a avaliação! O conteédo do arquivo não é do tipo HTML ou XHTML.", | |
230 | + "warning")); | |
231 | + return false; | |
232 | + } | |
233 | + | |
234 | + return true; | |
235 | + } | |
236 | + | |
237 | + public Validator getMessage() { | |
238 | + return this.validator; | |
239 | + } | |
240 | + | |
241 | +} | ... | ... |