Search in sources :

Example 1 with ContactsService

use of com.google.gdata.client.contacts.ContactsService in project janrufmonitor by tbrandt77.

the class GoogleContactsFetcher method main.

public static void main(String[] args) {
    // SimpleDateFormat sfd = new SimpleDateFormat("HH:mm:ss");
    // //1238838480000
    // System.out.println(sfd.format(new Date(1238609940000L)));
    // 
    // sfd = new SimpleDateFormat("HH:mm");
    // System.out.println(sfd.format(new Date(1238609940000L)));
    // 
    // 
    // sfd = new SimpleDateFormat("HH:mm:ss");
    // System.out.println(sfd.format(new Date(1238609880000L)));
    // 
    // sfd = new SimpleDateFormat("HH:mm");
    // System.out.println(sfd.format(new Date(1238609880000L)));
    // System.exit(0);
    // 
    // 
    // System.out.println(System.currentTimeMillis());
    System.out.println("74918".matches("[\\d]+[\\D]*"));
    ContactsService cs = new ContactsService("jam-googlecontacts-callermanager");
    try {
        cs.setUserCredentials("thilo.brandt@googlemail.com", "diplom2001");
        URL feedUrl = new URL("http://www.google.com/m8/feeds/contacts/thilo.brandt@googlemail.com/full");
        feedUrl = new URL("http://www.google.com/m8/feeds/contacts/thilo.brandt%40googlemail.com/full/4dbaf7080d386b");
        // Query q = new Query(feedUrl);
        // q.setMaxResults(2);
        // q.setUpdatedMin(new DateTime(System.currentTimeMillis()-1000000));
        ContactEntry entry = (ContactEntry) cs.getEntry(feedUrl, ContactEntry.class);
        // System.out.print("["+resultFeed.getEntries().size()+"] ");
        // System.out.println(resultFeed.getTitle().getPlainText());
        // for (int i = 0; i < resultFeed.getEntries().size(); i++) {
        // ContactEntry entry = (ContactEntry) resultFeed.getEntries()
        // .get(i);
        System.out.println(entry.getSelfLink().getHref());
        System.out.println(entry.getTitle().getPlainText() + " (UUID:" + entry.getId() + ")");
        if (entry.getOrganizations().size() > 0)
            System.out.println(((Organization) entry.getOrganizations().get(0)).getOrgName().getValue());
        if (entry.getPostalAddresses().size() > 0) {
            System.out.println(((PostalAddress) entry.getPostalAddresses().get(0)).getValue());
        }
        Link photoLink = entry.getContactPhotoLink();
        if (photoLink != null && photoLink.getEtag() != null) {
            System.out.print("Bild: " + entry.getContactPhotoLink().getHref());
            try {
                InputStream in = cs.createLinkQueryRequest(photoLink).getResponseStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                RandomAccessFile file = new RandomAccessFile("x:\\" + entry.getSelfLink().getHref().substring(entry.getSelfLink().getHref().lastIndexOf('/') + 1), "rw");
                byte[] buffer = new byte[4096];
                for (int read = 0; (read = in.read(buffer)) != -1; out.write(buffer, 0, read)) ;
                file.write(out.toByteArray());
                file.close();
            } catch (Exception e) {
            // e.printStackTrace();
            }
        }
        List phones = entry.getPhoneNumbers();
        for (int j = 0; j < phones.size(); j++) {
            System.out.print(((PhoneNumber) phones.get(j)).getPhoneNumber());
            System.out.println(" (" + ((PhoneNumber) phones.get(j)).getRel() + ")");
        }
        List s = entry.getGroupMembershipInfos();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            System.out.println(((GroupMembershipInfo) iter.next()).getHref());
            URL feedUrlg = new URL("http://www.google.com/m8/feeds/groups/thilo.brandt@googlemail.com/full");
            ContactGroupFeed resultFeedg = (ContactGroupFeed) cs.getFeed(feedUrlg, ContactGroupFeed.class);
            // Print the results
            for (int k = 0; k < resultFeedg.getEntries().size(); k++) {
                ContactGroupEntry groupEntry = (ContactGroupEntry) resultFeedg.getEntries().get(k);
                System.out.println("Id: " + groupEntry.getId());
                System.out.println("Group Name: " + groupEntry.getTitle().getPlainText());
            }
        }
        System.out.println();
        System.out.println("---");
    // }
    } catch (AuthenticationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ServiceException e) {
        e.printStackTrace();
    }
}
Also used : AuthenticationException(com.google.gdata.util.AuthenticationException) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) URL(java.net.URL) AuthenticationException(com.google.gdata.util.AuthenticationException) IOException(java.io.IOException) ServiceException(com.google.gdata.util.ServiceException) RandomAccessFile(java.io.RandomAccessFile) ServiceException(com.google.gdata.util.ServiceException) ContactsService(com.google.gdata.client.contacts.ContactsService) ContactGroupEntry(com.google.gdata.data.contacts.ContactGroupEntry) ContactEntry(com.google.gdata.data.contacts.ContactEntry) ContactGroupFeed(com.google.gdata.data.contacts.ContactGroupFeed) Iterator(java.util.Iterator) List(java.util.List) Link(com.google.gdata.data.Link)

