Ticket #693: psync.dataprev.patch
File psync.dataprev.patch, 83.1 KB (added by lucas.dacosta, 14 years ago) |
---|
-
src/main/sql/postgresql/create_schema.sql
1 ALTER TABLE phpgw_cal ALTER title TYPE character varying(255);2 ALTER TABLE phpgw_cal ADD COLUMN last_status char(1) DEFAULT 'N'::bpchar;3 ALTER TABLE phpgw_cal ADD COLUMN last_update int8 DEFAULT (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision);1 --ALTER TABLE phpgw_cal ALTER title TYPE character varying(255); 2 --ALTER TABLE phpgw_cal ADD COLUMN last_status char(1) DEFAULT 'N'::bpchar; 3 --ALTER TABLE phpgw_cal ADD COLUMN last_update int8 DEFAULT (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision); 4 4 5 ALTER TABLE phpgw_cc_contact ADD COLUMN last_status char(1) DEFAULT 'N'::bpchar;6 ALTER TABLE phpgw_cc_contact ADD COLUMN last_update int8 DEFAULT (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision);7 ALTER TABLE phpgw_cc_contact ADD COLUMN category character varying(20);5 --ALTER TABLE phpgw_cc_contact ADD COLUMN last_status char(1) DEFAULT 'N'::bpchar; 6 --ALTER TABLE phpgw_cc_contact ADD COLUMN last_update int8 DEFAULT (date_part('epoch'::text, ('now'::text)::timestamp(3) with time zone) * (1000)::double precision); 7 --ALTER TABLE phpgw_cc_contact ADD COLUMN category character varying(20); 8 8 -
src/main/sql/postgresql/drop_schema.sql
1 ALTER TABLE public.phpgw_cal DROP COLUMN last_status;2 ALTER TABLE public.phpgw_cal DROP COLUMN last_update;1 --ALTER TABLE public.phpgw_cal DROP COLUMN last_status; 2 --ALTER TABLE public.phpgw_cal DROP COLUMN last_update; 3 3 4 ALTER TABLE public.phpgw_cc_contact DROP COLUMN last_status;5 ALTER TABLE public.phpgw_cc_contact DROP COLUMN last_update;6 ALTER TABLE public.phpgw_cc_contact DROP COLUMN category;7 No newline at end of file 4 --ALTER TABLE public.phpgw_cc_contact DROP COLUMN last_status; 5 --ALTER TABLE public.phpgw_cc_contact DROP COLUMN last_update; 6 --ALTER TABLE public.phpgw_cc_contact DROP COLUMN category; 7 No newline at end of file -
src/main/java/br/com/prognus/psync/items/dao/PIMContactDAO.java
31 31 32 32 import br.com.prognus.psync.exception.PIMDBAccessException; 33 33 import br.com.prognus.psync.items.model.ContactWrapper; 34 import br.com.prognus.psync.util.Country; 34 35 import br.com.prognus.psync.util.Def; 36 import br.com.prognus.psync.util.TypeAddress; 35 37 38 import com.funambol.common.pim.common.Property; 39 import com.funambol.common.pim.contact.Address; 36 40 import com.funambol.common.pim.contact.BusinessDetail; 37 41 import com.funambol.common.pim.contact.Contact; 38 42 import com.funambol.common.pim.contact.Email; 39 43 import com.funambol.common.pim.contact.Name; 44 import com.funambol.common.pim.contact.Note; 40 45 import com.funambol.common.pim.contact.PersonalDetail; 41 46 import com.funambol.common.pim.contact.Phone; 47 import com.funambol.common.pim.contact.Photo; 42 48 import com.funambol.framework.security.Sync4jPrincipal; 43 49 import com.funambol.framework.server.store.NotFoundException; 44 50 import com.funambol.framework.tools.DBTools; … … 54 60 private static final String SQL_GET_CONTACT_ID_LIST_BY_USER = SQL_GET_CONTACT_ID_LIST 55 61 + "WHERE id_owner = ?"; 56 62 57 private static final String SQL_GET_CONTACT_BY_ID_USER = "SELECT id_contact, id_owner, last_update, last_status, given_names, family_names, birthdate, category FROM phpgw_cc_contact WHERE id_contact = ? AND id_owner = ? LIMIT 1";63 private static final String SQL_GET_CONTACT_BY_ID_USER = "SELECT id_contact, id_owner, last_update, last_status, given_names, family_names, birthdate, category, photo, notes, alias FROM phpgw_cc_contact WHERE id_contact = ? AND id_owner = ? LIMIT 1"; 58 64 59 65 private static final String SQL_GET_CONTACT_ITEM_BY_ID = "SELECT connection_name, connection_value, id_typeof_contact_connection FROM phpgw_cc_connections AS CO LEFT JOIN phpgw_cc_contact_conns AS CC ON CO.id_connection = CC.id_connection WHERE id_contact = ?"; 60 66 … … 72 78 + "OR (family_names = ?)) "; 73 79 74 80 private static final String SQL_INSERT_INTO_FNBL_PIM_CONTACT = "INSERT INTO phpgw_cc_contact " 75 + "(id_contact, id_owner, id_status, given_names, family_names, names_ordered, birthdate, category, last_update, last_status ) "76 + "VALUES " + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) ";81 + "(id_contact, id_owner, id_status, given_names, family_names, names_ordered, birthdate, category, photo, notes, alias, last_update, last_status ) " 82 + "VALUES " + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) "; 77 83 78 84 private static final String SQL_INSERT_INTO_CONTACT_ITEM = "INSERT INTO phpgw_cc_connections " 79 85 + "(id_connection, connection_name, connection_value, connection_is_default) " … … 86 92 private static final String SQL_UPDATE_FNBL_PIM_CONTACT_BEGIN = "UPDATE phpgw_cc_contact SET "; 87 93 88 94 private static final String SQL_UPDATE_FNBL_PIM_CONTACT_END = " WHERE id_contact = ? AND id_owner = ? "; 95 96 97 private static final String SQL_GET_CONTACT_ADDRESS_ITEM = " select ad.complement, ad.address1, ad.postal_code, ca.id_typeof_contact_address, city_name, state_name, cy.id_country from phpgw_cc_addresses AS ad JOIN phpgw_cc_contact_addrs AS ca ON ad.id_address=ca.id_address, phpgw_cc_city AS cy JOIN phpgw_cc_state AS st ON cy.id_state=st.id_state where cy.id_city=ad.id_city and ad.id_address in (select min(ca.id_address) from phpgw_cc_contact AS cc JOIN phpgw_cc_contact_addrs AS ca ON cc.id_contact=ca.id_contact JOIN phpgw_cc_addresses AS ad ON ca.id_address=ad.id_address where cc.id_contact = ? AND cc.id_owner = ? and ca.id_typeof_contact_address = ? )"; 89 98 99 private static final String SQL_GET_STATE_ITEM = "SELECT id_state, id_country, state_name, state_symbol FROM phpgw_cc_state "; 100 101 private static final String SQL_GET_CITY_ITEM = "SELECT id_city, id_state, id_country, city_timezone, city_geo_location, city_name FROM phpgw_cc_city "; 102 103 private static final String SQL_INSERT_STATE = "INSERT INTO phpgw_cc_state(id_state, id_country, state_name, state_symbol) VALUES (?, ?, ?, ?)"; 104 105 private static final String SQL_INSERT_CITY = "INSERT INTO phpgw_cc_city(id_city, id_state, id_country, city_timezone, city_geo_location, city_name) VALUES (?, ?, ?, ?, ?, ?)"; 106 107 private static final String SQL_INSERT_ADDRESS = "INSERT INTO phpgw_cc_addresses(id_address, id_city, id_state, id_country, address1, complement, postal_code, address_is_default) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; 108 109 private static final String SQL_INSERT_CONTACT_ADDRESS = "INSERT INTO phpgw_cc_contact_addrs(id_contact, id_address, id_typeof_contact_address) VALUES (?, ?, ?)"; 110 111 private static final int SQL_ADDRESS_DIM = 60; 112 113 private static final String SQL_ID_MAX_ADDRESS = "select max(id_address) FROM phpgw_cc_addresses"; 114 115 private static final String SQL_ID_MAX_STATE = "select max(id_state) FROM phpgw_cc_state"; 116 117 private static final String SQL_ID_MAX_CITY = "select max(id_city) FROM phpgw_cc_city"; 118 90 119 private static final String SQL_EQUALS_QUESTIONMARK = " = ?"; 91 120 92 121 private static final String SQL_EQUALS_QUESTIONMARK_COMMA = " = ?, "; … … 110 139 protected static final String SQL_FIELD_BIRTHDAY = "birthdate"; 111 140 112 141 protected static final String SQL_FIELD_CATEGORY = "category"; 142 143 protected static final String SQL_FIELD_PHOTO = "photo"; 144 145 protected static final String SQL_FIELD_NOTES = "notes"; 113 146 114 147 protected static final int SQL_FIRSTNAME_DIM = 49; 115 148 … … 120 153 protected static final int SQL_PHONE_DIM = 50; 121 154 122 155 protected static final int SQL_CATEGORY_DIM = 20; 156 157 protected static final int SQL_NICK_DIM = 30; 123 158 124 159 protected static final String TYPE_EMAIL_1_ADDRESS = "Principal"; 125 160 … … 158 193 protected static final String SQL_FIELD_ITEM_VALUE = "connection_value"; 159 194 160 195 protected static final String SQL_FIELD_ITEM_TYPE = "id_typeof_contact_connection"; 196 197 protected static final String SQL_FIELD_TYPEOF = "id_typeof_contact_address"; 198 199 protected static final String SQL_FIELD_COMPLEMENT = "complement"; 200 201 protected static final String SQL_FIELD_ADDRESS1 = "address1"; 202 203 protected static final String SQL_FIELD_POSTALCODE = "postal_code"; 204 205 protected static final String SQL_FIELD_CITY_NAME = "city_name"; 206 207 protected static final String SQL_FIELD_STATE_NAME = "state_name"; 208 209 protected static final String SQL_FIELD_COUNTRY_ID = "id_country"; 210 211 protected static final String SQL_FIELD_STATE_ID = "id_state"; 212 213 protected static final String SQL_FIELD_CITY_ID = "id_city"; 161 214 162 215 private Sync4jPrincipal principal = null; 163 216 … … 192 245 * 193 246 * @see ContactWrapper 194 247 */ 248 @SuppressWarnings("unchecked") 195 249 public void addItem(ContactWrapper cw) throws PIMDBAccessException { 196 250 197 251 log.info("\n\n=> PIMContactDAO addItem begin\n"); … … 222 276 223 277 StringBuffer fullName = null; 224 278 Date anniversary = null; 279 byte[] imagePhoto = null; 280 String note = null; 281 String alias = null; 225 282 226 283 try { 227 284 … … 231 288 Contact c = cw.getContact(); 232 289 Timestamp lastUpdate = cw.getLastUpdate(); 233 290 291 try { 292 List<Note> notes = c.getNotes(); 293 int i = notes.size() - 1; 294 note = notes.get(i).getPropertyValueAsString(); 295 } catch (Exception e) { 296 // log.error(e.getMessage(),e); 297 } 298 if(note==null) { 299 note=""; 300 } 301 234 302 personalDetail = c.getPersonalDetail(); 235 303 businessDetail = c.getBusinessDetail(); 236 304 name = c.getName(); … … 243 311 phones.addAll(personalDetail.getPhones()); 244 312 birthday = personalDetail.getBirthday(); 245 313 category = stringFrom(c.getCategories()); 314 imagePhoto = personalDetail.getPhotoObject().getImage(); 246 315 } 247 316 248 317 if (businessDetail != null) { … … 253 322 if (name != null) { 254 323 firstName = stringFrom(name.getFirstName()); 255 324 lastName = stringFrom(name.getLastName()); 325 alias = stringFrom(name.getNickname()); 256 326 } 257 327 258 328 // consulta o id maximo da tabela contatos … … 296 366 ps.setString(6, fullName.toString()); // names_ordered 297 367 ps.setDate(7, anniversary); // birthdate 298 368 ps.setString(8, truncate(category, SQL_CATEGORY_DIM)); // category 299 ps.setLong(9, lastUpdate.getTime()); // last_update 300 ps.setString(10, String.valueOf(Def.PIM_STATE_NEW)); // last_status 369 ps.setBytes(9, imagePhoto); // photo 370 ps.setString(10, note); // note 371 ps.setString(11, truncate(alias, SQL_NICK_DIM)); // alias 372 ps.setLong(12, lastUpdate.getTime()); // last_update 373 ps.setString(13, String.valueOf(Def.PIM_STATE_NEW)); // last_status 301 374 ps.executeUpdate(); 302 375 303 376 // emails … … 482 555 String birthday = null; 483 556 String category = null; 484 557 Date anniversary = null; 558 byte[] imagePhoto = null; 559 String note = null; 560 String alias = null; 561 Address persoAddress = null; 562 Address businAddress = null; 485 563 486 564 try { 487 565 … … 494 572 495 573 Contact c = cw.getContact(); 496 574 575 try { 576 List<Note> notes = c.getNotes(); 577 int i = notes.size() - 1; 578 note = notes.get(i).getPropertyValueAsString(); 579 } catch (Exception e) { 580 // log.error(e.getMessage(),e); 581 } 582 if(note==null) { 583 note=""; 584 } 585 497 586 personalDetail = c.getPersonalDetail(); 498 587 businessDetail = c.getBusinessDetail(); 499 588 name = c.getName(); … … 504 593 phones.addAll(personalDetail.getPhones()); 505 594 birthday = personalDetail.getBirthday(); 506 595 category = stringFrom(c.getCategories()); 596 imagePhoto = personalDetail.getPhotoObject().getImage(); 597 persoAddress = personalDetail.getAddress(); 507 598 } 508 599 509 600 if (businessDetail != null) { 510 601 emails.addAll(businessDetail.getEmails()); 511 602 phones.addAll(businessDetail.getPhones()); 603 businAddress = businessDetail.getAddress(); 512 604 } 513 605 514 606 if (name != null) { 515 607 firstName = stringFrom(name.getFirstName()); 516 608 lastName = stringFrom(name.getLastName()); 609 alias = stringFrom(name.getNickname()); 517 610 } 518 611 519 612 if (firstName != null && firstName.length() > SQL_FIRSTNAME_DIM) { … … 525 618 if (category != null && category.length() > SQL_CATEGORY_DIM) { 526 619 category = category.substring(0, SQL_CATEGORY_DIM); 527 620 } 621 if (alias != null && alias.length() > SQL_NICK_DIM) { 622 alias = alias.substring(0, SQL_NICK_DIM); 623 } 528 624 529 625 // Formata o nome completo do contato 530 626 fullName = new StringBuffer(); … … 562 658 + SQL_EQUALS_QUESTIONMARK_COMMA); 563 659 queryUpdateFunPimContact.append(SQL_FIELD_CATEGORY 564 660 + SQL_EQUALS_QUESTIONMARK_COMMA); 661 queryUpdateFunPimContact.append(SQL_FIELD_PHOTO 662 + SQL_EQUALS_QUESTIONMARK_COMMA); 663 queryUpdateFunPimContact.append(SQL_FIELD_NOTES 664 + SQL_EQUALS_QUESTIONMARK_COMMA); 665 queryUpdateFunPimContact.append(SQL_FIELD_NICK 666 + SQL_EQUALS_QUESTIONMARK_COMMA); 565 667 queryUpdateFunPimContact 566 668 .append(SQL_FIELD_STATUS + SQL_EQUALS_QUESTIONMARK 567 669 + SQL_UPDATE_FNBL_PIM_CONTACT_END); … … 574 676 ps.setString(4, fullName.toString()); // names_ordered 575 677 ps.setDate(5, anniversary); // birthdate 576 678 ps.setString(6, category); // category 577 ps.setString(7, String.valueOf(Def.PIM_STATE_UPDATED)); // last_status 578 ps.setLong(8, id_contact); // id_contact 579 ps.setLong(9, Long.parseLong(userId)); // id_owner 679 ps.setBytes(7, imagePhoto); // photo 680 ps.setString(8, note); // notes 681 ps.setString(9, alias); // alias 682 ps.setString(10, String.valueOf(Def.PIM_STATE_UPDATED)); // last_status 683 ps.setLong(11, id_contact); // id_contact 684 ps.setLong(12, Long.parseLong(userId)); // id_owner 580 685 ps.executeUpdate(); 581 686 582 687 // emails … … 729 834 } 730 835 } 731 836 837 838 // endereco 839 boolean syncAddress = true; 840 if(syncAddress) { 841 // Apaga os contatos na tabela phpgw_cc_addresses 842 ps = con 843 .prepareStatement("DELETE FROM phpgw_cc_addresses WHERE id_address IN (SELECT id_address FROM phpgw_cc_contact_addrs WHERE id_contact = ?)"); 844 ps.setLong(1, id_contact); 845 ps.executeUpdate(); 846 847 // Apaga os contatos na tabela phpgw_cc_contact_addrs 848 ps = con 849 .prepareStatement("DELETE FROM phpgw_cc_contact_addrs WHERE id_contact = ?"); 850 ps.setLong(1, id_contact); 851 ps.executeUpdate(); 852 853 // Para cadas tipo de endereco RESIDENCIAL | COMERCIAL 854 for (TypeAddress typeAddress: TypeAddress.values()) { 855 856 Address address = null; 857 boolean defaultAddress = false; 858 859 // se ambos sao nulos não faz nada aqui 860 if (persoAddress == null && businAddress == null) { 861 break; 862 } 863 864 if (typeAddress.equals(TypeAddress.RESIDENCIAL)) { 865 address=persoAddress; 866 defaultAddress = true; 867 } else if (typeAddress.equals(TypeAddress.COMERCIAL)) { 868 address=businAddress; 869 } 870 871 // Pula para o proximo 872 if(address == null) { 873 continue; 874 } 875 876 String addressValue = address.getStreet().getPropertyValueAsString(); 877 addressValue = truncate(addressValue, SQL_ADDRESS_DIM); 878 879 if (addressValue != null && addressValue.length() != 0) { 880 881 // Verifica o id maximo da tabela phpgw_cc_addresses 882 ps = con.prepareStatement(SQL_ID_MAX_ADDRESS); 883 rs = ps.executeQuery(); 884 long id_address = 0; 885 886 if (rs.next()) { 887 long id = rs.getInt(1); 888 id_address = (id == 0) ? 1 : ++id; 889 } 890 891 // Insere o endereco do contato na tabela 892 String idCountry; 893 try { 894 idCountry = Country.getCountryByName(address 895 .getCountry().getPropertyValueAsString()).getCode(); 896 } catch (Exception e) { 897 idCountry = "BR"; 898 } 899 long idState = searchStateId(con, address.getState().getPropertyValueAsString(), idCountry); 900 long idCity = searchCityId(con, address.getCity().getPropertyValueAsString(), idState, idCountry); 901 902 ps = con.prepareStatement(SQL_INSERT_ADDRESS); 903 ps.setLong(1, id_address); 904 ps.setLong(2, idCity); 905 ps.setLong(3, idState); 906 ps.setString(4, idCountry); 907 ps.setString(5, addressValue); 908 ps.setString(6, address.getExtendedAddress().getPropertyValueAsString()); 909 ps.setString(7, address.getPostalCode().getPropertyValueAsString()); 910 ps.setBoolean(8, defaultAddress); 911 ps.executeUpdate(); 912 913 ps = con.prepareStatement(SQL_INSERT_CONTACT_ADDRESS); 914 ps.setLong(1, id_contact); 915 ps.setLong(2, id_address); 916 ps.setInt(3, typeAddress.getId()); 917 ps.executeUpdate(); 918 } 919 } 920 } 732 921 con.commit(); 733 922 con.setAutoCommit(true); 734 923 … … 744 933 return Long.toString(id_contact); 745 934 } 746 935 936 937 private long searchStateId(Connection con, String stateName, String idCountry) throws Exception { 938 939 PreparedStatement ps = null; 940 ResultSet rs = null; 941 long stateId = 0; 942 943 try { 944 String SQL_GET_STATE = SQL_GET_STATE_ITEM + " where upper(" 945 + SQL_FIELD_STATE_NAME + ")" + SQL_EQUALS_QUESTIONMARK 946 + " and " + SQL_FIELD_COUNTRY_ID + SQL_EQUALS_QUESTIONMARK; 947 ps = con.prepareStatement(SQL_GET_STATE); 948 ps.setString(1, stateName.toUpperCase()); 949 ps.setString(2, idCountry); 950 rs = ps.executeQuery(); 951 if (rs.next()) { 952 stateId = rs.getInt(SQL_FIELD_STATE_ID); 953 return stateId; 954 } 955 } catch (Exception e) { 956 e.printStackTrace(); 957 } 958 959 ps = con.prepareStatement(SQL_ID_MAX_STATE); 960 rs = ps.executeQuery(); 961 962 if (rs.next()) { 963 long id = rs.getInt(1); 964 stateId = (id == 0) ? 1 : ++id; 965 } 966 else { 967 return 0; 968 } 969 970 ps = con.prepareStatement(SQL_INSERT_STATE); 971 ps.setLong(1, stateId); 972 ps.setString(2, idCountry); 973 ps.setString(3, stateName); 974 ps.setString(4, stateName.substring(0, 2)); 975 if(ps.executeUpdate() > 0) { 976 return stateId; 977 } 978 979 return 0; 980 } 981 982 private long searchCityId(Connection con, String cityName, long idState, String idCountry) throws Exception { 983 984 PreparedStatement ps = null; 985 ResultSet rs = null; 986 long cityId = 0; 987 988 try { 989 String SQL_GET_CITY = SQL_GET_CITY_ITEM + " where " 990 + SQL_FIELD_STATE_ID + SQL_EQUALS_QUESTIONMARK + " and " 991 + SQL_FIELD_COUNTRY_ID + SQL_EQUALS_QUESTIONMARK 992 + " and upper(" + SQL_FIELD_CITY_NAME + ")" 993 + SQL_EQUALS_QUESTIONMARK; 994 ps = con.prepareStatement(SQL_GET_CITY); 995 ps.setLong(1, idState); 996 ps.setString(2, idCountry); 997 ps.setString(3, cityName.toUpperCase()); 998 rs = ps.executeQuery(); 999 if (rs.next()) { 1000 cityId = rs.getInt(SQL_FIELD_CITY_ID); 1001 return cityId; 1002 } 1003 } catch (Exception e) { 1004 e.printStackTrace(); 1005 } 1006 ps = con.prepareStatement(SQL_ID_MAX_CITY); 1007 rs = ps.executeQuery(); 1008 1009 if (rs.next()) { 1010 long id = rs.getInt(1); 1011 cityId = (id == 0) ? 1 : ++id; 1012 } 1013 else { 1014 return 0; 1015 } 1016 1017 ps = con.prepareStatement(SQL_INSERT_CITY); 1018 ps.setLong(1, cityId); 1019 ps.setLong(2, idState); 1020 ps.setString(3, idCountry); 1021 ps.setNull(4, 0); 1022 ps.setString(5, null); 1023 ps.setString(6, cityName); 1024 if(ps.executeUpdate() > 0) { 1025 return cityId; 1026 } 1027 1028 return 0; 1029 } 1030 747 1031 /** 748 1032 * Removes the contact with given UID and sets its last_update field, 749 1033 * provided it has the same userId as this DAO. The deletion is soft … … 953 1237 + "information.\n" + sqle.getMessage(), sqle 954 1238 .getSQLState()); 955 1239 } 1240 1241 try { 1242 ps = con.prepareStatement(SQL_GET_CONTACT_ADDRESS_ITEM); 1243 ps.setLong(1, Long.parseLong(uid)); 1244 ps.setLong(2, Long.parseLong(userId)); 1245 ps.setInt(3, TypeAddress.RESIDENCIAL.getId()); 1246 rs = ps.executeQuery(); 1247 1248 Address address = c.getContact().getPersonalDetail().getAddress(); 1249 addAddress(address, rs); 1250 } catch (Exception e) { 1251 throw new SQLException("\nError while adding personal address information"); 1252 } 956 1253 1254 try { 1255 ps = con.prepareStatement(SQL_GET_CONTACT_ADDRESS_ITEM); 1256 ps.setLong(1, Long.parseLong(uid)); 1257 ps.setLong(2, Long.parseLong(userId)); 1258 ps.setInt(3, TypeAddress.COMERCIAL.getId()); 1259 rs = ps.executeQuery(); 1260 1261 Address address = c.getContact().getBusinessDetail().getAddress(); 1262 addAddress(address, rs); 1263 } catch (Exception e) { 1264 throw new SQLException("\nError while adding business address information"); 1265 } 1266 957 1267 } catch (Exception e) { 958 1268 e.printStackTrace(); 959 1269 throw new PIMDBAccessException("\n=> Error seeking contact.\n", e); … … 963 1273 964 1274 return c; 965 1275 } 1276 1277 1278 private static void addAddress(Address address, ResultSet rs) throws Exception { 1279 1280 ResultSetMetaData rsmd = rs.getMetaData(); 966 1281 1282 String column = null; 1283 int columnCount = 0; 1284 1285 while (rs.next()) { 1286 1287 columnCount = rsmd.getColumnCount(); 1288 1289 if (rs.getString(SQL_FIELD_ADDRESS1)==null || (rs.getString(SQL_FIELD_ADDRESS1)!=null && "".equals(rs.getString(SQL_FIELD_ADDRESS1).trim()))) { 1290 break; 1291 } 1292 1293 for (int i = 1; i <= columnCount; ++i) { 1294 1295 column = rsmd.getColumnName(i); 1296 if (SQL_FIELD_TYPEOF.equalsIgnoreCase(column)) { 1297 continue; 1298 } else if (SQL_FIELD_COMPLEMENT.equalsIgnoreCase(column)) { 1299 address.getExtendedAddress().setPropertyValue(rs.getString(i)); 1300 } else if (SQL_FIELD_ADDRESS1.equalsIgnoreCase(column)) { 1301 address.getStreet().setPropertyValue(rs.getString(i)); 1302 } else if (SQL_FIELD_POSTALCODE.equalsIgnoreCase(column)) { 1303 address.getPostalCode().setPropertyValue(rs.getString(i)); 1304 } else if (SQL_FIELD_CITY_NAME.equalsIgnoreCase(column)) { 1305 address.getCity().setPropertyValue(rs.getString(i)); 1306 } else if (SQL_FIELD_STATE_NAME.equalsIgnoreCase(column)) { 1307 address.getState().setPropertyValue(rs.getString(i)); 1308 } else if (SQL_FIELD_COUNTRY_ID.equalsIgnoreCase(column)) { 1309 Country country = Country.getCountryByCode(rs.getString(i)); 1310 address.getCountry().setPropertyValue(country.getName()); 1311 } else { 1312 throw new SQLException("\n=> Unhandled column: " + column); 1313 } 1314 1315 } 1316 } 1317 } 1318 967 1319 /** 968 1320 * Retrieves the UID list of the contacts considered to be "twins" of a 969 1321 * given contact. … … 1168 1520 c.getPersonalDetail().setBirthday(rs.getString(i)); 1169 1521 } else if (SQL_FIELD_CATEGORY.equalsIgnoreCase(column)) { 1170 1522 c.getCategories().setPropertyValue(rs.getString(i)); 1523 } else if (SQL_FIELD_PHOTO.equalsIgnoreCase(column)) { 1524 if(c.getPersonalDetail()==null) { 1525 PersonalDetail pd = new PersonalDetail(); 1526 c.setPersonalDetail(pd); 1527 } 1528 Photo photo = new Photo(); 1529 photo.setImage(rs.getBytes(i)); 1530 c.getPersonalDetail().setPhotoObject(photo); 1531 } else if (SQL_FIELD_NOTES.equalsIgnoreCase(column)) { 1532 1533 try { 1534 if(rs.getString(i) != null) { 1535 if (c.getNotes() == null 1536 || (c.getNotes() != null && c.getNotes().size() != 1) 1537 || (c.getNotes() != null 1538 && c.getNotes().size() == 1 && c.getNotes() 1539 .get(0) == null)) { 1540 List<Note> l = new ArrayList<Note>(1); 1541 l.add(new Note()); 1542 c.setNotes(l); 1543 } 1544 Note n = (Note) c.getNotes().get(0); 1545 n.setPropertyValue(rs.getString(i)); 1546 n.setPropertyType("Body"); // HARD CODE 1547 } 1548 } catch (Exception e) { 1549 log.error(e.getMessage(),e); 1550 } 1551 1552 } else if (SQL_FIELD_NICK.equalsIgnoreCase(column)) { 1553 c.getName().getNickname().setPropertyValue(rs.getString(i)); 1171 1554 } else { 1172 1555 throw new SQLException("\n=> Unhandled column: " + column); 1173 1556 } -
src/main/java/br/com/prognus/psync/items/dao/PIMEntityDAO.java
22 22 import java.sql.PreparedStatement; 23 23 import java.sql.ResultSet; 24 24 import java.sql.Timestamp; 25 import java.util.Enumeration; 26 import java.util.Iterator; 25 import java.util.Hashtable; 27 26 import java.util.List; 28 27 28 import javax.naming.Context; 29 import javax.naming.NamingEnumeration; 29 30 import javax.naming.NamingException; 31 import javax.naming.directory.DirContext; 32 import javax.naming.directory.InitialDirContext; 33 import javax.naming.directory.SearchControls; 34 import javax.naming.directory.SearchResult; 30 35 import javax.sql.DataSource; 31 36 32 37 import br.com.prognus.psync.exception.PIMDBAccessException; … … 37 42 import com.funambol.framework.logging.FunambolLoggerFactory; 38 43 import com.funambol.framework.tools.DBTools; 39 44 import com.funambol.framework.tools.DataSourceTools; 40 import com.novell.ldap.LDAPAttribute;41 import com.novell.ldap.LDAPAttributeSet;42 import com.novell.ldap.LDAPConnection;43 import com.novell.ldap.LDAPEntry;44 import com.novell.ldap.LDAPException;45 import com.novell.ldap.LDAPSearchResults;46 import com.novell.ldap.util.Base64;47 45 48 46 public abstract class PIMEntityDAO { 49 47 … … 122 120 123 121 // -----------------------------------------------------------Public methods 124 122 123 @SuppressWarnings("unchecked") 125 124 protected String LdapUID(String server, String dc, String user) 126 125 throws Exception { 127 126 128 LDAPConnection ldap = new LDAPConnection(); 129 String searchAttrs[] = { "uidNumber" }; 130 String id_owner = null; 127 String uidNumber=null; 128 129 Hashtable env = new Hashtable(); 130 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 131 env.put(Context.PROVIDER_URL, "ldap://" + server + ":" + 389); 132 133 DirContext ctx = new InitialDirContext(env); 134 // Searching User 135 SearchControls ctls = new SearchControls(); 136 ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 137 ctls.setCountLimit(1); 138 ctls.setTimeLimit(10000); // Espera no max 10 segundos 139 String filter = "(uid=" + user + ")"; 140 141 NamingEnumeration answer = ctx.search(dc, filter, ctls); 142 if (answer.hasMore()) { 143 SearchResult sr = (SearchResult) answer.next(); 144 try { 145 // Getting User attributes 146 uidNumber = sr.getAttributes().get("uidNumber").get(0).toString(); 147 } catch (Exception e) { 148 e.printStackTrace(); 149 } 150 } 151 ctx.close(); 152 if (uidNumber == null) { 153 log.info("Error while connecting and binding to LDAP"); 154 } 131 155 132 try {156 return uidNumber; 133 157 134 ldap.connect(server, 389); 135 LDAPSearchResults searchResults = ldap.search(dc, 136 LDAPConnection.SCOPE_SUB, "(uid=" + user + ")", 137 searchAttrs, false); 138 139 while (searchResults.hasMore()) { 140 141 LDAPEntry nextEntry = null; 142 try { 143 nextEntry = searchResults.next(); 144 } catch (LDAPException e) { 145 if (e.getResultCode() == LDAPException.LDAP_TIMEOUT 146 || e.getResultCode() == LDAPException.CONNECT_ERROR) 147 break; 148 else 149 continue; 150 } 151 LDAPAttributeSet attributeSet = nextEntry.getAttributeSet(); 152 Iterator allAttributes = attributeSet.iterator(); 153 154 while (allAttributes.hasNext()) { 155 156 LDAPAttribute attribute = (LDAPAttribute) allAttributes 157 .next(); 158 String attributeName = attribute.getName(); 159 Enumeration allValues = attribute.getStringValues(); 160 161 if (allValues != null) { 162 163 while (allValues.hasMoreElements()) { 164 165 String Value = (String) allValues.nextElement(); 166 167 if (Base64.isLDIFSafe(Value)) { 168 } else { 169 Value = Base64.encode(Value.getBytes()); 170 } 171 172 if (attributeName.equalsIgnoreCase("uidNumber")) { 173 id_owner = Value; 174 break; 175 } 176 177 } 178 } 179 } 180 } 181 182 ldap.disconnect(); 183 184 if (id_owner == null) { 185 throw new LDAPException(); 186 } 187 188 } catch (LDAPException e) { 189 log.info("Error while connecting and binding to LDAP: " 190 + e.toString()); 191 } 192 193 return id_owner; 158 159 // LDAPConnection ldap = new LDAPConnection(); 160 // String searchAttrs[] = { "uidNumber" }; 161 // String id_owner = null; 162 // 163 // try { 164 // 165 // ldap.connect(server, 389); 166 // LDAPSearchResults searchResults = ldap.search(dc, 167 // LDAPConnection.SCOPE_SUB, "(uid=" + user + ")", 168 // searchAttrs, false); 169 // 170 // while (searchResults.hasMore()) { 171 // 172 // LDAPEntry nextEntry = null; 173 // try { 174 // nextEntry = searchResults.next(); 175 // } catch (LDAPException e) { 176 // if (e.getResultCode() == LDAPException.LDAP_TIMEOUT 177 // || e.getResultCode() == LDAPException.CONNECT_ERROR) 178 // break; 179 // else 180 // continue; 181 // } 182 // LDAPAttributeSet attributeSet = nextEntry.getAttributeSet(); 183 // Iterator allAttributes = attributeSet.iterator(); 184 // 185 // while (allAttributes.hasNext()) { 186 // 187 // LDAPAttribute attribute = (LDAPAttribute) allAttributes 188 // .next(); 189 // String attributeName = attribute.getName(); 190 // Enumeration allValues = attribute.getStringValues(); 191 // 192 // if (allValues != null) { 193 // 194 // while (allValues.hasMoreElements()) { 195 // 196 // String Value = (String) allValues.nextElement(); 197 // 198 // if (Base64.isLDIFSafe(Value)) { 199 // } else { 200 // Value = Base64.encode(Value.getBytes()); 201 // } 202 // 203 // if (attributeName.equalsIgnoreCase("uidNumber")) { 204 // id_owner = Value; 205 // break; 206 // } 207 // 208 // } 209 // } 210 // } 211 // } 212 // 213 // ldap.disconnect(); 214 // 215 // if (id_owner == null) { 216 // throw new LDAPException(); 217 // } 218 // 219 // } catch (LDAPException e) { 220 // log.info("Error while connecting and binding to LDAP: " 221 // + e.toString()); 222 // } 223 // return id_owner; 194 224 } 195 225 196 226 /** -
src/main/java/br/com/prognus/psync/engine/source/PIMCalendarSyncSource.java
76 76 try { 77 77 this.manager = new PIMCalendarManager(JNDI_DATA_SOURCE_NAME, context.getPrincipal(), context.getSourceQuery()); 78 78 } catch (Exception e) { 79 // TODO Auto-generated catch block80 79 e.printStackTrace(); 81 80 } 82 81 super.manager = this.manager; … … 452 451 453 452 private String hackFix(String text) { 454 453 455 String result, head, title, clas, description, tail, new_title; 456 int p_categories, p_classes, p_location, p_dstart, values1, values2, op1, op2, op3, op4, op5, op6, op7, op8, qt1, qt2; 457 458 qt1 = 0; 459 qt2 = 0; 460 461 // Pega as posicoes das tags 462 p_categories = text.indexOf("CATEGORIES"); 463 p_classes = text.indexOf("CLASS"); 464 465 p_location = text.indexOf("LOCATION"); 466 p_dstart = text.indexOf("DTSTART"); 467 468 // Pega as posicoes das varias formas da tag SUMMARY 469 op1 = text.indexOf("SUMMARY:"); 470 op2 = text.indexOf("SUMMARY;CHARSET=UTF-8:"); 471 op3 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE:"); 472 op4 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 473 474 // Pega as posicoes das varias formas da tag DESCRIPTION 475 op5 = text.indexOf("DESCRIPTION:"); 476 op6 = text.indexOf("DESCRIPTION;CHARSET=UTF-8:"); 477 op7 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:"); 478 op8 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 479 480 qt1 += (op1 == -1) ? 0 : op1 + 8; 481 qt1 += (op2 == -1) ? 0 : op2 + 22; 482 qt1 += (op3 == -1) ? 0 : op3 + 34; 483 qt1 += (op4 == -1) ? 0 : op4 + 48; 484 485 qt2 += (op5 == -1) ? 0 : op5 + 12; 486 qt2 += (op6 == -1) ? 0 : op6 + 26; 487 qt2 += (op7 == -1) ? 0 : op7 + 38; 488 qt2 += (op8 == -1) ? 0 : op8 + 52; 489 490 values1 = (p_categories == -1) ? p_classes : p_categories; 491 values2 = (p_location == -1) ? p_dstart : p_location; 492 493 head = text.substring(0, qt1); 494 title = text.substring(qt1, values1); 495 clas = text.substring(values1, qt2); 496 description = text.substring(qt2, values2); 497 tail = text.substring(values2); 498 499 new_title = title.replaceAll("\r\n ", " "); 500 501 // Retira os enters no final do titulo 502 int title_t, title_bl; 503 504 while(true){ 505 title_t = new_title.length(); 506 title_bl = new_title.lastIndexOf("=0D=0A=\r\n"); 507 508 if(title_bl != -1 && title_t == title_bl + 11){ 509 new_title = new_title.substring(0, title_bl) + "\r\n"; 510 } else { 511 break; 454 try { 455 456 String result, head, title, clas, description, tail, new_title; 457 int p_categories, p_classes, p_location, p_dstart, values1, values2, op1, op2, op3, op4, op5, op6, op7, op8, qt1, qt2; 458 459 qt1 = 0; 460 qt2 = 0; 461 462 // Pega as posicoes das tags 463 p_categories = text.indexOf("CATEGORIES"); 464 p_classes = text.indexOf("CLASS"); 465 466 p_location = text.indexOf("LOCATION"); 467 p_dstart = text.indexOf("DTSTART"); 468 469 // Pega as posicoes das varias formas da tag SUMMARY 470 op1 = text.indexOf("SUMMARY:"); 471 op2 = text.indexOf("SUMMARY;CHARSET=UTF-8:"); 472 op3 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE:"); 473 op4 = text.indexOf("SUMMARY;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 474 475 // Pega as posicoes das varias formas da tag DESCRIPTION 476 op5 = text.indexOf("DESCRIPTION:"); 477 op6 = text.indexOf("DESCRIPTION;CHARSET=UTF-8:"); 478 op7 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE:"); 479 op8 = text.indexOf("DESCRIPTION;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:"); 480 481 qt1 += (op1 == -1) ? 0 : op1 + 8; 482 qt1 += (op2 == -1) ? 0 : op2 + 22; 483 qt1 += (op3 == -1) ? 0 : op3 + 34; 484 qt1 += (op4 == -1) ? 0 : op4 + 48; 485 486 qt2 += (op5 == -1) ? 0 : op5 + 12; 487 qt2 += (op6 == -1) ? 0 : op6 + 26; 488 qt2 += (op7 == -1) ? 0 : op7 + 38; 489 qt2 += (op8 == -1) ? 0 : op8 + 52; 490 491 values1 = (p_categories == -1) ? p_classes : p_categories; 492 values2 = (p_location == -1) ? p_dstart : p_location; 493 494 head = text.substring(0, qt1); 495 title = text.substring(qt1, values1); 496 clas = text.substring(values1, qt2); 497 description = text.substring(qt2, values2); 498 tail = text.substring(values2); 499 500 new_title = title.replaceAll("\r\n ", " "); 501 502 // Retira os enters no final do titulo 503 int title_t, title_bl; 504 505 while(true){ 506 title_t = new_title.length(); 507 title_bl = new_title.lastIndexOf("=0D=0A=\r\n"); 508 509 if(title_bl != -1 && title_t == title_bl + 11){ 510 new_title = new_title.substring(0, title_bl) + "\r\n"; 511 } else { 512 break; 513 } 512 514 } 513 } 515 516 // Retira os enters no final da descricao 517 int description_t, description_bl; 518 519 while(true){ 520 description_t = description.length(); 521 description_bl = description.lastIndexOf("=0D=0A=\r\n"); 522 523 if(description_bl != -1 && description_t == description_bl + 11){ 524 description = description.substring(0, description_bl) + "\r\n"; 525 } else { 526 break; 527 } 528 } 529 530 result = head + new_title + clas + description + tail; 514 531 515 // Retira os enters no final da descricao 516 int description_t, description_bl; 517 518 while(true){ 519 description_t = description.length(); 520 description_bl = description.lastIndexOf("=0D=0A=\r\n"); 521 522 if(description_bl != -1 && description_t == description_bl + 11){ 523 description = description.substring(0, description_bl) + "\r\n"; 524 } else { 525 break; 532 return result; 533 }catch (Exception e) { 534 if (log.isTraceEnabled()) { 535 log.trace(e); 526 536 } 537 return text; 527 538 } 528 529 result = head + new_title + clas + description + tail;530 531 return result;532 539 } 533 540 534 541 private Calendar webCalendar2Calendar(String text, String vCalType) -
src/main/java/br/com/prognus/psync/util/Country.java
1 package br.com.prognus.psync.util; 2 3 /** 4 * From phpgwapi/inc/class.country.inc.php 5 * From http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html 6 * 7 */ 8 public enum Country { 9 10 AF ("AFGHANISTAN"), 11 AL ("ALBANIA"), 12 DZ ("ALGERIA"), 13 AS ("AMERICAN SAMOA"), 14 AD ("ANDORRA"), 15 AO ("ANGOLA"), 16 AI ("ANGUILLA"), 17 AQ ("ANTARCTICA"), 18 AG ("ANTIGUA AND BARBUDA"), 19 AR ("ARGENTINA"), 20 AM ("ARMENIA"), 21 AW ("ARUBA"), 22 AU ("AUSTRALIA"), 23 AT ("AUSTRIA"), 24 AZ ("AZERBAIJAN"), 25 BS ("BAHAMAS"), 26 BH ("BAHRAIN"), 27 BD ("BANGLADESH"), 28 BB ("BARBADOS"), 29 BY ("BELARUS"), 30 BE ("BELGIUM"), 31 BZ ("BELIZE"), 32 BJ ("BENIN"), 33 BM ("BERMUDA"), 34 BT ("BHUTAN"), 35 BO ("BOLIVIA"), 36 BA ("BOSNIA AND HERZEGOVINA"), 37 BW ("BOTSWANA"), 38 BV ("BOUVET ISLAND"), 39 BR ("BRAZIL"), 40 IO ("BRITISH INDIAN OCEAN TERRITORY"), 41 BN ("BRUNEI DARUSSALAM"), 42 BG ("BULGARIA"), 43 BF ("BURKINA FASO"), 44 BI ("BURUNDI"), 45 KH ("CAMBODIA"), 46 CM ("CAMEROON"), 47 CA ("CANADA"), 48 CV ("CAPE VERDE"), 49 KY ("CAYMAN ISLANDS"), 50 CF ("CENTRAL AFRICAN REPUBLIC"), 51 TD ("CHAD"), 52 CL ("CHILE"), 53 CN ("CHINA"), 54 CX ("CHRISTMAS ISLAND"), 55 CC ("COCOS (KEELING) ISLANDS"), 56 CO ("COLOMBIA"), 57 KM ("COMOROS"), 58 CG ("CONGO"), 59 CD ("CONGO, THE DEMOCRATIC REPUBLIC OF THE"), 60 CK ("COOK ISLANDS"), 61 CR ("COSTA RICA"), 62 CI ("COTE D IVOIRE"), 63 HR ("CROATIA"), 64 CU ("CUBA"), 65 CY ("CYPRUS"), 66 CZ ("CZECH REPUBLIC"), 67 DK ("DENMARK"), 68 DJ ("DJIBOUTI"), 69 DM ("DOMINICA"), 70 DO ("DOMINICAN REPUBLIC"), 71 TP ("EAST TIMOR"), 72 EC ("ECUADOR"), 73 EG ("EGYPT"), 74 SV ("EL SALVADOR"), 75 GQ ("EQUATORIAL GUINEA"), 76 ER ("ERITREA"), 77 EE ("ESTONIA"), 78 ET ("ETHIOPIA"), 79 FK ("FALKLAND ISLANDS (MALVINAS)"), 80 FO ("FAROE ISLANDS"), 81 FJ ("FIJI"), 82 FI ("FINLAND"), 83 FR ("FRANCE"), 84 GF ("FRENCH GUIANA"), 85 PF ("FRENCH POLYNESIA"), 86 TF ("FRENCH SOUTHERN TERRITORIES"), 87 GA ("GABON"), 88 GM ("GAMBIA"), 89 GE ("GEORGIA"), 90 DE ("GERMANY"), 91 GH ("GHANA"), 92 GI ("GIBRALTAR"), 93 GR ("GREECE"), 94 GL ("GREENLAND"), 95 GD ("GRENADA"), 96 GP ("GUADELOUPE"), 97 GU ("GUAM"), 98 GT ("GUATEMALA"), 99 GN ("GUINEA"), 100 GW ("GUINEA-BISSAU"), 101 GY ("GUYANA"), 102 HT ("HAITI"), 103 HM ("HEARD ISLAND AND MCDONALD ISLANDS"), 104 VA ("HOLY SEE (VATICAN CITY STATE)"), 105 HN ("HONDURAS"), 106 HK ("HONG KONG"), 107 HU ("HUNGARY"), 108 IS ("ICELAND"), 109 IN ("INDIA"), 110 ID ("INDONESIA"), 111 IR ("IRAN, ISLAMIC REPUBLIC OF"), 112 IQ ("IRAQ"), 113 IE ("IRELAND"), 114 IL ("ISRAEL"), 115 IT ("ITALY"), 116 JM ("JAMAICA"), 117 JP ("JAPAN"), 118 JO ("JORDAN"), 119 KZ ("KAZAKSTAN"), 120 KE ("KENYA"), 121 KI ("KIRIBATI"), 122 KP ("KOREA DEMOCRATIC PEOPLES REPUBLIC OF"), 123 KR ("KOREA REPUBLIC OF"), 124 KW ("KUWAIT"), 125 KG ("KYRGYZSTAN"), 126 LA ("LAO PEOPLES DEMOCRATIC REPUBLIC"), 127 LV ("LATVIA"), 128 LB ("LEBANON"), 129 LS ("LESOTHO"), 130 LR ("LIBERIA"), 131 LY ("LIBYAN ARAB JAMAHIRIYA"), 132 LI ("LIECHTENSTEIN"), 133 LT ("LITHUANIA"), 134 LU ("LUXEMBOURG"), 135 MO ("MACAU"), 136 MK ("MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF"), 137 MG ("MADAGASCAR"), 138 MW ("MALAWI"), 139 MY ("MALAYSIA"), 140 MV ("MALDIVES"), 141 ML ("MALI"), 142 MT ("MALTA"), 143 MH ("MARSHALL ISLANDS"), 144 MQ ("MARTINIQUE"), 145 MR ("MAURITANIA"), 146 MU ("MAURITIUS"), 147 YT ("MAYOTTE"), 148 MX ("MEXICO"), 149 FM ("MICRONESIA, FEDERATED STATES OF"), 150 MD ("MOLDOVA, REPUBLIC OF"), 151 MC ("MONACO"), 152 MN ("MONGOLIA"), 153 MS ("MONTSERRAT"), 154 MA ("MOROCCO"), 155 MZ ("MOZAMBIQUE"), 156 MM ("MYANMAR"), 157 NA ("NAMIBIA"), 158 NR ("NAURU"), 159 NP ("NEPAL"), 160 NL ("NETHERLANDS"), 161 AN ("NETHERLANDS ANTILLES"), 162 NC ("NEW CALEDONIA"), 163 NZ ("NEW ZEALAND"), 164 NI ("NICARAGUA"), 165 NE ("NIGER"), 166 NG ("NIGERIA"), 167 NU ("NIUE"), 168 NF ("NORFOLK ISLAND"), 169 MP ("NORTHERN MARIANA ISLANDS"), 170 NO ("NORWAY"), 171 OM ("OMAN"), 172 PK ("PAKISTAN"), 173 PW ("PALAU"), 174 PS ("PALESTINIAN TERRITORY, OCCUPIED"), 175 PA ("PANAMA"), 176 PG ("PAPUA NEW GUINEA"), 177 PY ("PARAGUAY"), 178 PE ("PERU"), 179 PH ("PHILIPPINES"), 180 PN ("PITCAIRN"), 181 PL ("POLAND"), 182 PT ("PORTUGAL"), 183 PR ("PUERTO RICO"), 184 QA ("QATAR"), 185 RE ("REUNION"), 186 RO ("ROMANIA"), 187 RU ("RUSSIAN FEDERATION"), 188 RW ("RWANDA"), 189 SH ("SAINT HELENA"), 190 KN ("SAINT KITTS AND NEVIS"), 191 LC ("SAINT LUCIA"), 192 PM ("SAINT PIERRE AND MIQUELON"), 193 VC ("SAINT VINCENT AND THE GRENADINES"), 194 WS ("SAMOA"), 195 SM ("SAN MARINO"), 196 ST ("SAO TOME AND PRINCIPE"), 197 SA ("SAUDI ARABIA"), 198 SN ("SENEGAL"), 199 SC ("SEYCHELLES"), 200 SL ("SIERRA LEONE"), 201 SG ("SINGAPORE"), 202 SK ("SLOVAKIA"), 203 SI ("SLOVENIA"), 204 SB ("SOLOMON ISLANDS"), 205 SO ("SOMALIA"), 206 ZA ("SOUTH AFRICA"), 207 GS ("SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS"), 208 ES ("SPAIN"), 209 LK ("SRI LANKA"), 210 SD ("SUDAN"), 211 SR ("SURINAME"), 212 SJ ("SVALBARD AND JAN MAYEN"), 213 SZ ("SWAZILAND"), 214 SE ("SWEDEN"), 215 CH ("SWITZERLAND"), 216 SY ("SYRIAN ARAB REPUBLIC"), 217 TW ("TAIWAN"), 218 TJ ("TAJIKISTAN"), 219 TZ ("TANZANIA, UNITED REPUBLIC OF"), 220 TH ("THAILAND"), 221 TG ("TOGO"), 222 TK ("TOKELAU"), 223 TO ("TONGA"), 224 TT ("TRINIDAD AND TOBAGO"), 225 TN ("TUNISIA"), 226 TR ("TURKEY"), 227 TM ("TURKMENISTAN"), 228 TC ("TURKS AND CAICOS ISLANDS"), 229 TV ("TUVALU"), 230 UG ("UGANDA"), 231 UA ("UKRAINE"), 232 AE ("UNITED ARAB EMIRATES"), 233 GB ("UNITED KINGDOM"), 234 US ("UNITED STATES"), 235 UM ("UNITED STATES MINOR OUTLYING ISLANDS"), 236 UY ("URUGUAY"), 237 UZ ("UZBEKISTAN"), 238 VU ("VANUATU"), 239 VE ("VENEZUELA"), 240 VN ("VIET NAM"), 241 VG ("VIRGIN ISLANDS, BRITISH"), 242 VI ("VIRGIN ISLANDS, U.S."), 243 WF ("WALLIS AND FUTUNA"), 244 EH ("WESTERN SAHARA"), 245 YE ("YEMEN"), 246 YU ("YUGOSLAVIA"), 247 ZM ("ZAMBIA"), 248 ZW ("ZIMBABWE"); 249 250 private String nameCountry; 251 private String codeCountry; 252 253 Country(String nameCountry) { 254 this.nameCountry=nameCountry; 255 this.codeCountry=this.name(); 256 } 257 258 public String getCode() { 259 return this.codeCountry; 260 } 261 262 public String getName() { 263 return this.nameCountry; 264 } 265 266 public static Country getCountryByCode(String code){ 267 return Country.valueOf(code); 268 } 269 270 public static Country getCountryByName(String name){ 271 for (Country country : Country.values()) { 272 if(country.getName().equalsIgnoreCase(name)) { 273 return country; 274 } 275 } 276 return Country.BR; 277 } 278 279 } -
src/main/java/br/com/prognus/psync/util/TypeAddress.java
1 package br.com.prognus.psync.util; 2 3 /** 4 * phpgw_cc_typeof_ct_addrs 5 * 6 */ 7 public enum TypeAddress { 8 9 RESIDENCIAL(1), 10 COMERCIAL(2); 11 12 int id; 13 14 TypeAddress(int num) { 15 this.id = num; 16 } 17 18 public int getId() { 19 return id; 20 } 21 22 } -
src/main/java/com/funambol/server/security/LdapUserProvisioningOfficer.java
1 /******************************************************************************* 2 * Author: Emerson faria Nobre - emerson-faria.nobre@serpro.gov.br - january/09 3 * Organization: SERPRO - Servico Federal de Processamento de Dados 4 * Description: This source code is an extension of UserProvisioningOfficer.java 5 * New capabilities implemented: 6 * - Authenticate user in Ldap. 7 * - Automatically Create/Update the MailServerAccount 8 * (tables: fnbl_email_account, fnbl_email_enable_account, 9 * fnbl_email_push_registry) 10 * Changes: 11 * Author/Date/Description: 12 * Emerson Faria Nobre - june/2009 - Inserted parameters UserFieldName and 13 * PwdFieldName because the LDAP Server of each Company that I 14 * need to install Funambol use diferent names for this fields. 15 * 16 * Changes: 17 * Autor/Description/Date: 18 * Lucas da Costa Silva / Sync with notes, photo, address, and the possibility 19 * to login with any uid from the overlay and it will use 20 * only the uid from the ldap not to add multiple principal 21 * to the same person that use different user in the same 22 * device. 23 * / Sep to Oct-2009 24 * 25 ******************************************************************************* 26 * 27 * 28 * 29 * Funambol is a mobile platform developed by Funambol, Inc. 30 * Copyright (C) 2006 - 2007 Funambol, Inc. 31 * 32 * This program is free software; you can redistribute it and/or modify it under 33 * the terms of the GNU Affero General Public License version 3 as published by 34 * the Free Software Foundation with the addition of the following permission 35 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED 36 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 37 * WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS. 38 * 39 * This program is distributed in the hope that it will be useful, but WITHOUT 40 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 41 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 42 * details. 43 * 44 * You should have received a copy of the GNU Affero General Public License 45 * along with this program; if not, see http://www.gnu.org/licenses or write to 46 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 47 * MA 02110-1301 USA. 48 * 49 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 50 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com. 51 * 52 * The interactive user interfaces in modified source and object code versions 53 * of this program must display Appropriate Legal Notices, as required under 54 * Section 5 of the GNU Affero General Public License version 3. 55 * 56 * In accordance with Section 7(b) of the GNU Affero General Public License 57 * version 3, these Appropriate Legal Notices must retain the display of the 58 * "Powered by Funambol" logo. If the display of the logo is not reasonably 59 * feasible for technical reasons, the Appropriate Legal Notices must display 60 * the words "Powered by Funambol". 61 */ 62 package com.funambol.server.security; 63 64 import java.util.Hashtable; 65 import java.util.List; 66 67 import javax.naming.Context; 68 import javax.naming.NamingEnumeration; 69 import javax.naming.NamingException; 70 import javax.naming.directory.DirContext; 71 import javax.naming.directory.InitialDirContext; 72 import javax.naming.directory.SearchControls; 73 import javax.naming.directory.SearchResult; 74 75 import sun.util.logging.resources.logging; 76 77 import com.funambol.email.console.dao.ConsoleDAO; 78 import com.funambol.email.exception.DBAccessException; 79 import com.funambol.email.exception.InboxListenerConfigException; 80 import com.funambol.email.model.MailServer; 81 import com.funambol.email.model.MailServerAccount; 82 import com.funambol.email.util.Def; 83 import com.funambol.framework.core.Authentication; 84 import com.funambol.framework.core.Cred; 85 import com.funambol.framework.filter.WhereClause; 86 import com.funambol.framework.security.Sync4jPrincipal; 87 import com.funambol.framework.server.Sync4jUser; 88 import com.funambol.framework.server.store.NotFoundException; 89 import com.funambol.framework.server.store.PersistentStoreException; 90 import com.funambol.framework.tools.Base64; 91 import com.funambol.framework.tools.beans.LazyInitBean; 92 import com.funambol.pushlistener.service.registry.RegistryEntryStatus; 93 import com.funambol.server.admin.AdminException; 94 import com.funambol.server.admin.UserManager; 95 import com.funambol.server.config.Configuration; 96 97 98 /** 99 * This is an implementation of the <i>Officier</i> interface. It provides 100 * the user provisioning so if an user is not in the database he will be added. 101 * It requires basic authentication 102 * 103 * @version $Id: UserProvisioningOfficer.java,v 1.4 2008-06-24 12:50:06 piter_may Exp $ 104 */ 105 public class LdapUserProvisioningOfficer 106 extends DBOfficer 107 implements LazyInitBean { 108 109 private static final long serialVersionUID = 1978810349147209602L; 110 111 Ldap objLdap; 112 MailServerAccount msa; 113 Boolean InsertMSA; 114 String ldapIP; 115 String ldapPort; 116 String ldapStartSearchPath; 117 118 boolean MsaEnablePush; 119 boolean MsaEnablePolling; 120 int MsaRefreshTime; 121 int MsaMaxEmailNumber; 122 int MsaMaxImapEmails; 123 124 // ------------------------------------------------------------ Constructors 125 public LdapUserProvisioningOfficer() { 126 super(); 127 msa = new MailServerAccount(); 128 } 129 130 // ---------------------------------------------------------- Public methods 131 public void init() { 132 super.init(); 133 } 134 135 /** 136 * Authenticates a credential. 137 * 138 * @param credential the credential to be authenticated 139 * 140 * @return the Sync4jUser if the credential is autenticated, null otherwise 141 */ 142 public Sync4jUser authenticateUser(Cred credential) { 143 144 if (log.isTraceEnabled()) { 145 StringBuffer sb = new StringBuffer("##########"); 146 sb.append(credential.getAuthentication().getPassword()); 147 sb.append("::"); 148 sb.append(credential.getAuthentication().getData()); 149 sb.append("::"); 150 sb.append(credential.getAuthentication().getUsername()); 151 sb.append("::"); 152 sb.append(credential.getUsername()); 153 sb.append("::"); 154 sb.append(credential.getData()); 155 sb.append("::"); 156 sb.append(credential.getFormat()); 157 sb.append("::"); 158 sb.append(credential.getType()); 159 sb.append("##############"); 160 log.trace(sb.toString()); 161 } 162 163 164 Configuration config = Configuration.getConfiguration(); 165 ps = config.getStore(); 166 167 userManager = (UserManager) config.getUserManager(); 168 169 String type = credential.getType(); 170 171 if ((Cred.AUTH_TYPE_BASIC).equals(type)) { 172 return authenticateBasicCredential(credential,"simple"); 173 174 } else if ((Cred.AUTH_TYPE_MD5).equals(type)) { 175 return authenticateMD5Credential(credential); 176 177 } 178 return null; 179 } 180 181 protected Sync4jUser getUser(String userName, String password) { 182 183 try { 184 objLdap = new Ldap(this.getLdapIP(), this.getLdapPort(), userName, 185 "empty", this.getLdapStartSearchPath(), "nothing is so simple"); 186 187 return super.getUser(objLdap.getUid(),password); 188 189 } catch (Exception e) { 190 return null; 191 } 192 193 } 194 195 /** 196 * Gets the supported authentication type 197 * 198 * @return the basic authentication type 199 */ 200 public String getClientAuth() { 201 return Cred.AUTH_TYPE_BASIC; 202 } 203 204 // ------------------------------------------------------- Protected Methods 205 206 // Insert/Update MailServerAccount (MSA) 207 protected void InsertUpdateMSA(String user, String pwd) { 208 209 if (log.isTraceEnabled()) { 210 log.trace("LdapUserProvisioningOfficer - I will create cdao object"); 211 } 212 try { 213 ConsoleDAO cdao = new ConsoleDAO(); 214 215 if (log.isTraceEnabled()) { 216 log.trace("LdapUserProvisioningOfficer - I will verify if it is insert or update"); 217 } 218 msa = null; 219 220 if (log.isTraceEnabled()) { 221 log.trace("LdapUserProvisioningOfficer - before command cdao.getUser(user)"); 222 } 223 224 List<MailServerAccount> accounts = cdao.getUserAccounts(user); 225 if(accounts!=null && accounts.size()>0) { 226 for (MailServerAccount mailServerAccount : accounts) { 227 if(mailServerAccount.getMailServer().getDescription().equals("expresso")) { 228 msa = accounts.get(0); 229 } 230 } 231 } 232 233 if (log.isTraceEnabled()) { 234 log.trace("LdapUserProvisioningOfficer - after command cdao.getUser(user)"); 235 } 236 237 if (msa == null) { 238 msa = new MailServerAccount(); 239 this.InsertMSA = true; 240 } else { 241 this.InsertMSA = false; 242 } 243 244 if (log.isTraceEnabled()) { 245 log.trace("LdapUserProvisioningOfficer - after if (msa == null)"); 246 log.trace("LdapUserProvisioningOfficer - msa.getUsername() = " + msa.getUsername()); 247 log.trace("LdapUserProvisioningOfficer - It is insert: " + this.InsertMSA + "- username = " + msa.getUsername()); 248 } 249 250 if (!this.InsertMSA) { 251 if (log.isTraceEnabled()) { 252 log.trace("LdapUserProvisioningOfficer - The key Id = " + msa.getId()); 253 log.trace("LdapUserProvisioningOfficer - The key UserName = " + msa.getUsername()); 254 } 255 } 256 if (this.InsertMSA) { 257 if (log.isTraceEnabled()) { 258 log.trace("LdapUserProvisioningOfficer - I will set setUserName" + user); 259 } 260 msa.setUsername(user); 261 } 262 msa.setMsLogin(user); 263 msa.setMsPassword(pwd); 264 msa.setMsAddress(objLdap.getmail()); 265 msa.setPush(this.getMsaEnablePush()); 266 msa.setMaxEmailNumber(this.getMsaMaxEmailNumber()); 267 msa.setMaxImapEmail(this.getMsaMaxImapEmails()); 268 msa.setPeriod(this.getMsaRefreshTime()); 269 msa.setActive(this.getMsaEnablePolling()); 270 msa.setTaskBeanFile(Def.DEFAULT_INBOX_LISTENER_BEAN_FILE); 271 msa.setLastUpdate(System.currentTimeMillis()); 272 273 if (this.InsertMSA) { 274 msa.setStatus(RegistryEntryStatus.NEW); 275 } else { 276 msa.setStatus(RegistryEntryStatus.UPDATED); 277 } 278 279 if (log.isTraceEnabled()) { 280 log.trace("LdapUserProvisioningOfficer - I already set setMsLogin" + user); 281 log.trace("LdapUserProvisioningOfficer - I will set setMsPassword"); 282 log.trace("LdapUserProvisioningOfficer - I will set setMsAddress = " + objLdap.getmail()); 283 log.trace("LdapUserProvisioningOfficer - I will set setPush = " + this.getMsaEnablePush()); 284 log.trace("LdapUserProvisioningOfficer - I will set setMaxEmailNumber = " + this.getMsaMaxEmailNumber()); 285 log.trace("LdapUserProvisioningOfficer - I will set setMaxImapEmail = " + this.getMsaMaxImapEmails()); 286 log.trace("LdapUserProvisioningOfficer - I will set setPeriod = " + this.getMsaRefreshTime()); 287 log.trace("LdapUserProvisioningOfficer - I will set setActive = " + this.getMsaEnablePolling()); 288 log.trace("LdapUserProvisioningOfficer - I will set fnbl_email_push_registry"); 289 } 290 291 String[] param = {"description"}; 292 String[] value = {"expresso"}; 293 String[] operator = {WhereClause.OPT_EQ}; 294 295 MailServer[] ms = cdao.getPubMailServers(new WhereClause(param[0], new String[]{value[0]}, operator[0], false)); 296 297 if (log.isTraceEnabled()) { 298 log.trace("LdapUserProvisioningOfficer - I found the MailServer expresso - ID = " + ms[0].getMailServerId()); 299 } 300 msa.setMailServer(cdao.getPubMailServer(ms[0].getMailServerId())); 301 302 if (this.InsertMSA) { 303 int ret = cdao.insertUserAccount(msa); 304 if (log.isTraceEnabled()) { 305 log.trace("LdapUserProvisioningOfficer - return of insertUser(msa): " + ret); 306 } 307 } else { 308 int ret = cdao.updateUserAccount(msa); 309 if (log.isTraceEnabled()) { 310 log.trace("LdapUserProvisioningOfficer - return of updateUser(msa): " + ret); 311 } 312 } 313 } catch (InboxListenerConfigException e) { 314 log.error("LdapUserProvisioningOfficer - Error creating DAO layer to Insert/Update Mail User: ", e); 315 } catch (DBAccessException e) { 316 log.error("LdapUserProvisioningOfficer - Error accessing Database to Insert/Update Mail User: ", e); 317 } 318 } 319 320 /** 321 * Checks the given credential. If the user or the principal isn't found, 322 * they are created. 323 * 324 * @param credential the credential to check 325 * 326 * @return the Sync4jUser if the credential is autenticated, null otherwise 327 */ 328 protected Sync4jUser authenticateBasicCredential(Cred credential, String authType) { 329 String username = null, password = null; 330 331 Authentication auth = credential.getAuthentication(); 332 String deviceId = auth.getDeviceId(); 333 334 String userpwd = new String(Base64.decode(auth.getData())); 335 336 int p = userpwd.indexOf(':'); 337 338 if (p == -1) { 339 username = userpwd; 340 password = ""; 341 } else { 342 username = (p > 0) ? userpwd.substring(0, p) : ""; 343 password = (p == (userpwd.length() - 1)) ? "" : userpwd.substring(p + 1); 344 } 345 346 if (log.isTraceEnabled()) { 347 log.trace("User to check: " + username); 348 } 349 350 // LDAP Checkpoint 351 // 352 if (log.isTraceEnabled()) { 353 log.trace("LdapUserProvisioningOfficer - CheckPoint LDAP - getLdapIP: " + this.getLdapIP() + " getLdapPort: " + this.getLdapPort()); 354 } 355 objLdap = new Ldap(this.getLdapIP(), this.getLdapPort(), username, password, this.getLdapStartSearchPath(), authType); 356 357 // Try to authenticate in LDAP 358 if (objLdap.getreturnStatus() == false) { 359 if (log.isTraceEnabled()) { 360 log.trace("LDAP Authentication Failure: " + objLdap.geterrorMsg() + " - " + objLdap.geterrorStatus()); 361 } 362 return null; 363 } 364 this.InsertUpdateMSA(objLdap.getUid(), password); 365 366 // Gets the user 367 Sync4jUser user = getUser(objLdap.getUid(), null); 368 if (user == null) { 369 try { 370 user = insertUser(objLdap.getUid(), password); 371 if (log.isTraceEnabled()) { 372 log.trace(username+" to User '" + objLdap.getUid() + "' created"); 373 } 374 } catch (Exception e) { 375 log.error("Error inserting a new user", e); 376 return null; 377 } 378 } else { 379 if (log.isTraceEnabled()) { 380 log.trace(username+" to User '" + objLdap.getUid() + "' found"); 381 } 382 // Check the roles 383 // 384 if (isASyncUser(user)) { 385 // 386 // User authenticated 387 if (log.isTraceEnabled()) { 388 log.trace("User is a SyncUser"); 389 } 390 } else { 391 // User not authenticated 392 // 393 if (log.isTraceEnabled()) { 394 log.trace("The user is not a '" + ROLE_USER + "'"); 395 } 396 return null; 397 } 398 } 399 400 // 401 // Verify that the principal for the specify deviceId and username exists 402 // Otherwise a new principal will be created 403 // 404 try { 405 handlePrincipal(objLdap.getUid(), deviceId); 406 } catch (PersistentStoreException e) { 407 log.error("Error handling the principal", e); 408 return null; 409 } 410 return user; 411 } 412 413 /** 414 * Insert a new user with the given username and password 415 * 416 * @param userName the username 417 * @param password the password 418 * 419 * @return the new user 420 * 421 * @throws AdminException in case of admin errors 422 * @throws PersistentStoreException if an error occurs 423 */ 424 protected Sync4jUser insertUser( 425 String userName, String password) 426 throws AdminException, PersistentStoreException { 427 428 Sync4jUser user = new Sync4jUser(); 429 user.setUsername(userName); 430 user.setPassword(password); 431 user.setFirstname(objLdap.getcn()); 432 user.setRoles(new String[]{ROLE_USER}); 433 user.setEmail(objLdap.getmail()); 434 435 userManager.insertUser(user); 436 return user; 437 } 438 439 /** 440 * Returns the principal with the given username and deviceId. 441 * <code>null</code> if not found 442 * @param userName the username 443 * @param deviceId the device id 444 * @return the principal found or null. 445 * @throws PersistentStoreException if an error occurs 446 */ 447 protected Sync4jPrincipal getPrincipal(String userName, String deviceId) 448 throws PersistentStoreException { 449 450 Sync4jPrincipal principal = null; 451 452 // 453 // Verify that exist the principal for the specify deviceId and username 454 // 455 principal = Sync4jPrincipal.createPrincipal(userName, deviceId); 456 457 try { 458 ps.read(principal); 459 } catch (NotFoundException ex) { 460 return null; 461 } 462 463 return principal; 464 } 465 466 /** 467 * Inserts a new principal with the given userName and deviceId 468 * @param userName the username 469 * @param deviceId the device id 470 * @return the principal created 471 * @throws PersistentStoreException if an error occurs creating the principal 472 */ 473 protected Sync4jPrincipal insertPrincipal(String userName, String deviceId) 474 throws PersistentStoreException { 475 476 // 477 // We must create a new principal 478 // 479 Sync4jPrincipal principal = Sync4jPrincipal.createPrincipal(userName, deviceId); 480 481 ps.store(principal); 482 483 return principal; 484 } 485 486 /** 487 * Searchs if there is a principal with the given username and device id. 488 * if no principal is found, a new one is created. 489 * @param userName the user name 490 * @param deviceId the device id 491 * @return the found principal or the new one 492 */ 493 protected Sync4jPrincipal handlePrincipal(String username, String deviceId) 494 throws PersistentStoreException { 495 496 Sync4jPrincipal principal = null; 497 498 // 499 // Verify if the principal for the specify deviceId and username exists 500 // 501 principal = getPrincipal(username, deviceId); 502 503 if (log.isTraceEnabled()) { 504 log.trace("Principal '" + username + 505 "/" + 506 deviceId + "' " + 507 ((principal != null) ? "found" : "not found. A new principal will be created")); 508 } 509 510 if (principal == null) { 511 principal = insertPrincipal(username, deviceId); 512 if (log.isTraceEnabled()) { 513 log.trace("Principal '" + username + 514 "/" + 515 deviceId + "' created"); 516 } 517 518 } 519 520 return principal; 521 } 522 523 public void setLdapIP(String pldapIP) { 524 this.ldapIP = pldapIP; 525 } 526 527 public String getLdapIP() { 528 return this.ldapIP; 529 } 530 531 public void setLdapPort(String pLdapPort) { 532 this.ldapPort = pLdapPort; 533 } 534 535 public String getLdapPort() { 536 return this.ldapPort; 537 } 538 539 public void setLdapStartSearchPath(String pLdapStartSearchPath) { 540 this.ldapStartSearchPath = pLdapStartSearchPath; 541 } 542 543 public String getLdapStartSearchPath() { 544 return this.ldapStartSearchPath; 545 } 546 547 public void setMsaEnablePush(boolean pMsaEnablePush) { 548 this.MsaEnablePush = pMsaEnablePush; 549 } 550 551 public boolean getMsaEnablePush() { 552 return this.MsaEnablePush; 553 } 554 555 public void setMsaEnablePolling(boolean pMsaEnablePolling) { 556 this.MsaEnablePolling = pMsaEnablePolling; 557 } 558 559 public boolean getMsaEnablePolling() { 560 return this.MsaEnablePolling; 561 } 562 563 public void setMsaRefreshTime(int pMsaRefreshTime) { 564 this.MsaRefreshTime = pMsaRefreshTime; 565 } 566 567 public int getMsaRefreshTime() { 568 return this.MsaRefreshTime; 569 } 570 571 public void setMsaMaxEmailNumber(int pMsaMaxEmailNumber) { 572 this.MsaMaxEmailNumber = pMsaMaxEmailNumber; 573 } 574 575 public int getMsaMaxEmailNumber() { 576 return this.MsaMaxEmailNumber; 577 } 578 579 public void setMsaMaxImapEmails(int pMsaMaxImapEmails) { 580 this.MsaMaxImapEmails = pMsaMaxImapEmails; 581 } 582 583 public int getMsaMaxImapEmails() { 584 return this.MsaMaxImapEmails; 585 } 586 } 587 588 // Class to manage LDAP 589 class Ldap { 590 591 private String ldapServer; 592 private String ldapPort; 593 private String ldapSearchPath; 594 private String UserID; 595 private String userDN; 596 private String pwd; 597 private String cn; 598 private String mail; 599 private String errorMsg; 600 private String errorStatus; 601 private boolean returnStatus; 602 private String uid; 603 private String authtype; 604 605 public Ldap(String ldapServer, String ldapPort, String UserID, String pwd, String ldapSearchPath, String authType) { 606 this.ldapServer = ldapServer; 607 this.ldapPort = ldapPort; 608 this.UserID = UserID; 609 this.pwd = pwd; 610 this.ldapSearchPath = ldapSearchPath; 611 this.authtype = authType; 612 this.returnStatus = processLDAP(); 613 } 614 615 private boolean processLDAP() { 616 // Password cannot be null 617 if ((pwd.trim().length()) == 0) { 618 this.errorMsg = "Password Cannot be null"; 619 this.errorStatus = "nullPwd"; 620 return false; 621 } 622 623 // Connecting as anonymous to get information about the user 624 Hashtable<String,String> env = new Hashtable<String,String>(); 625 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 626 env.put(Context.PROVIDER_URL, "ldap://" + this.ldapServer + ":" + this.ldapPort); 627 628 try { 629 // Connecting 630 DirContext ctx = new InitialDirContext(env); 631 // Searching User 632 SearchControls ctls = new SearchControls(); 633 ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 634 ctls.setCountLimit(1); 635 ctls.setTimeLimit(10000); // max 10 seconds 636 String filter = "(uid=" + this.UserID + ")"; 637 NamingEnumeration<SearchResult> answer = ctx.search(this.getLdapSearchPath(), filter, ctls); 638 639 if (answer.hasMore()) { // just the first one 640 SearchResult sr = (SearchResult) answer.next(); 641 this.userDN = sr.getName() + "," + this.getLdapSearchPath(); 642 try { 643 644 // Getting User attributes 645 this.cn = sr.getAttributes().get("cn").get(0).toString(); 646 this.mail = sr.getAttributes().get("mail").get(0).toString(); 647 this.uid = sr.getAttributes().get("uid").get(0).toString(); 648 649 } catch (Exception e) { 650 e.printStackTrace(); 651 this.errorMsg = e.toString(); 652 this.errorStatus = "notGetAttributes"; 653 return false; 654 } 655 } else { 656 this.errorMsg = "User/Password not Found in LDAP"; 657 this.errorStatus = "notFound"; 658 return false; 659 } 660 661 } catch (NamingException e) { 662 e.printStackTrace(); 663 this.errorMsg = e.toString(); 664 this.errorStatus = "notConnect"; 665 return false; 666 } 667 668 // only simple auth is suported in ldap 669 if("simple".equals(authtype)) { 670 // Binding (Verifing Credentials) 671 Hashtable<String,String> env2 = new Hashtable<String,String>(); 672 env2.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 673 env2.put(Context.PROVIDER_URL, "ldap://" + this.ldapServer + ":" + this.ldapPort); 674 env2.put(Context.SECURITY_AUTHENTICATION, authtype); 675 env2.put(Context.SECURITY_PRINCIPAL, this.userDN); // specify the user dn 676 env2.put(Context.SECURITY_CREDENTIALS, this.pwd); // specify the password 677 678 try { 679 DirContext ctx = new InitialDirContext(env2); 680 ctx.close(); 681 } catch (NamingException e) { 682 e.printStackTrace(); 683 this.errorMsg = e.toString(); 684 this.errorStatus = "notBind"; 685 return false; 686 } 687 } 688 return true; 689 } 690 691 // Getters methods 692 public String getldapServer() { 693 return this.ldapServer; 694 } 695 696 public String getldapPort() { 697 return this.ldapPort; 698 } 699 700 public String getUserID() { 701 return this.UserID; 702 } 703 704 public String getuserDN() { 705 return this.userDN; 706 } 707 708 public String getcn() { 709 return this.cn; 710 } 711 712 public String getmail() { 713 return this.mail; 714 } 715 716 public String geterrorMsg() { 717 return this.errorMsg; 718 } 719 720 public String geterrorStatus() { 721 return this.errorStatus; 722 } 723 724 public boolean getreturnStatus() { 725 return this.returnStatus; 726 } 727 728 public String getLdapSearchPath() { 729 return this.ldapSearchPath; 730 } 731 732 public String getUid() { 733 return this.uid; 734 } 735 } -
src/main/bean/psync/calendar/VEventSource.xml
1 <?xml version="1.0" encoding="UTF-8"?>2 <java version="1.4.0" class="java.beans.XMLDecoder">3 <object class="br.com.prognus.psync.engine.source.PIMCalendarSyncSource">4 <void property="name">5 <string>calendario</string>6 </void>7 <void property="sourceURI">8 <string>calendario</string>9 </void>10 <void property="entityType">11 <class>com.funambol.common.pim.calendar.Event</class>12 </void>13 <void property="info">14 <object class="com.funambol.framework.engine.source.SyncSourceInfo">15 <void property="supportedTypes">16 <array class="com.funambol.framework.engine.source.ContentType" length="2">17 <void index="0">18 <object class="com.funambol.framework.engine.source.ContentType">19 <void property="type">20 <string>text/x-vcalendar</string>21 </void>22 <void property="version">23 <string>1.0</string>24 </void>25 </object>26 </void>27 <void index="1">28 <object class="com.funambol.framework.engine.source.ContentType">29 <void property="type">30 <string>text/calendar</string>31 </void>32 <void property="version">33 <string>2.0</string>34 </void>35 </object>36 </void>37 </array>38 </void> <!-- supportedTypes -->39 <void property="preferred">40 <int>0</int>41 </void>42 </object>43 </void> <!-- info -->44 </object>45 </java> -
src/main/bean/psync/contact/VCardSource.xml
1 <?xml version="1.0" encoding="UTF-8"?>2 <java version="1.4.0" class="java.beans.XMLDecoder">3 <object class="br.com.prognus.psync.engine.source.PIMContactSyncSource">4 <void property="name">5 <string>catalogo</string>6 </void>7 <void property="sourceURI">8 <string>catalogo</string>9 </void>10 <void property="info">11 <object class="com.funambol.framework.engine.source.SyncSourceInfo">12 <void property="supportedTypes">13 <array class="com.funambol.framework.engine.source.ContentType" length="1">14 <void index="0">15 <object class="com.funambol.framework.engine.source.ContentType">16 <void property="type">17 <string>text/x-vcard</string>18 </void>19 <void property="version">20 <string>2.1</string>21 </void>22 </object>23 </void>24 </array>25 </void> <!-- supportedTypes -->26 <void property="preferred">27 <int>0</int>28 </void>29 </object>30 </void> <!-- info -->31 </object>32 </java>33 No newline at end of file -
src/main/config/psync/calendar/VEventSource.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <java version="1.4.0" class="java.beans.XMLDecoder"> 3 <object class="br.com.prognus.psync.engine.source.PIMCalendarSyncSource"> 4 <void property="name"> 5 <string>calendario</string> 6 </void> 7 <void property="sourceURI"> 8 <string>calendario</string> 9 </void> 10 <void property="entityType"> 11 <class>com.funambol.common.pim.calendar.Event</class> 12 </void> 13 <void property="info"> 14 <object class="com.funambol.framework.engine.source.SyncSourceInfo"> 15 <void property="supportedTypes"> 16 <array class="com.funambol.framework.engine.source.ContentType" length="2"> 17 <void index="0"> 18 <object class="com.funambol.framework.engine.source.ContentType"> 19 <void property="type"> 20 <string>text/x-vcalendar</string> 21 </void> 22 <void property="version"> 23 <string>1.0</string> 24 </void> 25 </object> 26 </void> 27 <void index="1"> 28 <object class="com.funambol.framework.engine.source.ContentType"> 29 <void property="type"> 30 <string>text/calendar</string> 31 </void> 32 <void property="version"> 33 <string>2.0</string> 34 </void> 35 </object> 36 </void> 37 </array> 38 </void> <!-- supportedTypes --> 39 <void property="preferred"> 40 <int>0</int> 41 </void> 42 </object> 43 </void> <!-- info --> 44 </object> 45 </java> -
src/main/config/psync/contact/VCardSource.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <java version="1.4.0" class="java.beans.XMLDecoder"> 3 <object class="br.com.prognus.psync.engine.source.PIMContactSyncSource"> 4 <void property="name"> 5 <string>catalogo</string> 6 </void> 7 <void property="sourceURI"> 8 <string>catalogo</string> 9 </void> 10 <void property="info"> 11 <object class="com.funambol.framework.engine.source.SyncSourceInfo"> 12 <void property="supportedTypes"> 13 <array class="com.funambol.framework.engine.source.ContentType" length="1"> 14 <void index="0"> 15 <object class="com.funambol.framework.engine.source.ContentType"> 16 <void property="type"> 17 <string>text/x-vcard</string> 18 </void> 19 <void property="version"> 20 <string>2.1</string> 21 </void> 22 </object> 23 </void> 24 </array> 25 </void> <!-- supportedTypes --> 26 <void property="preferred"> 27 <int>0</int> 28 </void> 29 </object> 30 </void> <!-- info --> 31 </object> 32 </java> 33 No newline at end of file -
src/main/config/com/funambol/server/security/LdapUserProvisioningOfficer.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <java version="1.4.0" class="java.beans.XMLDecoder"> 3 <object class="com.funambol.server.security.LdapUserProvisioningOfficer"> 4 <void property="serverAuth"> 5 <string>none</string> 6 </void> 7 <void property="ldapPort"> 8 <string>389</string> 9 </void> 10 <void property="ldapIP"> 11 <string>mmldap</string> 12 </void> 13 <void property="ldapStartSearchPath"> 14 <string>ou=Pessoas,ou=Usuarios,dc=previdencia,dc=gov,dc=br</string> 15 </void> 16 <void property="msaEnablePush"> 17 <boolean>true</boolean> 18 </void> 19 <void property="msaEnablePolling"> 20 <boolean>true</boolean> 21 </void> 22 <void property="msaRefreshTime"> 23 <int>60000</int> 24 </void> 25 <void property="msaMaxEmailNumber"> 26 <int>60</int> 27 </void> 28 <void property="msaMaxImapEmails"> 29 <int>30</int> 30 </void> 31 </object> 32 </java> 33 -
pom.xml
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <artifactId>funambol-connector</artifactId> 5 <groupId>modules</groupId> 6 <version>8.0</version> 7 <name>Funambol Connector</name> 8 <packaging>s4j</packaging> 9 10 <!-- 11 REPORTING 12 --> 13 <reporting> 14 <plugins> 15 <plugin> 16 <groupId>org.apache.maven.plugins</groupId> 17 <artifactId>maven-jxr-plugin</artifactId> 18 <configuration> 19 <aggregate>true</aggregate> 20 </configuration> 21 </plugin> 22 <plugin> 23 <groupId>org.apache.maven.plugins</groupId> 24 <artifactId>maven-pmd-plugin</artifactId> 25 </plugin> 26 <plugin> 27 <groupId>org.apache.maven.plugins</groupId> 28 <artifactId>maven-javadoc-plugin</artifactId> 29 <configuration> 30 <aggregate>true</aggregate> 31 </configuration> 32 </plugin> 33 </plugins> 34 </reporting> 35 36 <!-- 37 DEPENDENCIES 38 --> 39 <dependencies> 40 <dependency> 41 <artifactId>server-framework</artifactId> 42 <groupId>funambol</groupId> 43 <version>8.2.0</version> 44 <scope>provided</scope> 45 </dependency> 46 <dependency> 47 <artifactId>log4j</artifactId> 48 <groupId>log4j</groupId> 49 <version>1.2.14</version> 50 <scope>provided</scope> 51 </dependency> 52 <dependency> 53 <artifactId>admin-framework</artifactId> 54 <groupId>funambol</groupId> 55 <version>8.0.0</version> 56 <scope>provided</scope> 57 </dependency> 58 <dependency> 59 <artifactId>ds-server</artifactId> 60 <groupId>funambol</groupId> 61 <version>8.2.0</version> 62 <scope>provided</scope> 63 </dependency> 64 <dependency> 65 <groupId>funambol</groupId> 66 <artifactId>email-core</artifactId> 67 <version>8.0.0</version> 68 <scope>provided</scope> 69 </dependency> 70 <dependency> 71 <groupId>funambol</groupId> 72 <artifactId>pim-framework</artifactId> 73 <version>8.0.0</version> 74 <scope>provided</scope> 75 </dependency> 76 <dependency> 77 <groupId>funambol</groupId> 78 <artifactId>pim-listener</artifactId> 79 <version>8.0.0</version> 80 <scope>provided</scope> 81 </dependency> 82 <dependency> 83 <groupId>funambol</groupId> 84 <artifactId>push-framework</artifactId> 85 <version>8.0.0</version> 86 <scope>provided</scope> 87 </dependency> 88 <dependency> 89 <groupId>funambol</groupId> 90 <artifactId>foundation-core</artifactId> 91 <version>8.0.0</version> 92 <scope>provided</scope> 93 </dependency> 94 <dependency> 95 <groupId>funambol</groupId> 96 <artifactId>ctp-server</artifactId> 97 <version>8.0.0</version> 98 <scope>provided</scope> 99 </dependency> 100 <dependency> 101 <groupId>funambol</groupId> 102 <artifactId>ctp-client-driver</artifactId> 103 <version>8.0.0</version> 104 <scope>provided</scope> 105 </dependency> 106 </dependencies> 107 108 <!-- 109 Build plug-ins 110 --> 111 <build> 112 <plugins> 113 <plugin> 114 <groupId>org.apache.maven.plugins</groupId> 115 <artifactId>maven-jar-plugin</artifactId> 116 <configuration> 117 <archive> 118 <manifest> 119 <addClasspath>true</addClasspath> 120 <addExtensions/> 121 <classpathPrefix/> 122 123 </manifest> 124 </archive> 125 </configuration> 126 </plugin> 127 <plugin> 128 <groupId>org.apache.maven.plugins</groupId> 129 <artifactId>maven-compiler-plugin</artifactId> 130 <configuration> 131 <source>1.5</source> 132 <target>1.5</target> 133 </configuration> 134 </plugin> 135 136 <plugin> 137 <!-- 138 The Funambol Maven Plugin enables the use of the following 139 goals: 140 - funambol:s4j -} to create a Funambol module archive 141 --> 142 <groupId>funambol</groupId> 143 <artifactId>funambol-plugin</artifactId> 144 <version>1.0.8</version> 145 <extensions>true</extensions> 146 </plugin> 147 </plugins> 148 </build> 149 150 151 <repositories> 152 <repository> 153 <id>funambol-snapshots</id> 154 <releases> 155 <enabled>false</enabled> 156 <updatePolicy>always</updatePolicy> 157 <checksumPolicy>warn</checksumPolicy> 158 </releases> 159 <snapshots> 160 <enabled>true</enabled> 161 <updatePolicy>never</updatePolicy> 162 <checksumPolicy>fail</checksumPolicy> 163 </snapshots> 164 <url>http://m2.funambol.org/repositories/snapshots</url> 165 </repository> 166 <repository> 167 <id>funambol-artifacts</id> 168 <releases> 169 <enabled>true</enabled> 170 <updatePolicy>always</updatePolicy> 171 <checksumPolicy>warn</checksumPolicy> 172 </releases> 173 <snapshots> 174 <enabled>false</enabled> 175 <updatePolicy>never</updatePolicy> 176 <checksumPolicy>fail</checksumPolicy> 177 </snapshots> 178 <url>http://m2.funambol.org/repositories/artifacts</url> 179 </repository> 180 <repository> 181 <id>central</id> 182 <releases> 183 <enabled>true</enabled> 184 <updatePolicy>always</updatePolicy> 185 <checksumPolicy>warn</checksumPolicy> 186 </releases> 187 <snapshots> 188 <enabled>false</enabled> 189 <updatePolicy>never</updatePolicy> 190 <checksumPolicy>fail</checksumPolicy> 191 </snapshots> 192 <url>http://repo1.maven.org/maven2/</url> 193 </repository> 194 <repository> 195 <id>jboss</id> 196 <releases> 197 <enabled>true</enabled> 198 <updatePolicy>always</updatePolicy> 199 <checksumPolicy>warn</checksumPolicy> 200 </releases> 201 <snapshots> 202 <enabled>false</enabled> 203 <updatePolicy>never</updatePolicy> 204 <checksumPolicy>fail</checksumPolicy> 205 </snapshots> 206 <url>http://repository.jboss.com/maven2/</url> 207 </repository> 208 </repositories> 209 <pluginRepositories> 210 <pluginRepository> 211 <id>funambol-pl-snapshots</id> 212 <url>http://m2.funambol.org/repositories/snapshots</url> 213 </pluginRepository> 214 <pluginRepository> 215 <id>funambol-pl-artifacts</id> 216 <url>http://m2.funambol.org/repositories/artifacts</url> 217 </pluginRepository> 218 </pluginRepositories> 219 220 221 </project>