diff --git a/src/main/java/br/com/ases/model/utilities/Validate.java b/src/main/java/br/com/ases/model/utilities/Validate.java index f60eb9a..314f5b9 100644 --- a/src/main/java/br/com/ases/model/utilities/Validate.java +++ b/src/main/java/br/com/ases/model/utilities/Validate.java @@ -18,195 +18,223 @@ import br.com.caelum.vraptor.interceptor.multipart.UploadedFile; import br.com.caelum.vraptor.validator.ValidationMessage; public class Validate { - - private final Validator validator; - - public Validate(Validator validator){ - this.validator = validator; - } - - public boolean contato(Contato contato){ - boolean isValido = true; - - if(contato.getNome() == null){ - this.validator.add(new ValidationMessage("Favor preencher o campo NOME!", "error")); - isValido = false; - }else - if(contato.getNome().length() > 200){ - this.validator.add(new ValidationMessage("O campo NOME aceita até 200 caracteres.", "error")); - isValido = false; - } - - if(contato.getEmail() == null){ - this.validator.add(new ValidationMessage("Favor preencher o campo E-MAIL!", "error")); - isValido = false; - }else{ - Pattern p = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-zA-Z]{2,7}$"); - Matcher m = p.matcher(contato.getEmail()); - if (!m.find()){ - //this.validator.add(new ValidationMessage("Não foi possível enviar a mensagem! E-MAIL '"+contato.getEmail()+"' considerado inválido!", "error")); - this.validator.add(new ValidationMessage("E-MAIL considerado inválido!", "error")); - isValido = false; - } - } - - if(contato.getAssunto() == null){ - this.validator.add(new ValidationMessage("Favor escolher uma das opções no campo ASSUNTO!", "error")); - isValido = false; - } - - if(contato.getMensagem() == null){ - this.validator.add(new ValidationMessage("Favor preencher o campo MENSAGEM!", "error")); - isValido = false; - }else - if(contato.getMensagem().length() > 500){ - this.validator.add(new ValidationMessage("O campo MENSAGEM aceita até 500 caracteres.", "error")); - isValido = false; - } - - return isValido; - } - - - - - public boolean url(String campo){ - - boolean isValido = true; - int codResponse; - - if(campo == null || campo.length() <= 10 ){ - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor preencher o campo URL.", "warning")); - isValido = false; - }else{ - - codResponse = verificarConexao(campo ,"user-agent", "NewUseAgent/1.0"); - - if(codResponse != 200) - { - codResponse = verificarConexao(campo ,"http.agent", "Jakarta Commons-HttpClient/3.1"); - if(codResponse != 200) - { - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); - isValido = false; - } - else - { - isValido = true; - } - } - else - { - isValido = true; - } - - } - - return isValido; - } - - private Integer verificarConexao(String campo, String usuario, String agente) - { - System.setProperty("jsse.enableSNIExtension", "false"); - int codResponse = -1; - HttpMethod metodoRequisicaoGET = null; - HttpClient clienteHTTPJakartaCommons; - URL UrlConvertida; - - try { - - UrlConvertida = new URL(campo); - - clienteHTTPJakartaCommons = new HttpClient(); - clienteHTTPJakartaCommons.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false)); - clienteHTTPJakartaCommons.getParams().setParameter("http.protocol.allow-circular-redirects", true); - - metodoRequisicaoGET = new GetMethod(UrlConvertida.toExternalForm()); - - metodoRequisicaoGET.setRequestHeader(usuario, agente); - metodoRequisicaoGET.setFollowRedirects(true); - - codResponse = clienteHTTPJakartaCommons.executeMethod(metodoRequisicaoGET); - - return codResponse; - - } catch (MalformedURLException e) { - e.printStackTrace(); - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); - return -1; - } catch (IOException e) { - e.printStackTrace(); - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); - return -1; - } - - } - - public boolean condigoFonte(String campo){ - - if(campo == null || campo.length() <= 10 ){ - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor preencher o campo Código a analisar.", "warning")); - return false; - } - - String reg = "(.*)<\\/html>"; - - Pattern p = Pattern.compile(reg,Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE ); - Matcher m = p.matcher(campo); - - if(!m.find()){ - 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")); - return false; - } - - if(campo.length() > 1048576){ - 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")); - return false; - } - - return true; - - } - - public boolean uploadForm(UploadedFile file){ - - if(file == null ){ - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor realizar o upload do arquivo.", "warning")); - return false; - } - - String fileType = file.getContentType(); - if(!(fileType.equals("text/html") || fileType.equals("application/xhtml+xml") || fileType.equals("application/xhtml") || fileType.equals("application/xml"))){ - 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")); - return false; - } - - if(file.getSize() > 1048576){ - this.validator.add(new ValidationMessage("Não foi possével realizar o upload do arquivo! Tamanho máximo permitido é de 1024KB.", "warning")); - return false; - } - - return true; - - } - - public boolean conteudoUploadForm(String html){ - - String reg = "(.*)<\\/html>"; - - Pattern p = Pattern.compile(reg,Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); - Matcher m = p.matcher(html); - - if(!m.find()){ - 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")); - return false; - } - - return true; - } - - public Validator getMessage(){ - return this.validator; - } - -} \ No newline at end of file + + private final Validator validator; + + public Validate(Validator validator) { + this.validator = validator; + } + + public boolean contato(Contato contato) { + boolean isValido = true; + + if (contato.getNome() == null) { + this.validator.add(new ValidationMessage("Favor preencher o campo NOME!", "error")); + isValido = false; + } else if (contato.getNome().length() > 200) { + this.validator.add(new ValidationMessage("O campo NOME aceita até 200 caracteres.", + "error")); + isValido = false; + } + + if (contato.getEmail() == null) { + this.validator.add(new ValidationMessage("Favor preencher o campo E-MAIL!", "error")); + isValido = false; + } else { + Pattern p = Pattern.compile("^[\\w-]+(\\.[\\w-]+)*@([\\w-]+\\.)+[a-zA-Z]{2,7}$"); + Matcher m = p.matcher(contato.getEmail()); + if (!m.find()) { + // this.validator.add(new + // ValidationMessage("Não foi possível enviar a mensagem! E-MAIL '"+contato.getEmail()+"' considerado inválido!", + // "error")); + this.validator.add(new ValidationMessage("E-MAIL considerado inválido!", "error")); + isValido = false; + } + } + + if (contato.getAssunto() == null) { + this.validator.add(new ValidationMessage( + "Favor escolher uma das opções no campo ASSUNTO!", "error")); + isValido = false; + } + + if (contato.getMensagem() == null) { + this.validator.add(new ValidationMessage("Favor preencher o campo MENSAGEM!", "error")); + isValido = false; + } else if (contato.getMensagem().length() > 500) { + this.validator.add(new ValidationMessage( + "O campo MENSAGEM aceita até 500 caracteres.", "error")); + isValido = false; + } + + return isValido; + } + + + + public boolean url(String campo) { + + boolean isValido = true; + int codResponse; + + if (campo == null || campo.length() <= 10) { + this.validator + .add(new ValidationMessage( + "Não foi possível realizar a avaliação! Favor preencher o campo URL.", + "warning")); + isValido = false; + } else { + + codResponse = verificarConexao(campo, "user-agent", "NewUseAgent/1.0"); + + if (codResponse != 200) { + codResponse = verificarConexao(campo, "http.agent", "Jakarta Commons-HttpClient/3.1"); + if (codResponse != 200) { + this.validator.add(new ValidationMessage( + "Não foi possível realizar a avaliação! URL " + campo + + " é considerada inválida.", "warning")); + isValido = false; + } else { + isValido = true; + } + } else { + isValido = true; + } + + } + + return isValido; + } + + private Integer verificarConexao(String campo, String usuario, String agente) { + System.setProperty("jsse.enableSNIExtension", "false"); + int codResponse = -1; + HttpMethod metodoRequisicaoGET = null; + HttpClient clienteHTTPJakartaCommons; + URL UrlConvertida; + + try { + + UrlConvertida = new URL(campo); + + clienteHTTPJakartaCommons = new HttpClient(); + clienteHTTPJakartaCommons.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, + new DefaultHttpMethodRetryHandler(3, false)); + clienteHTTPJakartaCommons.getParams().setParameter("http.protocol.allow-circular-redirects", + true); + + metodoRequisicaoGET = new GetMethod(UrlConvertida.toExternalForm()); + + metodoRequisicaoGET.setRequestHeader(usuario, agente); + metodoRequisicaoGET.setFollowRedirects(true); + + codResponse = clienteHTTPJakartaCommons.executeMethod(metodoRequisicaoGET); + + return codResponse; + + } catch (MalformedURLException e) { + e.printStackTrace(); + // this.validator.add(new ValidationMessage( + // "Não foi possível realizar a avaliação! URL " + campo + // + " é considerada inválida.", "warning")); + return -1; + } catch (IOException e) { + e.printStackTrace(); + //this.validator.add(new ValidationMessage( + // "Não foi possível realizar a avaliação! URL " + campo + // + " é considerada inválida.", "warning")); + return -1; + } + + } + + public boolean condigoFonte(String campo) { + + if (campo == null || campo.length() <= 10) { + this.validator + .add(new ValidationMessage( + "Não foi possível realizar a avaliação! Favor preencher o campo Código a analisar.", + "warning")); + return false; + } + + String reg = "(.*)<\\/html>"; + + Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); + Matcher m = p.matcher(campo); + + if (!m.find()) { + 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")); + return false; + } + + if (campo.length() > 1048576) { + 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")); + return false; + } + + return true; + + } + + public boolean uploadForm(UploadedFile file) { + + if (file == null) { + this.validator + .add(new ValidationMessage( + "Não foi possível realizar a avaliação! Favor realizar o upload do arquivo.", + "warning")); + return false; + } + + String fileType = file.getContentType(); + if (!(fileType.equals("text/html") || fileType.equals("application/xhtml+xml") + || fileType.equals("application/xhtml") || fileType.equals("application/xml"))) { + 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")); + return false; + } + + if (file.getSize() > 1048576) { + this.validator + .add(new ValidationMessage( + "Não foi possével realizar o upload do arquivo! Tamanho máximo permitido é de 1024KB.", + "warning")); + return false; + } + + return true; + + } + + public boolean conteudoUploadForm(String html) { + + String reg = "(.*)<\\/html>"; + + Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE); + Matcher m = p.matcher(html); + + if (!m.find()) { + 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")); + return false; + } + + return true; + } + + public Validator getMessage() { + return this.validator; + } + +} -- libgit2 0.21.2