none
[E2010] [EWS Java API 1.1][JAVA] - Retrieving contacts using delegation doesn't seems to work. RRS feed

  • General discussion

  • Hi,

    I am trying to use delegation for getting the contacts from another mailbox.

    I can explain the scenario. There are 2 users. "user1" and "user2". "user2" has access to "user1"'s mailbox. This permission I provided through EMC (Manage Full Access Permission...) .

    It seems when there is a delegate for a user, all the functionalities are not working fine. For example, If I create a contact in "user1"'s mailbox, its getting created in "user2"'s mailbox even if I am not using delegation. Even if I am deleting everything from "user2"'s contacts folder, it is emptying "user1"'s contact folder (not always. sometimes it is working fine). I feel a bit troubled to explain the problem in words. So I am pasting the test case also along with this post.

    Please check that and let me know if I am doing anything wrong.

    Please run the following test code.

    public class TestDelegate
    {
      public static void main( String [] args ) throws Exception
      {
        ExchangeService service1 = getService( "user1", "P@$$w0rd" );
        ExchangeService service2 = getService( "user2", "P@$$w0rd" );
    
        // Clearing all the contacts from "user1".
        clearContactsAndAssertContactsFolderIsEmpty( service1 );
    
        // Clearing all the contacts from "user2".
        clearContactsAndAssertContactsFolderIsEmpty( service2 );
    
        // Creating a contact in "user1"'s mailbox and verify that the contact is created fine.
        createContact( service1 );
        Thread.sleep( 5000 );
    
        // Just verifying that in "user2"'s mailbox there are no contact items.
        System.out.println( "Verifying contacts is empty for "
            + ( ( WebCredentials ) service2.getCredentials() ).getUser() );
        Folder contactsFolder = Folder.bind( service2, WellKnownFolderName.Contacts );
        FindItemsResults < Item > contacts = contactsFolder.findItems( new ItemView( 1000 ) );
        // Mostly the test case fails here in this condition. Actually the
        // contacts folder of user2 should be empty. We have already cleared
        // that in the 4th line of this function. And the contact is created in 
        // "user1"'s mailbox. So it should be empty. 
        Assert.assertTrue( "Contacts contain " + contacts.getTotalCount() + " contacts.", 0 == contacts.getTotalCount() );
    
        // Using delegation to get the contacts from "user1"'s mailbox, by
        // user2.
        Mailbox delegateMailbox = new Mailbox( "user1@domain.com" );
        delegateMailbox.setRoutingType( "SMTP" );
        Folder delegatedContactFolder = Folder.bind( service2, new FolderId( WellKnownFolderName.Contacts,
            delegateMailbox ) );
        contacts = delegatedContactFolder.findItems( new ItemView( 1000 ) );
        Assert.assertTrue( "Contacts contain " + contacts.getTotalCount() + " contacts.", 1 == contacts.getTotalCount() );
        Contact retrievedContact = ( Contact ) contacts.getItems().get( 0 );
        Assert.assertEquals( "Paul", retrievedContact.getGivenName() );
        Assert.assertEquals( Importance.High, retrievedContact.getImportance() );
        Assert.assertEquals( "9895176934",
            retrievedContact.getPhoneNumbers().getPhoneNumber( PhoneNumberKey.MobilePhone ) );
        System.out.println( "Successfully retrieved contact using delegation." );
    
        // Verifying that if delegation is not used, then user2 should get 0
        // contacts.
        System.out.println( "Verifying contacts doesnot showup if delegation is not used." );
        contactsFolder = Folder.bind( service2, new FolderId( WellKnownFolderName.Contacts ) );
        contacts = contactsFolder.findItems( new ItemView( 1000 ) );
        Assert.assertTrue( "Contacts contain " + contacts.getTotalCount() + " contacts.", 0 == contacts.getTotalCount() );
        System.out.println( "Delegation working fine." );
      }
    
      private static void createContact( ExchangeService service ) throws Exception, ServiceLocalException,
          InterruptedException
      {
        System.out.println( "Creating a contact in " + ( ( WebCredentials ) service.getCredentials() ).getUser() );
        Contact newContact = new Contact( service );
        newContact.setGivenName( "Paul" );
        newContact.setImportance( Importance.High );
        newContact.getPhoneNumbers().setPhoneNumber( PhoneNumberKey.MobilePhone, "9895176934" );
        newContact.save( new FolderId( WellKnownFolderName.Contacts ) );
    
        System.out.println( "Verifying contacts contain 1 contact for "
            + ( ( WebCredentials ) service.getCredentials() ).getUser() );
        Folder contactsFolder = Folder.bind( service, WellKnownFolderName.Contacts );
        FindItemsResults < Item > contacts = contactsFolder.findItems( new ItemView( 1000 ) );
        Assert.assertTrue( "Contacts contain " + contacts.getTotalCount() + " contacts.", 1 == contacts.getTotalCount() );
        Contact retrievedContact = ( Contact ) contacts.getItems().get( 0 );
        Assert.assertEquals( "Paul", retrievedContact.getGivenName() );
        Assert.assertEquals( Importance.High, retrievedContact.getImportance() );
        Assert.assertEquals( "9895176934",
            retrievedContact.getPhoneNumbers().getPhoneNumber( PhoneNumberKey.MobilePhone ) );
    
      }
    
      private static void clearContactsAndAssertContactsFolderIsEmpty( ExchangeService service ) throws Exception
      {
        System.out
            .println( "Clearing all the contacts in " + ( ( WebCredentials ) service.getCredentials() ).getUser() );
        Folder contactsFolder = Folder.bind( service, WellKnownFolderName.Contacts );
        contactsFolder.empty( DeleteMode.HardDelete, false );
        System.out.println( "Verifying contacts is empty" );
        contactsFolder = Folder.bind( service, WellKnownFolderName.Contacts );
        FindItemsResults < Item > contacts = contactsFolder.findItems( new ItemView( 1000 ) );
        Assert.assertTrue( "Contacts contain " + contacts.getTotalCount() + " contacts.", 0 == contacts.getTotalCount() );
      }
    
      private static ExchangeService getService( String userName, String password ) throws Exception
      {
        ExchangeService service = new ExchangeService();
        service.setUrl( new URI( "http://<Exchange Server IP>/ews/Exchange.asmx" ) );
        service.setCredentials( new WebCredentials( userName, password ) );
        return service;
      }
    }
    Please change exchange server ip in getService() method and provide correct user name and password for user1 and user2 and also the correct email address while delegating. Provide full access of "user1"'s mailboxto "user2".

    Sometimes the test case passes. but most of the time it is failing.

    Please, if anyone has come across this problem, please let me know what could be done.

    Thanks,

    Paul

     

    Thursday, March 17, 2011 2:42 PM