Example 2 with ContactsService

use of com.google.gdata.client.contacts.ContactsService in project janrufmonitor by tbrandt77.

the class GoogleContactsProxy method checkAuthentication.

public synchronized void checkAuthentication(String user, String password) throws GoogleContactsLoginException {
    ContactsService cs = login(user, password);
    try {
        URL feedUrlg = new URL("http://www.google.com/m8/feeds/groups/" + user + "/full");
        cs.getFeed(feedUrlg, ContactGroupFeed.class);
    } catch (IOException e) {
        throw new GoogleContactsLoginException(e);
    } catch (ServiceException e) {
        throw new GoogleContactsLoginException(e);
    }
}
Also used : ServiceException(com.google.gdata.util.ServiceException) ContactsService(com.google.gdata.client.contacts.ContactsService) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with ContactsService

use of com.google.gdata.client.contacts.ContactsService in project janrufmonitor by tbrandt77.

the class GoogleContactsProxy method getContacts.

public synchronized ICallerList getContacts(String category) throws GoogleContactsException {
    this.m_current = 0;
    this.m_total = 0;
    try {
        fetchCategories();
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    ICallerList cl = getRuntime().getCallerFactory().createCallerList(getMaxResults());
    if (category != null && !this.m_reverseCategories.containsKey(category))
        return cl;
    ContactsService cs = login();
    try {
        URL feedUrl = new URL("http://www.google.com/m8/feeds/contacts/" + getLoginUser() + "/full");
        Query q = new Query(feedUrl);
        q.setMaxResults(getMaxResults());
        if (category != null)
            q.setStringCustomParameter("group", (String) this.m_reverseCategories.get(category));
        ContactFeed resultFeed = (ContactFeed) cs.getFeed(q, ContactFeed.class);
        List entries = resultFeed.getEntries();
        this.m_total = entries.size();
        this.m_logger.info("Fetched " + entries.size() + " entries from google account " + getLoginUser());
        Object o = null;
        for (int i = 0, j = entries.size(); i < j; i++) {
            o = entries.get(i);
            if (o instanceof ContactEntry) {
                // && (category==null || matchCategory(category, (ContactEntry) o))) {
                ICaller c = this.parse(cs, (ContactEntry) o);
                if (c != null) {
                    cl.add(c);
                    this.m_current++;
                }
            }
        }
    } catch (MalformedURLException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    // 
    if (this.m_dbh != null) {
        try {
            if (category == null)
                this.m_dbh.deleteAll();
            ICaller c = null;
            for (int i = 0, j = cl.size(); i < j; i++) {
                c = cl.get(i);
                if (c instanceof IMultiPhoneCaller) {
                    List phones = ((IMultiPhoneCaller) c).getPhonenumbers();
                    IPhonenumber pn = null;
                    if (category != null)
                        this.m_dbh.delete(c.getUUID());
                    for (int k = 0; k < phones.size(); k++) {
                        pn = (IPhonenumber) phones.get(k);
                        this.m_dbh.insert(c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber());
                    }
                } else {
                    IPhonenumber pn = c.getPhoneNumber();
                    if (category != null)
                        this.m_dbh.delete(c.getUUID());
                    this.m_dbh.insert(c.getUUID(), pn.getIntAreaCode(), pn.getAreaCode(), pn.getCallNumber());
                }
            }
        } catch (SQLException e) {
            throw new GoogleContactsException(e.getMessage(), e);
        }
    } else {
        this.m_logger.warning("GoogleContacts proxy datahandler not initialized. Could not insert google contacts...");
    }
    return cl;
}
Also used : MalformedURLException(java.net.MalformedURLException) Query(com.google.gdata.client.Query) SQLException(java.sql.SQLException) IOException(java.io.IOException) ContactFeed(com.google.gdata.data.contacts.ContactFeed) URL(java.net.URL) ICaller(de.janrufmonitor.framework.ICaller) ICallerList(de.janrufmonitor.framework.ICallerList) ServiceException(com.google.gdata.util.ServiceException) ContactsService(com.google.gdata.client.contacts.ContactsService) ContactEntry(com.google.gdata.data.contacts.ContactEntry) List(java.util.List) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Example 4 with ContactsService

use of com.google.gdata.client.contacts.ContactsService in project janrufmonitor by tbrandt77.

the class GoogleContactsProxy method deleteContacts.

public synchronized void deleteContacts(ICallerList cl) throws GoogleContactsException {
    this.m_current = 0;
    this.m_total = 0;
    this.m_total = cl.size();
    try {
        fetchCategories();
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    ContactsService cs = login();
    ICaller c = null;
    for (int i = 0, j = cl.size(); i < j; i++) {
        c = cl.get(i);
        try {
            if (c.getAttributes().contains("entryUrl")) {
                String entryUrl = c.getAttribute("entryUrl").getValue();
                if (entryUrl.length() > 0) {
                    ContactEntry entry = (ContactEntry) cs.getEntry(new URL(entryUrl), ContactEntry.class);
                    if (entry != null) {
                        this.m_logger.info("Deleting google contact: " + entry.getEtag());
                        entry.delete();
                        this.m_current++;
                    }
                }
            }
            if (this.m_dbh != null) {
                try {
                    this.m_dbh.delete(c.getUUID());
                } catch (SQLException e) {
                    throw new GoogleContactsException(e.getMessage(), e);
                }
            } else {
                this.m_logger.warning("GoogleContacts proxy datahandler not initialized. Could not insert google contacts...");
            }
        } catch (MalformedURLException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        } catch (IOException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        } catch (ServiceException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        }
    }
}
Also used : ICaller(de.janrufmonitor.framework.ICaller) MalformedURLException(java.net.MalformedURLException) ServiceException(com.google.gdata.util.ServiceException) ContactsService(com.google.gdata.client.contacts.ContactsService) SQLException(java.sql.SQLException) ContactEntry(com.google.gdata.data.contacts.ContactEntry) IOException(java.io.IOException) URL(java.net.URL)

Example 5 with ContactsService

use of com.google.gdata.client.contacts.ContactsService in project janrufmonitor by tbrandt77.

the class GoogleContactsProxy method createContacts.

public synchronized void createContacts(ICallerList cl) throws GoogleContactsException {
    this.m_current = 0;
    this.m_total = 0;
    this.m_total = cl.size();
    try {
        fetchCategories();
    } catch (IOException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    } catch (ServiceException e) {
        throw new GoogleContactsException(e.getMessage(), e);
    }
    if (cl.size() == 0)
        return;
    ContactsService cs = login();
    ICaller caller = null;
    ContactEntry entry = null;
    for (int i = 0, j = cl.size(); i < j; i++) {
        this.m_current++;
        caller = cl.get(i);
        IAttributeMap m = caller.getAttributes();
        try {
            entry = new ContactEntry();
            Name name = new Name();
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_LASTNAME))
                name.setFamilyName(new FamilyName((m.get(IJAMConst.ATTRIBUTE_NAME_LASTNAME).getValue().length() == 0 ? " " : m.get(IJAMConst.ATTRIBUTE_NAME_LASTNAME).getValue()), null));
            else
                name.setFamilyName(new FamilyName(" ", null));
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME))
                name.setGivenName(new GivenName((m.get(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME).getValue().length() == 0 ? " " : m.get(IJAMConst.ATTRIBUTE_NAME_FIRSTNAME).getValue()), null));
            else
                name.setGivenName(new GivenName(" ", null));
            name.setFullName(new FullName(Formatter.getInstance(getRuntime()).parse("%a:ln%, %a:fn%", m), null));
            entry.setName(name);
            entry.addStructuredPostalAddress(createPostalAddress(m));
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_EMAIL)) {
                String emails = m.get(IJAMConst.ATTRIBUTE_NAME_EMAIL).getValue();
                Email email = new Email();
                email.setAddress(emails);
                entry.addEmailAddress(email);
            }
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_CATEGORY)) {
                String cat = m.get(IJAMConst.ATTRIBUTE_NAME_CATEGORY).getValue();
                if (this.m_reverseCategories.containsKey(cat)) {
                    GroupMembershipInfo gmi = new GroupMembershipInfo();
                    gmi.setHref((String) this.m_reverseCategories.get(cat));
                    entry.addGroupMembershipInfo(gmi);
                }
            }
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_GEO_ACC)) {
                ExtendedProperty acc = new ExtendedProperty();
                acc.setName(IJAMConst.ATTRIBUTE_NAME_GEO_ACC);
                acc.setValue(m.get(IJAMConst.ATTRIBUTE_NAME_GEO_ACC).getValue());
                entry.addExtendedProperty(acc);
            }
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_GEO_LNG)) {
                ExtendedProperty acc = new ExtendedProperty();
                acc.setName(IJAMConst.ATTRIBUTE_NAME_GEO_LNG);
                acc.setValue(m.get(IJAMConst.ATTRIBUTE_NAME_GEO_LNG).getValue());
                entry.addExtendedProperty(acc);
            }
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_GEO_LAT)) {
                ExtendedProperty acc = new ExtendedProperty();
                acc.setName(IJAMConst.ATTRIBUTE_NAME_GEO_LAT);
                acc.setValue(m.get(IJAMConst.ATTRIBUTE_NAME_GEO_LAT).getValue());
                entry.addExtendedProperty(acc);
            }
            PhoneNumber pn = null;
            if (caller instanceof IMultiPhoneCaller) {
                List phones = ((IMultiPhoneCaller) caller).getPhonenumbers();
                IPhonenumber p = null;
                for (int k = 0, l = phones.size(); k < l; k++) {
                    p = (IPhonenumber) phones.get(k);
                    pn = new PhoneNumber();
                    pn.setPrimary(k == 0);
                    IAttribute type = m.get(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + p.getTelephoneNumber());
                    if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
                        pn.setRel(PhoneNumber.Rel.MOBILE);
                    } else if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
                        pn.setRel(PhoneNumber.Rel.HOME_FAX);
                    } else {
                        pn.setRel(PhoneNumber.Rel.HOME);
                    }
                    pn.setPhoneNumber(Formatter.getInstance(getRuntime()).parse(IJAMConst.GLOBAL_VARIABLE_CALLERNUMBER, p));
                    entry.addPhoneNumber(pn);
                }
            } else {
                pn = new PhoneNumber();
                IAttribute type = m.get(IJAMConst.ATTRIBUTE_NAME_NUMBER_TYPE + caller.getPhoneNumber().getTelephoneNumber());
                if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_MOBILE_TYPE)) {
                    pn.setRel(PhoneNumber.Rel.MOBILE);
                } else if (type != null && type.getValue().equalsIgnoreCase(IJAMConst.ATTRIBUTE_VALUE_FAX_TYPE)) {
                    pn.setRel(PhoneNumber.Rel.HOME_FAX);
                } else {
                    pn.setRel(PhoneNumber.Rel.HOME);
                }
                pn.setPhoneNumber(Formatter.getInstance(getRuntime()).parse(IJAMConst.GLOBAL_VARIABLE_CALLERNUMBER, caller.getPhoneNumber()));
                entry.addPhoneNumber(pn);
            }
            URL postUrl = new URL("https://www.google.com/m8/feeds/contacts/" + getLoginUser() + "/full");
            entry = (ContactEntry) cs.insert(postUrl, entry);
            if (m.contains(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH)) {
                String file = PathResolver.getInstance(getRuntime()).resolve(m.get(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH).getValue());
                if (new File(file).exists()) {
                    FileInputStream in = new FileInputStream(file);
                    Link photoLink = entry.getContactPhotoLink();
                    URL photoUrl = new URL(photoLink.getHref());
                    GDataRequest request = cs.createRequest(GDataRequest.RequestType.UPDATE, photoUrl, new ContentType("image/jpeg"));
                    Stream.copy(in, request.getRequestStream());
                    request.execute();
                }
            }
            if (entry != null && this.m_dbh != null) {
                try {
                    if (caller instanceof IMultiPhoneCaller) {
                        List phones = ((IMultiPhoneCaller) caller).getPhonenumbers();
                        IPhonenumber p = null;
                        for (int k = 0; k < phones.size(); k++) {
                            p = (IPhonenumber) phones.get(k);
                            this.m_dbh.insert(caller.getUUID(), p.getIntAreaCode(), p.getAreaCode(), p.getCallNumber());
                        }
                    } else {
                        IPhonenumber p = caller.getPhoneNumber();
                        this.m_dbh.insert(caller.getUUID(), p.getIntAreaCode(), p.getAreaCode(), p.getCallNumber());
                    }
                } catch (SQLException e) {
                    throw new GoogleContactsException(e.getMessage(), e);
                }
            } else {
                this.m_logger.warning("GoogleContacts proxy datahandler not initialized. Could not insert google contacts...");
            }
        } catch (MalformedURLException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        } catch (IOException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        } catch (ServiceException e) {
            this.m_logger.log(Level.SEVERE, e.toString(), e);
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Email(com.google.gdata.data.extensions.Email) ContentType(com.google.gdata.util.ContentType) FamilyName(com.google.gdata.data.extensions.FamilyName) SQLException(java.sql.SQLException) GivenName(com.google.gdata.data.extensions.GivenName) GDataRequest(com.google.gdata.client.Service.GDataRequest) URL(java.net.URL) FamilyName(com.google.gdata.data.extensions.FamilyName) Name(com.google.gdata.data.extensions.Name) GivenName(com.google.gdata.data.extensions.GivenName) FullName(com.google.gdata.data.extensions.FullName) ContactsService(com.google.gdata.client.contacts.ContactsService) ContactEntry(com.google.gdata.data.contacts.ContactEntry) GroupMembershipInfo(com.google.gdata.data.contacts.GroupMembershipInfo) IAttributeMap(de.janrufmonitor.framework.IAttributeMap) IMultiPhoneCaller(de.janrufmonitor.framework.IMultiPhoneCaller) List(java.util.List) ArrayList(java.util.ArrayList) ICallerList(de.janrufmonitor.framework.ICallerList) IOException(java.io.IOException) ExtendedProperty(com.google.gdata.data.extensions.ExtendedProperty) FileInputStream(java.io.FileInputStream) ICaller(de.janrufmonitor.framework.ICaller) ServiceException(com.google.gdata.util.ServiceException) FullName(com.google.gdata.data.extensions.FullName) IAttribute(de.janrufmonitor.framework.IAttribute) PhoneNumber(com.google.gdata.data.extensions.PhoneNumber) File(java.io.File) Link(com.google.gdata.data.Link) IPhonenumber(de.janrufmonitor.framework.IPhonenumber)

Aggregations

ContactsService (com.google.gdata.client.contacts.ContactsService)9 URL (java.net.URL)9 ServiceException (com.google.gdata.util.ServiceException)8 IOException (java.io.IOException)8 ContactEntry (com.google.gdata.data.contacts.ContactEntry)7 MalformedURLException (java.net.MalformedURLException)6 SQLException (java.sql.SQLException)5 List (java.util.List)5 ICaller (de.janrufmonitor.framework.ICaller)4 ICallerList (de.janrufmonitor.framework.ICallerList)4 IMultiPhoneCaller (de.janrufmonitor.framework.IMultiPhoneCaller)4 IPhonenumber (de.janrufmonitor.framework.IPhonenumber)4 ArrayList (java.util.ArrayList)4 Link (com.google.gdata.data.Link)3 Query (com.google.gdata.client.Query)2 GDataRequest (com.google.gdata.client.Service.GDataRequest)2 ContactFeed (com.google.gdata.data.contacts.ContactFeed)2 ContactGroupEntry (com.google.gdata.data.contacts.ContactGroupEntry)2 ContactGroupFeed (com.google.gdata.data.contacts.ContactGroupFeed)2 GroupMembershipInfo (com.google.gdata.data.contacts.GroupMembershipInfo)2