Commit 38d211be76bd22179d8b0b1d6a265129c2cf4441
1 parent
82dc5cfe
Exists in
master
criação de método para verificar a conexão separadamente pelos agentes
httUser e pelo jakarta.
Showing
1 changed file
with
47 additions
and
32 deletions
Show diff stats
src/main/java/br/com/ases/model/utilities/Validate.java
@@ -80,58 +80,73 @@ public class Validate { | @@ -80,58 +80,73 @@ public class Validate { | ||
80 | 80 | ||
81 | boolean isValido = true; | 81 | boolean isValido = true; |
82 | int codResponse; | 82 | int codResponse; |
83 | - HttpMethod metodoRequisicaoGET = null; | ||
84 | - HttpClient clienteHTTPJakartaCommons; | ||
85 | - URL UrlConvertida; | ||
86 | 83 | ||
87 | if(campo == null || campo.length() <= 10 ){ | 84 | if(campo == null || campo.length() <= 10 ){ |
88 | this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor preencher o campo URL.", "warning")); | 85 | this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! Favor preencher o campo URL.", "warning")); |
89 | isValido = false; | 86 | isValido = false; |
90 | }else{ | 87 | }else{ |
91 | - try { | ||
92 | - //URL url = new URL(campo); | ||
93 | - | ||
94 | - //URLConnection conn = url.openConnection(); | ||
95 | - // conn.connect(); | ||
96 | - | ||
97 | - UrlConvertida = new URL(campo); | ||
98 | - | ||
99 | - clienteHTTPJakartaCommons = new HttpClient(); | ||
100 | - clienteHTTPJakartaCommons.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false)); | ||
101 | - clienteHTTPJakartaCommons.getParams().setParameter("http.protocol.allow-circular-redirects", true); | ||
102 | - | ||
103 | - metodoRequisicaoGET = new GetMethod(UrlConvertida.toExternalForm()); | ||
104 | - | ||
105 | - metodoRequisicaoGET.setRequestHeader("user-agent", "NewUseAgent/1.0"); | ||
106 | - metodoRequisicaoGET.setRequestHeader("http.agent", "Jakarta Commons-HttpClient/3.1"); | ||
107 | - metodoRequisicaoGET.setFollowRedirects(true); | ||
108 | - | ||
109 | - codResponse = clienteHTTPJakartaCommons.executeMethod(metodoRequisicaoGET); | 88 | + |
89 | + codResponse = verificarConexao(campo ,"user-agent", "NewUseAgent/1.0"); | ||
110 | 90 | ||
111 | if(codResponse != 200) | 91 | if(codResponse != 200) |
112 | { | 92 | { |
113 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" é considerada inválida.", "warning")); | 93 | + codResponse = verificarConexao(campo ,"http.agent", "Jakarta Commons-HttpClient/3.1"); |
94 | + if(codResponse != 200) | ||
95 | + { | ||
96 | + this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); | ||
114 | isValido = false; | 97 | isValido = false; |
98 | + } | ||
99 | + else | ||
100 | + { | ||
101 | + isValido = true; | ||
102 | + } | ||
115 | } | 103 | } |
116 | else | 104 | else |
117 | { | 105 | { |
118 | isValido = true; | 106 | isValido = true; |
119 | } | 107 | } |
120 | 108 | ||
121 | - } catch (MalformedURLException e) { | ||
122 | - e.printStackTrace(); | ||
123 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" é considerada inválida.", "warning")); | ||
124 | - isValido = false; | ||
125 | - } catch (IOException e) { | ||
126 | - e.printStackTrace(); | ||
127 | - this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" é considerada inválida.", "warning")); | ||
128 | - isValido = false; | ||
129 | - } | ||
130 | } | 109 | } |
131 | 110 | ||
132 | return isValido; | 111 | return isValido; |
133 | } | 112 | } |
134 | 113 | ||
114 | + private Integer verificarConexao(String campo, String usuario, String agente) | ||
115 | + { | ||
116 | + | ||
117 | + int codResponse = -1; | ||
118 | + HttpMethod metodoRequisicaoGET = null; | ||
119 | + HttpClient clienteHTTPJakartaCommons; | ||
120 | + URL UrlConvertida; | ||
121 | + | ||
122 | + try { | ||
123 | + | ||
124 | + UrlConvertida = new URL(campo); | ||
125 | + | ||
126 | + clienteHTTPJakartaCommons = new HttpClient(); | ||
127 | + clienteHTTPJakartaCommons.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3,false)); | ||
128 | + clienteHTTPJakartaCommons.getParams().setParameter("http.protocol.allow-circular-redirects", true); | ||
129 | + | ||
130 | + metodoRequisicaoGET = new GetMethod(UrlConvertida.toExternalForm()); | ||
131 | + | ||
132 | + metodoRequisicaoGET.setRequestHeader(usuario, agente); | ||
133 | + metodoRequisicaoGET.setFollowRedirects(true); | ||
134 | + | ||
135 | + codResponse = clienteHTTPJakartaCommons.executeMethod(metodoRequisicaoGET); | ||
136 | + | ||
137 | + return codResponse; | ||
138 | + | ||
139 | + } catch (MalformedURLException e) { | ||
140 | + e.printStackTrace(); | ||
141 | + this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); | ||
142 | + return -1; | ||
143 | + } catch (IOException e) { | ||
144 | + e.printStackTrace(); | ||
145 | + this.validator.add(new ValidationMessage("Não foi possível realizar a avaliação! URL "+campo+" � considerada inv�lida.", "warning")); | ||
146 | + return -1; | ||
147 | + } | ||
148 | + | ||
149 | + } | ||
135 | 150 | ||
136 | public boolean condigoFonte(String campo){ | 151 | public boolean condigoFonte(String campo){ |
137 | 152 |