1
2
3
4
5 package comum.util;
6
7 import java.lang.reflect.Field;
8 import java.lang.reflect.InvocationTargetException;
9 import java.lang.reflect.Method;
10 import java.net.URLEncoder;
11 import java.security.cert.CertificateException;
12 import java.security.cert.X509Certificate;
13 import java.text.DecimalFormat;
14 import java.text.NumberFormat;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.Date;
18 import java.util.List;
19 import java.util.Properties;
20
21 import javax.mail.Message;
22 import javax.mail.MessagingException;
23 import javax.mail.Session;
24 import javax.mail.Transport;
25 import javax.mail.internet.AddressException;
26 import javax.mail.internet.InternetAddress;
27 import javax.mail.internet.MimeMessage;
28 import javax.net.ssl.HostnameVerifier;
29 import javax.net.ssl.HttpsURLConnection;
30 import javax.net.ssl.SSLContext;
31 import javax.net.ssl.SSLSession;
32 import javax.net.ssl.TrustManager;
33 import javax.net.ssl.X509TrustManager;
34 import javax.servlet.http.HttpServletRequest;
35
36 import ecar.dao.ConfiguracaoDao;
37 import ecar.dao.CorDao;
38 import ecar.dao.EmailDao;
39 import ecar.exception.ECARException;
40 import ecar.pojo.ConfiguracaoCfg;
41 import ecar.pojo.Cor;
42 import ecar.pojo.Email;
43 import ecar.pojo.TipoFuncAcompTpfa;
44 import ecar.pojo.UsuarioUsu;
45 import ecar.util.Dominios;
46
47
48
49
50
51 public class Util {
52
53
54
55
56
57
58
59
60
61
62 public static List<Method> listaMetodosGet(Object o) {
63 return listaMetodos(o, Dominios.REGEXP_METODOS_GET);
64 }
65
66
67
68
69
70
71
72
73
74
75 public static List<Method> listaMetodosSet(Object o) {
76 return listaMetodos(o, Dominios.REGEXP_METODOS_SET);
77 }
78
79
80
81
82
83
84
85
86
87
88 public static List<Method> listaMetodos(Object o) {
89 return listaMetodos(o, Dominios.REGEXP_TODOS);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109 public static List<Method> listaMetodos(Object o, String pattern) {
110 List<Method> l = new ArrayList<Method>();
111
112 try {
113 Method[] m = o.getClass().getDeclaredMethods();
114
115 for (int i = 0; i < m.length; i++)
116 if (m[i].getName().matches(pattern))
117 l.add(m[i]);
118
119 } catch (SecurityException s) {
120
121 }
122
123 return l;
124 }
125
126
127
128
129
130
131
132
133
134
135
136 public static List<Field> listaAtributos(Object o) {
137 return listaAtributos(o, Dominios.REGEXP_TODOS);
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public static List<Field> listaAtributos(Object o, String pattern) {
158 List<Field> l = new ArrayList<Field>();
159
160 try {
161 Field[] f = o.getClass().getDeclaredFields();
162
163 for (int i = 0; i < f.length; i++)
164 if (f[i].getName().matches(pattern))
165 l.add(f[i]);
166
167 } catch (SecurityException s) {
168
169 }
170
171 return l;
172 }
173
174
175
176
177
178
179
180
181
182
183
184
185
186 public static Object invocaGet(Object o, String atributo, Object[] objParams) throws ECARException {
187 String nomeGetter = "get" + primeiraLetraToUpperCase(atributo);
188 try {
189
190
191
192 return o.getClass().getMethod(nomeGetter, new Class[]{}).invoke(o, objParams);
193 } catch (IllegalArgumentException e) {
194 throw new ECARException("erro.exception");
195 } catch (SecurityException e) {
196 throw new ECARException("erro.exception");
197 } catch (IllegalAccessException e) {
198 throw new ECARException("erro.exception");
199 } catch (InvocationTargetException e) {
200 throw new ECARException("erro.exception");
201 } catch (NoSuchMethodException e) {
202 throw new ECARException("erro.exception");
203 }
204 }
205
206
207
208
209
210
211
212
213
214
215
216
217 public static Object invocaGet(Object o, String atributo) throws ECARException {
218 return invocaGet(o, atributo, null);
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233 public static Object invocaSet(Object o, String atributo, Object[] objParams) throws ECARException {
234 String nomeSetter = "set" + primeiraLetraToUpperCase(atributo);
235 try {
236
237
238
239 return o.getClass().getMethod(nomeSetter, new Class[]{}).invoke(o, objParams);
240 } catch (IllegalArgumentException e) {
241 throw new ECARException("erro.exception");
242 } catch (SecurityException e) {
243 throw new ECARException("erro.exception");
244 } catch (IllegalAccessException e) {
245 throw new ECARException("erro.exception");
246 } catch (InvocationTargetException e) {
247 throw new ECARException("erro.exception");
248 } catch (NoSuchMethodException e) {
249 throw new ECARException("erro.exception");
250 }
251 }
252
253
254
255
256
257
258
259
260
261
262 public static String primeiraLetraToUpperCase(String string){
263 return string.substring(0,1).toUpperCase().concat(string.substring(1));
264 }
265
266
267
268
269
270
271
272
273
274 public static String soPrimeiraLetraToUpperCase(String string){
275 return primeiraLetraToUpperCase(string.toLowerCase());
276 }
277
278
279
280
281
282
283
284
285
286
287
288
289
290 public static String todasPrimeirasLetrasToUpperCase(String string){
291 if(string == null || (string != null && "".equals(string.trim()))){
292 return "";
293 }
294
295 string = string.trim().toLowerCase().replaceAll("\"", "");
296
297 StringBuilder stringModificada = new StringBuilder();
298 String[] temp = string.split(" ");
299
300 for(int i = 0; i < temp.length; i++){
301 String aux = temp[i].trim();
302 if("".equals(aux))
303 continue;
304
305 if(!"de".equals(temp[i]) &&
306 !"da".equals(temp[i]) &&
307 !"das".equals(temp[i]) &&
308 !"do".equals(temp[i]) &&
309 !"dos".equals(temp[i]) &&
310 !"em".equals(temp[i]) &&
311 !"no".equals(temp[i]) &&
312 !"na".equals(temp[i]) &&
313 !"nos".equals(temp[i]) &&
314 !"nas".equals(temp[i]) &&
315 !"a".equals(temp[i]) &&
316 !"e".equals(temp[i]) &&
317 !"i".equals(temp[i]) &&
318 !"o".equals(temp[i]) &&
319 !"u".equals(temp[i]) &&
320 !"com".equals(temp[i]) &&
321 !"sem".equals(temp[i])
322 ){
323 aux = primeiraLetraToUpperCase(aux);
324 }
325
326 stringModificada.append(aux).append(" ");
327 }
328 return stringModificada.toString();
329 }
330
331
332
333
334
335
336
337
338
339 public static String removeEspacosDuplicados(String string){
340 if(string == null || (string != null && "".equals(string.trim()))){
341 return "";
342 }
343
344 StringBuilder stringModificada = new StringBuilder();
345 String[] temp = string.split(" ");
346
347 for(int i = 0; i < temp.length; i++){
348 String aux = temp[i].trim();
349 if("".equals(aux))
350 continue;
351
352 stringModificada.append(aux).append(" ");
353 }
354
355 return stringModificada.toString().trim();
356 }
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371 public static boolean entre(int valorReferencia, int valor1, int valor2)
372 {
373 try{
374 if ((valor1 > valor2)
375 && (valorReferencia < valor1)
376 && (valorReferencia > valor2))
377 return true;
378 if ((valor2 > valor1)
379 && (valorReferencia < valor2)
380 && (valorReferencia > valor1))
381 return true;
382 return false;
383 }catch (NumberFormatException e){
384 return false;
385 }
386
387 }
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405 public static boolean entre(String valorReferencia, String valor1, String valor2)
406 {
407 try{
408 return (entre(Integer.parseInt(valorReferencia), valor1, valor2));
409 }catch (NumberFormatException e){
410 return false;
411 }
412
413 }
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431 public static boolean entre(int valorReferencia, String valor1, String valor2)
432 {
433 try{
434 if (("sup".equals(valor1) && "inf".equals(valor2))
435 || ("sup".equals(valor2) && "inf".equals(valor1)))
436 return true;
437 if ("sup".equals(valor1))
438 if (valorReferencia > Integer.parseInt(valor2))
439 return true;
440 if ("inf".equals(valor1))
441 if (valorReferencia < Integer.parseInt(valor2))
442 return true;
443 if ("sup".equals(valor2))
444 if (valorReferencia > Integer.parseInt(valor1))
445 return true;
446 if ("inf".equals(valor2))
447 if (valorReferencia < Integer.parseInt(valor1))
448 return true;
449 if ((Integer.parseInt(valor1) > Integer.parseInt(valor2))
450 && (valorReferencia < Integer.parseInt(valor1))
451 && (valorReferencia > Integer.parseInt(valor2)))
452 return true;
453 if ((Integer.parseInt(valor2) > Integer.parseInt(valor1))
454 && (valorReferencia < Integer.parseInt(valor2))
455 && (valorReferencia > Integer.parseInt(valor1)))
456 return true;
457 return false;
458 } catch(NumberFormatException e) {
459 return false;
460 }
461 }
462
463
464
465
466
467
468
469
470
471
472
473
474
475 public static String substring(String string, int inicio, int fim) {
476 return string.substring(inicio, Math.min(fim, string.length()));
477 }
478
479
480
481
482
483
484
485
486
487
488 public static String formataMoeda(double number){
489 NumberFormat formatter = new DecimalFormat("###,###,##0.00");
490
491 return formatter.format(number);
492 }
493
494
495
496
497
498
499
500
501
502
503 public static String formataNumeroDecimal(double number){
504 NumberFormat formatter = new DecimalFormat("###,###,##0.##");
505
506 return formatter.format(number);
507 }
508
509 public static String formataNumeroDecimal(Double number){
510 NumberFormat formatter = new DecimalFormat("###,###,##0.##");
511
512 return formatter.format(number);
513 }
514
515
516
517
518
519
520
521
522
523
524
525
526 public static String formataNumeroDecimalParaExportacao(double number, int tamanho){
527 String temp = formataMoeda(number);
528 String retorno = "";
529 for(int i = 0; i < temp.length(); i++){
530 if(temp.charAt(i) != '.' && temp.charAt(i) != ','){
531 retorno += temp.charAt(i);
532 }
533 }
534 int tam = retorno.length();
535 for(int i = tam; i < tamanho; i++){
536 retorno = " " + retorno;
537 }
538 return retorno;
539 }
540
541
542
543
544
545
546
547
548
549
550 public static String formataNumeroSemDecimal(double number){
551 NumberFormat formatter = new DecimalFormat("###,###,##0");
552
553 return formatter.format(number);
554 }
555
556
557
558
559
560
561
562
563
564
565 public static String formataNumeroDecimalSemMilhar(double number){
566 NumberFormat formatter = new DecimalFormat("#0.00");
567 return formatter.format(number);
568 }
569
570
571
572
573
574
575
576
577
578
579 public static String formataNumeroInteiroSemMilhar(double number){
580 NumberFormat formatter = new DecimalFormat("#0");
581
582 return formatter.format(number);
583 }
584
585
586
587
588
589
590
591
592
593
594
595 public static String formataQtdValor(double number, String indQtd){
596
597
598
599 return formataMoeda(number);
600
601 }
602
603
604
605
606
607
608
609
610
611
612 public static String formataNumero(String original){
613 original = original.replaceAll("\\.","");
614 original = original.replaceAll(",",".");
615 return original;
616 }
617
618
619
620
621
622
623
624
625
626
627 public static String formataByte(Long bytes){
628 double kb = converteParaKb(bytes.doubleValue());
629 if(kb > 1000){
630 return formataNumeroDecimal(converteParaMb(kb)) + " MB";
631 } else
632 return formataNumeroDecimal(kb) + " KB";
633 }
634
635
636
637
638
639
640
641
642
643
644 public static double converteParaKb(double bytes){
645 return bytes / new Double(1024).doubleValue();
646 }
647
648
649
650
651
652
653
654
655
656
657 public static double converteParaMb(double kBytes){
658 return kBytes / new Double(1024).doubleValue();
659 }
660
661
662
663
664
665
666
667
668
669
670
671
672
673 public static Collection intersecao(Collection col1, Collection col2){
674 Collection a = new ArrayList(col1);
675 Collection b = new ArrayList(col2);
676 a.retainAll(b);
677 return a;
678 }
679
680
681
682
683
684
685
686
687
688
689
690
691 public static Collection<Object> diferenca(Collection<Object> col1, Collection<Object> col2){
692 Collection<Object> a = new ArrayList<Object>(col1);
693 Collection<Object> b = new ArrayList<Object>(col2);
694 a.removeAll(b);
695 return a;
696 }
697
698
699
700
701
702
703
704
705
706
707 public static String retiraAcentuacao(String string){
708 return string.replace('à','a').
709 replace('á','a').
710 replace('é','e').
711 replace('í','i').
712 replace('ó','o').
713 replace('ú','u').
714 replace('ü','u');
715 }
716
717
718
719
720
721
722
723
724
725
726
727 public static String trocarEspacoPorCaracter(String string, String caracter){
728 return string.replaceAll(" ", caracter);
729 }
730
731
732
733
734
735
736
737
738
739
740
741
742
743 public static String trocaBarraParaDuasBarras(String texto){
744 return texto.replaceAll("\\\\", "\\\\\\\\");
745 }
746
747
748
749
750
751
752
753
754
755
756 public static double calculaMediaValores(Collection<Double> valores){
757 double total = 0;
758 for (Double valor: valores) {
759 total += valor.doubleValue();
760 }
761 return total / valores.size();
762 }
763
764
765
766
767
768
769
770
771
772
773 public static double calculaMediaValoresInteger(Collection<Double> valores){
774 double total = 0;
775 for (Double valor: valores) {
776 total += valor.intValue();
777 }
778 return total / valores.size();
779 }
780
781
782
783
784
785
786
787
788
789
790
791
792 public static String trocar(String palavra, String original, String novo){
793 String retorno = "";
794 for(int i = 0; i< palavra.length(); i++){
795 if(palavra.charAt(i) == original.charAt(0)){
796 retorno += novo;
797 } else {
798 retorno += palavra.charAt(i);
799 }
800 }
801 return retorno;
802
803 }
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823 public static void enviarEmail(String assunto, String nomeRemetente, String remetente,
824 String texto, String destinatarioPara, String destinatarioCc, String destinatarioBcc, UsuarioUsu usuario)
825 throws AddressException, MessagingException, Exception, ECARException {
826 ConfiguracaoCfg config = new ConfiguracaoDao(null).getConfiguracao();
827 if(config.getEmailServer() == null || "".equals(config.getEmailServer().trim())) {
828 throw new ECARException("erro.servidor.email.invalido");
829 }
830
831 Properties mailProps = new Properties();
832 mailProps.put("mail.smtp.host", config.getEmailServer());
833
834 Session mailSession = Session.getInstance(mailProps, null);
835 mailSession.setDebug(false);
836 Message email = new MimeMessage(mailSession);
837 email.setRecipients( Message.RecipientType.TO, InternetAddress.parse(destinatarioPara));
838 if (destinatarioCc != null && !"".equals(destinatarioCc.trim())) {
839 email.setRecipients( Message.RecipientType.CC, InternetAddress.parse(destinatarioCc));
840 }
841 if (destinatarioBcc != null && !"".equals(destinatarioBcc.trim())) {
842 email.setRecipients( Message.RecipientType.BCC, InternetAddress.parse(destinatarioBcc));
843 }
844 InternetAddress iAdd = new InternetAddress();
845
846 if (remetente != null) iAdd.setAddress(remetente);
847 if (nomeRemetente != null) iAdd.setPersonal(nomeRemetente);
848
849 email.setFrom(iAdd);
850 email.setSubject(assunto);
851 email.setContent(texto, "text/html");
852
853 Transport.send(email);
854
855 if (usuario != null){
856 Email mailUsu = new Email(nomeRemetente, new Date(), assunto, destinatarioPara, destinatarioCc, destinatarioBcc, texto,"", usuario);
857 EmailDao emailDao = new EmailDao();
858 emailDao.salvar(mailUsu);
859 }
860 }
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875 public static void normalizeChars(String s, StringBuffer str, int i) {
876 final char ch = s.charAt(i);
877 switch (ch) {
878 case 60:
879 str.append("<");
880 break;
881 case 62:
882 str.append(">");
883 break;
884 case 38:
885 str.append("&");
886 break;
887 case 34:
888 str.append(""");
889 break;
890 case 39:
891 str.append("'");
892 break;
893 default:
894 str.append(ch);
895 break;
896 }
897 }
898
899
900
901
902
903
904
905
906
907
908
909 public static void normalizeEnter(String s, StringBuffer str, int i) {
910 final char ch = s.charAt(i);
911 switch (ch) {
912 case 10:
913 case 13:
914
915 str.append("&#");
916 str.append(Integer.toString(ch));
917 str.append(';');
918 }
919 }
920
921
922
923
924
925
926
927
928
929 public static String normalize(String s) {
930 StringBuffer str = new StringBuffer();
931 final int len = (s == null) ? 0 : s.length();
932 for (int i = 0; i < len; i++) {
933 normalizeChars(s, str, i);
934 normalizeEnter(s, str, i);
935 }
936 return str.toString();
937 }
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954 public static String normalizaQuebraDeLinha(String s) {
955 return s.replaceAll("\r\n","\n");
956 }
957
958
959
960
961
962
963
964 public static String normalizaQuebraDeLinhaHTML(String s){
965 return (s != null) ? (normalizaQuebraDeLinha(s.trim())).replaceAll("\n", "<br>") : "";
966 }
967
968
969
970
971
972
973
974
975
976
977 public static String normalizaCaracterMarcador(String s){
978 if(s != null && !"".equals(s))
979 return s.replace(Dominios.CARACTER_ESTRANHO_MARCADOR, "-").
980 replace(Dominios.CARACTER_ESTRANHO_MARCADOR2, "-").
981 replace(Dominios.CARACTER_ESTRANHO_ABREASPAS, "\"").
982 replace(Dominios.CARACTER_ESTRANHO_FECHAASPAS, "\"").
983 replace(Dominios.CARACTER_ESTRANHO_ABREASPAS_SIMPLES, "'").
984 replace(Dominios.CARACTER_ESTRANHO_FECHAASPAS_SIMPLES, "'");
985
986 return "";
987 }
988
989
990
991
992
993
994
995
996
997
998
999 public static String completarZerosEsquerda(Long numero, int tamanho){
1000
1001 String mascara = "";
1002 for(int i = 0; i < tamanho; i++){
1003 mascara = "0" + mascara;
1004 }
1005
1006 NumberFormat nf = new DecimalFormat(mascara);
1007
1008 if(numero != null)
1009 return nf.format(numero.longValue());
1010 else
1011 return mascara;
1012 }
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026 public static String completarCaracteres(String string, String caracter, int tamanho, String direcao){
1027 String retorno = string;
1028 if(string.length() < tamanho){
1029 if("E".equalsIgnoreCase(direcao)){
1030 int tam = string.length();
1031 for(int i = tam; i < tamanho; i++){
1032 retorno = caracter + retorno;
1033 }
1034 }
1035 if("D".equalsIgnoreCase(direcao)){
1036 int tam = string.length();
1037 for(int i = tam; i < tamanho; i++){
1038 retorno = retorno + caracter;
1039 }
1040 }
1041 }
1042 return retorno;
1043 }
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055 public static String getNomeArquivo(String path) {
1056 String nome = null;
1057
1058 if( path != null ) {
1059 if( path.lastIndexOf("/") != -1 )
1060 nome = path.substring(path.lastIndexOf("/")+1);
1061 else if( path.lastIndexOf("\\") != -1 )
1062 nome = path.substring(path.lastIndexOf("\\")+1);
1063 else
1064 nome = path;
1065 }
1066
1067 return nome;
1068 }
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080 public static String getTagDica(String nome, String contexto, String dica) {
1081 StringBuffer s = new StringBuffer(" ") ;
1082
1083
1084
1085
1086 try {
1087 s.append("<label class=\"dica\" onmouseover=\"javascript:viewFieldTip(this, '"+nome+"SPAN');\" onmouseout=\"javascript:noViewFieldTip('"+nome+"SPAN');\" >");
1088 s.append("<img src=\""+contexto+"/images/dica.png\" align=\"absmiddle\" border=\"0\" onclick=\"javascript:viewFieldTipPopUp(\'" + URLEncoder.encode(dica, "ISO-8859-1") + "\')\" >" );
1089 s.append("<span id=\""+nome+"SPAN\">" + dica + "</span></label>");
1090 } catch (Exception e) {
1091
1092 }
1093
1094 return s.toString();
1095 }
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105 public static String getTagDicaComImagemParecer(String nome, String contexto, String urlImagem, String dica) {
1106 StringBuffer s = new StringBuffer(" ") ;
1107
1108
1109
1110
1111 try {
1112 s.append("<label class=\"dica\" onmouseover=\"javascript:viewFieldTip(this, '"+nome+"SPAN');\" onmouseout=\"javascript:noViewFieldTip('"+nome+"SPAN');\" >");
1113 if(urlImagem.equals(contexto))
1114 s.append("<img src=\""+urlImagem+"/images/dica.png\" align=\"absmiddle\" border=\"0\" onclick=\"javascript:viewFieldTipPopUp(\'" + URLEncoder.encode(dica, "ISO-8859-1") + "\')\" >" );
1115 else
1116 s.append("<img src=\""+urlImagem+"\" align=\"absmiddle\" border=\"0\" style=\"width:16px;height:16px;\" onclick=\"javascript:viewFieldTipPopUp(\'" + URLEncoder.encode(dica, "ISO-8859-1") + "\')\" >" );
1117
1118 s.append("<span id=\""+nome+"SPAN\">" + dica + "</span></label>");
1119 } catch (Exception e) {
1120
1121 }
1122
1123 return s.toString();
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139 public static String getTagDica(String nome, String contexto, String urlImagem, String dica) {
1140 StringBuffer s = new StringBuffer(" ") ;
1141
1142
1143
1144
1145 try {
1146 s.append("<label class=\"dica\" onmouseover=\"javascript:viewFieldTip(this, '"+nome+"SPAN');\" onmouseout=\"javascript:noViewFieldTip('"+nome+"SPAN');\" >");
1147 if(contexto==null ||contexto.equals("") )
1148 s.append("<img src=\""+urlImagem+"\" align=\"absmiddle\" border=\"0\" onclick=\"javascript:viewFieldTipPopUp(\'" + URLEncoder.encode(dica, "ISO-8859-1") + "\')\" >" );
1149 else
1150 s.append("<img src=\""+contexto+"/images/dica.png\" align=\"absmiddle\" border=\"0\" onclick=\"javascript:viewFieldTipPopUp(\'" + URLEncoder.encode(dica, "ISO-8859-1") + "\')\" >" );
1151 s.append("<span id=\""+nome+"SPAN\">" + dica + "</span></label>");
1152 } catch (Exception e) {
1153
1154 }
1155
1156 return s.toString();
1157 }
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176 public static void liberarImagem() throws ECARException {
1177 try {
1178 trustAllHttpsCertificates();
1179
1180 HostnameVerifier hv = new HostnameVerifier() {
1181 public boolean verify(String urlHostName, SSLSession session) {
1182
1183 return true;
1184 }
1185 };
1186
1187 HttpsURLConnection.setDefaultHostnameVerifier(hv);
1188
1189
1190
1191
1192
1193
1194
1195
1196 } catch (Exception e) {
1197 new ECARException("erro.liberar.imagem.ssl");
1198 }
1199 }
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210 private static void trustAllHttpsCertificates() throws Exception {
1211
1212 TrustManager[] trustAllCerts = new TrustManager[1];
1213 TrustManager tm = new miTM();
1214 trustAllCerts[0] = tm;
1215 SSLContext sc = SSLContext.getInstance("SSL");
1216 sc.init(null, trustAllCerts, null);
1217 HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
1218 }
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228 public static class miTM implements TrustManager, X509TrustManager {
1229 public X509Certificate[] getAcceptedIssuers() { return null; }
1230 public boolean isServerTrusted(X509Certificate[] certs) { return true; }
1231 public boolean isClientTrusted(X509Certificate[] certs) { return true; }
1232 public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { return; }
1233 public void checkClientTrusted(X509Certificate[] certs, String authType) throws java.security.cert.CertificateException { return; }
1234 }
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245 public static String getURLImagemAcompanhamento(Cor cor, HttpServletRequest request , TipoFuncAcompTpfa funcao ) throws ECARException{
1246 String url=null;
1247 CorDao corDao = new CorDao(request);
1248
1249 if(cor.getIndPosicoesGeraisCor().equals("S")){
1250 cor.getCodCor();
1251
1252 url = corDao.getImagemPersonalizada(cor, funcao, "D");
1253 if( url != null ) {
1254 url=request.getContextPath()+"/DownloadFile?tipo=open&RemoteFile="+ url ;
1255 } else {
1256 if( cor.getCodCor() != null ) {
1257 url =request.getContextPath() + "/images/" + corDao.getImagemSinal(cor,funcao)+ "" ;
1258 }
1259 }
1260 }
1261
1262 return url;
1263 }
1264
1265
1266
1267
1268
1269
1270
1271
1272 public static String stripHTML(String strHtml){
1273 String newString = new String();
1274 if (strHtml != null){
1275 newString = strHtml.replaceAll("<br>", " ").replaceAll(" ", " ").replaceAll("<.*?>", "").trim();
1276 }
1277
1278 return newString;
1279 }
1280
1281
1282
1283
1284
1285 public static boolean ehValor( String str) {
1286 try {
1287 Float.parseFloat(str);
1288 return true;
1289 } catch (NumberFormatException e) {
1290 return false;
1291 }
1292 }
1293
1294
1295
1296
1297
1298
1299
1300
1301 public static double trataDivisaoPorZero(double dividendo, double divisor){
1302 if (divisor == 0)
1303 return 0;
1304 else {
1305 return dividendo/divisor;
1306 }
1307 }
1308
1309
1310 }