From 76bdf71ee92dbd8e7386d72bc76e69366d0a0b5d Mon Sep 17 00:00:00 2001 From: mguessan Date: Fri, 5 Dec 2008 09:29:44 +0000 Subject: [PATCH] SearchMethod based oldest trash message purge git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@203 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 64 +++++++++---------- 1 file changed, 30 insertions(+), 34 deletions(-) diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 2d0b0e35..de3a69a0 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -489,7 +489,6 @@ public class ExchangeSession { ex.setReasonCode(status); throw ex; } - // one level search Enumeration folderEnum = searchMethod.getResponses(); while (folderEnum.hasMoreElements()) { @@ -515,46 +514,43 @@ public class ExchangeSession { if (keepDelay == 0) { keepDelay = DEFAULT_KEEP_DELAY; } - Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, -keepDelay); LOGGER.debug("Delete messages in trash since " + cal.getTime()); - long keepTimestamp = cal.getTimeInMillis(); - Vector deleteRequestProperties = new Vector(); - deleteRequestProperties.add("DAV:getlastmodified"); - deleteRequestProperties.add("urn:schemas:mailheader:content-class"); + SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); - Enumeration folderEnum = wdr.propfindMethod(deleteditemsUrl, 1, deleteRequestProperties); - while (folderEnum.hasMoreElements()) { - ResponseEntity entity = (ResponseEntity) folderEnum.nextElement(); - String messageUrl = URIUtil.decode(entity.getHref()); - String lastModifiedString = null; - String contentClass = null; - Enumeration propertiesEnum = entity.getProperties(); - while (propertiesEnum.hasMoreElements()) { - Property prop = (Property) propertiesEnum.nextElement(); - String localName = prop.getLocalName(); - if ("getlastmodified".equals(localName)) { - lastModifiedString = prop.getPropertyAsString(); - } else if ("content-class".equals(prop.getLocalName())) { - contentClass = prop.getPropertyAsString(); - } - } - if ("urn:content-classes:message".equals(contentClass) && - lastModifiedString != null && lastModifiedString.length() > 0) { - Date parsedDate; - try { - parsedDate = dateParser.parse(lastModifiedString); - if (parsedDate.getTime() < keepTimestamp) { - LOGGER.debug("Delete " + messageUrl + " last modified " + parsedDate); - wdr.deleteMethod(messageUrl); - } - } catch (ParseException e) { - LOGGER.warn("Invalid message modified date " + lastModifiedString + " on " + messageUrl); - } + String searchRequest = "\n" + + "\n" + + " Select \"DAV:uid\"" + + " FROM Scope('SHALLOW TRAVERSAL OF \"" + deleteditemsUrl + "\"')\n" + + " WHERE \"DAV:isfolder\" = False\n" + + " AND \"DAV:getlastmodified\" < '"+dateFormatter.format(cal.getTime())+"'\n" + + " \n" + + ""; + SearchMethod searchMethod = new SearchMethod(URIUtil.encodePath(currentFolderUrl), searchRequest); + searchMethod.setDebug(4); + try { + int status = wdr.retrieveSessionInstance().executeMethod(searchMethod); + // Also accept OK sent by buggy servers. + if (status != HttpStatus.SC_MULTI_STATUS + && status != HttpStatus.SC_OK) { + HttpException ex = new HttpException(); + ex.setReasonCode(status); + throw ex; } + Enumeration folderEnum = searchMethod.getResponses(); + + while (folderEnum.hasMoreElements()) { + ResponseEntity entity = (ResponseEntity) folderEnum.nextElement(); + String messageUrl = URIUtil.decode(entity.getHref()); + + LOGGER.debug("Delete " + messageUrl); + wdr.deleteMethod(messageUrl); + } + } finally { + searchMethod.releaseConnection(); } }