diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 36958904..7f1e7a76 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -187,7 +187,7 @@ public abstract class ExchangeSession { return dateFormatter.format(date); } - protected static SimpleDateFormat getZuluDateFormat() { + public static SimpleDateFormat getZuluDateFormat() { SimpleDateFormat dateFormat = new SimpleDateFormat(YYYYMMDD_T_HHMMSS_Z, Locale.ENGLISH); dateFormat.setTimeZone(GMT_TIMEZONE); return dateFormat; diff --git a/src/java/davmail/exchange/dav/DavExchangeSession.java b/src/java/davmail/exchange/dav/DavExchangeSession.java index 618bcb84..6b3c6acc 100644 --- a/src/java/davmail/exchange/dav/DavExchangeSession.java +++ b/src/java/davmail/exchange/dav/DavExchangeSession.java @@ -563,7 +563,7 @@ public class DavExchangeSession extends ExchangeSession { * @param multiStatusResponse response * @throws URIException on error */ - public Contact(MultiStatusResponse multiStatusResponse) throws URIException { + public Contact(MultiStatusResponse multiStatusResponse) throws URIException, DavMailException { setHref(URIUtil.decode(multiStatusResponse.getHref())); DavPropertySet properties = multiStatusResponse.getProperties(HttpStatus.SC_OK); permanentUrl = getPropertyIfExists(properties, "permanenturl"); @@ -573,11 +573,7 @@ public class DavExchangeSession extends ExchangeSession { String value = getPropertyIfExists(properties, attributeName); if (value != null) { if ("bday".equals(attributeName) || "lastmodified".equals(attributeName)) { - try { - value = ExchangeSession.getZuluDateFormat().format(ExchangeSession.getExchangeZuluDateFormatMillisecond().parse(value)); - } catch (ParseException e) { - LOGGER.warn("Invalid date: " + value); - } + value = convertDate(value); } put(attributeName, value); } @@ -1652,4 +1648,14 @@ public class DavExchangeSession extends ExchangeSession { LOGGER.debug("Deleted to :" + destination); } + + protected String convertDate(String exchangeDateValue) throws DavMailException { + String zuluDateValue; + try { + zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormatMillisecond().parse(exchangeDateValue)); + } catch (ParseException e) { + throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue); + } + return zuluDateValue; + } } diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index f515408a..2c000332 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -241,6 +241,7 @@ public class EwsExchangeSession extends ExchangeSession { /** * Get item MIME content. + * * @param itemId EWS item id * @return item content as byte array * @throws IOException on error @@ -251,7 +252,7 @@ public class EwsExchangeSession extends ExchangeSession { return getItemMethod.getMimeContent(); } - protected Message buildMessage(EWSMethod.Item response) throws URIException { + protected Message buildMessage(EWSMethod.Item response) throws URIException, DavMailException { Message message = new Message(); // get item id @@ -269,7 +270,7 @@ public class EwsExchangeSession extends ExchangeSession { String lastVerbExecuted = response.get(Field.get("lastVerbExecuted").getResponseName()); message.answered = "102".equals(lastVerbExecuted) || "103".equals(lastVerbExecuted); message.forwarded = "104".equals(lastVerbExecuted); - message.date = response.get(Field.get("date").getResponseName()); + message.date = convertDate(response.get(Field.get("date").getResponseName())); message.deleted = "1".equals(response.get(Field.get("deleted").getResponseName())); if (LOGGER.isDebugEnabled()) { @@ -656,22 +657,18 @@ public class EwsExchangeSession extends ExchangeSession { // item id ItemId itemId; - protected Contact(EWSMethod.Item response) throws URIException { + protected Contact(EWSMethod.Item response) throws URIException, DavMailException { itemId = new ItemId(response); permanentUrl = response.get(Field.get("permanenturl").getResponseName()); etag = response.get(Field.get("etag").getResponseName()); displayName = response.get(Field.get("displayname").getResponseName()); - itemName = response.get(Field.get("urlcompname").getResponseName()); + itemName = response.get(Field.get("urlcompname").getResponseName()); for (String attributeName : CONTACT_ATTRIBUTES) { String value = response.get(Field.get(attributeName).getResponseName()); if (value != null) { if ("bday".equals(attributeName) || "lastmodified".equals(attributeName)) { - try { - value = ExchangeSession.getZuluDateFormat().format(ExchangeSession.getExchangeZuluDateFormat().parse(value)); - } catch (ParseException e) { - LOGGER.warn("Invalid date: " + value); - } + value = convertDate(value); } put(attributeName, value); } @@ -681,7 +678,7 @@ public class EwsExchangeSession extends ExchangeSession { /** * @inheritDoc */ - public Contact(String folderPath, String itemName, Map properties, String etag, String noneMatch) { + protected Contact(String folderPath, String itemName, Map properties, String etag, String noneMatch) { super(folderPath, itemName, properties, etag, noneMatch); } @@ -711,7 +708,7 @@ public class EwsExchangeSession extends ExchangeSession { String currentEtag = null; ItemId currentItemId = null; List responses = searchItems(folderPath, EVENT_REQUEST_PROPERTIES, EwsExchangeSession.this.equals("urlcompname", urlcompname), FolderQueryTraversal.SHALLOW); - if (responses.size() > 0) { + if (!responses.isEmpty()) { EWSMethod.Item response = responses.get(0); currentItemId = new ItemId(response); currentEtag = response.get(Field.get("etag").getResponseName()); @@ -748,6 +745,7 @@ public class EwsExchangeSession extends ExchangeSession { itemResult.status = createOrUpdateItemMethod.getStatusCode(); if (itemResult.status == HttpURLConnection.HTTP_OK) { + //noinspection VariableNotUsedInsideIf if (etag != null) { itemResult.status = HttpStatus.SC_CREATED; LOGGER.debug("Updated event " + getHref()); @@ -781,7 +779,7 @@ public class EwsExchangeSession extends ExchangeSession { /** * @inheritDoc */ - public Event(String folderPath, String itemName, String contentClass, String itemBody, String etag, String noneMatch) { + protected Event(String folderPath, String itemName, String contentClass, String itemBody, String etag, String noneMatch) { super(folderPath, itemName, contentClass, itemBody, etag, noneMatch); } @@ -831,7 +829,7 @@ public class EwsExchangeSession extends ExchangeSession { @Override public List searchContacts(String folderPath, Set attributes, Condition condition) throws IOException { List contacts = new ArrayList(); - List responses = searchItems(folderPath, attributes,condition, + List responses = searchItems(folderPath, attributes, condition, FolderQueryTraversal.SHALLOW); for (EWSMethod.Item response : responses) { @@ -954,10 +952,11 @@ public class EwsExchangeSession extends ExchangeSession { private FolderId getFolderIdIfExists(String folderPath) throws IOException { String[] folderNames; FolderId currentFolderId; - String currentMailboxPath = "/users/"+email+ '/'; + String currentMailboxPath = "/users/" + email + '/'; if (folderPath.startsWith(currentMailboxPath)) { return getFolderIdIfExists(folderPath.substring(currentMailboxPath.length())); - } if (folderPath.startsWith(PUBLIC_ROOT)) { + } + if (folderPath.startsWith(PUBLIC_ROOT)) { currentFolderId = DistinguishedFolderId.PUBLICFOLDERSROOT; folderNames = folderPath.substring(PUBLIC_ROOT.length()).split("/"); } else if (folderPath.startsWith(INBOX) || folderPath.startsWith(LOWER_CASE_INBOX)) { @@ -1028,5 +1027,15 @@ public class EwsExchangeSession extends ExchangeSession { return status; } + protected String convertDate(String exchangeDateValue) throws DavMailException { + String zuluDateValue; + try { + zuluDateValue = getZuluDateFormat().format(getExchangeZuluDateFormat().parse(exchangeDateValue)); + } catch (ParseException e) { + throw new DavMailException("EXCEPTION_INVALID_DATE", exchangeDateValue); + } + return zuluDateValue; + } + } diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index b08043ad..f7f0ba0c 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -600,7 +600,7 @@ public class ImapConnection extends AbstractConnection { try { SimpleDateFormat dateParser = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); dateParser.setTimeZone(ExchangeSession.GMT_TIMEZONE); - Date date = dateParser.parse(message.date); + Date date = ExchangeSession.getZuluDateFormat().parse(message.date); SimpleDateFormat dateFormatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss Z", Locale.ENGLISH); buffer.append(" INTERNALDATE \"").append(dateFormatter.format(date)).append('\"'); } catch (ParseException e) { diff --git a/src/test/davmail/imap/TestImap.java b/src/test/davmail/imap/TestImap.java index 4d956598..fa15042b 100644 --- a/src/test/davmail/imap/TestImap.java +++ b/src/test/davmail/imap/TestImap.java @@ -246,6 +246,11 @@ public class TestImap extends AbstractDavMailTestCase { assertEquals(". OK UID FETCH completed",readFullAnswer(".")); } + public void testFetchInternalDate() throws IOException { + writeLine(". UID FETCH "+messageUid+" (INTERNALDATE)"); + assertEquals(". OK UID FETCH completed",readFullAnswer(".")); + } + public void testDeleteFolder() throws IOException { writeLine(". DELETE testfolder");