OfficeDev / ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
MIT License
869 stars 560 forks source link

AssertionError on call to findItems #193

Open superelchi opened 9 years ago

superelchi commented 9 years ago

Hi,

I' trying to read Emails from an Office 365 account. Code:

ExchangeService service = new ExchangeService();
ExchangeCredentials credentials = new WebCredentials("XXX@YYY.onmicrosoft.com", "ZZZ");
service.setCredentials(credentials);
URI uri = new URI("https://outlook.office365.com/EWS/Exchange.asmx");
service.setUrl(uri);
PropertySet itemPropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
itemPropertySet.setRequestedBodyType(BodyType.Text);
ItemView view = new ItemView(100);
view.setPropertySet(itemPropertySet);
view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
FindItemsResults<Item> results = service.findItems(WellKnownFolderName.Inbox, view);

This throws:

Exception in thread "main" java.lang.AssertionError: [ExchangeServiceBase.SaveHttpResponseHeaders] expect no headers in the dictionary yet.
    at microsoft.exchange.webservices.data.EwsUtilities.EwsAssert(EwsUtilities.java:350)
    at microsoft.exchange.webservices.data.ExchangeServiceBase.saveHttpResponseHeaders(ExchangeServiceBase.java:787)
    at microsoft.exchange.webservices.data.ExchangeServiceBase.processHttpResponseHeaders(ExchangeServiceBase.java:778)
    at microsoft.exchange.webservices.data.ServiceRequestBase.traceResponse(ServiceRequestBase.java:325)
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.readResponse(SimpleServiceRequestBase.java:150)
    at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:47)
    at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:136)
    at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:807)
    at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:850)
    at microsoft.exchange.webservices.data.ExchangeService.findItems(ExchangeService.java:926)
    at EWSMailer.readMail(EWSMailer.java:25)

If this code is run in non-debug mode (no "-ea") everything works fine.

OS: Linux Mint 64 bit SDK: 1.6.0_45

Any clues?

superelchi

serious6 commented 9 years ago

running it without "-ea" will prevent java.lang.assertions from being checked. That´s why you dont see the error in that case.

superelchi commented 9 years ago

Yeah. Sure. Running without "-ea" works. But what use is an assert() that is always thrown - I thought assert should help you debug your code. Not crash it. ;)

superelchi

serious6 commented 9 years ago

you are right. Looking at the code I think the provided assertion might be wrong because it just checks if httpResponseHeaders this.httpResponseHeaders.size() == 0 and afterwards clears it. This makes no sense from my point of view. Maybe there are some other assumptions why the headers should be clear here?

kentongray commented 9 years ago

I've been using the "old" EWS java API for months and attempted to update to this one and ran into this error. Looking back we removed that code from the "old" version because it did not seem to have a purpose and would always break otherwise working code. I think it should definitely be removed as it causes difficulty... Pull request coming